From d921fc6857e32a50fdbceb087547ef4939756a35 Mon Sep 17 00:00:00 2001 From: Edward Haas Date: Thu, 9 Jul 2020 12:21:27 +0300 Subject: [PATCH] tests: Do not execute code in ginkgo containers body Ginkgo containers body are evaluated at definition, i.e. at a pre-main stage. Running code at this stage is not recommended as some initializations are occurring only in the test `main` (which is auto-generated by the test framework). An example of such a scenario is the command line flag parsing. Libraries, tested code and the test infra is expected to declare the flags and the parsing is expected to be execute only once (at main usually). This change moves all code that executes in the containers body to the Before* setup bodies, while leaving on the containers body the variable declarations. Signed-off-by: Edward Haas --- tests/config_test.go | 11 +- tests/console_test.go | 8 +- tests/container_disk_test.go | 7 +- tests/datavolume_test.go | 8 +- tests/expose_test.go | 10 +- tests/imageupload_test.go | 10 +- tests/infra_test.go | 22 +- tests/kubectl_test.go | 9 +- tests/migration_test.go | 8 +- tests/networkpolicy_test.go | 8 +- tests/operator_test.go | 670 ++++++++++++----------- tests/pausing_test.go | 7 +- tests/probes_test.go | 7 +- tests/replicaset_test.go | 8 +- tests/security_features_test.go | 7 +- tests/snapshot_test.go | 9 +- tests/stability_test.go | 7 +- tests/storage_test.go | 7 +- tests/subresource_api_test.go | 7 +- tests/template_test.go | 7 +- tests/version_test.go | 7 +- tests/virt_control_plane_test.go | 17 +- tests/vm_test.go | 7 +- tests/vm_watch_test.go | 8 +- tests/vmi_cloudinit_hook_sidecar_test.go | 7 +- tests/vmi_cloudinit_test.go | 142 ++--- tests/vmi_configuration_test.go | 14 +- tests/vmi_gpu_test.go | 9 +- tests/vmi_headless_test.go | 8 +- tests/vmi_hook_sidecar_test.go | 7 +- tests/vmi_ignition_test.go | 58 +- tests/vmi_iothreads_test.go | 12 +- tests/vmi_lifecycle_test.go | 7 +- tests/vmi_monitoring_test.go | 23 +- tests/vmi_multiqueue_test.go | 12 +- tests/vmi_multus_test.go | 14 +- tests/vmi_networking_test.go | 44 +- tests/vmi_slirp_interface_test.go | 7 +- tests/vmidefaults_test.go | 7 +- tests/vmipreset_test.go | 7 +- tests/vnc_test.go | 7 +- tests/windows_test.go | 7 +- 42 files changed, 737 insertions(+), 531 deletions(-) diff --git a/tests/config_test.go b/tests/config_test.go index b09a9668660a..2cb5c930d547 100644 --- a/tests/config_test.go +++ b/tests/config_test.go @@ -37,10 +37,14 @@ import ( var _ = Describe("[rfe_id:899][crit:medium][vendor:cnv-qe@redhat.com][level:component]Config", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var virtClient kubecli.KubevirtClient BeforeEach(func() { + var err error + + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) @@ -237,9 +241,6 @@ var _ = Describe("[rfe_id:899][crit:medium][vendor:cnv-qe@redhat.com][level:comp Context("With a ServiceAccount defined", func() { - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) - serviceAccountPath := config.ServiceAccountSourceDir It("[test_id:998]Should be the namespace and token the same for a pod and vmi", func() { diff --git a/tests/console_test.go b/tests/console_test.go index 387ccbf6a657..3ad7ecc550a4 100644 --- a/tests/console_test.go +++ b/tests/console_test.go @@ -38,11 +38,15 @@ import ( var _ = Describe("[rfe_id:127][posneg:negative][crit:medium][vendor:cnv-qe@redhat.com][level:component]Console", func() { tests.FlagParse() + var virtClient kubecli.KubevirtClient - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) BeforeEach(func() { + var err error + + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) diff --git a/tests/container_disk_test.go b/tests/container_disk_test.go index 8115e378528e..e586710cdd2c 100644 --- a/tests/container_disk_test.go +++ b/tests/container_disk_test.go @@ -42,10 +42,13 @@ var _ = Describe("[rfe_id:588][crit:medium][vendor:cnv-qe@redhat.com][level:comp tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var virtClient kubecli.KubevirtClient + var err error BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) diff --git a/tests/datavolume_test.go b/tests/datavolume_test.go index 0abb5baee4da..899e6a2c372e 100644 --- a/tests/datavolume_test.go +++ b/tests/datavolume_test.go @@ -48,10 +48,14 @@ const DummyFilePath = "/usr/share/nginx/html/dummy.file" var _ = Describe("DataVolume Integration", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + + var virtClient kubecli.KubevirtClient + var err error BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() if !tests.HasCDI() { Skip("Skip DataVolume tests when CDI is not present") diff --git a/tests/expose_test.go b/tests/expose_test.go index 5f7f889d83a9..dfe783c183bb 100644 --- a/tests/expose_test.go +++ b/tests/expose_test.go @@ -48,10 +48,16 @@ var _ = Describe("[rfe_id:253][crit:medium][vendor:cnv-qe@redhat.com][level:comp tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var virtClient kubecli.KubevirtClient + var err error + const testPort = 1500 + BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + }) + Context("Expose service on a VM", func() { var tcpVM *v1.VirtualMachineInstance tests.BeforeAll(func() { diff --git a/tests/imageupload_test.go b/tests/imageupload_test.go index 1fcd42875804..fef617706f7c 100644 --- a/tests/imageupload_test.go +++ b/tests/imageupload_test.go @@ -32,8 +32,14 @@ var _ = Describe("ImageUpload", func() { dvName := "alpine-dv" pvcSize := "100Mi" - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var virtClient kubecli.KubevirtClient + + BeforeEach(func() { + var err error + + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + }) BeforeEach(func() { By("Getting CDI HTTP import server pod") diff --git a/tests/infra_test.go b/tests/infra_test.go index eb90b666b2a3..d52387c5d009 100644 --- a/tests/infra_test.go +++ b/tests/infra_test.go @@ -55,15 +55,23 @@ import ( var _ = Describe("Infrastructure", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var virtClient kubecli.KubevirtClient + var aggregatorClient *aggregatorclient.Clientset + var err error - config, err := kubecli.GetConfig() - if err != nil { - panic(err) - } + BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) - aggregatorClient := aggregatorclient.NewForConfigOrDie(config) + if aggregatorClient == nil { + config, err := kubecli.GetConfig() + if err != nil { + panic(err) + } + + aggregatorClient = aggregatorclient.NewForConfigOrDie(config) + } + }) Describe("[rfe_id:4102][crit:medium][vendor:cnv-qe@redhat.com][level:component]certificates", func() { diff --git a/tests/kubectl_test.go b/tests/kubectl_test.go index 09adfe95a590..a92942aa2253 100644 --- a/tests/kubectl_test.go +++ b/tests/kubectl_test.go @@ -19,11 +19,14 @@ var _ = Describe("[rfe_id:3423][vendor:cnv-qe@redhat.com][level:component]oc/kub var k8sClient, result string var vm *v1.VirtualMachine - - virtCli, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var virtCli kubecli.KubevirtClient + var err error BeforeEach(func() { + + virtCli, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + k8sClient = tests.GetK8sCmdClient() tests.SkipIfNoCmd(k8sClient) tests.BeforeTestCleanup() diff --git a/tests/migration_test.go b/tests/migration_test.go index 5ccc57c746b0..133fd425ef6c 100644 --- a/tests/migration_test.go +++ b/tests/migration_test.go @@ -62,14 +62,16 @@ const ( var _ = Describe("[rfe_id:393][crit:high][vendor:cnv-qe@redhat.com][level:system] VM Live Migration", func() { tests.FlagParse() - - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var virtClient kubecli.KubevirtClient var originalKubeVirtConfig *k8sv1.ConfigMap + var err error tests.BeforeAll(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + originalKubeVirtConfig, err = virtClient.CoreV1().ConfigMaps(tests.KubeVirtInstallNamespace).Get(virtconfig.ConfigMapName, metav1.GetOptions{}) if err != nil && !errors.IsNotFound(err) { Expect(err).ToNot(HaveOccurred()) diff --git a/tests/networkpolicy_test.go b/tests/networkpolicy_test.go index 8bb9cfda8e08..77c4d5d97834 100644 --- a/tests/networkpolicy_test.go +++ b/tests/networkpolicy_test.go @@ -42,14 +42,18 @@ var _ = Describe("[rfe_id:150][crit:high][vendor:cnv-qe@redhat.com][level:compon tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var virtClient kubecli.KubevirtClient var vmia *v1.VirtualMachineInstance var vmib *v1.VirtualMachineInstance var vmic *v1.VirtualMachineInstance tests.BeforeAll(func() { + var err error + + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.SkipIfUseFlannel(virtClient) tests.SkipIfNotUseNetworkPolicy(virtClient) tests.BeforeTestCleanup() diff --git a/tests/operator_test.go b/tests/operator_test.go index 00196a5c01d8..3d0c6bc8fdf5 100644 --- a/tests/operator_test.go +++ b/tests/operator_test.go @@ -53,6 +53,13 @@ import ( "kubevirt.io/kubevirt/tests" ) +type vmYamlDefinition struct { + apiVersion string + vmName string + generatedYaml string + yamlFile string +} + var _ = Describe("Operator", func() { tests.FlagParse() var originalKv *v1.KubeVirt @@ -62,372 +69,393 @@ var _ = Describe("Operator", func() { var err error var workDir string - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) - config, err := kubecli.GetConfig() - tests.PanicOnError(err) - aggregatorClient := aggregatorclient.NewForConfigOrDie(config) + var virtClient kubecli.KubevirtClient + var aggregatorClient *aggregatorclient.Clientset + var k8sClient string + var vmYamls []vmYamlDefinition + + var ( + copyOriginalCDI func() *cdiv1.CDI + copyOriginalKv func() *v1.KubeVirt + createKv func(*v1.KubeVirt) + createCdi func() + sanityCheckDeploymentsExistWithNS func(string) + sanityCheckDeploymentsExist func() + sanityCheckDeploymentsDeleted func() + allPodsAreReady func(*v1.KubeVirt) + waitForUpdateCondition func(*v1.KubeVirt) + waitForKvWithTimeout func(*v1.KubeVirt, int) + waitForKv func(*v1.KubeVirt) + patchKvVersionAndRegistry func(string, string, string) + patchKvVersion func(string, string) + parseDaemonset func(string) (*v12.DaemonSet, string, string, string, string) + parseImage func(string, string) (string, string, string) + parseDeployment func(string) (*v12.Deployment, string, string, string, string) + parseOperatorImage func() (*v12.Deployment, string, string, string, string) + patchOperator func(*string, *string) bool + deleteAllKvAndWait func(bool) + usesSha func(string) bool + ensureShasums func() + generatePreviousVersionVmYamls func(string, string) + ) - k8sClient := tests.GetK8sCmdClient() + tests.BeforeAll(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + config, err := kubecli.GetConfig() + tests.PanicOnError(err) + aggregatorClient = aggregatorclient.NewForConfigOrDie(config) - type vmYamlDefinition struct { - apiVersion string - vmName string - generatedYaml string - yamlFile string - } - var vmYamls []vmYamlDefinition + k8sClient = tests.GetK8sCmdClient() + + copyOriginalCDI = func() *cdiv1.CDI { + newCDI := &cdiv1.CDI{ + Spec: *originalCDI.Spec.DeepCopy(), + } + newCDI.Name = originalCDI.Name + newCDI.Namespace = originalCDI.Namespace + newCDI.ObjectMeta.Labels = originalCDI.ObjectMeta.Labels + newCDI.ObjectMeta.Annotations = originalCDI.ObjectMeta.Annotations + + return newCDI - copyOriginalCDI := func() *cdiv1.CDI { - newCDI := &cdiv1.CDI{ - Spec: *originalCDI.Spec.DeepCopy(), } - newCDI.Name = originalCDI.Name - newCDI.Namespace = originalCDI.Namespace - newCDI.ObjectMeta.Labels = originalCDI.ObjectMeta.Labels - newCDI.ObjectMeta.Annotations = originalCDI.ObjectMeta.Annotations - return newCDI + copyOriginalKv = func() *v1.KubeVirt { + newKv := &v1.KubeVirt{ + Spec: *originalKv.Spec.DeepCopy(), + } + newKv.Name = originalKv.Name + newKv.Namespace = originalKv.Namespace + newKv.ObjectMeta.Labels = originalKv.ObjectMeta.Labels + newKv.ObjectMeta.Annotations = originalKv.ObjectMeta.Annotations - } + return newKv - copyOriginalKv := func() *v1.KubeVirt { - newKv := &v1.KubeVirt{ - Spec: *originalKv.Spec.DeepCopy(), } - newKv.Name = originalKv.Name - newKv.Namespace = originalKv.Namespace - newKv.ObjectMeta.Labels = originalKv.ObjectMeta.Labels - newKv.ObjectMeta.Annotations = originalKv.ObjectMeta.Annotations - return newKv + createKv = func(newKv *v1.KubeVirt) { + Eventually(func() error { + _, err = virtClient.KubeVirt(newKv.Namespace).Create(newKv) + return err + }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) + } - } + createCdi = func() { + _, err = virtClient.CdiClient().CdiV1alpha1().CDIs().Create(copyOriginalCDI()) + Expect(err).ToNot(HaveOccurred()) - createKv := func(newKv *v1.KubeVirt) { - Eventually(func() error { - _, err = virtClient.KubeVirt(newKv.Namespace).Create(newKv) - return err - }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) - } + Eventually(func() bool { + cdi, err := virtClient.CdiClient().CdiV1alpha1().CDIs().Get(originalCDI.Name, metav1.GetOptions{}) + if err != nil { + return false + } else if cdi.Status.Phase != cdiv1.CDIPhaseDeployed { + return false + } + return true + }, 240*time.Second, 1*time.Second).Should(BeTrue()) + } - createCdi := func() { - _, err = virtClient.CdiClient().CdiV1alpha1().CDIs().Create(copyOriginalCDI()) - Expect(err).ToNot(HaveOccurred()) + sanityCheckDeploymentsExistWithNS = func(namespace string) { + Eventually(func() error { + for _, deployment := range []string{"virt-api", "virt-controller"} { + _, err := virtClient.AppsV1().Deployments(namespace).Get(deployment, metav1.GetOptions{}) + if err != nil { + return err + } + } + return nil + }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) + } - Eventually(func() bool { - cdi, err := virtClient.CdiClient().CdiV1alpha1().CDIs().Get(originalCDI.Name, metav1.GetOptions{}) - if err != nil { - return false - } else if cdi.Status.Phase != cdiv1.CDIPhaseDeployed { - return false - } - return true - }, 240*time.Second, 1*time.Second).Should(BeTrue()) - } - - sanityCheckDeploymentsExistWithNS := func(namespace string) { - Eventually(func() error { - for _, deployment := range []string{"virt-api", "virt-controller"} { - _, err := virtClient.AppsV1().Deployments(namespace).Get(deployment, metav1.GetOptions{}) + sanityCheckDeploymentsExist = func() { + sanityCheckDeploymentsExistWithNS(tests.KubeVirtInstallNamespace) + } + + sanityCheckDeploymentsDeleted = func() { + + Eventually(func() error { + for _, deployment := range []string{"virt-api", "virt-controller"} { + _, err := virtClient.AppsV1().Deployments(tests.KubeVirtInstallNamespace).Get(deployment, metav1.GetOptions{}) + if err != nil && !errors.IsNotFound(err) { + return err + } + } + return nil + }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) + } + + allPodsAreReady = func(kv *v1.KubeVirt) { + Eventually(func() error { + + curKv, err := virtClient.KubeVirt(kv.Namespace).Get(kv.Name, &metav1.GetOptions{}) if err != nil { return err } - } - return nil - }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) - } - - sanityCheckDeploymentsExist := func() { - sanityCheckDeploymentsExistWithNS(tests.KubeVirtInstallNamespace) - } + if curKv.Status.TargetDeploymentID != curKv.Status.ObservedDeploymentID { + return fmt.Errorf("Target and obeserved id don't match") + } - sanityCheckDeploymentsDeleted := func() { + podsReadyAndOwned := 0 - Eventually(func() error { - for _, deployment := range []string{"virt-api", "virt-controller"} { - _, err := virtClient.AppsV1().Deployments(tests.KubeVirtInstallNamespace).Get(deployment, metav1.GetOptions{}) - if err != nil && !errors.IsNotFound(err) { + pods, err := virtClient.CoreV1().Pods(curKv.Namespace).List(metav1.ListOptions{LabelSelector: "kubevirt.io"}) + if err != nil { return err } - } - return nil - }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) - } - allPodsAreReady := func(kv *v1.KubeVirt) { - Eventually(func() error { + for _, pod := range pods.Items { + managed, ok := pod.Labels[v1.ManagedByLabel] + if !ok || managed != v1.ManagedByLabelOperatorValue { + continue + } - curKv, err := virtClient.KubeVirt(kv.Namespace).Get(kv.Name, &metav1.GetOptions{}) - if err != nil { - return err - } - if curKv.Status.TargetDeploymentID != curKv.Status.ObservedDeploymentID { - return fmt.Errorf("Target and obeserved id don't match") - } + if pod.Status.Phase != k8sv1.PodRunning { + return fmt.Errorf("Waiting for pod %s with phase %s to reach Running phase", pod.Name, pod.Status.Phase) + } - podsReadyAndOwned := 0 + for _, containerStatus := range pod.Status.ContainerStatuses { + if !containerStatus.Ready { + return fmt.Errorf("Waiting for pod %s to have all containers in Ready state", pod.Name) + } + } - pods, err := virtClient.CoreV1().Pods(curKv.Namespace).List(metav1.ListOptions{LabelSelector: "kubevirt.io"}) - if err != nil { - return err - } + id, ok := pod.Annotations[v1.InstallStrategyIdentifierAnnotation] + if !ok { + return fmt.Errorf("Pod %s is owned by operator but has no id annotation", pod.Name) + } - for _, pod := range pods.Items { - managed, ok := pod.Labels[v1.ManagedByLabel] - if !ok || managed != v1.ManagedByLabelOperatorValue { - continue + expectedID := curKv.Status.ObservedDeploymentID + if id != expectedID { + return fmt.Errorf("Pod %s is of version %s when we expected id %s", pod.Name, id, expectedID) + } + podsReadyAndOwned++ } - if pod.Status.Phase != k8sv1.PodRunning { - return fmt.Errorf("Waiting for pod %s with phase %s to reach Running phase", pod.Name, pod.Status.Phase) + // this just sanity checks that at least one pod was found and verified. + // 0 would indicate our labeling was incorrect. + Expect(podsReadyAndOwned).ToNot(Equal(0)) + + return nil + }, 120*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) + } + + waitForUpdateCondition = func(kv *v1.KubeVirt) { + Eventually(func() error { + kv, err := virtClient.KubeVirt(kv.Namespace).Get(kv.Name, &metav1.GetOptions{}) + if err != nil { + return err } - for _, containerStatus := range pod.Status.ContainerStatuses { - if !containerStatus.Ready { - return fmt.Errorf("Waiting for pod %s to have all containers in Ready state", pod.Name) + available := false + progressing := false + degraded := false + for _, condition := range kv.Status.Conditions { + if condition.Type == v1.KubeVirtConditionAvailable && condition.Status == k8sv1.ConditionTrue { + available = true + } else if condition.Type == v1.KubeVirtConditionProgressing && condition.Status == k8sv1.ConditionTrue { + progressing = true + } else if condition.Type == v1.KubeVirtConditionDegraded && condition.Status == k8sv1.ConditionTrue { + degraded = true } } - id, ok := pod.Annotations[v1.InstallStrategyIdentifierAnnotation] - if !ok { - return fmt.Errorf("Pod %s is owned by operator but has no id annotation", pod.Name) + if !available || !progressing || !degraded { + return fmt.Errorf("Waiting for conditions to indicate update (conditions: %+v)", kv.Status.Conditions) } + return nil + }, 120*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) - expectedID := curKv.Status.ObservedDeploymentID - if id != expectedID { - return fmt.Errorf("Pod %s is of version %s when we expected id %s", pod.Name, id, expectedID) - } - podsReadyAndOwned++ - } + } - // this just sanity checks that at least one pod was found and verified. - // 0 would indicate our labeling was incorrect. - Expect(podsReadyAndOwned).ToNot(Equal(0)) + waitForKvWithTimeout = func(newKv *v1.KubeVirt, timeoutSeconds int) { + Eventually(func() error { + kv, err := virtClient.KubeVirt(newKv.Namespace).Get(newKv.Name, &metav1.GetOptions{}) + if err != nil { + return err + } - return nil - }, 120*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) - } + if kv.Status.Phase != v1.KubeVirtPhaseDeployed { + return fmt.Errorf("Waiting for phase to be deployed (current phase: %+v)", kv.Status.Phase) + } - waitForUpdateCondition := func(kv *v1.KubeVirt) { - Eventually(func() error { - kv, err := virtClient.KubeVirt(kv.Namespace).Get(kv.Name, &metav1.GetOptions{}) - if err != nil { - return err - } + available := false + progressing := true + degraded := true + created := false + for _, condition := range kv.Status.Conditions { + if condition.Type == v1.KubeVirtConditionAvailable && condition.Status == k8sv1.ConditionTrue { + available = true + } else if condition.Type == v1.KubeVirtConditionProgressing && condition.Status == k8sv1.ConditionFalse { + progressing = false + } else if condition.Type == v1.KubeVirtConditionDegraded && condition.Status == k8sv1.ConditionFalse { + degraded = false + } else if condition.Type == v1.KubeVirtConditionCreated && condition.Status == k8sv1.ConditionTrue { + created = true + } + } - available := false - progressing := false - degraded := false - for _, condition := range kv.Status.Conditions { - if condition.Type == v1.KubeVirtConditionAvailable && condition.Status == k8sv1.ConditionTrue { - available = true - } else if condition.Type == v1.KubeVirtConditionProgressing && condition.Status == k8sv1.ConditionTrue { - progressing = true - } else if condition.Type == v1.KubeVirtConditionDegraded && condition.Status == k8sv1.ConditionTrue { - degraded = true + if !available || progressing || degraded || !created { + return fmt.Errorf("Waiting for conditions to indicate deployment (conditions: %+v)", kv.Status.Conditions) } - } + return nil + }, time.Duration(timeoutSeconds)*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) + } - if !available || !progressing || !degraded { - return fmt.Errorf("Waiting for conditions to indicate update (conditions: %+v)", kv.Status.Conditions) - } - return nil - }, 120*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) + waitForKv = func(newKv *v1.KubeVirt) { + waitForKvWithTimeout(newKv, 300) + } - } + patchKvVersionAndRegistry = func(name string, version string, registry string) { + data := []byte(fmt.Sprintf(`[{ "op": "replace", "path": "/spec/imageTag", "value": "%s"},{ "op": "replace", "path": "/spec/imageRegistry", "value": "%s"}]`, version, registry)) + Eventually(func() error { + _, err := virtClient.KubeVirt(tests.KubeVirtInstallNamespace).Patch(name, types.JSONPatchType, data) - waitForKvWithTimeout := func(newKv *v1.KubeVirt, timeoutSeconds int) { - Eventually(func() error { - kv, err := virtClient.KubeVirt(newKv.Namespace).Get(newKv.Name, &metav1.GetOptions{}) - if err != nil { return err - } + }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) + } - if kv.Status.Phase != v1.KubeVirtPhaseDeployed { - return fmt.Errorf("Waiting for phase to be deployed (current phase: %+v)", kv.Status.Phase) - } + patchKvVersion = func(name string, version string) { + data := []byte(fmt.Sprintf(`[{ "op": "add", "path": "/spec/imageTag", "value": "%s"}]`, version)) + Eventually(func() error { + _, err := virtClient.KubeVirt(tests.KubeVirtInstallNamespace).Patch(name, types.JSONPatchType, data) - available := false - progressing := true - degraded := true - created := false - for _, condition := range kv.Status.Conditions { - if condition.Type == v1.KubeVirtConditionAvailable && condition.Status == k8sv1.ConditionTrue { - available = true - } else if condition.Type == v1.KubeVirtConditionProgressing && condition.Status == k8sv1.ConditionFalse { - progressing = false - } else if condition.Type == v1.KubeVirtConditionDegraded && condition.Status == k8sv1.ConditionFalse { - degraded = false - } else if condition.Type == v1.KubeVirtConditionCreated && condition.Status == k8sv1.ConditionTrue { - created = true - } - } + return err + }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) + } - if !available || progressing || degraded || !created { - return fmt.Errorf("Waiting for conditions to indicate deployment (conditions: %+v)", kv.Status.Conditions) - } - return nil - }, time.Duration(timeoutSeconds)*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) - } - - waitForKv := func(newKv *v1.KubeVirt) { - waitForKvWithTimeout(newKv, 300) - } - - patchKvVersionAndRegistry := func(name string, version string, registry string) { - data := []byte(fmt.Sprintf(`[{ "op": "replace", "path": "/spec/imageTag", "value": "%s"},{ "op": "replace", "path": "/spec/imageRegistry", "value": "%s"}]`, version, registry)) - Eventually(func() error { - _, err := virtClient.KubeVirt(tests.KubeVirtInstallNamespace).Patch(name, types.JSONPatchType, data) - - return err - }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) - } - - patchKvVersion := func(name string, version string) { - data := []byte(fmt.Sprintf(`[{ "op": "add", "path": "/spec/imageTag", "value": "%s"}]`, version)) - Eventually(func() error { - _, err := virtClient.KubeVirt(tests.KubeVirtInstallNamespace).Patch(name, types.JSONPatchType, data) - - return err - }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) - } - - parseDaemonset := func(name string) (daemonSet *v12.DaemonSet, image, registry, imagePrefix, version string) { - var err error - daemonSet, err = virtClient.AppsV1().DaemonSets(tests.KubeVirtInstallNamespace).Get(name, metav1.GetOptions{}) - Expect(err).ToNot(HaveOccurred()) - image = daemonSet.Spec.Template.Spec.Containers[0].Image - imageRegEx := regexp.MustCompile(fmt.Sprintf("%s%s%s", `^(.*)/(.*)`, name, `([@:].*)?$`)) - matches := imageRegEx.FindAllStringSubmatch(image, 1) - Expect(len(matches)).To(Equal(1)) - Expect(len(matches[0])).To(Equal(4)) - registry = matches[0][1] - imagePrefix = matches[0][2] - version = matches[0][3] - return - } - - parseImage := func(name, image string) (registry, imagePrefix, version string) { - imageRegEx := regexp.MustCompile(fmt.Sprintf("%s%s%s", `^(.*)/(.*)`, name, `([@:].*)?$`)) - matches := imageRegEx.FindAllStringSubmatch(image, 1) - Expect(len(matches)).To(Equal(1)) - Expect(len(matches[0])).To(Equal(4)) - registry = matches[0][1] - imagePrefix = matches[0][2] - version = matches[0][3] - return - } - - parseDeployment := func(name string) (deployment *v12.Deployment, image, registry, imagePrefix, version string) { - var err error - deployment, err = virtClient.AppsV1().Deployments(tests.KubeVirtInstallNamespace).Get(name, metav1.GetOptions{}) - Expect(err).ToNot(HaveOccurred()) - image = deployment.Spec.Template.Spec.Containers[0].Image - registry, imagePrefix, version = parseImage(name, image) - return - } + parseDaemonset = func(name string) (daemonSet *v12.DaemonSet, image, registry, imagePrefix, version string) { + var err error + daemonSet, err = virtClient.AppsV1().DaemonSets(tests.KubeVirtInstallNamespace).Get(name, metav1.GetOptions{}) + Expect(err).ToNot(HaveOccurred()) + image = daemonSet.Spec.Template.Spec.Containers[0].Image + imageRegEx := regexp.MustCompile(fmt.Sprintf("%s%s%s", `^(.*)/(.*)`, name, `([@:].*)?$`)) + matches := imageRegEx.FindAllStringSubmatch(image, 1) + Expect(len(matches)).To(Equal(1)) + Expect(len(matches[0])).To(Equal(4)) + registry = matches[0][1] + imagePrefix = matches[0][2] + version = matches[0][3] + return + } - parseOperatorImage := func() (operator *v12.Deployment, image, registry, imagePrefix, version string) { - return parseDeployment("virt-operator") - } + parseImage = func(name, image string) (registry, imagePrefix, version string) { + imageRegEx := regexp.MustCompile(fmt.Sprintf("%s%s%s", `^(.*)/(.*)`, name, `([@:].*)?$`)) + matches := imageRegEx.FindAllStringSubmatch(image, 1) + Expect(len(matches)).To(Equal(1)) + Expect(len(matches[0])).To(Equal(4)) + registry = matches[0][1] + imagePrefix = matches[0][2] + version = matches[0][3] + return + } - patchOperator := func(imagePrefix, version *string) bool { + parseDeployment = func(name string) (deployment *v12.Deployment, image, registry, imagePrefix, version string) { + var err error + deployment, err = virtClient.AppsV1().Deployments(tests.KubeVirtInstallNamespace).Get(name, metav1.GetOptions{}) + Expect(err).ToNot(HaveOccurred()) + image = deployment.Spec.Template.Spec.Containers[0].Image + registry, imagePrefix, version = parseImage(name, image) + return + } - modified := true + parseOperatorImage = func() (operator *v12.Deployment, image, registry, imagePrefix, version string) { + return parseDeployment("virt-operator") + } - Eventually(func() error { + patchOperator = func(imagePrefix, version *string) bool { - operator, oldImage, registry, oldPrefix, oldVersion := parseOperatorImage() - if imagePrefix == nil { - // keep old prefix - imagePrefix = &oldPrefix - } - if version == nil { - // keep old version - version = &oldVersion - } else { - newVersion := components.AddVersionSeparatorPrefix(*version) - version = &newVersion - } - newImage := fmt.Sprintf("%s/%svirt-operator%s", registry, *imagePrefix, *version) + modified := true - if oldImage == newImage { - modified = false - return nil - } + Eventually(func() error { - operator.Spec.Template.Spec.Containers[0].Image = newImage - for idx, env := range operator.Spec.Template.Spec.Containers[0].Env { - if env.Name == util.OperatorImageEnvName { - env.Value = newImage - operator.Spec.Template.Spec.Containers[0].Env[idx] = env - break + operator, oldImage, registry, oldPrefix, oldVersion := parseOperatorImage() + if imagePrefix == nil { + // keep old prefix + imagePrefix = &oldPrefix + } + if version == nil { + // keep old version + version = &oldVersion + } else { + newVersion := components.AddVersionSeparatorPrefix(*version) + version = &newVersion + } + newImage := fmt.Sprintf("%s/%svirt-operator%s", registry, *imagePrefix, *version) + + if oldImage == newImage { + modified = false + return nil + } + + operator.Spec.Template.Spec.Containers[0].Image = newImage + for idx, env := range operator.Spec.Template.Spec.Containers[0].Env { + if env.Name == util.OperatorImageEnvName { + env.Value = newImage + operator.Spec.Template.Spec.Containers[0].Env[idx] = env + break + } } - } - newTemplate, _ := json.Marshal(operator.Spec.Template) + newTemplate, _ := json.Marshal(operator.Spec.Template) - op := fmt.Sprintf(`[{ "op": "replace", "path": "/spec/template", "value": %s }]`, string(newTemplate)) + op := fmt.Sprintf(`[{ "op": "replace", "path": "/spec/template", "value": %s }]`, string(newTemplate)) - _, err = virtClient.AppsV1().Deployments(tests.KubeVirtInstallNamespace).Patch("virt-operator", types.JSONPatchType, []byte(op)) + _, err = virtClient.AppsV1().Deployments(tests.KubeVirtInstallNamespace).Patch("virt-operator", types.JSONPatchType, []byte(op)) - return err - }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) + return err + }, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) - return modified - } + return modified + } - deleteAllKvAndWait := func(ignoreOriginal bool) { - Eventually(func() error { + deleteAllKvAndWait = func(ignoreOriginal bool) { + Eventually(func() error { - kvs := tests.GetKvList(virtClient) + kvs := tests.GetKvList(virtClient) - deleteCount := 0 - for _, kv := range kvs { + deleteCount := 0 + for _, kv := range kvs { - if ignoreOriginal && kv.Name == originalKv.Name { - continue - } - deleteCount++ - if kv.DeletionTimestamp == nil { - err := virtClient.KubeVirt(kv.Namespace).Delete(kv.Name, &metav1.DeleteOptions{}) - if err != nil { - return err + if ignoreOriginal && kv.Name == originalKv.Name { + continue + } + deleteCount++ + if kv.DeletionTimestamp == nil { + err := virtClient.KubeVirt(kv.Namespace).Delete(kv.Name, &metav1.DeleteOptions{}) + if err != nil { + return err + } } } - } - if deleteCount != 0 { - return fmt.Errorf("still waiting on %d kvs to delete", deleteCount) - } - return nil - - }, 240*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) - - } + if deleteCount != 0 { + return fmt.Errorf("still waiting on %d kvs to delete", deleteCount) + } + return nil - usesSha := func(image string) bool { - return strings.Contains(image, "@sha256:") - } + }, 240*time.Second, 1*time.Second).ShouldNot(HaveOccurred()) - ensureShasums := func() { - if tests.SkipShasumCheck { - log.Log.Warning("Cannot use shasums, skipping") - return } - for _, name := range []string{"virt-operator", "virt-api", "virt-controller"} { - deployment, err := virtClient.AppsV1().Deployments(tests.KubeVirtInstallNamespace).Get(name, metav1.GetOptions{}) - Expect(err).ToNot(HaveOccurred()) - Expect(usesSha(deployment.Spec.Template.Spec.Containers[0].Image)).To(BeTrue(), fmt.Sprintf("%s should use sha", name)) + usesSha = func(image string) bool { + return strings.Contains(image, "@sha256:") } - handler, err := virtClient.AppsV1().DaemonSets(tests.KubeVirtInstallNamespace).Get("virt-handler", metav1.GetOptions{}) - Expect(err).ToNot(HaveOccurred()) - Expect(usesSha(handler.Spec.Template.Spec.Containers[0].Image)).To(BeTrue(), "virt-handler should use sha") - } + ensureShasums = func() { + if tests.SkipShasumCheck { + log.Log.Warning("Cannot use shasums, skipping") + return + } - tests.BeforeAll(func() { + for _, name := range []string{"virt-operator", "virt-api", "virt-controller"} { + deployment, err := virtClient.AppsV1().Deployments(tests.KubeVirtInstallNamespace).Get(name, metav1.GetOptions{}) + Expect(err).ToNot(HaveOccurred()) + Expect(usesSha(deployment.Spec.Template.Spec.Containers[0].Image)).To(BeTrue(), fmt.Sprintf("%s should use sha", name)) + } + + handler, err := virtClient.AppsV1().DaemonSets(tests.KubeVirtInstallNamespace).Get("virt-handler", metav1.GetOptions{}) + Expect(err).ToNot(HaveOccurred()) + Expect(usesSha(handler.Spec.Template.Spec.Containers[0].Image)).To(BeTrue(), "virt-handler should use sha") + } // make sure virt deployments use shasums before we start ensureShasums() @@ -465,29 +493,28 @@ var _ = Describe("Operator", func() { originalCDI = &cdiList.Items[0] } - }) - generatePreviousVersionVmYamls := func(previousImageRegistry string, previousImageTag string) { - ext, err := extclient.NewForConfig(virtClient.Config()) - Expect(err).ToNot(HaveOccurred()) + generatePreviousVersionVmYamls = func(previousImageRegistry string, previousImageTag string) { + ext, err := extclient.NewForConfig(virtClient.Config()) + Expect(err).ToNot(HaveOccurred()) - crd, err := ext.ApiextensionsV1beta1().CustomResourceDefinitions().Get("virtualmachines.kubevirt.io", metav1.GetOptions{}) - Expect(err).ToNot(HaveOccurred()) + crd, err := ext.ApiextensionsV1beta1().CustomResourceDefinitions().Get("virtualmachines.kubevirt.io", metav1.GetOptions{}) + Expect(err).ToNot(HaveOccurred()) - // Generate a vm Yaml for every version supported in the currently deployed KubeVirt + // Generate a vm Yaml for every version supported in the currently deployed KubeVirt - supportedVersions := []string{} + supportedVersions := []string{} - if len(crd.Spec.Versions) > 0 { - for _, version := range crd.Spec.Versions { - supportedVersions = append(supportedVersions, version.Name) + if len(crd.Spec.Versions) > 0 { + for _, version := range crd.Spec.Versions { + supportedVersions = append(supportedVersions, version.Name) + } + } else { + supportedVersions = append(supportedVersions, crd.Spec.Version) } - } else { - supportedVersions = append(supportedVersions, crd.Spec.Version) - } - for _, version := range supportedVersions { - vmYaml := fmt.Sprintf(`apiVersion: kubevirt.io/%s + for _, version := range supportedVersions { + vmYaml := fmt.Sprintf(`apiVersion: kubevirt.io/%s kind: VirtualMachine metadata: labels: @@ -527,20 +554,21 @@ spec: name: cloudinitdisk `, version, version, version, version, previousImageRegistry, tests.ContainerDiskCirros, previousImageTag) - yamlFile := filepath.Join(workDir, fmt.Sprintf("vm-%s.yaml", version)) - err = ioutil.WriteFile(yamlFile, []byte(vmYaml), 0644) + yamlFile := filepath.Join(workDir, fmt.Sprintf("vm-%s.yaml", version)) + err = ioutil.WriteFile(yamlFile, []byte(vmYaml), 0644) - Expect(err).ToNot(HaveOccurred()) + Expect(err).ToNot(HaveOccurred()) - vmYamls = append(vmYamls, vmYamlDefinition{ - apiVersion: version, - vmName: "vm-" + version, - generatedYaml: vmYaml, - yamlFile: yamlFile, - }) - } + vmYamls = append(vmYamls, vmYamlDefinition{ + apiVersion: version, + vmName: "vm-" + version, + generatedYaml: vmYaml, + yamlFile: yamlFile, + }) + } - } + } + }) BeforeEach(func() { tests.BeforeTestCleanup() diff --git a/tests/pausing_test.go b/tests/pausing_test.go index f5861e762dad..e46ccc7fcf56 100644 --- a/tests/pausing_test.go +++ b/tests/pausing_test.go @@ -46,10 +46,13 @@ var _ = Describe("[rfe_id:3064][crit:medium][vendor:cnv-qe@redhat.com][level:com tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) diff --git a/tests/probes_test.go b/tests/probes_test.go index a7da8d0bad6c..ba51a7c875c6 100644 --- a/tests/probes_test.go +++ b/tests/probes_test.go @@ -16,10 +16,13 @@ import ( var _ = Describe("[ref_id:1182]Probes", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) diff --git a/tests/replicaset_test.go b/tests/replicaset_test.go index f60b5c692651..92b8b5d16e19 100644 --- a/tests/replicaset_test.go +++ b/tests/replicaset_test.go @@ -41,13 +41,15 @@ import ( ) var _ = Describe("[rfe_id:588][crit:medium][vendor:cnv-qe@redhat.com][level:component]VirtualMachineInstanceReplicaSet", func() { + var err error + var virtClient kubecli.KubevirtClient tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) - BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) diff --git a/tests/security_features_test.go b/tests/security_features_test.go index ad48592cc84e..aeb6e9931f1f 100644 --- a/tests/security_features_test.go +++ b/tests/security_features_test.go @@ -36,10 +36,13 @@ import ( var _ = Describe("SecurityFeatures", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) diff --git a/tests/snapshot_test.go b/tests/snapshot_test.go index f51e850d2859..fa50d571d576 100644 --- a/tests/snapshot_test.go +++ b/tests/snapshot_test.go @@ -23,11 +23,16 @@ var _ = Describe("VirtualMachineSnapshot Tests", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient groupName := "kubevirt.io/v1alpha3" + BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + }) + Context("With simple VM", func() { var vm *v1.VirtualMachine diff --git a/tests/stability_test.go b/tests/stability_test.go index fd6996501f29..613398425b4b 100644 --- a/tests/stability_test.go +++ b/tests/stability_test.go @@ -12,10 +12,13 @@ var _ = PDescribe("Ensure stable functionality", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) diff --git a/tests/storage_test.go b/tests/storage_test.go index 781fa91a2bc1..eceb4386b377 100644 --- a/tests/storage_test.go +++ b/tests/storage_test.go @@ -54,10 +54,13 @@ type VMICreationFunc func(string) *v1.VirtualMachineInstance var _ = Describe("Storage", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) diff --git a/tests/subresource_api_test.go b/tests/subresource_api_test.go index cd02384f5970..17a50c62cf4a 100644 --- a/tests/subresource_api_test.go +++ b/tests/subresource_api_test.go @@ -37,13 +37,16 @@ var _ = Describe("Subresource Api", func() { tests.FlagParse() - virtCli, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtCli kubecli.KubevirtClient manual := v1.RunStrategyManual restartOnError := v1.RunStrategyRerunOnFailure BeforeEach(func() { + virtCli, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) diff --git a/tests/template_test.go b/tests/template_test.go index 65c79aab8646..8849f694b3cf 100644 --- a/tests/template_test.go +++ b/tests/template_test.go @@ -49,8 +49,8 @@ const ( var _ = Describe("Templates", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient var ( templateParams map[string]string @@ -60,6 +60,9 @@ var _ = Describe("Templates", func() { ) BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.SkipIfNoCmd("oc") tests.BeforeTestCleanup() SetDefaultEventuallyTimeout(120 * time.Second) diff --git a/tests/version_test.go b/tests/version_test.go index ded917b7dae2..46678fda5cca 100644 --- a/tests/version_test.go +++ b/tests/version_test.go @@ -34,10 +34,13 @@ var _ = Describe("Version", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) diff --git a/tests/virt_control_plane_test.go b/tests/virt_control_plane_test.go index b781374bd79d..09292f815c01 100644 --- a/tests/virt_control_plane_test.go +++ b/tests/virt_control_plane_test.go @@ -45,14 +45,18 @@ const ( var _ = Describe("[ref_id:2717]KubeVirt control plane resilience", func() { - RegisterFailHandler(Fail) + var err error + var virtCli kubecli.KubevirtClient - virtCli, err := kubecli.GetKubevirtClient() - Expect(err).ToNot(HaveOccurred()) - deploymentsClient := virtCli.AppsV1().Deployments(tests.KubeVirtInstallNamespace) + RegisterFailHandler(Fail) controlPlaneDeploymentNames := []string{"virt-api", "virt-controller"} + BeforeEach(func() { + virtCli, err = kubecli.GetKubevirtClient() + Expect(err).ToNot(HaveOccurred()) + }) + Context("pod eviction", func() { var nodeNames []string @@ -100,6 +104,7 @@ var _ = Describe("[ref_id:2717]KubeVirt control plane resilience", func() { } waitForDeploymentsToStabilize := func() (bool, error) { + deploymentsClient := virtCli.AppsV1().Deployments(tests.KubeVirtInstallNamespace) for _, deploymentName := range controlPlaneDeploymentNames { deployment, err := deploymentsClient.Get(deploymentName, metav1.GetOptions{}) if err != nil { @@ -133,6 +138,7 @@ var _ = Describe("[ref_id:2717]KubeVirt control plane resilience", func() { // Add nodeSelector to deployments so that they get scheduled to selectedNode addNodeSelectorToDeployments := func() (bool, error) { + deploymentsClient := virtCli.AppsV1().Deployments(tests.KubeVirtInstallNamespace) for _, deploymentName := range controlPlaneDeploymentNames { deployment, err := deploymentsClient.Get(deploymentName, metav1.GetOptions{}) if err != nil { @@ -215,6 +221,7 @@ var _ = Describe("[ref_id:2717]KubeVirt control plane resilience", func() { }) removeNodeSelectorFromDeployments := func() (bool, error) { + deploymentsClient := virtCli.AppsV1().Deployments(tests.KubeVirtInstallNamespace) for _, deploymentName := range controlPlaneDeploymentNames { deployment, err := deploymentsClient.Get(deploymentName, metav1.GetOptions{}) if err != nil { @@ -296,7 +303,7 @@ var _ = Describe("[ref_id:2717]KubeVirt control plane resilience", func() { When("control plane pods are running", func() { It("[test_id:2806]virt-controller and virt-api pods have a pod disruption budget", func() { - + deploymentsClient := virtCli.AppsV1().Deployments(tests.KubeVirtInstallNamespace) By("check deployments") deployments, err := deploymentsClient.List(metav1.ListOptions{}) Expect(err).ToNot(HaveOccurred()) diff --git a/tests/vm_test.go b/tests/vm_test.go index f74843b5b26c..f6938a63d053 100644 --- a/tests/vm_test.go +++ b/tests/vm_test.go @@ -54,13 +54,16 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:cnv-qe@redhat.com][level:com tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient runStrategyAlways := v1.RunStrategyAlways runStrategyHalted := v1.RunStrategyHalted BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) diff --git a/tests/vm_watch_test.go b/tests/vm_watch_test.go index d458b8961532..258919896641 100644 --- a/tests/vm_watch_test.go +++ b/tests/vm_watch_test.go @@ -143,8 +143,9 @@ func createCommandWithNSAndRedirect(namespace, cmdName string, args ...string) ( var _ = Describe("[rfe_id:3423][crit:high][vendor:cnv-qe@redhat.com][level:component]VmWatch", func() { tests.FlagParse() - virtCli, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtCli kubecli.KubevirtClient + var vm *v12.VirtualMachine // Reads an error from stderr and fails the test @@ -162,6 +163,9 @@ var _ = Describe("[rfe_id:3423][crit:high][vendor:cnv-qe@redhat.com][level:compo } BeforeEach(func() { + virtCli, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.SkipIfVersionBelow("Printing format for `kubectl get -w` on custom resources is only relevant for 1.16.2+", relevantk8sVer) tests.BeforeTestCleanup() diff --git a/tests/vmi_cloudinit_hook_sidecar_test.go b/tests/vmi_cloudinit_hook_sidecar_test.go index f5eb29f038e2..c7e6e35391ce 100644 --- a/tests/vmi_cloudinit_hook_sidecar_test.go +++ b/tests/vmi_cloudinit_hook_sidecar_test.go @@ -39,8 +39,8 @@ var _ = Describe("CloudInitHookSidecars", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient var vmi *v1.VirtualMachineInstance @@ -85,6 +85,9 @@ var _ = Describe("CloudInitHookSidecars", func() { } BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() vmi = tests.NewRandomVMIWithEphemeralDiskAndUserdata(tests.ContainerDiskFor(tests.ContainerDiskCirros), "#FAKE") vmi.ObjectMeta.Annotations = map[string]string{ diff --git a/tests/vmi_cloudinit_test.go b/tests/vmi_cloudinit_test.go index cdef516b8b0a..762adb3cf936 100644 --- a/tests/vmi_cloudinit_test.go +++ b/tests/vmi_cloudinit_test.go @@ -53,78 +53,92 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:cnv-qe@redhat.com][level:compon tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) - - LaunchVMI := func(vmi *v1.VirtualMachineInstance) { - By("Starting a VirtualMachineInstance") - obj, err := virtClient.RestClient().Post().Resource("virtualmachineinstances").Namespace(tests.NamespaceTestDefault).Body(vmi).Do().Get() - Expect(err).To(BeNil()) - - By("Waiting the VirtualMachineInstance start") - _, ok := obj.(*v1.VirtualMachineInstance) - Expect(ok).To(BeTrue(), "Object is not of type *v1.VirtualMachineInstance") - Expect(tests.WaitForSuccessfulVMIStart(obj)).ToNot(BeEmpty()) - } - - VerifyUserDataVMI := func(vmi *v1.VirtualMachineInstance, commands []expect.Batcher, timeout time.Duration) { - By("Expecting the VirtualMachineInstance console") - expecter, _, err := tests.NewConsoleExpecter(virtClient, vmi, 10*time.Second) - Expect(err).ToNot(HaveOccurred()) - defer expecter.Close() - - By("Checking that the VirtualMachineInstance serial console output equals to expected one") - resp, err := expecter.ExpectBatch(commands, timeout) - log.DefaultLogger().Object(vmi).Infof("%v", resp) - Expect(err).ToNot(HaveOccurred()) - } - - mountCloudInitFunc := func(devName string) func(*v1.VirtualMachineInstance, string) { - return func(vmi *v1.VirtualMachineInstance, prompt string) { - cmdCheck := fmt.Sprintf("mount $(blkid -L %s) /mnt/\n", devName) + var err error + var virtClient kubecli.KubevirtClient + + var ( + LaunchVMI func(*v1.VirtualMachineInstance) + VerifyUserDataVMI func(*v1.VirtualMachineInstance, []expect.Batcher, time.Duration) + MountCloudInitNoCloud func(*v1.VirtualMachineInstance, string) + MountCloudInitConfigDrive func(*v1.VirtualMachineInstance, string) + CheckCloudInitFile func(*v1.VirtualMachineInstance, string, string, string) + CheckCloudInitMetaData func(*v1.VirtualMachineInstance, string, string, string) + ) + + tests.BeforeAll(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + + LaunchVMI = func(vmi *v1.VirtualMachineInstance) { + By("Starting a VirtualMachineInstance") + obj, err := virtClient.RestClient().Post().Resource("virtualmachineinstances").Namespace(tests.NamespaceTestDefault).Body(vmi).Do().Get() + Expect(err).To(BeNil()) + + By("Waiting the VirtualMachineInstance start") + _, ok := obj.(*v1.VirtualMachineInstance) + Expect(ok).To(BeTrue(), "Object is not of type *v1.VirtualMachineInstance") + Expect(tests.WaitForSuccessfulVMIStart(obj)).ToNot(BeEmpty()) + } + + VerifyUserDataVMI = func(vmi *v1.VirtualMachineInstance, commands []expect.Batcher, timeout time.Duration) { + By("Expecting the VirtualMachineInstance console") + expecter, _, err := tests.NewConsoleExpecter(virtClient, vmi, 10*time.Second) + Expect(err).ToNot(HaveOccurred()) + defer expecter.Close() + + By("Checking that the VirtualMachineInstance serial console output equals to expected one") + resp, err := expecter.ExpectBatch(commands, timeout) + log.DefaultLogger().Object(vmi).Infof("%v", resp) + Expect(err).ToNot(HaveOccurred()) + } + + mountCloudInitFunc := func(devName string) func(*v1.VirtualMachineInstance, string) { + return func(vmi *v1.VirtualMachineInstance, prompt string) { + cmdCheck := fmt.Sprintf("mount $(blkid -L %s) /mnt/\n", devName) + err := tests.CheckForTextExpecter(vmi, []expect.Batcher{ + &expect.BSnd{S: "sudo su -\n"}, + &expect.BExp{R: prompt}, + &expect.BSnd{S: cmdCheck}, + &expect.BExp{R: prompt}, + &expect.BSnd{S: "echo $?\n"}, + &expect.BExp{R: "0"}, + }, 15) + Expect(err).ToNot(HaveOccurred()) + } + } + + MountCloudInitNoCloud = mountCloudInitFunc("cidata") + MountCloudInitConfigDrive = mountCloudInitFunc("config-2") + + CheckCloudInitFile = func(vmi *v1.VirtualMachineInstance, prompt, testFile, testData string) { + cmdCheck := "cat /mnt/" + testFile + "\n" err := tests.CheckForTextExpecter(vmi, []expect.Batcher{ &expect.BSnd{S: "sudo su -\n"}, &expect.BExp{R: prompt}, &expect.BSnd{S: cmdCheck}, - &expect.BExp{R: prompt}, - &expect.BSnd{S: "echo $?\n"}, - &expect.BExp{R: "0"}, + &expect.BExp{R: testData}, }, 15) Expect(err).ToNot(HaveOccurred()) } - } - - MountCloudInitNoCloud := mountCloudInitFunc("cidata") - MountCloudInitConfigDrive := mountCloudInitFunc("config-2") - - CheckCloudInitFile := func(vmi *v1.VirtualMachineInstance, prompt, testFile, testData string) { - cmdCheck := "cat /mnt/" + testFile + "\n" - err := tests.CheckForTextExpecter(vmi, []expect.Batcher{ - &expect.BSnd{S: "sudo su -\n"}, - &expect.BExp{R: prompt}, - &expect.BSnd{S: cmdCheck}, - &expect.BExp{R: testData}, - }, 15) - Expect(err).ToNot(HaveOccurred()) - } - CheckCloudInitMetaData := func(vmi *v1.VirtualMachineInstance, prompt, testFile, testData string) { - cmdCheck := "cat /mnt/" + testFile + "\n" - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) - expecter, _, err := tests.NewConsoleExpecter(virtClient, vmi, 30*time.Second) - Expect(err).ToNot(HaveOccurred()) - defer expecter.Close() - - res, err := expecter.ExpectBatch([]expect.Batcher{ - &expect.BSnd{S: "sudo su -\n"}, - &expect.BExp{R: prompt}, - &expect.BSnd{S: cmdCheck}, - &expect.BExp{R: testData}, - }, 15*time.Second) - if err != nil { - Expect(res[1].Output).To(ContainSubstring(testData)) + CheckCloudInitMetaData = func(vmi *v1.VirtualMachineInstance, prompt, testFile, testData string) { + cmdCheck := "cat /mnt/" + testFile + "\n" + virtClient, err := kubecli.GetKubevirtClient() + tests.PanicOnError(err) + expecter, _, err := tests.NewConsoleExpecter(virtClient, vmi, 30*time.Second) + Expect(err).ToNot(HaveOccurred()) + defer expecter.Close() + + res, err := expecter.ExpectBatch([]expect.Batcher{ + &expect.BSnd{S: "sudo su -\n"}, + &expect.BExp{R: prompt}, + &expect.BSnd{S: cmdCheck}, + &expect.BExp{R: testData}, + }, 15*time.Second) + if err != nil { + Expect(res[1].Output).To(ContainSubstring(testData)) + } } - } + }) BeforeEach(func() { tests.BeforeTestCleanup() diff --git a/tests/vmi_configuration_test.go b/tests/vmi_configuration_test.go index 6a973ec0938e..15f7b5dcd078 100644 --- a/tests/vmi_configuration_test.go +++ b/tests/vmi_configuration_test.go @@ -55,10 +55,13 @@ var _ = Describe("Configurations", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) @@ -168,10 +171,13 @@ var _ = Describe("Configurations", func() { Describe("VirtualMachineInstance definition", func() { Context("[rfe_id:2065][crit:medium][vendor:cnv-qe@redhat.com][level:component]with 3 CPU cores", func() { - availableNumberOfCPUs := tests.GetHighestCPUNumberAmongNodes(virtClient) - + var availableNumberOfCPUs int var vmi *v1.VirtualMachineInstance + tests.BeforeAll(func() { + availableNumberOfCPUs = tests.GetHighestCPUNumberAmongNodes(virtClient) + }) + BeforeEach(func() { requiredNumberOfCpus := 3 Expect(availableNumberOfCPUs).ToNot(BeNumerically("<", requiredNumberOfCpus), diff --git a/tests/vmi_gpu_test.go b/tests/vmi_gpu_test.go index 99655c3bffae..68876e831c38 100644 --- a/tests/vmi_gpu_test.go +++ b/tests/vmi_gpu_test.go @@ -51,8 +51,13 @@ func checkGPUDevice(vmi *v1.VirtualMachineInstance, gpuName string, prompt strin var _ = Describe("GPU", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient + + BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + }) Context("with ephemeral disk", func() { It("Should create a valid VMI but pod should not go to running state", func() { diff --git a/tests/vmi_headless_test.go b/tests/vmi_headless_test.go index 1a216cab037e..6b5ac3969d8a 100644 --- a/tests/vmi_headless_test.go +++ b/tests/vmi_headless_test.go @@ -36,12 +36,14 @@ var _ = Describe("[rfe_id:609]VMIheadless", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) - + var err error + var virtClient kubecli.KubevirtClient var vmi *v1.VirtualMachineInstance BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() vmi = tests.NewRandomVMIWithEphemeralDisk(tests.ContainerDiskFor(tests.ContainerDiskAlpine)) }) diff --git a/tests/vmi_hook_sidecar_test.go b/tests/vmi_hook_sidecar_test.go index e9ab157896e4..0640900dab3d 100644 --- a/tests/vmi_hook_sidecar_test.go +++ b/tests/vmi_hook_sidecar_test.go @@ -44,12 +44,15 @@ var _ = Describe("HookSidecars", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient var vmi *v1.VirtualMachineInstance BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() vmi = tests.NewRandomVMIWithEphemeralDisk(tests.ContainerDiskFor(tests.ContainerDiskAlpine)) vmi.ObjectMeta.Annotations = RenderSidecar(hooksv1alpha1.Version) diff --git a/tests/vmi_ignition_test.go b/tests/vmi_ignition_test.go index 7717fb6623ef..a86951e5c9e9 100644 --- a/tests/vmi_ignition_test.go +++ b/tests/vmi_ignition_test.go @@ -36,31 +36,39 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:cnv-qe@redhat.com][level:compon tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) - - LaunchVMI := func(vmi *v1.VirtualMachineInstance) { - By("Starting a VirtualMachineInstance") - obj, err := virtClient.RestClient().Post().Resource("virtualmachineinstances").Namespace(tests.NamespaceTestDefault).Body(vmi).Do().Get() - Expect(err).To(BeNil()) - - By("Waiting the VirtualMachineInstance start") - _, ok := obj.(*v1.VirtualMachineInstance) - Expect(ok).To(BeTrue(), "Object is not of type *v1.VirtualMachineInstance") - Expect(tests.WaitForSuccessfulVMIStart(obj)).ToNot(BeEmpty()) - } - - VerifyIgnitionDataVMI := func(vmi *v1.VirtualMachineInstance, commands []expect.Batcher, timeout time.Duration) { - By("Expecting the VirtualMachineInstance console") - expecter, _, err := tests.NewConsoleExpecter(virtClient, vmi, 10*time.Second) - Expect(err).ToNot(HaveOccurred()) - defer expecter.Close() - - By("Checking that the VirtualMachineInstance serial console output equals to expected one") - resp, err := expecter.ExpectBatch(commands, timeout) - log.DefaultLogger().Object(vmi).Infof("%v", resp) - Expect(err).ToNot(HaveOccurred()) - } + var err error + var virtClient kubecli.KubevirtClient + + var LaunchVMI func(*v1.VirtualMachineInstance) + var VerifyIgnitionDataVMI func(*v1.VirtualMachineInstance, []expect.Batcher, time.Duration) + + tests.BeforeAll(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + + LaunchVMI = func(vmi *v1.VirtualMachineInstance) { + By("Starting a VirtualMachineInstance") + obj, err := virtClient.RestClient().Post().Resource("virtualmachineinstances").Namespace(tests.NamespaceTestDefault).Body(vmi).Do().Get() + Expect(err).To(BeNil()) + + By("Waiting the VirtualMachineInstance start") + _, ok := obj.(*v1.VirtualMachineInstance) + Expect(ok).To(BeTrue(), "Object is not of type *v1.VirtualMachineInstance") + Expect(tests.WaitForSuccessfulVMIStart(obj)).ToNot(BeEmpty()) + } + + VerifyIgnitionDataVMI = func(vmi *v1.VirtualMachineInstance, commands []expect.Batcher, timeout time.Duration) { + By("Expecting the VirtualMachineInstance console") + expecter, _, err := tests.NewConsoleExpecter(virtClient, vmi, 10*time.Second) + Expect(err).ToNot(HaveOccurred()) + defer expecter.Close() + + By("Checking that the VirtualMachineInstance serial console output equals to expected one") + resp, err := expecter.ExpectBatch(commands, timeout) + log.DefaultLogger().Object(vmi).Infof("%v", resp) + Expect(err).ToNot(HaveOccurred()) + } + }) BeforeEach(func() { tests.BeforeTestCleanup() diff --git a/tests/vmi_iothreads_test.go b/tests/vmi_iothreads_test.go index 2014ea5279b6..a98c4cb30555 100644 --- a/tests/vmi_iothreads_test.go +++ b/tests/vmi_iothreads_test.go @@ -41,20 +41,26 @@ import ( var _ = Describe("IOThreads", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient var vmi *v1.VirtualMachineInstance dedicated := true BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() vmi = tests.NewRandomVMIWithEphemeralDisk(tests.ContainerDiskFor(tests.ContainerDiskAlpine)) }) Context("IOThreads Policies", func() { + var availableCPUs int - availableCPUs := tests.GetHighestCPUNumberAmongNodes(virtClient) + tests.BeforeAll(func() { + availableCPUs = tests.GetHighestCPUNumberAmongNodes(virtClient) + }) It("[test_id:4122]Should honor shared ioThreadsPolicy for single disk", func() { policy := v1.IOThreadsPolicyShared diff --git a/tests/vmi_lifecycle_test.go b/tests/vmi_lifecycle_test.go index 8316ffddd69f..ab14b1ce54d4 100644 --- a/tests/vmi_lifecycle_test.go +++ b/tests/vmi_lifecycle_test.go @@ -75,14 +75,17 @@ var _ = Describe("[rfe_id:273][crit:high][vendor:cnv-qe@redhat.com][level:compon tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient var vmi *v1.VirtualMachineInstance var useEmulation *bool BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() vmi = tests.NewRandomVMIWithEphemeralDisk(tests.ContainerDiskFor(tests.ContainerDiskAlpine)) }) diff --git a/tests/vmi_monitoring_test.go b/tests/vmi_monitoring_test.go index df6f3d784d4a..4eb0ade886b4 100644 --- a/tests/vmi_monitoring_test.go +++ b/tests/vmi_monitoring_test.go @@ -36,16 +36,23 @@ var _ = Describe("Health Monitoring", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient - launchVMI := func(vmi *v1.VirtualMachineInstance) { - By("Starting a VirtualMachineInstance") - obj, err := virtClient.RestClient().Post().Resource("virtualmachineinstances").Namespace(tests.NamespaceTestDefault).Body(vmi).Do().Get() - Expect(err).To(BeNil()) + var launchVMI func(*v1.VirtualMachineInstance) - tests.WaitForSuccessfulVMIStart(obj) - } + tests.BeforeAll(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + + launchVMI = func(vmi *v1.VirtualMachineInstance) { + By("Starting a VirtualMachineInstance") + obj, err := virtClient.RestClient().Post().Resource("virtualmachineinstances").Namespace(tests.NamespaceTestDefault).Body(vmi).Do().Get() + Expect(err).To(BeNil()) + + tests.WaitForSuccessfulVMIStart(obj) + } + }) BeforeEach(func() { tests.BeforeTestCleanup() diff --git a/tests/vmi_multiqueue_test.go b/tests/vmi_multiqueue_test.go index 3e2dd90ae6b3..ad43b2beffee 100644 --- a/tests/vmi_multiqueue_test.go +++ b/tests/vmi_multiqueue_test.go @@ -39,16 +39,22 @@ import ( var _ = Describe("MultiQueue", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() }) Context("MultiQueue Behavior", func() { + var availableCPUs int - availableCPUs := tests.GetHighestCPUNumberAmongNodes(virtClient) + tests.BeforeAll(func() { + availableCPUs = tests.GetHighestCPUNumberAmongNodes(virtClient) + }) It("should be able to successfully boot fedora to the login prompt with networking mutiqueues enabled without being blocked by selinux", func() { vmi := tests.NewRandomFedoraVMIWitGuestAgent() diff --git a/tests/vmi_multus_test.go b/tests/vmi_multus_test.go index fed153b5512b..ecb035be9045 100644 --- a/tests/vmi_multus_test.go +++ b/tests/vmi_multus_test.go @@ -59,8 +59,8 @@ var _ = Describe("Multus", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient var nodes *k8sv1.NodeList @@ -95,6 +95,9 @@ var _ = Describe("Multus", func() { } BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + // Multus tests need to ensure that old VMIs are gone Expect(virtClient.RestClient().Delete().Namespace(tests.NamespaceTestDefault).Resource("virtualmachineinstances").Do().Error()).To(Succeed()) Expect(virtClient.RestClient().Delete().Namespace(tests.NamespaceTestAlternative).Resource("virtualmachineinstances").Do().Error()).To(Succeed()) @@ -569,8 +572,8 @@ var _ = Describe("SRIOV", func() { flag.Parse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient sriovResourceName := os.Getenv("SRIOV_RESOURCE_NAME") @@ -579,6 +582,9 @@ var _ = Describe("SRIOV", func() { } tests.BeforeAll(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() // Check if the hardware supports SRIOV sriovcheck := checkSriovEnabled(virtClient, sriovResourceName) diff --git a/tests/vmi_networking_test.go b/tests/vmi_networking_test.go index 370549b44c9b..49a994996cf9 100644 --- a/tests/vmi_networking_test.go +++ b/tests/vmi_networking_test.go @@ -51,37 +51,45 @@ var _ = Describe("[rfe_id:694][crit:medium][vendor:cnv-qe@redhat.com][level:comp tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient var inboundVMI *v1.VirtualMachineInstance var inboundVMIWithPodNetworkSet *v1.VirtualMachineInstance var inboundVMIWithCustomMacAddress *v1.VirtualMachineInstance var outboundVMI *v1.VirtualMachineInstance + var logPodLogs func(*v12.Pod) + var waitForPodToFinish func(*v12.Pod) v12.PodPhase + const testPort = 1500 - logPodLogs := func(pod *v12.Pod) { - defer GinkgoRecover() + tests.BeforeAll(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) - var s int64 = 500 - logs := virtClient.CoreV1().Pods(inboundVMI.Namespace).GetLogs(pod.Name, &v12.PodLogOptions{SinceSeconds: &s}) - rawLogs, err := logs.DoRaw() - Expect(err).ToNot(HaveOccurred()) - log.Log.Infof("%s", string(rawLogs)) - } + logPodLogs = func(pod *v12.Pod) { + defer GinkgoRecover() + + var s int64 = 500 + logs := virtClient.CoreV1().Pods(inboundVMI.Namespace).GetLogs(pod.Name, &v12.PodLogOptions{SinceSeconds: &s}) + rawLogs, err := logs.DoRaw() + Expect(err).ToNot(HaveOccurred()) + log.Log.Infof("%s", string(rawLogs)) + } - waitForPodToFinish := func(pod *v12.Pod) v12.PodPhase { - Eventually(func() v12.PodPhase { + waitForPodToFinish = func(pod *v12.Pod) v12.PodPhase { + Eventually(func() v12.PodPhase { + j, err := virtClient.CoreV1().Pods(inboundVMI.ObjectMeta.Namespace).Get(pod.ObjectMeta.Name, v13.GetOptions{}) + Expect(err).ToNot(HaveOccurred()) + return j.Status.Phase + }, 90*time.Second, 1*time.Second).Should(Or(Equal(v12.PodSucceeded), Equal(v12.PodFailed))) j, err := virtClient.CoreV1().Pods(inboundVMI.ObjectMeta.Namespace).Get(pod.ObjectMeta.Name, v13.GetOptions{}) Expect(err).ToNot(HaveOccurred()) + logPodLogs(pod) return j.Status.Phase - }, 90*time.Second, 1*time.Second).Should(Or(Equal(v12.PodSucceeded), Equal(v12.PodFailed))) - j, err := virtClient.CoreV1().Pods(inboundVMI.ObjectMeta.Namespace).Get(pod.ObjectMeta.Name, v13.GetOptions{}) - Expect(err).ToNot(HaveOccurred()) - logPodLogs(pod) - return j.Status.Phase - } + } + }) checkMacAddress := func(vmi *v1.VirtualMachineInstance, expectedMacAddress string, prompt string) { err := tests.CheckForTextExpecter(vmi, []expect.Batcher{ diff --git a/tests/vmi_slirp_interface_test.go b/tests/vmi_slirp_interface_test.go index 167e686d39e8..8899bf377171 100644 --- a/tests/vmi_slirp_interface_test.go +++ b/tests/vmi_slirp_interface_test.go @@ -42,8 +42,8 @@ var _ = Describe("Slirp Networking", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient var genericVmi *v1.VirtualMachineInstance var deadbeafVmi *v1.VirtualMachineInstance @@ -57,6 +57,9 @@ var _ = Describe("Slirp Networking", func() { } tests.BeforeAll(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + setSlirpEnabled(true) ports := []v1.Port{{Name: "http", Port: 80}} genericVmi = tests.NewRandomVMIWithSlirpInterfaceEphemeralDiskAndUserdata(tests.ContainerDiskFor(tests.ContainerDiskCirros), "#!/bin/bash\necho 'hello'\n", ports) diff --git a/tests/vmidefaults_test.go b/tests/vmidefaults_test.go index f6fe2071039c..52d10d5bb256 100644 --- a/tests/vmidefaults_test.go +++ b/tests/vmidefaults_test.go @@ -34,12 +34,15 @@ import ( var _ = Describe("VMIDefaults", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient var vmi *v1.VirtualMachineInstance BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() // create VMI with missing disk target vmi = tests.NewRandomVMI() diff --git a/tests/vmipreset_test.go b/tests/vmipreset_test.go index 908fed23134d..96ce399972d7 100644 --- a/tests/vmipreset_test.go +++ b/tests/vmipreset_test.go @@ -41,8 +41,8 @@ import ( var _ = Describe("[rfe_id:609][crit:medium][vendor:cnv-qe@redhat.com][level:component]VMIPreset", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient var vmi *v1.VirtualMachineInstance var memoryPreset *v1.VirtualMachineInstancePreset @@ -58,6 +58,9 @@ var _ = Describe("[rfe_id:609][crit:medium][vendor:cnv-qe@redhat.com][level:comp cores := 7 BeforeEach(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() vmi = tests.NewRandomVMIWithEphemeralDisk(tests.ContainerDiskFor(tests.ContainerDiskAlpine)) vmi.Labels = map[string]string{flavorKey: memoryFlavor} diff --git a/tests/vnc_test.go b/tests/vnc_test.go index 27b9c3650a5b..a21a102317f2 100644 --- a/tests/vnc_test.go +++ b/tests/vnc_test.go @@ -42,12 +42,15 @@ var _ = Describe("[rfe_id:127][crit:medium][vendor:cnv-qe@redhat.com][level:comp tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient var vmi *v1.VirtualMachineInstance Describe("[rfe_id:127][crit:medium][vendor:cnv-qe@redhat.com][level:component]A new VirtualMachineInstance", func() { tests.BeforeAll(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.BeforeTestCleanup() vmi = tests.NewRandomVMI() Expect(virtClient.RestClient().Post().Resource("virtualmachineinstances").Namespace(tests.NamespaceTestDefault).Body(vmi).Do().Error()).To(Succeed()) diff --git a/tests/windows_test.go b/tests/windows_test.go index 3219ed483078..bf744e02b2cb 100644 --- a/tests/windows_test.go +++ b/tests/windows_test.go @@ -114,12 +114,15 @@ var getWindowsVMISpec = func() v1.VirtualMachineInstanceSpec { var _ = Describe("Windows VirtualMachineInstance", func() { tests.FlagParse() - virtClient, err := kubecli.GetKubevirtClient() - tests.PanicOnError(err) + var err error + var virtClient kubecli.KubevirtClient var windowsVMI *v1.VirtualMachineInstance tests.BeforeAll(func() { + virtClient, err = kubecli.GetKubevirtClient() + tests.PanicOnError(err) + tests.SkipIfNoWindowsImage(virtClient) })