Skip to content

Commit

Permalink
e2e test for firtlet pod restart
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszo committed Mar 28, 2018
1 parent c298055 commit 2932867
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
40 changes: 34 additions & 6 deletions tests/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ import (

"github.com/Mirantis/virtlet/tests/e2e/framework"
. "github.com/Mirantis/virtlet/tests/e2e/ginkgo-ext"

"k8s.io/client-go/pkg/api/v1"
)

var (
vmImageLocation = flag.String("image", defaultVMImageLocation, "VM image URL (*without http(s)://*")
sshUser = flag.String("sshuser", defaultSSHUser, "default SSH user for VMs")
includeCloudInitTests = flag.Bool("include-cloud-init-tests", false, "include Cloud-Init tests")
includeUnsafeTests = flag.Bool("include-unsafe-tests", false, "include tests that can be unsafe if they're run outside the build container")
memoryLimit = flag.Int("memoryLimit", 160, "default VM memory limit (in MiB)")
junitOutput = flag.String("junitOutput", "", "JUnit XML output file")
vmImageLocation = flag.String("image", defaultVMImageLocation, "VM image URL (*without http(s)://*")
sshUser = flag.String("sshuser", defaultSSHUser, "default SSH user for VMs")
includeCloudInitTests = flag.Bool("include-cloud-init-tests", false, "include Cloud-Init tests")
includeUnsafeTests = flag.Bool("include-unsafe-tests", false, "include tests that can be unsafe if they're run outside the build container")
includeDisruptiveTests = flag.Bool("include-disruptive", false, "include tests that can disrupt other tests")
memoryLimit = flag.Int("memoryLimit", 160, "default VM memory limit (in MiB)")
junitOutput = flag.String("junitOutput", "", "JUnit XML output file")
)

// scheduleWaitSSH schedules SSH interface initialization before the test context starts
Expand Down Expand Up @@ -65,6 +68,25 @@ func waitSSH(vm *framework.VMInterface) framework.Executor {
return ssh
}

func waitVirtletPod(vm *framework.VMInterface) *framework.PodInterface {
var virtletPod *framework.PodInterface
Eventually(
func() error {
var err error
virtletPod, err = vm.VirtletPod()
if err != nil {
return err
}
for _, c := range virtletPod.Pod.Status.Conditions {
if c.Type == v1.PodReady && c.Status == v1.ConditionTrue {
return nil
}
}
return fmt.Errorf("Pod not ready yet: %+v", virtletPod.Pod.Status)
}, 60*5, 3).Should(Succeed())
return virtletPod
}

func checkCPUCount(vm *framework.VMInterface, ssh framework.Executor, cpus int) {
proc := do(framework.RunSimple(ssh, "cat", "/proc/cpuinfo")).(string)
Expect(regexp.MustCompile(`(?m)^processor`).FindAllString(proc, -1)).To(HaveLen(cpus))
Expand Down Expand Up @@ -153,3 +175,9 @@ func includeUnsafe() {
Skip("Tests that are unsafe outside the build container are disabled")
}
}

func includeDisruptive() {
if !*includeDisruptiveTests {
Skip("Tests that may disrupt another tests are disabled")
}
}
5 changes: 2 additions & 3 deletions tests/e2e/framework/pod_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"time"

"github.com/davecgh/go-spew/spew"
"k8s.io/apimachinery/pkg/api/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -68,9 +67,9 @@ func (pi *PodInterface) Create() error {
// Delete deletes the pod and associated service, which was earlier created by `controller.Run()`
func (pi *PodInterface) Delete() error {
if pi.hasService {
pi.controller.client.Services(pi.controller.Namespace()).Delete(pi.Pod.Name, nil)
pi.controller.client.Services(pi.Pod.Namespace).Delete(pi.Pod.Name, nil)
}
return pi.controller.client.Pods(pi.controller.Namespace()).Delete(pi.Pod.Name, nil)
return pi.controller.client.Pods(pi.Pod.Namespace).Delete(pi.Pod.Name, nil)
}

// Wait waits for pod to start and checks that it doesn't fail immediately after that
Expand Down
44 changes: 35 additions & 9 deletions tests/e2e/restart_virtlet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package e2e

import (
"bytes"
"context"
"fmt"
"time"

. "github.com/onsi/gomega"
Expand All @@ -32,30 +35,53 @@ var _ = Describe("Virtlet restart", func() {
)

BeforeAll(func() {
includeDisruptive()
vm = controller.VM("cirros-vm")
vm.Create(VMOptions{}.applyDefaults(), time.Minute*5, nil)
var err error
vmPod, err = vm.Pod()
Expect(err).NotTo(HaveOccurred())

// restart virtlet before all tests
virtletPod, err := vm.VirtletPod()
Expect(err).NotTo(HaveOccurred())

err = virtletPod.Delete()
Expect(err).NotTo(HaveOccurred())

waitVirtletPod(vm)
})

AfterAll(func() {
deleteVM(vm)
})

It("Should allow to ssh to VM after virtlet pod and vm restart [Conformance]", func() {
pod, err := vm.VirtletPod()
Expect(err).NotTo(HaveOccurred())
It("Should allow to ssh to VM after virtlet pod restart", func() {
waitSSH(vm)
}, 3*60)

virtletPodExecutor, err := pod.Container("virtlet")
Expect(err).NotTo(HaveOccurred())
It("Should contain logs from attach session", func() {
var stdout bytes.Buffer
ctx, closeFunc := context.WithCancel(context.Background())
defer closeFunc()
localExecutor := framework.LocalExecutor(ctx)

do(framework.ExecSimple(virtletPodExecutor, "pkill", "-9", "virtlet"))
Expect(pod.Wait()).NotTo(HaveOccurred())
By(fmt.Sprintf("Running command: kubectl logs -n %s %s", controller.Namespace(), vm.Name))
err := localExecutor.Run(nil, &stdout, &stdout, "kubectl", "-n", controller.Namespace(), "logs", vm.Name)
fmt.Sprintf(stdout.String())
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).Should(ContainSubstring("login as 'cirros' user."))

_, err = vm.VirshCommand("reboot", "<domain>")
By(fmt.Sprintf("Running command: kubectl attach -n %s -i %s", controller.Namespace(), vm.Name))
stdin := bytes.NewBufferString("\nTESTTEXT\n\n")
stdout.Reset()
err = localExecutor.Run(stdin, &stdout, &stdout, "kubectl", "-n", controller.Namespace(), "attach", "-i", vm.Name)
Expect(err).NotTo(HaveOccurred())

waitSSH(vm)
By(fmt.Sprintf("Running again command: kubectl logs -n %s %s", controller.Namespace(), vm.Name))
stdout.Reset()
err = localExecutor.Run(nil, &stdout, &stdout, "kubectl", "-n", controller.Namespace(), "logs", vm.Name)
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).Should(ContainSubstring("TESTTEXT"))
}, 3*60)
})

0 comments on commit 2932867

Please sign in to comment.