From ccfa1f62e875feb650c6b6972f8ead0164d4d7cd Mon Sep 17 00:00:00 2001 From: Nikita Korolev <141920865+universal-itengineer@users.noreply.github.com> Date: Mon, 4 Mar 2024 15:58:29 +0300 Subject: [PATCH 01/12] Update build_dev.yml comment install task Signed-off-by: Nikita Korolev <141920865+universal-itengineer@users.noreply.github.com> --- .github/workflows/build_dev.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_dev.yml b/.github/workflows/build_dev.yml index 3097c265f..20934b361 100644 --- a/.github/workflows/build_dev.yml +++ b/.github/workflows/build_dev.yml @@ -87,8 +87,8 @@ jobs: with: go-version: "1.20" - - name: Install Task - uses: arduino/setup-task@v1 + # - name: Install Task + # uses: arduino/setup-task@v1 - uses: actions/checkout@v4 From fe83547a7d740567cc13bd8516dbc88efb35a2c0 Mon Sep 17 00:00:00 2001 From: Nikita Korolev <141920865+universal-itengineer@users.noreply.github.com> Date: Mon, 4 Mar 2024 16:01:47 +0300 Subject: [PATCH 02/12] Update build_dev.yml edit for run on self-hosted runners Signed-off-by: Nikita Korolev <141920865+universal-itengineer@users.noreply.github.com> --- .github/workflows/build_dev.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_dev.yml b/.github/workflows/build_dev.yml index 20934b361..b3feef049 100644 --- a/.github/workflows/build_dev.yml +++ b/.github/workflows/build_dev.yml @@ -108,8 +108,8 @@ jobs: with: go-version: "1.20" - - name: Install Task - uses: arduino/setup-task@v1 + # - name: Install Task + # uses: arduino/setup-task@v1 - uses: actions/checkout@v4 From 56097a59c726fe1bd1d14b7f16c583fb6beab413 Mon Sep 17 00:00:00 2001 From: Nikita Korolev Date: Mon, 4 Mar 2024 17:00:33 +0300 Subject: [PATCH 03/12] chore: resovle conflict Signed-off-by: Nikita Korolev --- .github/workflows/build_dev.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build_dev.yml b/.github/workflows/build_dev.yml index b3feef049..3097c265f 100644 --- a/.github/workflows/build_dev.yml +++ b/.github/workflows/build_dev.yml @@ -87,8 +87,8 @@ jobs: with: go-version: "1.20" - # - name: Install Task - # uses: arduino/setup-task@v1 + - name: Install Task + uses: arduino/setup-task@v1 - uses: actions/checkout@v4 @@ -108,8 +108,8 @@ jobs: with: go-version: "1.20" - # - name: Install Task - # uses: arduino/setup-task@v1 + - name: Install Task + uses: arduino/setup-task@v1 - uses: actions/checkout@v4 From 68dbac89016f5e991045af4b34595d073d558252 Mon Sep 17 00:00:00 2001 From: Nikita Korolev Date: Thu, 8 Feb 2024 15:06:05 +0300 Subject: [PATCH 04/12] chore: test for label and annotation Signed-off-by: Nikita Korolev --- tests/e2e/Taskfile.yaml | 32 +++++++++++++++++-- tests/e2e/testdata/vm/boot/vm_cvmi.yaml | 2 +- tests/e2e/testdata/vm/boot/vm_vmd.yaml | 2 +- tests/e2e/testdata/vm/boot/vm_vmi.yaml | 2 +- tests/e2e/testdata/vm/image.yaml | 2 +- .../provisioning/vm_provisioning_secret.yaml | 2 +- .../vm_provisioning_useradata.yaml | 2 +- tests/e2e/testdata/vm/resources/vm_100.yaml | 2 +- tests/e2e/testdata/vm/resources/vm_25.yaml | 2 +- tests/e2e/testdata/vm/resources/vm_50.yaml | 2 +- tests/e2e/testdata/vm/vm_graceperiod.yaml | 2 +- tests/e2e/testdata/vm/vm_label.yaml | 19 +++++++++++ .../e2e/testdata/vm/vm_priorityclassname.yaml | 2 +- tests/e2e/testdata/vm/vm_runpolicy.yaml | 2 +- tests/e2e/vm_test.go | 9 ++++++ 15 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 tests/e2e/testdata/vm/vm_label.yaml diff --git a/tests/e2e/Taskfile.yaml b/tests/e2e/Taskfile.yaml index e5abb797f..5045f92f2 100644 --- a/tests/e2e/Taskfile.yaml +++ b/tests/e2e/Taskfile.yaml @@ -6,11 +6,16 @@ tasks: virtctl: vars: VERSION: "v1.0.0" - CMD_PATH: "/usr/local/bin/virtctl" cmds: - | - URL="https://github.com/kubevirt/kubevirt/releases/download/{{ .VERSION }}/virtctl-{{ .VERSION }}-linux-amd64" - virtctl &>/dev/null || ( sudo curl -L $URL -o {{ .CMD_PATH }} && sudo chmod +x {{ .CMD_PATH }} ) + case {{OS}} in + darwin*) CMD_PATH="/opt/homebrew/bin/virtctl" ;; + linux*) CMD_PATH="/usr/local/bin/virtctl" ;; + *) echo "unknown: {{OS}}"; exit 1 ;; + esac + + URL="https://github.com/kubevirt/kubevirt/releases/download/{{ .VERSION }}/virtctl-{{ .VERSION }}-{{OS}}-{{ARCH}}" + test -f $CMD_PATH || ( sudo curl -L $URL -o $CMD_PATH && sudo chmod +x $CMD_PATH ) ginkgo: vars: VERSION: "2.13.2" @@ -28,3 +33,24 @@ tasks: cmds: - ginkgo + set_local_env: + cmds: + - | + if [ -z "$KUBECONFIG" ]; then export KUBECONFIG=$HOME/.kube/config;else echo $KUBECONFIG - KUBECONFIG;fi; + if [ -z "$E2E_CLUSTERTRANSPORT_KUBECONFIG" ]; then + export E2E_CLUSTERTRANSPORT_KUBECONFIG=$KUBECONFIG + else + echo $E2E_CLUSTERTRANSPORT_KUBECONFIG - E2E_CLUSTERTRANSPORT_KUBECONFIG + fi + + run_local: + desc: "Run localy e2e tests" + deps: + - set_local_env + - virtctl + - ginkgo + cmds: + - ginkgo + +# ginkgo --focus +# ginkgo --focus "Disks" diff --git a/tests/e2e/testdata/vm/boot/vm_cvmi.yaml b/tests/e2e/testdata/vm/boot/vm_cvmi.yaml index f83afd223..c0f3818fd 100644 --- a/tests/e2e/testdata/vm/boot/vm_cvmi.yaml +++ b/tests/e2e/testdata/vm/boot/vm_cvmi.yaml @@ -16,7 +16,7 @@ metadata: namespace: test-d8-virtualization spec: disruptions: - approvalMode: Automatic + restartApprovalMode: Automatic runPolicy: AlwaysOn osType: Generic bootloader: BIOS diff --git a/tests/e2e/testdata/vm/boot/vm_vmd.yaml b/tests/e2e/testdata/vm/boot/vm_vmd.yaml index e5d30e0f5..28dbf685e 100644 --- a/tests/e2e/testdata/vm/boot/vm_vmd.yaml +++ b/tests/e2e/testdata/vm/boot/vm_vmd.yaml @@ -19,7 +19,7 @@ metadata: namespace: test-d8-virtualization spec: disruptions: - approvalMode: Automatic + restartApprovalMode: Automatic runPolicy: AlwaysOn osType: Generic bootloader: BIOS diff --git a/tests/e2e/testdata/vm/boot/vm_vmi.yaml b/tests/e2e/testdata/vm/boot/vm_vmi.yaml index 5cbffc5c7..67fa89441 100644 --- a/tests/e2e/testdata/vm/boot/vm_vmi.yaml +++ b/tests/e2e/testdata/vm/boot/vm_vmi.yaml @@ -5,7 +5,7 @@ metadata: namespace: test-d8-virtualization spec: disruptions: - approvalMode: Automatic + restartApprovalMode: Automatic runPolicy: AlwaysOn osType: Generic bootloader: BIOS diff --git a/tests/e2e/testdata/vm/image.yaml b/tests/e2e/testdata/vm/image.yaml index fa2b738b9..b4866640c 100644 --- a/tests/e2e/testdata/vm/image.yaml +++ b/tests/e2e/testdata/vm/image.yaml @@ -9,4 +9,4 @@ spec: type: "HTTP" http: insecureSkipVerify: true - url: "https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img" + url: "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img" diff --git a/tests/e2e/testdata/vm/provisioning/vm_provisioning_secret.yaml b/tests/e2e/testdata/vm/provisioning/vm_provisioning_secret.yaml index 27804fb27..8212580d0 100644 --- a/tests/e2e/testdata/vm/provisioning/vm_provisioning_secret.yaml +++ b/tests/e2e/testdata/vm/provisioning/vm_provisioning_secret.yaml @@ -14,7 +14,7 @@ metadata: namespace: test-d8-virtualization spec: disruptions: - approvalMode: Automatic + restartApprovalMode: Automatic osType: Generic bootloader: BIOS cpu: diff --git a/tests/e2e/testdata/vm/provisioning/vm_provisioning_useradata.yaml b/tests/e2e/testdata/vm/provisioning/vm_provisioning_useradata.yaml index 6d875d59b..64530163d 100644 --- a/tests/e2e/testdata/vm/provisioning/vm_provisioning_useradata.yaml +++ b/tests/e2e/testdata/vm/provisioning/vm_provisioning_useradata.yaml @@ -5,7 +5,7 @@ metadata: namespace: test-d8-virtualization spec: disruptions: - approvalMode: Automatic + restartApprovalMode: Automatic osType: Generic bootloader: BIOS cpu: diff --git a/tests/e2e/testdata/vm/resources/vm_100.yaml b/tests/e2e/testdata/vm/resources/vm_100.yaml index 963822fad..6371c79f1 100644 --- a/tests/e2e/testdata/vm/resources/vm_100.yaml +++ b/tests/e2e/testdata/vm/resources/vm_100.yaml @@ -5,7 +5,7 @@ metadata: namespace: test-d8-virtualization spec: disruptions: - approvalMode: Automatic + restartApprovalMode: Automatic runPolicy: AlwaysOn osType: Generic bootloader: BIOS diff --git a/tests/e2e/testdata/vm/resources/vm_25.yaml b/tests/e2e/testdata/vm/resources/vm_25.yaml index 822e62593..b07652e11 100644 --- a/tests/e2e/testdata/vm/resources/vm_25.yaml +++ b/tests/e2e/testdata/vm/resources/vm_25.yaml @@ -5,7 +5,7 @@ metadata: namespace: test-d8-virtualization spec: disruptions: - approvalMode: Automatic + restartApprovalMode: Automatic runPolicy: AlwaysOn osType: Generic bootloader: BIOS diff --git a/tests/e2e/testdata/vm/resources/vm_50.yaml b/tests/e2e/testdata/vm/resources/vm_50.yaml index eca9fba9b..908a475b8 100644 --- a/tests/e2e/testdata/vm/resources/vm_50.yaml +++ b/tests/e2e/testdata/vm/resources/vm_50.yaml @@ -5,7 +5,7 @@ metadata: namespace: test-d8-virtualization spec: disruptions: - approvalMode: Automatic + restartApprovalMode: Automatic runPolicy: AlwaysOn osType: Generic bootloader: BIOS diff --git a/tests/e2e/testdata/vm/vm_graceperiod.yaml b/tests/e2e/testdata/vm/vm_graceperiod.yaml index 5e75ee9db..d35704c19 100644 --- a/tests/e2e/testdata/vm/vm_graceperiod.yaml +++ b/tests/e2e/testdata/vm/vm_graceperiod.yaml @@ -5,7 +5,7 @@ metadata: namespace: test-d8-virtualization spec: disruptions: - approvalMode: Automatic + restartApprovalMode: Automatic runPolicy: AlwaysOn osType: Generic bootloader: BIOS diff --git a/tests/e2e/testdata/vm/vm_label.yaml b/tests/e2e/testdata/vm/vm_label.yaml new file mode 100644 index 000000000..10c87f49e --- /dev/null +++ b/tests/e2e/testdata/vm/vm_label.yaml @@ -0,0 +1,19 @@ +apiVersion: virtualization.deckhouse.io/v1alpha2 +kind: VirtualMachine +metadata: + name: test-vm-label + namespace: test-d8-virtualization +spec: + disruptions: + restartApprovalMode: Automatic + runPolicy: AlwaysOn + osType: Generic + bootloader: BIOS + cpu: + cores: 1 + memory: + size: 1Gi + blockDevices: + - type: VirtualMachineImage + virtualMachineImage: + name: test-vm-image \ No newline at end of file diff --git a/tests/e2e/testdata/vm/vm_priorityclassname.yaml b/tests/e2e/testdata/vm/vm_priorityclassname.yaml index 55f3ebe14..bfbddc066 100644 --- a/tests/e2e/testdata/vm/vm_priorityclassname.yaml +++ b/tests/e2e/testdata/vm/vm_priorityclassname.yaml @@ -13,7 +13,7 @@ metadata: namespace: test-d8-virtualization spec: disruptions: - approvalMode: Automatic + restartApprovalMode: Automatic runPolicy: AlwaysOn osType: Generic bootloader: BIOS diff --git a/tests/e2e/testdata/vm/vm_runpolicy.yaml b/tests/e2e/testdata/vm/vm_runpolicy.yaml index be0a92711..6904745b4 100644 --- a/tests/e2e/testdata/vm/vm_runpolicy.yaml +++ b/tests/e2e/testdata/vm/vm_runpolicy.yaml @@ -5,7 +5,7 @@ metadata: namespace: test-d8-virtualization spec: disruptions: - approvalMode: Automatic + restartApprovalMode: Automatic runPolicy: AlwaysOn osType: Generic bootloader: BIOS diff --git a/tests/e2e/vm_test.go b/tests/e2e/vm_test.go index 012821755..88432ff99 100644 --- a/tests/e2e/vm_test.go +++ b/tests/e2e/vm_test.go @@ -64,6 +64,7 @@ var _ = Describe("VM", Ordered, ContinueOnFailure, func() { GinkgoHelper() WaitResource(kc.ResourceKubevirtVM, name, "jsonpath={.status.printableStatus}="+printableStatus, LongWaitDuration) } + WaitVmStatus := func(name, phase string) { GinkgoHelper() WaitResource(kc.ResourceVM, name, "jsonpath={.status.phase}="+phase, LongWaitDuration) @@ -73,6 +74,7 @@ var _ = Describe("VM", Ordered, ContinueOnFailure, func() { GinkgoHelper() CheckField(kc.ResourceVM, name, "jsonpath={.status.phase}", phase) } + Context("Boot", func() { AfterAll(func() { kubectl.Delete(vmPath("boot/"), kc.DeleteOptions{}) @@ -106,6 +108,7 @@ var _ = Describe("VM", Ordered, ContinueOnFailure, func() { Test(manifest) }) }) + Context("RunPolicy", func() { manifest := vmPath("vm_runpolicy.yaml") var name string @@ -179,6 +182,7 @@ var _ = Describe("VM", Ordered, ContinueOnFailure, func() { }) }) + Context("Provisioning", func() { CheckSsh := func(vmName string) { GinkgoHelper() @@ -220,9 +224,11 @@ var _ = Describe("VM", Ordered, ContinueOnFailure, func() { Test(manifest) }) }) + Context("Network", func() { }) + Context("Resources", func() { GetKubevirtResources := func(name string) (*corev1.ResourceRequirements, error) { GinkgoHelper() @@ -287,9 +293,11 @@ var _ = Describe("VM", Ordered, ContinueOnFailure, func() { Test(manifest, "250m", "1Gi") }) }) + Context("NodePlacement", func() { }) + Context("PriorityClassName", func() { manifest := vmPath("vm_priorityclassname.yaml") var name string @@ -321,6 +329,7 @@ var _ = Describe("VM", Ordered, ContinueOnFailure, func() { }) }) }) + Context("TerminationGracePeriod", func() { manifest := vmPath("vm_graceperiod.yaml") jsonpath := "jsonpath={.spec.template.spec.terminationGracePeriodSeconds}" From 4eb93d9774c4db51f1c30f1dc4ed60f54b15170e Mon Sep 17 00:00:00 2001 From: Nikita Korolev Date: Thu, 8 Feb 2024 15:45:09 +0300 Subject: [PATCH 05/12] chore: add file with tests Signed-off-by: Nikita Korolev chore: fix file name Signed-off-by: Nikita Korolev --- tests/e2e/vm_label_annotation_test.go | 239 ++++++++++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 tests/e2e/vm_label_annotation_test.go diff --git a/tests/e2e/vm_label_annotation_test.go b/tests/e2e/vm_label_annotation_test.go new file mode 100644 index 000000000..f213aa427 --- /dev/null +++ b/tests/e2e/vm_label_annotation_test.go @@ -0,0 +1,239 @@ +package e2e + +import ( + "fmt" + kc "github.com/deckhouse/virtualization/tests/e2e/kubectl" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "io/fs" + "path/filepath" + "strings" +) + +var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { + imageManifest := vmPath("image.yaml") + BeforeAll(func() { + By("Apply image for vms") + ApplyFromFile(imageManifest) + WaitFromFile(imageManifest, PhaseReady, LongWaitDuration) + }) + AfterAll(func() { + By("Delete all manifests") + files := make([]string, 0) + err := filepath.Walk(conf.VM.TestDataDir, func(path string, info fs.FileInfo, err error) error { + if err == nil && strings.HasSuffix(info.Name(), "yaml") { + files = append(files, path) + } + return nil + }) + if err != nil || len(files) == 0 { + kubectl.Delete(imageManifest, kc.DeleteOptions{}) + kubectl.Delete(conf.VM.TestDataDir, kc.DeleteOptions{}) + } else { + for _, f := range files { + kubectl.Delete(f, kc.DeleteOptions{}) + } + } + }) + + WaitVmStatus := func(name, phase string) { + GinkgoHelper() + WaitResource(kc.ResourceVM, name, "jsonpath={.status.phase}="+phase, LongWaitDuration) + } + + Context("Label", func() { + manifest := vmPath("vm_label.yaml") + var name string + + label := "os=ubuntu" + vm, err := GetVMFromManifest(manifest) + + BeforeAll(func() { + By("Apply manifest") + vm, err := GetVMFromManifest(manifest) + Expect(err).To(BeNil()) + name = vm.Name + ApplyFromFile(manifest) + WaitVmStatus(name, VMStatusRunning) + }) + + AfterAll(func() { + By("Delete manifest") + kubectl.Delete(manifest, kc.DeleteOptions{}) + }) + + Describe("Add label os=ubuntu", func() { + + It("Labeled", func() { + Expect(err).To(BeNil()) + subCMD := fmt.Sprintf("-n %s label vm %s %s", conf.Namespace, vm.Name, label) + + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.Error()).NotTo(HaveOccurred()) + }) + }) + Describe("Check label on resource", func() { + It("VM", func() { + subCMD := fmt.Sprintf("-n %s get vm %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.StdOut()).To(Equal("ubuntu")) + }) + It("KVVM", func() { + subCMD := fmt.Sprintf("-n %s get virtualmachines.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.StdOut()).To(Equal("ubuntu")) + }) + It("KVVMI", func() { + subCMD := fmt.Sprintf("-n %s get virtualmachineinstances.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.StdOut()).To(Equal("ubuntu")) + }) + It("POD virtlauncher", func() { + subCMD := fmt.Sprintf("-n %s get pod --no-headers -o custom-columns=':metadata.name' | grep %s", conf.Namespace, vm.Name) + podCMD := kubectl.RawCommand(subCMD, ShortWaitDuration) + pod := strings.TrimSuffix(podCMD.StdOut(), "\n") + + subCMDPod := fmt.Sprintf("-n %s get po %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, pod) + res := kubectl.RawCommand(subCMDPod, ShortWaitDuration) + Expect(res.StdOut()).To(Equal("ubuntu")) + //Expect(pod.StdOut()).To(Equal("ubuntu")) + }) + }) + Describe("Remove label os=ubuntu", func() { + + It("Label was removed", func() { + Expect(err).To(BeNil()) + subCMD := fmt.Sprintf("-n %s label vm %s %s", conf.Namespace, vm.Name, "os-") + + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.Error()).NotTo(HaveOccurred()) + }) + }) + Describe("Label must be removed from resource", func() { + It("VM", func() { + subCMD := fmt.Sprintf("-n %s get vm %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.StdOut()).To(BeZero()) + }) + It("KVVM", func() { + subCMD := fmt.Sprintf("-n %s get virtualmachines.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + //Expect(res.StdOut()).To(Equal("")) + Expect(res.StdOut()).To(BeZero()) + }) + It("KVVMI", func() { + subCMD := fmt.Sprintf("-n %s get virtualmachineinstances.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.StdOut()).To(Equal("")) + //Expect(res.StdOut()).To(BeNil()) + }) + It("POD virtlauncher", func() { + subCMD := fmt.Sprintf("-n %s get pod --no-headers -o custom-columns=':metadata.name' | grep %s", conf.Namespace, vm.Name) + podCMD := kubectl.RawCommand(subCMD, ShortWaitDuration) + pod := strings.TrimSuffix(podCMD.StdOut(), "\n") + + subCMDPod := fmt.Sprintf("-n %s get po %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, pod) + res := kubectl.RawCommand(subCMDPod, ShortWaitDuration) + Expect(res.StdOut()).To(Equal("")) + //Expect(res.StdOut()).To(BeNil()) + }) + }) + }) + + Context("Annotation", func() { + manifest := vmPath("vm_label.yaml") + var name string + + annotation := "test-annotation=true" + vm, err := GetVMFromManifest(manifest) + + BeforeAll(func() { + By("Apply manifest") + vm, err := GetVMFromManifest(manifest) + Expect(err).To(BeNil()) + name = vm.Name + ApplyFromFile(manifest) + WaitVmStatus(name, VMStatusRunning) + }) + + AfterAll(func() { + By("Delete manifest") + kubectl.Delete(manifest, kc.DeleteOptions{}) + }) + + Describe("Add annotation test-annotation=true", func() { + + It("Annotated", func() { + Expect(err).To(BeNil()) + subCMD := fmt.Sprintf("-n %s annotate vm %s %s", conf.Namespace, vm.Name, annotation) + + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.Error()).NotTo(HaveOccurred()) + }) + }) + Describe("Check annotation on resource", func() { + It("VM", func() { + subCMD := fmt.Sprintf("-n %s get vm %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.StdOut()).To(Equal("true")) + }) + It("KVVM", func() { + subCMD := fmt.Sprintf("-n %s get virtualmachines.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.StdOut()).To(Equal("true")) + }) + It("KVVMI", func() { + subCMD := fmt.Sprintf("-n %s get virtualmachineinstances.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.StdOut()).To(Equal("true")) + }) + It("POD virtlauncher", func() { + subCMD := fmt.Sprintf("-n %s get pod --no-headers -o custom-columns=':metadata.name' | grep %s", conf.Namespace, vm.Name) + podCMD := kubectl.RawCommand(subCMD, ShortWaitDuration) + pod := strings.TrimSuffix(podCMD.StdOut(), "\n") + + subCMDPod := fmt.Sprintf("-n %s get po %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, pod) + res := kubectl.RawCommand(subCMDPod, ShortWaitDuration) + Expect(res.StdOut()).To(Equal("true")) + }) + }) + Describe("Remove annotation test-annotation=true", func() { + + It("Was removed", func() { + Expect(err).To(BeNil()) + subCMD := fmt.Sprintf("-n %s annotate vm %s %s", conf.Namespace, vm.Name, "test-annotation-") + + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.Error()).NotTo(HaveOccurred()) + }) + }) + Describe("Annotation must be removed from resource", func() { + It("VM", func() { + subCMD := fmt.Sprintf("-n %s get vm %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.StdOut()).To(BeZero()) + }) + It("KVVM", func() { + subCMD := fmt.Sprintf("-n %s get virtualmachines.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.StdOut()).To(BeZero()) + }) + It("KVVMI", func() { + subCMD := fmt.Sprintf("-n %s get virtualmachineinstances.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) + res := kubectl.RawCommand(subCMD, ShortWaitDuration) + Expect(res.StdOut()).To(BeZero()) + }) + It("POD virtlauncher", func() { + subCMD := fmt.Sprintf("-n %s get pod --no-headers -o custom-columns=':metadata.name' | grep %s", conf.Namespace, vm.Name) + podCMD := kubectl.RawCommand(subCMD, ShortWaitDuration) + pod := strings.TrimSuffix(podCMD.StdOut(), "\n") + + subCMDPod := fmt.Sprintf("-n %s get po %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, pod) + res := kubectl.RawCommand(subCMDPod, ShortWaitDuration) + Expect(res.StdOut()).To(BeZero()) + }) + }) + }) + + Context("Some") +}) From 79e394c93ccfc445eef463eff92c3d262d48be8f Mon Sep 17 00:00:00 2001 From: Nikita Korolev Date: Thu, 8 Feb 2024 19:46:49 +0300 Subject: [PATCH 06/12] chore: taskfile add task with predefined vars and run one test (ginkgo --focus) Signed-off-by: Nikita Korolev --- .gitignore | 5 ++- tests/e2e/Taskfile.yaml | 39 ++++++++++++------- ...vm_label.yaml => vm_label_annotation.yaml} | 0 tests/e2e/vm_label_annotation_test.go | 7 ++-- 4 files changed, 33 insertions(+), 18 deletions(-) rename tests/e2e/testdata/vm/{vm_label.yaml => vm_label_annotation.yaml} (100%) diff --git a/.gitignore b/.gitignore index ce019f8df..8a957d706 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,7 @@ __pycache__/ tmp/* # Kubernetes -.kube \ No newline at end of file +.kube + +# direnv +.envrc diff --git a/tests/e2e/Taskfile.yaml b/tests/e2e/Taskfile.yaml index 5045f92f2..004c67949 100644 --- a/tests/e2e/Taskfile.yaml +++ b/tests/e2e/Taskfile.yaml @@ -33,24 +33,35 @@ tasks: cmds: - ginkgo - set_local_env: - cmds: - - | - if [ -z "$KUBECONFIG" ]; then export KUBECONFIG=$HOME/.kube/config;else echo $KUBECONFIG - KUBECONFIG;fi; - if [ -z "$E2E_CLUSTERTRANSPORT_KUBECONFIG" ]; then - export E2E_CLUSTERTRANSPORT_KUBECONFIG=$KUBECONFIG - else - echo $E2E_CLUSTERTRANSPORT_KUBECONFIG - E2E_CLUSTERTRANSPORT_KUBECONFIG - fi - run_local: desc: "Run localy e2e tests" deps: - - set_local_env - virtctl - ginkgo cmds: - - ginkgo + - | + export KUBECONFIG={{.KUBECONFIG}} + export E2E_CLUSTERTRANSPORT_KUBECONFIG={{.E2E_CLUSTERTRANSPORT_KUBECONFIG}} + ginkgo + vars: + KUBECONFIG: '{{default "$HOME/.kube/config" .KUBECONFIG}}' + E2E_CLUSTERTRANSPORT_KUBECONFIG: '{{default "$HOME/.kube/config" .E2E_CLUSTERTRANSPORT_KUBECONFIG}}' -# ginkgo --focus -# ginkgo --focus "Disks" + run_one: + desc: "Run one test or group" + deps: + - virtctl + - ginkgo + cmds: + - | + {{if .TEST }} + export KUBECONFIG={{.KUBECONFIG}} + export E2E_CLUSTERTRANSPORT_KUBECONFIG={{.E2E_CLUSTERTRANSPORT_KUBECONFIG}} + ginkgo --focus "{{ .TEST }}" -v + {{else}} + echo "Test not passed" + echo 'Example: TEST="Label and Annotation" task ginkgo_test' + {{end}} + vars: + KUBECONFIG: '{{default "$HOME/.kube/config" .KUBECONFIG}}' + E2E_CLUSTERTRANSPORT_KUBECONFIG: '{{default "$HOME/.kube/config" .E2E_CLUSTERTRANSPORT_KUBECONFIG}}' diff --git a/tests/e2e/testdata/vm/vm_label.yaml b/tests/e2e/testdata/vm/vm_label_annotation.yaml similarity index 100% rename from tests/e2e/testdata/vm/vm_label.yaml rename to tests/e2e/testdata/vm/vm_label_annotation.yaml diff --git a/tests/e2e/vm_label_annotation_test.go b/tests/e2e/vm_label_annotation_test.go index f213aa427..6cf47ecb9 100644 --- a/tests/e2e/vm_label_annotation_test.go +++ b/tests/e2e/vm_label_annotation_test.go @@ -12,6 +12,7 @@ import ( var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { imageManifest := vmPath("image.yaml") + manifestVM := vmPath("vm_label_annotation.yaml") BeforeAll(func() { By("Apply image for vms") ApplyFromFile(imageManifest) @@ -42,7 +43,7 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { } Context("Label", func() { - manifest := vmPath("vm_label.yaml") + manifest := manifestVM var name string label := "os=ubuntu" @@ -141,7 +142,7 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) Context("Annotation", func() { - manifest := vmPath("vm_label.yaml") + manifest := vmPath("vm_label_annotation.yaml") var name string annotation := "test-annotation=true" @@ -235,5 +236,5 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) }) - Context("Some") + //Context("Some") }) From 253ddd4e9bf20e243cffa81904ac7caeef0b17ca Mon Sep 17 00:00:00 2001 From: Nikita Korolev Date: Thu, 8 Feb 2024 22:40:31 +0300 Subject: [PATCH 07/12] fix: task run_one and var Signed-off-by: Nikita Korolev --- tests/e2e/Taskfile.yaml | 2 +- tests/e2e/vm_label_annotation_test.go | 20 ++++++++------------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/tests/e2e/Taskfile.yaml b/tests/e2e/Taskfile.yaml index 004c67949..0438ef024 100644 --- a/tests/e2e/Taskfile.yaml +++ b/tests/e2e/Taskfile.yaml @@ -60,7 +60,7 @@ tasks: ginkgo --focus "{{ .TEST }}" -v {{else}} echo "Test not passed" - echo 'Example: TEST="Label and Annotation" task ginkgo_test' + echo 'Example: TEST="Label and Annotation" task run_one' {{end}} vars: KUBECONFIG: '{{default "$HOME/.kube/config" .KUBECONFIG}}' diff --git a/tests/e2e/vm_label_annotation_test.go b/tests/e2e/vm_label_annotation_test.go index 6cf47ecb9..a110df5ba 100644 --- a/tests/e2e/vm_label_annotation_test.go +++ b/tests/e2e/vm_label_annotation_test.go @@ -43,24 +43,23 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { } Context("Label", func() { - manifest := manifestVM var name string label := "os=ubuntu" - vm, err := GetVMFromManifest(manifest) + vm, err := GetVMFromManifest(manifestVM) BeforeAll(func() { By("Apply manifest") - vm, err := GetVMFromManifest(manifest) + vm, err := GetVMFromManifest(manifestVM) Expect(err).To(BeNil()) name = vm.Name - ApplyFromFile(manifest) + ApplyFromFile(manifestVM) WaitVmStatus(name, VMStatusRunning) }) AfterAll(func() { By("Delete manifest") - kubectl.Delete(manifest, kc.DeleteOptions{}) + kubectl.Delete(manifestVM, kc.DeleteOptions{}) }) Describe("Add label os=ubuntu", func() { @@ -142,24 +141,23 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) Context("Annotation", func() { - manifest := vmPath("vm_label_annotation.yaml") var name string annotation := "test-annotation=true" - vm, err := GetVMFromManifest(manifest) + vm, err := GetVMFromManifest(manifestVM) BeforeAll(func() { By("Apply manifest") - vm, err := GetVMFromManifest(manifest) + vm, err := GetVMFromManifest(manifestVM) Expect(err).To(BeNil()) name = vm.Name - ApplyFromFile(manifest) + ApplyFromFile(manifestVM) WaitVmStatus(name, VMStatusRunning) }) AfterAll(func() { By("Delete manifest") - kubectl.Delete(manifest, kc.DeleteOptions{}) + kubectl.Delete(manifestVM, kc.DeleteOptions{}) }) Describe("Add annotation test-annotation=true", func() { @@ -235,6 +233,4 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) }) }) - - //Context("Some") }) From e3df7eb325a2144c5193316d8ce538335c1d48e6 Mon Sep 17 00:00:00 2001 From: Nikita Korolev Date: Fri, 9 Feb 2024 12:03:53 +0300 Subject: [PATCH 08/12] chore: fix test condition Signed-off-by: Nikita Korolev --- tests/e2e/kubectl/resource.go | 15 +++---- tests/e2e/vm_label_annotation_test.go | 56 +++++++++++++++++++-------- 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/tests/e2e/kubectl/resource.go b/tests/e2e/kubectl/resource.go index 732fcc6a1..f2e9ca226 100644 --- a/tests/e2e/kubectl/resource.go +++ b/tests/e2e/kubectl/resource.go @@ -1,11 +1,12 @@ package kubectl const ( - ResourceNode Resource = "node" - ResourceNamespace Resource = "namespace" - ResourcePod Resource = "pod" - ResourceKubevirtVM Resource = "virtualmachines.x.virtualization.deckhouse.io" - ResourceVM Resource = "virtualmachine.virtualization.deckhouse.io" - ResourceVMIPClaim Resource = "virtualmachineipaddressclaims.virtualization.deckhouse.io" - ResourceVMIPLeas Resource = "virtualmachineipaddressleases.virtualization.deckhouse.io" + ResourceNode Resource = "node" + ResourceNamespace Resource = "namespace" + ResourcePod Resource = "pod" + ResourceKubevirtVM Resource = "virtualmachines.x.virtualization.deckhouse.io" + ResourceKubevirtVMI Resource = "virtualmachineinstances.x.virtualization.deckhouse.io" + ResourceVM Resource = "virtualmachine.virtualization.deckhouse.io" + ResourceVMIPClaim Resource = "virtualmachineipaddressclaims.virtualization.deckhouse.io" + ResourceVMIPLeas Resource = "virtualmachineipaddressleases.virtualization.deckhouse.io" ) diff --git a/tests/e2e/vm_label_annotation_test.go b/tests/e2e/vm_label_annotation_test.go index a110df5ba..042cd5a80 100644 --- a/tests/e2e/vm_label_annotation_test.go +++ b/tests/e2e/vm_label_annotation_test.go @@ -13,6 +13,31 @@ import ( var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { imageManifest := vmPath("image.yaml") manifestVM := vmPath("vm_label_annotation.yaml") + + type jsonPathCMD struct { + label string + annotation string + } + + type resourceCMD struct { + getVM string + getKVVM string + getKVVMi string + getPOD string + getJsonPath jsonPathCMD + } + + kctlCMD := resourceCMD{ + getPOD: "get pod --no-headers -o custom-columns=':metadata.name'", + getVM: "get vm", + getKVVM: "get virtualmachines.x.virtualization.deckhouse.io", + getKVVMi: "get virtualmachineinstances.x.virtualization.deckhouse.io", + getJsonPath: jsonPathCMD{ + label: "-o jsonpath='{.metadata.labels.os}'", + annotation: "-o jsonpath='{.metadata.annotations.test-annotation}'", + }, + } + BeforeAll(func() { By("Apply image for vms") ApplyFromFile(imageManifest) @@ -74,29 +99,28 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) Describe("Check label on resource", func() { It("VM", func() { - subCMD := fmt.Sprintf("-n %s get vm %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) + subCMD := fmt.Sprintf("-n %s %s %s %s", conf.Namespace, kctlCMD.getVM, vm.Name, kctlCMD.getJsonPath.label) res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.StdOut()).To(Equal("ubuntu")) }) It("KVVM", func() { - subCMD := fmt.Sprintf("-n %s get virtualmachines.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) + subCMD := fmt.Sprintf("-n %s %s %s %s", conf.Namespace, kctlCMD.getKVVM, vm.Name, kctlCMD.getJsonPath.label) res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.StdOut()).To(Equal("ubuntu")) }) It("KVVMI", func() { - subCMD := fmt.Sprintf("-n %s get virtualmachineinstances.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) + subCMD := fmt.Sprintf("-n %s %s %s %s", conf.Namespace, kctlCMD.getKVVMi, vm.Name, kctlCMD.getJsonPath.label) res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.StdOut()).To(Equal("ubuntu")) }) It("POD virtlauncher", func() { - subCMD := fmt.Sprintf("-n %s get pod --no-headers -o custom-columns=':metadata.name' | grep %s", conf.Namespace, vm.Name) + subCMD := fmt.Sprintf("-n %s %s | grep %s", conf.Namespace, kctlCMD.getPOD, vm.Name) podCMD := kubectl.RawCommand(subCMD, ShortWaitDuration) pod := strings.TrimSuffix(podCMD.StdOut(), "\n") - subCMDPod := fmt.Sprintf("-n %s get po %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, pod) + subCMDPod := fmt.Sprintf("-n %s get po %s %s", conf.Namespace, pod, kctlCMD.getJsonPath.label) res := kubectl.RawCommand(subCMDPod, ShortWaitDuration) Expect(res.StdOut()).To(Equal("ubuntu")) - //Expect(pod.StdOut()).To(Equal("ubuntu")) }) }) Describe("Remove label os=ubuntu", func() { @@ -110,22 +134,21 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) }) Describe("Label must be removed from resource", func() { + It("VM", func() { subCMD := fmt.Sprintf("-n %s get vm %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) res := kubectl.RawCommand(subCMD, ShortWaitDuration) - Expect(res.StdOut()).To(BeZero()) + Expect(res.StdOut()).To(BeEmpty()) }) It("KVVM", func() { subCMD := fmt.Sprintf("-n %s get virtualmachines.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) res := kubectl.RawCommand(subCMD, ShortWaitDuration) - //Expect(res.StdOut()).To(Equal("")) - Expect(res.StdOut()).To(BeZero()) + Expect(res.StdOut()).To(BeEmpty()) }) It("KVVMI", func() { subCMD := fmt.Sprintf("-n %s get virtualmachineinstances.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) res := kubectl.RawCommand(subCMD, ShortWaitDuration) - Expect(res.StdOut()).To(Equal("")) - //Expect(res.StdOut()).To(BeNil()) + Expect(res.StdOut()).To(BeEmpty()) }) It("POD virtlauncher", func() { subCMD := fmt.Sprintf("-n %s get pod --no-headers -o custom-columns=':metadata.name' | grep %s", conf.Namespace, vm.Name) @@ -134,8 +157,7 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { subCMDPod := fmt.Sprintf("-n %s get po %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, pod) res := kubectl.RawCommand(subCMDPod, ShortWaitDuration) - Expect(res.StdOut()).To(Equal("")) - //Expect(res.StdOut()).To(BeNil()) + Expect(res.StdOut()).To(BeEmpty()) }) }) }) @@ -210,17 +232,17 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { It("VM", func() { subCMD := fmt.Sprintf("-n %s get vm %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) res := kubectl.RawCommand(subCMD, ShortWaitDuration) - Expect(res.StdOut()).To(BeZero()) + Expect(res.StdOut()).To(BeEmpty()) }) It("KVVM", func() { subCMD := fmt.Sprintf("-n %s get virtualmachines.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) res := kubectl.RawCommand(subCMD, ShortWaitDuration) - Expect(res.StdOut()).To(BeZero()) + Expect(res.StdOut()).To(BeEmpty()) }) It("KVVMI", func() { subCMD := fmt.Sprintf("-n %s get virtualmachineinstances.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) res := kubectl.RawCommand(subCMD, ShortWaitDuration) - Expect(res.StdOut()).To(BeZero()) + Expect(res.StdOut()).To(BeEmpty()) }) It("POD virtlauncher", func() { subCMD := fmt.Sprintf("-n %s get pod --no-headers -o custom-columns=':metadata.name' | grep %s", conf.Namespace, vm.Name) @@ -229,7 +251,7 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { subCMDPod := fmt.Sprintf("-n %s get po %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, pod) res := kubectl.RawCommand(subCMDPod, ShortWaitDuration) - Expect(res.StdOut()).To(BeZero()) + Expect(res.StdOut()).To(BeEmpty()) }) }) }) From 749ee79e720f790d9873554851ebc239bdf5d19b Mon Sep 17 00:00:00 2001 From: Nikita Korolev Date: Mon, 26 Feb 2024 17:24:39 +0300 Subject: [PATCH 09/12] chore: refactor tests Signed-off-by: Nikita Korolev chore: remove unnecessary constraints Signed-off-by: Nikita Korolev chore: refactor tests Signed-off-by: Nikita Korolev --- tests/e2e/vm_label_annotation_test.go | 147 +++++++++++--------------- 1 file changed, 63 insertions(+), 84 deletions(-) diff --git a/tests/e2e/vm_label_annotation_test.go b/tests/e2e/vm_label_annotation_test.go index 042cd5a80..756b39146 100644 --- a/tests/e2e/vm_label_annotation_test.go +++ b/tests/e2e/vm_label_annotation_test.go @@ -2,6 +2,7 @@ package e2e import ( "fmt" + "github.com/deckhouse/virtualization/tests/e2e/executor" kc "github.com/deckhouse/virtualization/tests/e2e/kubectl" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -14,28 +15,37 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { imageManifest := vmPath("image.yaml") manifestVM := vmPath("vm_label_annotation.yaml") - type jsonPathCMD struct { - label string - annotation string + const ( + labelName = "os" + labelValue = "ubuntu" + annotationName = "test-annotation" + annotationValue = "true" + ) + + getPodName := func(namespace, resourceName string) string { + getPodCMD := "get pod --no-headers -o custom-columns=':metadata.name'" + subCMD := fmt.Sprintf("-n %s %s | grep %s", namespace, getPodCMD, resourceName) + podCMD := kubectl.RawCommand(subCMD, ShortWaitDuration) + podName := strings.TrimSuffix(podCMD.StdOut(), "\n") + return podName } - type resourceCMD struct { - getVM string - getKVVM string - getKVVMi string - getPOD string - getJsonPath jsonPathCMD + getRecourseLabel := func(resourceType kc.Resource, resourceName string) *executor.CMDResult { + label := fmt.Sprintf("jsonpath='{.metadata.labels.%s}'", labelName) + stdout := kubectl.GetResource(resourceType, resourceName, kc.GetOptions{ + Output: label, + Namespace: conf.Namespace, + }) + return stdout } - kctlCMD := resourceCMD{ - getPOD: "get pod --no-headers -o custom-columns=':metadata.name'", - getVM: "get vm", - getKVVM: "get virtualmachines.x.virtualization.deckhouse.io", - getKVVMi: "get virtualmachineinstances.x.virtualization.deckhouse.io", - getJsonPath: jsonPathCMD{ - label: "-o jsonpath='{.metadata.labels.os}'", - annotation: "-o jsonpath='{.metadata.annotations.test-annotation}'", - }, + getRecourseAnnotation := func(resourceType kc.Resource, resourceName string) *executor.CMDResult { + annotation := fmt.Sprintf("jsonpath='{.metadata.annotations.%s}'", annotationName) + stdout := kubectl.GetResource(resourceType, resourceName, kc.GetOptions{ + Output: annotation, + Namespace: conf.Namespace, + }) + return stdout } BeforeAll(func() { @@ -70,7 +80,6 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { Context("Label", func() { var name string - label := "os=ubuntu" vm, err := GetVMFromManifest(manifestVM) BeforeAll(func() { @@ -87,11 +96,11 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { kubectl.Delete(manifestVM, kc.DeleteOptions{}) }) - Describe("Add label os=ubuntu", func() { + Describe(fmt.Sprintf("Add label %s=%s", labelName, labelValue), func() { It("Labeled", func() { Expect(err).To(BeNil()) - subCMD := fmt.Sprintf("-n %s label vm %s %s", conf.Namespace, vm.Name, label) + subCMD := fmt.Sprintf("-n %s label vm %s %s=%s", conf.Namespace, vm.Name, labelName, labelValue) res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.Error()).NotTo(HaveOccurred()) @@ -99,64 +108,49 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) Describe("Check label on resource", func() { It("VM", func() { - subCMD := fmt.Sprintf("-n %s %s %s %s", conf.Namespace, kctlCMD.getVM, vm.Name, kctlCMD.getJsonPath.label) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) - Expect(res.StdOut()).To(Equal("ubuntu")) + res := getRecourseLabel(kc.ResourceVM, vm.Name) + Expect(res.StdOut()).To(Equal(labelValue)) }) It("KVVM", func() { - subCMD := fmt.Sprintf("-n %s %s %s %s", conf.Namespace, kctlCMD.getKVVM, vm.Name, kctlCMD.getJsonPath.label) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) - Expect(res.StdOut()).To(Equal("ubuntu")) + res := getRecourseLabel(kc.ResourceKubevirtVM, vm.Name) + Expect(res.StdOut()).To(Equal(labelValue)) }) It("KVVMI", func() { - subCMD := fmt.Sprintf("-n %s %s %s %s", conf.Namespace, kctlCMD.getKVVMi, vm.Name, kctlCMD.getJsonPath.label) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) - Expect(res.StdOut()).To(Equal("ubuntu")) + res := getRecourseLabel(kc.ResourceKubevirtVMI, vm.Name) + Expect(res.StdOut()).To(Equal(labelValue)) }) It("POD virtlauncher", func() { - subCMD := fmt.Sprintf("-n %s %s | grep %s", conf.Namespace, kctlCMD.getPOD, vm.Name) - podCMD := kubectl.RawCommand(subCMD, ShortWaitDuration) - pod := strings.TrimSuffix(podCMD.StdOut(), "\n") - - subCMDPod := fmt.Sprintf("-n %s get po %s %s", conf.Namespace, pod, kctlCMD.getJsonPath.label) - res := kubectl.RawCommand(subCMDPod, ShortWaitDuration) - Expect(res.StdOut()).To(Equal("ubuntu")) + pod := getPodName(conf.Namespace, vm.Name) + res := getRecourseLabel(kc.ResourcePod, pod) + Expect(res.StdOut()).To(Equal(labelValue)) }) }) - Describe("Remove label os=ubuntu", func() { + Describe(fmt.Sprintf("Remove label %s=%s", labelName, labelValue), func() { It("Label was removed", func() { Expect(err).To(BeNil()) - subCMD := fmt.Sprintf("-n %s label vm %s %s", conf.Namespace, vm.Name, "os-") + subCMD := fmt.Sprintf("-n %s label vm %s %s-", conf.Namespace, vm.Name, labelName) res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.Error()).NotTo(HaveOccurred()) }) }) Describe("Label must be removed from resource", func() { - It("VM", func() { - subCMD := fmt.Sprintf("-n %s get vm %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) + res := getRecourseLabel(kc.ResourceVM, vm.Name) Expect(res.StdOut()).To(BeEmpty()) }) It("KVVM", func() { - subCMD := fmt.Sprintf("-n %s get virtualmachines.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) + res := getRecourseLabel(kc.ResourceKubevirtVM, vm.Name) Expect(res.StdOut()).To(BeEmpty()) }) It("KVVMI", func() { - subCMD := fmt.Sprintf("-n %s get virtualmachineinstances.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, vm.Name) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) + res := getRecourseLabel(kc.ResourceKubevirtVMI, vm.Name) Expect(res.StdOut()).To(BeEmpty()) }) It("POD virtlauncher", func() { - subCMD := fmt.Sprintf("-n %s get pod --no-headers -o custom-columns=':metadata.name' | grep %s", conf.Namespace, vm.Name) - podCMD := kubectl.RawCommand(subCMD, ShortWaitDuration) - pod := strings.TrimSuffix(podCMD.StdOut(), "\n") - - subCMDPod := fmt.Sprintf("-n %s get po %s -o jsonpath='{.metadata.labels.os}'", conf.Namespace, pod) - res := kubectl.RawCommand(subCMDPod, ShortWaitDuration) + pod := getPodName(conf.Namespace, vm.Name) + res := getRecourseLabel(kc.ResourcePod, pod) Expect(res.StdOut()).To(BeEmpty()) }) }) @@ -165,7 +159,6 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { Context("Annotation", func() { var name string - annotation := "test-annotation=true" vm, err := GetVMFromManifest(manifestVM) BeforeAll(func() { @@ -182,11 +175,11 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { kubectl.Delete(manifestVM, kc.DeleteOptions{}) }) - Describe("Add annotation test-annotation=true", func() { + Describe(fmt.Sprintf("Add annotation %s=%s", annotationName, annotationValue), func() { It("Annotated", func() { Expect(err).To(BeNil()) - subCMD := fmt.Sprintf("-n %s annotate vm %s %s", conf.Namespace, vm.Name, annotation) + subCMD := fmt.Sprintf("-n %s annotate vm %s %s=%s", conf.Namespace, vm.Name, annotationName, annotationValue) res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.Error()).NotTo(HaveOccurred()) @@ -194,35 +187,28 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) Describe("Check annotation on resource", func() { It("VM", func() { - subCMD := fmt.Sprintf("-n %s get vm %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) - Expect(res.StdOut()).To(Equal("true")) + res := getRecourseAnnotation(kc.ResourceVM, vm.Name) + Expect(res.StdOut()).To(Equal(annotationValue)) }) It("KVVM", func() { - subCMD := fmt.Sprintf("-n %s get virtualmachines.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) - Expect(res.StdOut()).To(Equal("true")) + res := getRecourseAnnotation(kc.ResourceKubevirtVM, vm.Name) + Expect(res.StdOut()).To(Equal(annotationValue)) }) It("KVVMI", func() { - subCMD := fmt.Sprintf("-n %s get virtualmachineinstances.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) - Expect(res.StdOut()).To(Equal("true")) + res := getRecourseAnnotation(kc.ResourceKubevirtVMI, vm.Name) + Expect(res.StdOut()).To(Equal(annotationValue)) }) It("POD virtlauncher", func() { - subCMD := fmt.Sprintf("-n %s get pod --no-headers -o custom-columns=':metadata.name' | grep %s", conf.Namespace, vm.Name) - podCMD := kubectl.RawCommand(subCMD, ShortWaitDuration) - pod := strings.TrimSuffix(podCMD.StdOut(), "\n") - - subCMDPod := fmt.Sprintf("-n %s get po %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, pod) - res := kubectl.RawCommand(subCMDPod, ShortWaitDuration) - Expect(res.StdOut()).To(Equal("true")) + pod := getPodName(conf.Namespace, vm.Name) + res := getRecourseAnnotation(kc.ResourcePod, pod) + Expect(res.StdOut()).To(Equal(annotationValue)) }) }) Describe("Remove annotation test-annotation=true", func() { It("Was removed", func() { Expect(err).To(BeNil()) - subCMD := fmt.Sprintf("-n %s annotate vm %s %s", conf.Namespace, vm.Name, "test-annotation-") + subCMD := fmt.Sprintf("-n %s annotate vm %s %s-", conf.Namespace, vm.Name, annotationName) res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.Error()).NotTo(HaveOccurred()) @@ -230,27 +216,20 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) Describe("Annotation must be removed from resource", func() { It("VM", func() { - subCMD := fmt.Sprintf("-n %s get vm %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) + res := getRecourseAnnotation(kc.ResourceVM, vm.Name) Expect(res.StdOut()).To(BeEmpty()) }) It("KVVM", func() { - subCMD := fmt.Sprintf("-n %s get virtualmachines.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) + res := getRecourseAnnotation(kc.ResourceKubevirtVM, vm.Name) Expect(res.StdOut()).To(BeEmpty()) }) It("KVVMI", func() { - subCMD := fmt.Sprintf("-n %s get virtualmachineinstances.x.virtualization.deckhouse.io %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, vm.Name) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) + res := getRecourseAnnotation(kc.ResourceKubevirtVMI, vm.Name) Expect(res.StdOut()).To(BeEmpty()) }) It("POD virtlauncher", func() { - subCMD := fmt.Sprintf("-n %s get pod --no-headers -o custom-columns=':metadata.name' | grep %s", conf.Namespace, vm.Name) - podCMD := kubectl.RawCommand(subCMD, ShortWaitDuration) - pod := strings.TrimSuffix(podCMD.StdOut(), "\n") - - subCMDPod := fmt.Sprintf("-n %s get po %s -o jsonpath='{.metadata.annotations.test-annotation}'", conf.Namespace, pod) - res := kubectl.RawCommand(subCMDPod, ShortWaitDuration) + pod := getPodName(conf.Namespace, vm.Name) + res := getRecourseAnnotation(kc.ResourcePod, pod) Expect(res.StdOut()).To(BeEmpty()) }) }) From 18b9cc09674f92a8c868b54f837c0cc359bc9786 Mon Sep 17 00:00:00 2001 From: Nikita Korolev Date: Wed, 28 Feb 2024 10:42:39 +0300 Subject: [PATCH 10/12] chore: add handling errors Signed-off-by: Nikita Korolev chore: add handling errors Signed-off-by: Nikita Korolev chore: update getPodName Signed-off-by: Nikita Korolev --- tests/e2e/vm_label_annotation_test.go | 51 +++++++++++++++++++-------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/tests/e2e/vm_label_annotation_test.go b/tests/e2e/vm_label_annotation_test.go index 756b39146..c76e83d4a 100644 --- a/tests/e2e/vm_label_annotation_test.go +++ b/tests/e2e/vm_label_annotation_test.go @@ -22,9 +22,9 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { annotationValue = "true" ) - getPodName := func(namespace, resourceName string) string { + getPodName := func(resourceName string) string { getPodCMD := "get pod --no-headers -o custom-columns=':metadata.name'" - subCMD := fmt.Sprintf("-n %s %s | grep %s", namespace, getPodCMD, resourceName) + subCMD := fmt.Sprintf("-n %s %s | grep %s", conf.Namespace, getPodCMD, resourceName) podCMD := kubectl.RawCommand(subCMD, ShortWaitDuration) podName := strings.TrimSuffix(podCMD.StdOut(), "\n") return podName @@ -32,20 +32,20 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { getRecourseLabel := func(resourceType kc.Resource, resourceName string) *executor.CMDResult { label := fmt.Sprintf("jsonpath='{.metadata.labels.%s}'", labelName) - stdout := kubectl.GetResource(resourceType, resourceName, kc.GetOptions{ + cmdResult := kubectl.GetResource(resourceType, resourceName, kc.GetOptions{ Output: label, Namespace: conf.Namespace, }) - return stdout + return cmdResult } getRecourseAnnotation := func(resourceType kc.Resource, resourceName string) *executor.CMDResult { annotation := fmt.Sprintf("jsonpath='{.metadata.annotations.%s}'", annotationName) - stdout := kubectl.GetResource(resourceType, resourceName, kc.GetOptions{ + cmdResult := kubectl.GetResource(resourceType, resourceName, kc.GetOptions{ Output: annotation, Namespace: conf.Namespace, }) - return stdout + return cmdResult } BeforeAll(func() { @@ -97,34 +97,39 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) Describe(fmt.Sprintf("Add label %s=%s", labelName, labelValue), func() { - It("Labeled", func() { Expect(err).To(BeNil()) subCMD := fmt.Sprintf("-n %s label vm %s %s=%s", conf.Namespace, vm.Name, labelName, labelValue) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.Error()).NotTo(HaveOccurred()) }) }) + Describe("Check label on resource", func() { It("VM", func() { res := getRecourseLabel(kc.ResourceVM, vm.Name) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get VM %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(Equal(labelValue)) }) It("KVVM", func() { res := getRecourseLabel(kc.ResourceKubevirtVM, vm.Name) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get KVVM %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(Equal(labelValue)) }) It("KVVMI", func() { res := getRecourseLabel(kc.ResourceKubevirtVMI, vm.Name) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get KVVMI %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(Equal(labelValue)) }) It("POD virtlauncher", func() { - pod := getPodName(conf.Namespace, vm.Name) + //pod := getPodName(conf.Namespace, vm.Name) + pod := getPodName(vm.Name) res := getRecourseLabel(kc.ResourcePod, pod) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get pod %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(Equal(labelValue)) }) }) + Describe(fmt.Sprintf("Remove label %s=%s", labelName, labelValue), func() { It("Label was removed", func() { @@ -135,22 +140,28 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { Expect(res.Error()).NotTo(HaveOccurred()) }) }) + Describe("Label must be removed from resource", func() { It("VM", func() { res := getRecourseLabel(kc.ResourceVM, vm.Name) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get VM %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(BeEmpty()) }) It("KVVM", func() { res := getRecourseLabel(kc.ResourceKubevirtVM, vm.Name) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get KVVM %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(BeEmpty()) }) It("KVVMI", func() { res := getRecourseLabel(kc.ResourceKubevirtVMI, vm.Name) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get KVVMI %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(BeEmpty()) }) It("POD virtlauncher", func() { - pod := getPodName(conf.Namespace, vm.Name) + //pod := getPodName(conf.Namespace, vm.Name) + pod := getPodName(vm.Name) res := getRecourseLabel(kc.ResourcePod, pod) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get pod %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(BeEmpty()) }) }) @@ -176,60 +187,70 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) Describe(fmt.Sprintf("Add annotation %s=%s", annotationName, annotationValue), func() { - It("Annotated", func() { Expect(err).To(BeNil()) subCMD := fmt.Sprintf("-n %s annotate vm %s %s=%s", conf.Namespace, vm.Name, annotationName, annotationValue) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.Error()).NotTo(HaveOccurred()) }) }) + Describe("Check annotation on resource", func() { It("VM", func() { res := getRecourseAnnotation(kc.ResourceVM, vm.Name) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get VM %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(Equal(annotationValue)) }) It("KVVM", func() { res := getRecourseAnnotation(kc.ResourceKubevirtVM, vm.Name) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get KVVM %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(Equal(annotationValue)) }) It("KVVMI", func() { res := getRecourseAnnotation(kc.ResourceKubevirtVMI, vm.Name) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get KVVMI %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(Equal(annotationValue)) }) It("POD virtlauncher", func() { - pod := getPodName(conf.Namespace, vm.Name) + //pod := getPodName(conf.Namespace, vm.Name) + pod := getPodName(vm.Name) res := getRecourseAnnotation(kc.ResourcePod, pod) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get pod %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(Equal(annotationValue)) }) }) + Describe("Remove annotation test-annotation=true", func() { It("Was removed", func() { Expect(err).To(BeNil()) subCMD := fmt.Sprintf("-n %s annotate vm %s %s-", conf.Namespace, vm.Name, annotationName) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.Error()).NotTo(HaveOccurred()) }) }) + Describe("Annotation must be removed from resource", func() { It("VM", func() { res := getRecourseAnnotation(kc.ResourceVM, vm.Name) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get VM %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(BeEmpty()) }) It("KVVM", func() { res := getRecourseAnnotation(kc.ResourceKubevirtVM, vm.Name) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get KVVM %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(BeEmpty()) }) It("KVVMI", func() { res := getRecourseAnnotation(kc.ResourceKubevirtVMI, vm.Name) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get KVVMI %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(BeEmpty()) }) It("POD virtlauncher", func() { - pod := getPodName(conf.Namespace, vm.Name) + //pod := getPodName(conf.Namespace, vm.Name) + pod := getPodName(vm.Name) res := getRecourseAnnotation(kc.ResourcePod, pod) + Expect(res.Error()).NotTo(HaveOccurred(), "failed to get pod %s.\n%s", vm.Name, res.StdErr()) Expect(res.StdOut()).To(BeEmpty()) }) }) From d5080ddda82e1b1c3749937bbf702c8ca11bc23f Mon Sep 17 00:00:00 2001 From: Nikita Korolev Date: Wed, 6 Mar 2024 18:07:03 +0300 Subject: [PATCH 11/12] chore: fixing comments Signed-off-by: Nikita Korolev --- tests/e2e/Taskfile.yaml | 2 +- tests/e2e/vm_label_annotation_test.go | 96 ++++++++++++++++----------- 2 files changed, 60 insertions(+), 38 deletions(-) diff --git a/tests/e2e/Taskfile.yaml b/tests/e2e/Taskfile.yaml index 0438ef024..0476498c0 100644 --- a/tests/e2e/Taskfile.yaml +++ b/tests/e2e/Taskfile.yaml @@ -59,7 +59,7 @@ tasks: export E2E_CLUSTERTRANSPORT_KUBECONFIG={{.E2E_CLUSTERTRANSPORT_KUBECONFIG}} ginkgo --focus "{{ .TEST }}" -v {{else}} - echo "Test not passed" + echo "Specify test to run" echo 'Example: TEST="Label and Annotation" task run_one' {{end}} vars: diff --git a/tests/e2e/vm_label_annotation_test.go b/tests/e2e/vm_label_annotation_test.go index c76e83d4a..879602824 100644 --- a/tests/e2e/vm_label_annotation_test.go +++ b/tests/e2e/vm_label_annotation_test.go @@ -2,18 +2,50 @@ package e2e import ( "fmt" - "github.com/deckhouse/virtualization/tests/e2e/executor" - kc "github.com/deckhouse/virtualization/tests/e2e/kubectl" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" "io/fs" "path/filepath" "strings" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/deckhouse/virtualization/tests/e2e/executor" + "github.com/deckhouse/virtualization/tests/e2e/helper" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + + kc "github.com/deckhouse/virtualization/tests/e2e/kubectl" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +type virtualMachine struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` +} + +func getVMFromManifest(manifest string) (*virtualMachine, error) { + unstructs, err := helper.ParseYaml(manifest) + if err != nil { + return nil, err + } + var unstruct *unstructured.Unstructured + for _, u := range unstructs { + if helper.GetFullApiResourceName(u) == kc.ResourceVM { + unstruct = u + break + } + } + var vm virtualMachine + + if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstruct.Object, &vm); err != nil { + return nil, err + } + return &vm, nil +} + var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { imageManifest := vmPath("image.yaml") - manifestVM := vmPath("vm_label_annotation.yaml") + vmManifest := vmPath("vm_label_annotation.yaml") const ( labelName = "os" @@ -48,11 +80,17 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { return cmdResult } + WaitVmStatus := func(name, phase string) { + GinkgoHelper() + WaitResource(kc.ResourceVM, name, "jsonpath={.status.phase}="+phase, LongWaitDuration) + } + BeforeAll(func() { By("Apply image for vms") ApplyFromFile(imageManifest) WaitFromFile(imageManifest, PhaseReady, LongWaitDuration) }) + AfterAll(func() { By("Delete all manifests") files := make([]string, 0) @@ -72,33 +110,25 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { } }) - WaitVmStatus := func(name, phase string) { - GinkgoHelper() - WaitResource(kc.ResourceVM, name, "jsonpath={.status.phase}="+phase, LongWaitDuration) - } - Context("Label", func() { - var name string - - vm, err := GetVMFromManifest(manifestVM) + var vm *virtualMachine + var err error BeforeAll(func() { By("Apply manifest") - vm, err := GetVMFromManifest(manifestVM) - Expect(err).To(BeNil()) - name = vm.Name - ApplyFromFile(manifestVM) - WaitVmStatus(name, VMStatusRunning) + vm, err = getVMFromManifest(vmManifest) + Expect(err).NotTo(HaveOccurred()) + ApplyFromFile(vmManifest) + WaitVmStatus(vm.Name, VMStatusRunning) }) AfterAll(func() { By("Delete manifest") - kubectl.Delete(manifestVM, kc.DeleteOptions{}) + kubectl.Delete(vmManifest, kc.DeleteOptions{}) }) Describe(fmt.Sprintf("Add label %s=%s", labelName, labelValue), func() { It("Labeled", func() { - Expect(err).To(BeNil()) subCMD := fmt.Sprintf("-n %s label vm %s %s=%s", conf.Namespace, vm.Name, labelName, labelValue) res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.Error()).NotTo(HaveOccurred()) @@ -106,6 +136,7 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) Describe("Check label on resource", func() { + It("VM", func() { res := getRecourseLabel(kc.ResourceVM, vm.Name) Expect(res.Error()).NotTo(HaveOccurred(), "failed to get VM %s.\n%s", vm.Name, res.StdErr()) @@ -122,7 +153,6 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { Expect(res.StdOut()).To(Equal(labelValue)) }) It("POD virtlauncher", func() { - //pod := getPodName(conf.Namespace, vm.Name) pod := getPodName(vm.Name) res := getRecourseLabel(kc.ResourcePod, pod) Expect(res.Error()).NotTo(HaveOccurred(), "failed to get pod %s.\n%s", vm.Name, res.StdErr()) @@ -133,15 +163,14 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { Describe(fmt.Sprintf("Remove label %s=%s", labelName, labelValue), func() { It("Label was removed", func() { - Expect(err).To(BeNil()) subCMD := fmt.Sprintf("-n %s label vm %s %s-", conf.Namespace, vm.Name, labelName) - res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.Error()).NotTo(HaveOccurred()) }) }) Describe("Label must be removed from resource", func() { + It("VM", func() { res := getRecourseLabel(kc.ResourceVM, vm.Name) Expect(res.Error()).NotTo(HaveOccurred(), "failed to get VM %s.\n%s", vm.Name, res.StdErr()) @@ -158,7 +187,6 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { Expect(res.StdOut()).To(BeEmpty()) }) It("POD virtlauncher", func() { - //pod := getPodName(conf.Namespace, vm.Name) pod := getPodName(vm.Name) res := getRecourseLabel(kc.ResourcePod, pod) Expect(res.Error()).NotTo(HaveOccurred(), "failed to get pod %s.\n%s", vm.Name, res.StdErr()) @@ -168,27 +196,24 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) Context("Annotation", func() { - var name string - - vm, err := GetVMFromManifest(manifestVM) + var vm *virtualMachine BeforeAll(func() { By("Apply manifest") - vm, err := GetVMFromManifest(manifestVM) - Expect(err).To(BeNil()) - name = vm.Name - ApplyFromFile(manifestVM) - WaitVmStatus(name, VMStatusRunning) + var err error + vm, err = getVMFromManifest(vmManifest) + Expect(err).NotTo(HaveOccurred()) + ApplyFromFile(vmManifest) + WaitVmStatus(vm.Name, VMStatusRunning) }) AfterAll(func() { By("Delete manifest") - kubectl.Delete(manifestVM, kc.DeleteOptions{}) + kubectl.Delete(vmManifest, kc.DeleteOptions{}) }) Describe(fmt.Sprintf("Add annotation %s=%s", annotationName, annotationValue), func() { It("Annotated", func() { - Expect(err).To(BeNil()) subCMD := fmt.Sprintf("-n %s annotate vm %s %s=%s", conf.Namespace, vm.Name, annotationName, annotationValue) res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.Error()).NotTo(HaveOccurred()) @@ -212,7 +237,6 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { Expect(res.StdOut()).To(Equal(annotationValue)) }) It("POD virtlauncher", func() { - //pod := getPodName(conf.Namespace, vm.Name) pod := getPodName(vm.Name) res := getRecourseAnnotation(kc.ResourcePod, pod) Expect(res.Error()).NotTo(HaveOccurred(), "failed to get pod %s.\n%s", vm.Name, res.StdErr()) @@ -223,7 +247,6 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { Describe("Remove annotation test-annotation=true", func() { It("Was removed", func() { - Expect(err).To(BeNil()) subCMD := fmt.Sprintf("-n %s annotate vm %s %s-", conf.Namespace, vm.Name, annotationName) res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.Error()).NotTo(HaveOccurred()) @@ -247,7 +270,6 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { Expect(res.StdOut()).To(BeEmpty()) }) It("POD virtlauncher", func() { - //pod := getPodName(conf.Namespace, vm.Name) pod := getPodName(vm.Name) res := getRecourseAnnotation(kc.ResourcePod, pod) Expect(res.Error()).NotTo(HaveOccurred(), "failed to get pod %s.\n%s", vm.Name, res.StdErr()) From 115b7a03835c4dd22a2a8ad59810c2ad6136ab8f Mon Sep 17 00:00:00 2001 From: Nikita Korolev Date: Tue, 19 Mar 2024 11:40:19 +0300 Subject: [PATCH 12/12] chore: resolve comments Signed-off-by: Nikita Korolev --- tests/e2e/util_test.go | 47 ++++++++++++++++++++++++- tests/e2e/vm_label_annotation_test.go | 41 ++++------------------ tests/e2e/vm_test.go | 49 +++------------------------ 3 files changed, 56 insertions(+), 81 deletions(-) diff --git a/tests/e2e/util_test.go b/tests/e2e/util_test.go index 4409d04af..600c074ec 100644 --- a/tests/e2e/util_test.go +++ b/tests/e2e/util_test.go @@ -7,7 +7,12 @@ import ( kc "github.com/deckhouse/virtualization/tests/e2e/kubectl" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "time" + + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" ) type ApplyWaitGetOptions struct { @@ -111,7 +116,8 @@ func PatchResource(resource kc.Resource, name string, patch *kc.JsonPatch) { Namespace: conf.Namespace, JsonPatch: patch, }) - Expect(res.Error()).NotTo(HaveOccurred(), "patch failed %s %s/%s.\n%s", resource, conf.Namespace, name, res.StdErr()) + Expect(res.Error()).NotTo(HaveOccurred(), "patch failed %s %s/%s.\n%s", resource, conf.Namespace, name, + res.StdErr()) } func CheckField(resource kc.Resource, name, output, compareValue string) { GinkgoHelper() @@ -122,3 +128,42 @@ func CheckField(resource kc.Resource, name, output, compareValue string) { Expect(res.Error()).NotTo(HaveOccurred(), "get failed %s %s/%s.\n%s", resource, conf.Namespace, name, res.StdErr()) Expect(res.StdOut()).To(Equal(compareValue)) } + +type VirtualMachine struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + Spec VirtualMachineSpec `json:"spec"` +} + +type VirtualMachineSpec struct { + RunPolicy RunPolicy `json:"runPolicy"` + VirtualMachineIPAddressClaimName string `json:"virtualMachineIPAddressClaimName,omitempty"` + NodeSelector map[string]string `json:"nodeSelector,omitempty"` + PriorityClassName string `json:"priorityClassName"` + Tolerations []corev1.Toleration `json:"tolerations,omitempty"` + TerminationGracePeriodSeconds *int64 `json:"terminationGracePeriodSeconds,omitempty"` + EnableParavirtualization bool `json:"enableParavirtualization,omitempty"` + + ApprovedChangeID string `json:"approvedChangeID,omitempty"` +} + +type RunPolicy string + +func GetVMFromManifest(manifest string) (*VirtualMachine, error) { + unstructs, err := helper.ParseYaml(manifest) + if err != nil { + return nil, err + } + var unstruct *unstructured.Unstructured + for _, u := range unstructs { + if helper.GetFullApiResourceName(u) == kc.ResourceVM { + unstruct = u + break + } + } + var vm VirtualMachine + if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstruct.Object, &vm); err != nil { + return nil, err + } + return &vm, nil +} diff --git a/tests/e2e/vm_label_annotation_test.go b/tests/e2e/vm_label_annotation_test.go index 879602824..7428f1aed 100644 --- a/tests/e2e/vm_label_annotation_test.go +++ b/tests/e2e/vm_label_annotation_test.go @@ -10,39 +10,9 @@ import ( . "github.com/onsi/gomega" "github.com/deckhouse/virtualization/tests/e2e/executor" - "github.com/deckhouse/virtualization/tests/e2e/helper" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" - kc "github.com/deckhouse/virtualization/tests/e2e/kubectl" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -type virtualMachine struct { - metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` -} - -func getVMFromManifest(manifest string) (*virtualMachine, error) { - unstructs, err := helper.ParseYaml(manifest) - if err != nil { - return nil, err - } - var unstruct *unstructured.Unstructured - for _, u := range unstructs { - if helper.GetFullApiResourceName(u) == kc.ResourceVM { - unstruct = u - break - } - } - var vm virtualMachine - - if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstruct.Object, &vm); err != nil { - return nil, err - } - return &vm, nil -} - var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { imageManifest := vmPath("image.yaml") vmManifest := vmPath("vm_label_annotation.yaml") @@ -111,12 +81,12 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) Context("Label", func() { - var vm *virtualMachine + var vm *VirtualMachine var err error BeforeAll(func() { By("Apply manifest") - vm, err = getVMFromManifest(vmManifest) + vm, err = GetVMFromManifest(vmManifest) Expect(err).NotTo(HaveOccurred()) ApplyFromFile(vmManifest) WaitVmStatus(vm.Name, VMStatusRunning) @@ -196,12 +166,12 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { }) Context("Annotation", func() { - var vm *virtualMachine + var vm *VirtualMachine BeforeAll(func() { By("Apply manifest") var err error - vm, err = getVMFromManifest(vmManifest) + vm, err = GetVMFromManifest(vmManifest) Expect(err).NotTo(HaveOccurred()) ApplyFromFile(vmManifest) WaitVmStatus(vm.Name, VMStatusRunning) @@ -214,7 +184,8 @@ var _ = Describe("Label and Annotation", Ordered, ContinueOnFailure, func() { Describe(fmt.Sprintf("Add annotation %s=%s", annotationName, annotationValue), func() { It("Annotated", func() { - subCMD := fmt.Sprintf("-n %s annotate vm %s %s=%s", conf.Namespace, vm.Name, annotationName, annotationValue) + subCMD := fmt.Sprintf("-n %s annotate vm %s %s=%s", conf.Namespace, vm.Name, annotationName, + annotationValue) res := kubectl.RawCommand(subCMD, ShortWaitDuration) Expect(res.Error()).NotTo(HaveOccurred()) }) diff --git a/tests/e2e/vm_test.go b/tests/e2e/vm_test.go index 88432ff99..5d1394fdf 100644 --- a/tests/e2e/vm_test.go +++ b/tests/e2e/vm_test.go @@ -3,16 +3,12 @@ package e2e import ( "encoding/json" "fmt" - "github.com/deckhouse/virtualization/tests/e2e/helper" kc "github.com/deckhouse/virtualization/tests/e2e/kubectl" virt "github.com/deckhouse/virtualization/tests/e2e/virtctl" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "io/fs" corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" "path" "path/filepath" "strconv" @@ -62,7 +58,8 @@ var _ = Describe("VM", Ordered, ContinueOnFailure, func() { WaitKubevirtVMStatus := func(name, printableStatus string) { GinkgoHelper() - WaitResource(kc.ResourceKubevirtVM, name, "jsonpath={.status.printableStatus}="+printableStatus, LongWaitDuration) + WaitResource(kc.ResourceKubevirtVM, name, "jsonpath={.status.printableStatus}="+printableStatus, + LongWaitDuration) } WaitVmStatus := func(name, phase string) { @@ -152,7 +149,8 @@ var _ = Describe("VM", Ordered, ContinueOnFailure, func() { When("Virtctl start", func() { It("Virtctl start", func() { res := virtctl.StartVm(name, conf.Namespace) - Expect(res.Error()).To(BeNil(), "virtctl start failed vm %s/%s.\n%s", conf.Namespace, name, res.StdErr()) + Expect(res.Error()).To(BeNil(), "virtctl start failed vm %s/%s.\n%s", conf.Namespace, name, + res.StdErr()) }) It("Get kubevirt vm", func() { time.Sleep(30 * time.Second) @@ -381,42 +379,3 @@ var _ = Describe("VM", Ordered, ContinueOnFailure, func() { }) }) - -type VirtualMachine struct { - metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` - Spec VirtualMachineSpec `json:"spec"` -} - -type VirtualMachineSpec struct { - RunPolicy RunPolicy `json:"runPolicy"` - VirtualMachineIPAddressClaimName string `json:"virtualMachineIPAddressClaimName,omitempty"` - NodeSelector map[string]string `json:"nodeSelector,omitempty"` - PriorityClassName string `json:"priorityClassName"` - Tolerations []corev1.Toleration `json:"tolerations,omitempty"` - TerminationGracePeriodSeconds *int64 `json:"terminationGracePeriodSeconds,omitempty"` - EnableParavirtualization bool `json:"enableParavirtualization,omitempty"` - - ApprovedChangeID string `json:"approvedChangeID,omitempty"` -} - -type RunPolicy string - -func GetVMFromManifest(manifest string) (*VirtualMachine, error) { - unstructs, err := helper.ParseYaml(manifest) - if err != nil { - return nil, err - } - var unstruct *unstructured.Unstructured - for _, u := range unstructs { - if helper.GetFullApiResourceName(u) == kc.ResourceVM { - unstruct = u - break - } - } - var vm VirtualMachine - if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstruct.Object, &vm); err != nil { - return nil, err - } - return &vm, nil -}