diff --git a/tests/e2e/complex_test.go b/tests/e2e/complex_test.go index 9a4bd9571..3cc3c9511 100644 --- a/tests/e2e/complex_test.go +++ b/tests/e2e/complex_test.go @@ -67,7 +67,10 @@ var _ = Describe("Complex test", ginkgoutil.CommonE2ETestDecorators(), func() { Context("When virtualization resources are applied:", func() { It("result should be succeeded", func() { - res := kubectl.Kustomize(conf.TestData.ComplexTest, kc.KustomizeOptions{}) + res := kubectl.Apply(kc.ApplyOptions{ + Filename: []string{conf.TestData.ComplexTest}, + FilenameOption: kc.Kustomize, + }) Expect(res.Error()).NotTo(HaveOccurred(), res.StdErr()) }) }) @@ -225,5 +228,24 @@ var _ = Describe("Complex test", ginkgoutil.CommonE2ETestDecorators(), func() { CheckExternalConnection(externalHost, httpStatusOk, vms...) }) }) + + Context("When test is complited:", func() { + It("tries to delete used resources", func() { + kustimizationFile := fmt.Sprintf("%s/%s", conf.TestData.ComplexTest, "kustomization.yaml") + err := kustomize.ExcludeResource(kustimizationFile, "ns.yaml") + Expect(err).NotTo(HaveOccurred(), "cannot exclude namespace from clean up operation:\n%s", err) + res := kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.TestData.ComplexTest}, + FilenameOption: kc.Kustomize, + }) + Expect(res.Error()).NotTo(HaveOccurred(), "cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + res = kubectl.Delete(kc.DeleteOptions{ + Labels: testCaseLabel, + Namespace: conf.Namespace, + Resource: kc.ResourceKubevirtVMIM, + }) + Expect(res.Error()).NotTo(HaveOccurred(), "cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + }) + }) }) }) diff --git a/tests/e2e/config/config.go b/tests/e2e/config/config.go index 8070b3c42..6eec9bab0 100644 --- a/tests/e2e/config/config.go +++ b/tests/e2e/config/config.go @@ -263,6 +263,39 @@ func (k *Kustomize) SetParams(filePath, namespace, namePrefix string) error { return nil } +func (k *Kustomize) ExcludeResource(filePath, resourceName string) error { + var kustomizeFile Kustomize + + data, readErr := os.ReadFile(filePath) + if readErr != nil { + return readErr + } + + unmarshalErr := yamlv3.Unmarshal([]byte(data), &kustomizeFile) + if unmarshalErr != nil { + return unmarshalErr + } + newResourceList := make([]string, 0, len(kustomizeFile.Resources)) + for _, v := range kustomizeFile.Resources { + if v != resourceName { + newResourceList = append(newResourceList, v) + } + } + + kustomizeFile.Resources = newResourceList + updatedKustomizeFile, marshalErr := yamlv3.Marshal(&kustomizeFile) + if marshalErr != nil { + return marshalErr + } + + writeErr := os.WriteFile(filePath, updatedKustomizeFile, 0o644) + if writeErr != nil { + return writeErr + } + + return nil +} + func GetModuleConfig() (*ModuleConfig, error) { res := kubectl.GetResource(kc.ResourceModuleConfig, "virtualization", kc.GetOptions{Output: "yaml"}) if !res.WasSuccess() { diff --git a/tests/e2e/disks_test.go b/tests/e2e/disks_test.go index 550c457a7..56b252172 100644 --- a/tests/e2e/disks_test.go +++ b/tests/e2e/disks_test.go @@ -75,7 +75,10 @@ var _ = Describe("Disks", ginkgoutil.CommonE2ETestDecorators(), func() { Context("CVI", func() { AfterAll(func() { By("Removing resources for cvi tests") - kubectl.Delete(conf.Disks.CviTestDataDir, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.Disks.CviTestDataDir}, + FilenameOption: kc.Filename, + }) }) When("http source", func() { filepath := cviPath("/cvi_http.yaml") @@ -104,8 +107,10 @@ var _ = Describe("Disks", ginkgoutil.CommonE2ETestDecorators(), func() { When("upload", func() { AfterAll(func() { By("Removing support resources for cvi upload test") - kubectl.DeleteResource(kc.ResourcePod, UploadHelpPod, kc.DeleteOptions{ + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{UploadHelpPod}, Namespace: conf.Namespace, + Resource: kc.ResourcePod, }) }) filepath := cviPath("/cvi_upload.yaml") @@ -116,7 +121,10 @@ var _ = Describe("Disks", ginkgoutil.CommonE2ETestDecorators(), func() { Context("VI", func() { AfterAll(func() { By("Removing resources for vi tests") - kubectl.Delete(conf.Disks.ViTestDataDir, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.Disks.ViTestDataDir}, + FilenameOption: kc.Filename, + }) }) When("http source", func() { filepath := viPath("/vi_http.yaml") @@ -145,8 +153,10 @@ var _ = Describe("Disks", ginkgoutil.CommonE2ETestDecorators(), func() { When("upload", func() { AfterAll(func() { By("Removing support resources for vi upload test") - kubectl.DeleteResource(kc.ResourcePod, UploadHelpPod, kc.DeleteOptions{ + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{UploadHelpPod}, Namespace: conf.Namespace, + Resource: kc.ResourcePod, }) }) filepath := viPath("/vi_upload.yaml") @@ -157,7 +167,10 @@ var _ = Describe("Disks", ginkgoutil.CommonE2ETestDecorators(), func() { Context("VD", func() { AfterAll(func() { By("Removing resources for vd tests") - kubectl.Delete(conf.Disks.VdTestDataDir, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.Disks.VdTestDataDir}, + FilenameOption: kc.Filename, + }) }) When("http source", func() { filepath := vdPath("/vd_http.yaml") @@ -197,8 +210,10 @@ var _ = Describe("Disks", ginkgoutil.CommonE2ETestDecorators(), func() { When("upload", func() { AfterAll(func() { By("Removing support resources for vd upload test") - kubectl.DeleteResource(kc.ResourcePod, UploadHelpPod, kc.DeleteOptions{ + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{UploadHelpPod}, Namespace: conf.Namespace, + Resource: kc.ResourcePod, }) }) filepath := vdPath("/vd_upload.yaml") diff --git a/tests/e2e/ipam_test.go b/tests/e2e/ipam_test.go index 42130ee23..507b02c29 100644 --- a/tests/e2e/ipam_test.go +++ b/tests/e2e/ipam_test.go @@ -34,7 +34,10 @@ var _ = Describe("Ipam", func() { Context("VirtualMachineIPAddressClaim", ginkgoutil.CommonE2ETestDecorators(), func() { AfterAll(func() { By("Removing resources for vmip tests") - kubectl.Delete(conf.Ipam.TestDataDir, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.Ipam.TestDataDir}, + FilenameOption: kc.Filename, + }) }) GetLeaseNameFromClaim := func(manifestClaim string) string { res := kubectl.Get(manifestClaim, kc.GetOptions{Output: "jsonpath={.spec.virtualMachineIPAddressLeaseName}"}) @@ -42,7 +45,10 @@ var _ = Describe("Ipam", func() { return res.StdOut() } DeleteVMIP := func(manifest string) { - res := kubectl.Delete(manifest, kc.DeleteOptions{}) + res := kubectl.Delete(kc.DeleteOptions{ + Filename: []string{manifest}, + FilenameOption: kc.Filename, + }) Expect(res.Error()).NotTo(HaveOccurred(), "failed delete vmip from file %s.\n%s", manifest, res.StdErr()) } When("reclaimPolicy Delete", func() { @@ -65,7 +71,11 @@ var _ = Describe("Ipam", func() { CheckField(kc.ResourceVMIPLease, leaseName, "jsonpath={'.status.phase'}", PhaseBound) DeleteVMIP(filepath) CheckField(kc.ResourceVMIPLease, leaseName, "jsonpath={'.status.phase'}", PhaseReleased) - kubectl.DeleteResource(kc.ResourceVMIPLease, leaseName, kc.DeleteOptions{Namespace: conf.Namespace}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{leaseName}, + Namespace: conf.Namespace, + Resource: kc.ResourceVMIPLease, + }) }) }) }) diff --git a/tests/e2e/kubectl/kubectl.go b/tests/e2e/kubectl/kubectl.go index f60c6e489..ed705ef72 100644 --- a/tests/e2e/kubectl/kubectl.go +++ b/tests/e2e/kubectl/kubectl.go @@ -29,24 +29,27 @@ import ( ) const ( - Cmd = "kubectl" - ShortTimeout = 10 * time.Second - MediumTimeout = 30 * time.Second - LongTimeout = 60 * time.Second - ExecExtraTimeout = 20 * time.Second + Cmd = "kubectl" + ShortTimeout = 10 * time.Second + MediumTimeout = 30 * time.Second + LongTimeout = 60 * time.Second + ExecExtraTimeout = 20 * time.Second + Filename FilenameOption = "--filename" + Kustomize FilenameOption = "--kustomize" ) -type Resource string +type ( + Resource string + FilenameOption string +) type Kubectl interface { - Apply(filepath string, opts ApplyOptions) *executor.CMDResult + Apply(opts ApplyOptions) *executor.CMDResult Create(filepath string, opts CreateOptions) *executor.CMDResult CreateResource(resource Resource, name string, opts CreateOptions) *executor.CMDResult Get(filepath string, opts GetOptions) *executor.CMDResult GetResource(resource Resource, name string, opts GetOptions) *executor.CMDResult - Delete(filepath string, opts DeleteOptions) *executor.CMDResult - DeleteResource(resource Resource, name string, opts DeleteOptions) *executor.CMDResult - Kustomize(directory string, opts KustomizeOptions) *executor.CMDResult + Delete(opts DeleteOptions) *executor.CMDResult List(resource Resource, opts GetOptions) *executor.CMDResult Wait(filepath string, opts WaitOptions) *executor.CMDResult WaitResource(resource Resource, name string, opts WaitOptions) *executor.CMDResult @@ -56,10 +59,17 @@ type Kubectl interface { RawCommand(subCmd string, timeout time.Duration) *executor.CMDResult } +// FilenameOption: +// +// kubectl.Filename // --filename +// kubectl.Kustomize // --kustomize type ApplyOptions struct { - Namespace string - Output string - Force bool + Filename []string + FilenameOption FilenameOption + Force bool + Namespace string + Output string + Recursive bool } type CreateOptions struct { @@ -69,8 +79,13 @@ type CreateOptions struct { type DeleteOptions struct { ExcludedLabels []string + Filename []string + FilenameOption FilenameOption + IgnoreNotFound bool Labels map[string]string Namespace string + Recursive bool + Resource Resource } type GetOptions struct { @@ -81,12 +96,6 @@ type GetOptions struct { Output string } -type KustomizeOptions struct { - Namespace string - Output string - Force bool -} - type WaitOptions struct { ExcludedLabels []string Labels map[string]string @@ -154,10 +163,10 @@ type KubectlCMD struct { cmd string } -func (k KubectlCMD) Apply(filepath string, opts ApplyOptions) *executor.CMDResult { - cmd := fmt.Sprintf("%s apply -f %s", k.cmd, filepath) +func (k KubectlCMD) Apply(opts ApplyOptions) *executor.CMDResult { + cmd := fmt.Sprintf("%s apply", k.cmd) cmd = k.applyOptions(cmd, opts) - ctx, cancel := context.WithTimeout(context.Background(), ShortTimeout) + ctx, cancel := context.WithTimeout(context.Background(), MediumTimeout) defer cancel() return k.ExecContext(ctx, cmd) } @@ -194,26 +203,12 @@ func (k KubectlCMD) GetResource(resource Resource, name string, opts GetOptions) return k.ExecContext(ctx, cmd) } -func (k KubectlCMD) Delete(filepath string, opts DeleteOptions) *executor.CMDResult { - cmd := fmt.Sprintf("%s delete -f %s", k.cmd, filepath) - cmd = k.deleteOptions(cmd, opts) - return k.Exec(cmd) -} - -func (k KubectlCMD) DeleteResource(resource Resource, name string, opts DeleteOptions) *executor.CMDResult { - cmd := fmt.Sprintf("%s delete %s %s", k.cmd, resource, name) +func (k KubectlCMD) Delete(opts DeleteOptions) *executor.CMDResult { + cmd := fmt.Sprintf("%s delete", k.cmd) cmd = k.deleteOptions(cmd, opts) return k.Exec(cmd) } -func (k KubectlCMD) Kustomize(directory string, opts KustomizeOptions) *executor.CMDResult { - cmd := fmt.Sprintf("%s apply --kustomize %s", k.cmd, directory) - cmd = k.kustomizeOptions(cmd, opts) - ctx, cancel := context.WithTimeout(context.Background(), LongTimeout) - defer cancel() - return k.ExecContext(ctx, cmd) -} - func (k KubectlCMD) List(resource Resource, opts GetOptions) *executor.CMDResult { cmd := fmt.Sprintf("%s get %s", k.cmd, resource) cmd = k.getOptions(cmd, opts) @@ -283,6 +278,23 @@ func (k KubectlCMD) addNamespace(cmd, ns string) string { return cmd } +func (k KubectlCMD) addFilenameOptions(cmd string, resource Resource, filenameOpt FilenameOption, recursive bool, filenames ...string) string { + if resource != "" { + cmd = fmt.Sprintf("%s %s", cmd, resource) + } + if filenameOpt != "" { + cmd = fmt.Sprintf("%s %s", cmd, filenameOpt) + } + if len(filenames) != 0 { + files := strings.Join(filenames, " ") + cmd = fmt.Sprintf("%s %s", cmd, files) + } + if recursive { + cmd = fmt.Sprintf("%s --recoursive=%t", cmd, recursive) + } + return cmd +} + func (k KubectlCMD) addLabels(cmd string, labels map[string]string, excludedLabels []string) string { if len(labels) != 0 || len(excludedLabels) != 0 { rawLabels := make([]string, 0, len(labels)+len(excludedLabels)) @@ -316,12 +328,8 @@ func (k KubectlCMD) addIgnoreNotFound(cmd string, ignoreNotFound bool) string { } func (k KubectlCMD) applyOptions(cmd string, opts ApplyOptions) string { - cmd = k.addNamespace(cmd, opts.Namespace) - cmd = k.addOutput(cmd, opts.Output) - return fmt.Sprintf("%s --force=%t", cmd, opts.Force) -} - -func (k KubectlCMD) kustomizeOptions(cmd string, opts KustomizeOptions) string { + var resourceEmptyValue Resource = "" + cmd = k.addFilenameOptions(cmd, resourceEmptyValue, opts.FilenameOption, opts.Recursive, opts.Filename...) cmd = k.addNamespace(cmd, opts.Namespace) cmd = k.addOutput(cmd, opts.Output) return fmt.Sprintf("%s --force=%t", cmd, opts.Force) @@ -342,8 +350,10 @@ func (k KubectlCMD) getOptions(cmd string, opts GetOptions) string { } func (k KubectlCMD) deleteOptions(cmd string, opts DeleteOptions) string { + cmd = k.addFilenameOptions(cmd, opts.Resource, opts.FilenameOption, opts.Recursive, opts.Filename...) cmd = k.addNamespace(cmd, opts.Namespace) cmd = k.addLabels(cmd, opts.Labels, opts.ExcludedLabels) + cmd = k.addIgnoreNotFound(cmd, opts.IgnoreNotFound) return cmd } diff --git a/tests/e2e/sizing_policy_test.go b/tests/e2e/sizing_policy_test.go index 7d9047c87..980878b30 100644 --- a/tests/e2e/sizing_policy_test.go +++ b/tests/e2e/sizing_policy_test.go @@ -72,6 +72,7 @@ var _ = Describe("Sizing policy", ginkgoutil.CommonE2ETestDecorators(), func() { vmNotValidSizingPolicyCreating string vmClassDiscovery string vmClassDiscoveryCopy string + newVmClassFilePath string notExistingVmClassChanging = map[string]string{"vm": "not-existing-vmclass-with-changing"} notExistingVmClassCreating = map[string]string{"vm": "not-existing-vmclass-with-creating"} existingVmClass = map[string]string{"vm": "existing-vmclass"} @@ -83,11 +84,15 @@ var _ = Describe("Sizing policy", ginkgoutil.CommonE2ETestDecorators(), func() { vmNotValidSizingPolicyCreating = fmt.Sprintf("%s-vm-%s", namePrefix, notExistingVmClassCreating["vm"]) vmClassDiscovery = fmt.Sprintf("%s-discovery", namePrefix) vmClassDiscoveryCopy = fmt.Sprintf("%s-discovery-copy", namePrefix) + newVmClassFilePath = fmt.Sprintf("%s/vmc-copy.yaml", conf.TestData.SizingPolicy) }) Context("When resources are applied:", func() { It("result should be succeeded", func() { - res := kubectl.Kustomize(conf.TestData.SizingPolicy, kc.KustomizeOptions{}) + res := kubectl.Apply(kc.ApplyOptions{ + Filename: []string{conf.TestData.SizingPolicy}, + FilenameOption: kc.Kustomize, + }) Expect(res.WasSuccess()).To(Equal(true), res.StdErr()) }) }) @@ -194,12 +199,14 @@ var _ = Describe("Sizing policy", ginkgoutil.CommonE2ETestDecorators(), func() { vmClass := virtv2.VirtualMachineClass{} err := GetObject(kc.ResourceVMClass, vmClassDiscovery, &vmClass, kc.GetOptions{}) Expect(err).NotTo(HaveOccurred(), err) - filePath := fmt.Sprintf("%s/vmc.yaml", conf.TestData.SizingPolicy) vmClass.Name = vmClassDiscoveryCopy vmClass.Labels = map[string]string{"id": namePrefix} - writeErr := WriteYamlObject(filePath, &vmClass) + writeErr := WriteYamlObject(newVmClassFilePath, &vmClass) Expect(writeErr).NotTo(HaveOccurred(), writeErr) - res := kubectl.Apply(filePath, kc.ApplyOptions{}) + res := kubectl.Apply(kc.ApplyOptions{ + Filename: []string{newVmClassFilePath}, + FilenameOption: kc.Filename, + }) Expect(res.Error()).NotTo(HaveOccurred(), res.StdErr()) }) @@ -239,4 +246,22 @@ var _ = Describe("Sizing policy", ginkgoutil.CommonE2ETestDecorators(), func() { } }) }) + + Context("When test is complited:", func() { + It("tries to delete used resources", func() { + kustomizationFile := fmt.Sprintf("%s/%s", conf.TestData.SizingPolicy, "kustomization.yaml") + err := kustomize.ExcludeResource(kustomizationFile, "ns.yaml") + Expect(err).NotTo(HaveOccurred(), "cannot exclude namespace from clean up operation:\n%s", err) + res := kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.TestData.SizingPolicy}, + FilenameOption: kc.Kustomize, + }) + Expect(res.Error()).NotTo(HaveOccurred(), "cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + res = kubectl.Delete(kc.DeleteOptions{ + Filename: []string{newVmClassFilePath}, + FilenameOption: kc.Filename, + }) + Expect(res.Error()).NotTo(HaveOccurred(), "cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + }) + }) }) diff --git a/tests/e2e/tests_suite_test.go b/tests/e2e/tests_suite_test.go index eccc9a64a..5ed071f69 100644 --- a/tests/e2e/tests_suite_test.go +++ b/tests/e2e/tests_suite_test.go @@ -104,7 +104,10 @@ func init() { log.Fatal(err) } } - Cleanup() + err = Cleanup() + if err != nil { + log.Fatal(err) + } res := kubectl.CreateResource(kc.ResourceNamespace, conf.Namespace, kc.CreateOptions{}) if !res.WasSuccess() { log.Fatalf("err: %v\n%s", res.Error(), res.StdErr()) @@ -120,12 +123,28 @@ func TestTests(t *testing.T) { } } -func Cleanup() { - kubectl.DeleteResource(kc.ResourceNamespace, conf.Namespace, kc.DeleteOptions{}) - kubectl.DeleteResource(kc.ResourceCVI, "", kc.DeleteOptions{ - Labels: map[string]string{"id": namePrefix}, +func Cleanup() error { + res := kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.Namespace}, + IgnoreNotFound: true, + Resource: kc.ResourceNamespace, + }) + if res.Error() != nil { + return fmt.Errorf("cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + } + res = kubectl.Delete(kc.DeleteOptions{ + Labels: map[string]string{"id": namePrefix}, + Resource: kc.ResourceCVI, }) - kubectl.DeleteResource(kc.ResourceVMClass, "", kc.DeleteOptions{ - Labels: map[string]string{"id": namePrefix}, + if res.Error() != nil { + return fmt.Errorf("cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + } + res = kubectl.Delete(kc.DeleteOptions{ + Labels: map[string]string{"id": namePrefix}, + Resource: kc.ResourceVMClass, }) + if res.Error() != nil { + return fmt.Errorf("cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + } + return nil } diff --git a/tests/e2e/util_test.go b/tests/e2e/util_test.go index ee3645793..c8029d1c7 100644 --- a/tests/e2e/util_test.go +++ b/tests/e2e/util_test.go @@ -73,7 +73,10 @@ func ItApplyFromFile(filepath string) { func ApplyFromFile(filepath string) { GinkgoHelper() fmt.Printf("Apply file %s\n", filepath) - res := kubectl.Apply(filepath, kc.ApplyOptions{}) + res := kubectl.Apply(kc.ApplyOptions{ + Filename: []string{filepath}, + FilenameOption: kc.Filename, + }) Expect(res.Error()).NotTo(HaveOccurred(), "apply failed for file %s\n%s", filepath, res.StdErr()) } diff --git a/tests/e2e/vm_configuration_test.go b/tests/e2e/vm_configuration_test.go index 10571dd6c..063bf5ed1 100644 --- a/tests/e2e/vm_configuration_test.go +++ b/tests/e2e/vm_configuration_test.go @@ -92,7 +92,10 @@ var _ = Describe("Virtual machine configuration", ginkgoutil.CommonE2ETestDecora Context("When resources are applied:", func() { It("result should be succeeded", func() { - res := kubectl.Kustomize(conf.TestData.VmConfiguration, kc.KustomizeOptions{}) + res := kubectl.Apply(kc.ApplyOptions{ + Filename: []string{conf.TestData.VmConfiguration}, + FilenameOption: kc.Kustomize, + }) Expect(res.WasSuccess()).To(Equal(true), res.StdErr()) }) }) @@ -249,4 +252,17 @@ var _ = Describe("Virtual machine configuration", ginkgoutil.CommonE2ETestDecora }) }) }) + + Context("When test is complited:", func() { + It("tries to delete used resources", func() { + kustimizationFile := fmt.Sprintf("%s/%s", conf.TestData.VmConfiguration, "kustomization.yaml") + err := kustomize.ExcludeResource(kustimizationFile, "ns.yaml") + Expect(err).NotTo(HaveOccurred(), "cannot exclude namespace from clean up operation:\n%s", err) + res := kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.TestData.VmConfiguration}, + FilenameOption: kc.Kustomize, + }) + Expect(res.Error()).NotTo(HaveOccurred(), "cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + }) + }) }) diff --git a/tests/e2e/vm_connectivity_test.go b/tests/e2e/vm_connectivity_test.go index 7cb7e7685..ecf77f5b6 100644 --- a/tests/e2e/vm_connectivity_test.go +++ b/tests/e2e/vm_connectivity_test.go @@ -104,7 +104,10 @@ var _ = Describe("VM connectivity", ginkgoutil.CommonE2ETestDecorators(), func() Context("When resources are applied:", func() { It("result should be succeeded", func() { - res := kubectl.Kustomize(conf.TestData.Connectivity, kc.KustomizeOptions{}) + res := kubectl.Apply(kc.ApplyOptions{ + Filename: []string{conf.TestData.Connectivity}, + FilenameOption: kc.Kustomize, + }) Expect(res.WasSuccess()).To(Equal(true), res.StdErr()) }) }) @@ -249,4 +252,23 @@ var _ = Describe("VM connectivity", ginkgoutil.CommonE2ETestDecorators(), func() }).WithTimeout(Timeout).WithPolling(Interval).Should(ContainSubstring(vmB.Name)) }) }) + + Context("When test is complited:", func() { + It("tries to delete used resources", func() { + kustimizationFile := fmt.Sprintf("%s/%s", conf.TestData.Connectivity, "kustomization.yaml") + err := kustomize.ExcludeResource(kustimizationFile, "ns.yaml") + Expect(err).NotTo(HaveOccurred(), "cannot exclude namespace from clean up operation:\n%s", err) + res := kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.TestData.Connectivity}, + FilenameOption: kc.Kustomize, + }) + Expect(res.Error()).NotTo(HaveOccurred(), "cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + res = kubectl.Delete(kc.DeleteOptions{ + Filename: []string{CurlPod}, + Namespace: conf.Namespace, + Resource: kc.ResourcePod, + }) + Expect(res.Error()).NotTo(HaveOccurred(), "cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + }) + }) }) diff --git a/tests/e2e/vm_disk_attachment_test.go b/tests/e2e/vm_disk_attachment_test.go index 7fed4c28f..1dedd9a50 100644 --- a/tests/e2e/vm_disk_attachment_test.go +++ b/tests/e2e/vm_disk_attachment_test.go @@ -51,8 +51,10 @@ func AttachVirtualDisk(virtualMachine, virtualDisk string, labels map[string]str err := CreateVMBDAManifest(vmbdaFilePath, virtualMachine, virtualDisk, labels) Expect(err).NotTo(HaveOccurred(), err) - res := kubectl.Apply(vmbdaFilePath, kc.ApplyOptions{ - Namespace: conf.Namespace, + res := kubectl.Apply(kc.ApplyOptions{ + Filename: []string{vmbdaFilePath}, + FilenameOption: kc.Filename, + Namespace: conf.Namespace, }) Expect(res.Error()).NotTo(HaveOccurred(), res.StdErr()) } @@ -122,11 +124,25 @@ var _ = Describe("Virtual disk attachment", ginkgoutil.CommonE2ETestDecorators() Context("When resources are applied:", func() { It("result should be succeeded", func() { - res := kubectl.Kustomize(conf.TestData.VmDiskAttachment, kc.KustomizeOptions{}) + res := kubectl.Apply(kc.ApplyOptions{ + Filename: []string{conf.TestData.VmDiskAttachment}, + FilenameOption: kc.Kustomize, + }) Expect(res.WasSuccess()).To(Equal(true), res.StdErr()) }) }) + Context("When virtual images are applied:", func() { + It("checks VIs phases", func() { + By(fmt.Sprintf("VIs should be in %s phases", PhaseReady)) + WaitPhaseByLabel(kc.ResourceVI, PhaseReady, kc.WaitOptions{ + Labels: testCaseLabel, + Namespace: conf.Namespace, + Timeout: MaxWaitTimeout, + }) + }) + }) + Context("When virtual disks are applied:", func() { It("checks VDs phases", func() { By(fmt.Sprintf("VDs with consumers should be in %s phases", PhaseReady)) @@ -202,8 +218,10 @@ var _ = Describe("Virtual disk attachment", ginkgoutil.CommonE2ETestDecorators() }).WithTimeout(Timeout).WithPolling(Interval).ShouldNot(HaveOccurred(), "virtual machine: %s", vmName) }) It("detaches virtual disk", func() { - res := kubectl.DeleteResource(kc.ResourceVMBDA, vmbdaName, kc.DeleteOptions{ + res := kubectl.Delete(kc.DeleteOptions{ + Filename: []string{vmbdaName}, Namespace: conf.Namespace, + Resource: kc.ResourceVMBDA, }) Expect(res.Error()).NotTo(HaveOccurred(), res.StdErr()) }) @@ -227,4 +245,17 @@ var _ = Describe("Virtual disk attachment", ginkgoutil.CommonE2ETestDecorators() }) }) }) + + Context("When test is complited:", func() { + It("tries to delete used resources", func() { + kustimizationFile := fmt.Sprintf("%s/%s", conf.TestData.VmDiskAttachment, "kustomization.yaml") + err := kustomize.ExcludeResource(kustimizationFile, "ns.yaml") + Expect(err).NotTo(HaveOccurred(), "cannot exclude namespace from clean up operation:\n%s", err) + res := kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.TestData.VmDiskAttachment}, + FilenameOption: kc.Kustomize, + }) + Expect(res.Error()).NotTo(HaveOccurred(), "cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + }) + }) }) diff --git a/tests/e2e/vm_disk_resizing_test.go b/tests/e2e/vm_disk_resizing_test.go index 2b94443e2..af695c3ef 100644 --- a/tests/e2e/vm_disk_resizing_test.go +++ b/tests/e2e/vm_disk_resizing_test.go @@ -159,20 +159,34 @@ func GetVirtualMachineDisks(vmName string, config *cfg.Config) (VirtualMachineDi } var _ = Describe("Virtual disk resizing", ginkgoutil.CommonE2ETestDecorators(), func() { - diskResizingLabel := map[string]string{"testcase": "disk-resizing"} + testCaseLabel := map[string]string{"testcase": "disk-resizing"} Context("When resources are applied:", func() { It("result should be succeeded", func() { - res := kubectl.Kustomize(conf.TestData.DiskResizing, kc.KustomizeOptions{}) + res := kubectl.Apply(kc.ApplyOptions{ + Filename: []string{conf.TestData.DiskResizing}, + FilenameOption: kc.Kustomize, + }) Expect(res.WasSuccess()).To(Equal(true), res.StdErr()) }) }) + Context("When virtual images are applied:", func() { + It("checks VIs phases", func() { + By(fmt.Sprintf("VIs should be in %s phases", PhaseReady)) + WaitPhaseByLabel(kc.ResourceVI, PhaseReady, kc.WaitOptions{ + Labels: testCaseLabel, + Namespace: conf.Namespace, + Timeout: MaxWaitTimeout, + }) + }) + }) + Context("When virtual disks are applied:", func() { It("checks VDs phases", func() { By(fmt.Sprintf("VDs should be in %s phases", PhaseReady)) WaitPhaseByLabel(kc.ResourceVD, PhaseReady, kc.WaitOptions{ - Labels: diskResizingLabel, + Labels: testCaseLabel, Namespace: conf.Namespace, Timeout: MaxWaitTimeout, }) @@ -183,7 +197,7 @@ var _ = Describe("Virtual disk resizing", ginkgoutil.CommonE2ETestDecorators(), It("checks VMs phases", func() { By(fmt.Sprintf("VMs should be in %s phases", PhaseRunning)) WaitPhaseByLabel(kc.ResourceVM, PhaseRunning, kc.WaitOptions{ - Labels: diskResizingLabel, + Labels: testCaseLabel, Namespace: conf.Namespace, Timeout: MaxWaitTimeout, }) @@ -194,7 +208,7 @@ var _ = Describe("Virtual disk resizing", ginkgoutil.CommonE2ETestDecorators(), It("checks VMBDAs phases", func() { By(fmt.Sprintf("VMBDAs should be in %s phases", PhaseAttached)) WaitPhaseByLabel(kc.ResourceVMBDA, PhaseAttached, kc.WaitOptions{ - Labels: diskResizingLabel, + Labels: testCaseLabel, Namespace: conf.Namespace, Timeout: MaxWaitTimeout, }) @@ -208,14 +222,14 @@ var _ = Describe("Virtual disk resizing", ginkgoutil.CommonE2ETestDecorators(), vmDisksAfter VirtualMachineDisks err error ) - vmName := fmt.Sprintf("%s-vm-%s", namePrefix, diskResizingLabel["testcase"]) + vmName := fmt.Sprintf("%s-vm-%s", namePrefix, testCaseLabel["testcase"]) It("get disks metadata before resizing", func() { vmDisksBefore, err = GetVirtualMachineDisks(vmName, conf) Expect(err).NotTo(HaveOccurred(), err) }) It("resizes disks", func() { res := kubectl.List(kc.ResourceVD, kc.GetOptions{ - Labels: diskResizingLabel, + Labels: testCaseLabel, Namespace: conf.Namespace, Output: "jsonpath='{.items[*].metadata.name}'", }) @@ -233,28 +247,28 @@ var _ = Describe("Virtual disk resizing", ginkgoutil.CommonE2ETestDecorators(), It("checks VDs, VMs and VMBDA phases", func() { By(fmt.Sprintf("VDs should be in %s phases", PhaseReady)) WaitPhaseByLabel(kc.ResourceVD, PhaseReady, kc.WaitOptions{ - Labels: diskResizingLabel, + Labels: testCaseLabel, Namespace: conf.Namespace, Timeout: MaxWaitTimeout, }) By(fmt.Sprintf("VMs should be in %s phases", PhaseRunning)) WaitPhaseByLabel(kc.ResourceVM, PhaseRunning, kc.WaitOptions{ - Labels: diskResizingLabel, + Labels: testCaseLabel, Namespace: conf.Namespace, Timeout: MaxWaitTimeout, }) By(fmt.Sprintf("VMBDAs should be in %s phases", PhaseAttached)) WaitPhaseByLabel(kc.ResourceVMBDA, PhaseAttached, kc.WaitOptions{ - Labels: diskResizingLabel, + Labels: testCaseLabel, Namespace: conf.Namespace, Timeout: MaxWaitTimeout, }) By("BlockDeviceRefsStatus: disks should be attached") res := kubectl.List(kc.ResourceVM, kc.GetOptions{ - Labels: diskResizingLabel, + Labels: testCaseLabel, Namespace: conf.Namespace, Output: "jsonpath='{.items[*].metadata.name}'", }) @@ -278,4 +292,17 @@ var _ = Describe("Virtual disk resizing", ginkgoutil.CommonE2ETestDecorators(), }) }) }) + + Context("When test is complited:", func() { + It("tries to delete used resources", func() { + kustimizationFile := fmt.Sprintf("%s/%s", conf.TestData.DiskResizing, "kustomization.yaml") + err := kustomize.ExcludeResource(kustimizationFile, "ns.yaml") + Expect(err).NotTo(HaveOccurred(), "cannot exclude namespace from clean up operation:\n%s", err) + res := kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.TestData.DiskResizing}, + FilenameOption: kc.Kustomize, + }) + Expect(res.Error()).NotTo(HaveOccurred(), "cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + }) + }) }) diff --git a/tests/e2e/vm_label_annotation_test.go b/tests/e2e/vm_label_annotation_test.go index 2b4161dc4..f7193873b 100644 --- a/tests/e2e/vm_label_annotation_test.go +++ b/tests/e2e/vm_label_annotation_test.go @@ -89,11 +89,20 @@ var _ = Describe("Label and Annotation", ginkgoutil.CommonE2ETestDecorators(), f return nil }) if err != nil || len(files) == 0 { - kubectl.Delete(imageManifest, kc.DeleteOptions{}) - kubectl.Delete(conf.VM.TestDataDir, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{imageManifest}, + FilenameOption: kc.Filename, + }) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.VM.TestDataDir}, + FilenameOption: kc.Filename, + }) } else { for _, f := range files { - kubectl.Delete(f, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{f}, + FilenameOption: kc.Filename, + }) } } }) @@ -112,7 +121,10 @@ var _ = Describe("Label and Annotation", ginkgoutil.CommonE2ETestDecorators(), f AfterAll(func() { By("Delete manifest") - kubectl.Delete(vmManifest, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{vmManifest}, + FilenameOption: kc.Filename, + }) }) Describe(fmt.Sprintf("Add label %s=%s", labelName, labelValue), func() { @@ -194,7 +206,10 @@ var _ = Describe("Label and Annotation", ginkgoutil.CommonE2ETestDecorators(), f AfterAll(func() { By("Delete manifest") - kubectl.Delete(vmManifest, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{vmManifest}, + FilenameOption: kc.Filename, + }) }) Describe(fmt.Sprintf("Add annotation %s=%s", annotationName, annotationValue), func() { diff --git a/tests/e2e/vm_migration_test.go b/tests/e2e/vm_migration_test.go index c8af7de67..ceb3632e6 100644 --- a/tests/e2e/vm_migration_test.go +++ b/tests/e2e/vm_migration_test.go @@ -42,8 +42,10 @@ func MigrateVirtualMachines(label map[string]string, templatePath string, virtua migrationFilePath := fmt.Sprintf("%s/%s.yaml", migrationFilesPath, vm) err := CreateMigrationManifest(vm, migrationFilePath, label) Expect(err).NotTo(HaveOccurred(), err) - res := kubectl.Apply(migrationFilePath, kc.ApplyOptions{ - Namespace: conf.Namespace, + res := kubectl.Apply(kc.ApplyOptions{ + Filename: []string{migrationFilePath}, + FilenameOption: kc.Filename, + Namespace: conf.Namespace, }) Expect(res.Error()).NotTo(HaveOccurred(), res.StdErr()) } @@ -77,7 +79,10 @@ var _ = Describe("Virtual machine migration", ginkgoutil.CommonE2ETestDecorators Context("When resources are applied:", func() { It("result should be succeeded", func() { - res := kubectl.Kustomize(conf.TestData.VmMigration, kc.KustomizeOptions{}) + res := kubectl.Apply(kc.ApplyOptions{ + Filename: []string{conf.TestData.VmMigration}, + FilenameOption: kc.Kustomize, + }) Expect(res.WasSuccess()).To(Equal(true), res.StdErr()) }) }) @@ -157,4 +162,23 @@ var _ = Describe("Virtual machine migration", ginkgoutil.CommonE2ETestDecorators CheckExternalConnection(externalHost, httpStatusOk, vms...) }) }) + + Context("When test is complited:", func() { + It("tries to delete used resources", func() { + kustimizationFile := fmt.Sprintf("%s/%s", conf.TestData.VmMigration, "kustomization.yaml") + err := kustomize.ExcludeResource(kustimizationFile, "ns.yaml") + Expect(err).NotTo(HaveOccurred(), "cannot exclude namespace from clean up operation:\n%s", err) + res := kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.TestData.VmMigration}, + FilenameOption: kc.Kustomize, + }) + Expect(res.Error()).NotTo(HaveOccurred(), "cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + res = kubectl.Delete(kc.DeleteOptions{ + Labels: testCaseLabel, + Namespace: conf.Namespace, + Resource: kc.ResourceKubevirtVMIM, + }) + Expect(res.Error()).NotTo(HaveOccurred(), "cmd: %s\nstderr: %s", res.GetCmd(), res.StdErr()) + }) + }) }) diff --git a/tests/e2e/vm_test.go b/tests/e2e/vm_test.go index 3df3ffad2..c7c670126 100644 --- a/tests/e2e/vm_test.go +++ b/tests/e2e/vm_test.go @@ -66,11 +66,20 @@ var _ = Describe("VM", ginkgoutil.CommonE2ETestDecorators(), func() { return nil }) if err != nil || len(files) == 0 { - kubectl.Delete(imageManifest, kc.DeleteOptions{}) - kubectl.Delete(conf.VM.TestDataDir, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{imageManifest}, + FilenameOption: kc.Filename, + }) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{conf.VM.TestDataDir}, + FilenameOption: kc.Filename, + }) } else { for _, f := range files { - kubectl.Delete(f, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{f}, + FilenameOption: kc.Filename, + }) } } }) @@ -93,7 +102,10 @@ var _ = Describe("VM", ginkgoutil.CommonE2ETestDecorators(), func() { Context("Boot", func() { AfterAll(func() { - kubectl.Delete(vmPath("boot/"), kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{vmPath("boot/")}, + FilenameOption: kc.Filename, + }) }) Test := func(manifest string) { GinkgoHelper() @@ -152,7 +164,10 @@ var _ = Describe("VM", ginkgoutil.CommonE2ETestDecorators(), func() { }) AfterAll(func() { By("Delete manifest") - kubectl.Delete(manifest, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{manifest}, + FilenameOption: kc.Filename, + }) }) When("On to AlwaysOff", func() { It("Patch runpolicy to AlwaysOff", func() { @@ -229,7 +244,10 @@ var _ = Describe("VM", ginkgoutil.CommonE2ETestDecorators(), func() { } AfterAll(func() { By("Delete manifests") - kubectl.Delete(vmPath("provisioning/"), kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{vmPath("provisioning/")}, + FilenameOption: kc.Filename, + }) }) When("UserData", func() { manifest := vmPath("provisioning/vm_provisioning_useradata.yaml") @@ -293,7 +311,10 @@ var _ = Describe("VM", ginkgoutil.CommonE2ETestDecorators(), func() { } AfterAll(func() { By("Delete manifests") - kubectl.Delete(vmPath("resources/"), kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{vmPath("resources/")}, + FilenameOption: kc.Filename, + }) }) When("Corefraction 100", func() { manifest := vmPath("resources/vm_100.yaml") @@ -335,7 +356,10 @@ var _ = Describe("VM", ginkgoutil.CommonE2ETestDecorators(), func() { }) AfterAll(func() { By("Delete manifests") - kubectl.Delete(manifest, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{manifest}, + FilenameOption: kc.Filename, + }) }) When("Compare priorityClassNames", func() { It("Compare priorityClassNames", func() { @@ -369,7 +393,10 @@ var _ = Describe("VM", ginkgoutil.CommonE2ETestDecorators(), func() { }) AfterAll(func() { By("Delete manifest") - kubectl.Delete(manifest, kc.DeleteOptions{}) + kubectl.Delete(kc.DeleteOptions{ + Filename: []string{manifest}, + FilenameOption: kc.Filename, + }) }) When("Compare periods", func() { It("Compare periods", func() {