Skip to content

Commit

Permalink
Merge pull request kubevirt#11178 from EdDev/tests-utils-conteiner-lo…
Browse files Browse the repository at this point in the history
…okup

tests, utils: Extract container lookup helpers to libpod
  • Loading branch information
kubevirt-bot authored Feb 9, 2024
2 parents a7f227b + e318b31 commit 766d99d
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 40 deletions.
3 changes: 2 additions & 1 deletion tests/container_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/console"
cd "kubevirt.io/kubevirt/tests/containerdisk"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libvmi"
"kubevirt.io/kubevirt/tests/libwait"
)
Expand Down Expand Up @@ -270,7 +271,7 @@ var _ = Describe("[rfe_id:588][crit:medium][vendor:[email protected]][level:comp

func getContainerDiskContainerOfPod(pod *k8sv1.Pod, volumeName string) *k8sv1.Container {
diskContainerName := fmt.Sprintf("volume%s", volumeName)
return tests.GetContainerOfPod(pod, diskContainerName)
return libpod.LookupContainer(pod, diskContainerName)
}

func hasContainerDisk(pods []k8sv1.Pod) bool {
Expand Down
1 change: 1 addition & 0 deletions tests/hotplug/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go_library(
"//tests/framework/kubevirt:go_default_library",
"//tests/framework/matcher:go_default_library",
"//tests/libmigration:go_default_library",
"//tests/libpod:go_default_library",
"//tests/libvmi:go_default_library",
"//tests/libwait:go_default_library",
"//tests/migration:go_default_library",
Expand Down
9 changes: 5 additions & 4 deletions tests/hotplug/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"kubevirt.io/kubevirt/tests/decorators"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
. "kubevirt.io/kubevirt/tests/framework/matcher"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libwait"
)

Expand Down Expand Up @@ -105,7 +106,7 @@ var _ = Describe("[sig-compute][Serial]CPU Hotplug", decorators.SigCompute, deco
libwait.WaitForSuccessfulVMIStart(vmi)

By("Ensuring the compute container has 200m CPU")
compute := tests.GetComputeContainerOfPod(tests.GetVmiPod(virtClient, vmi))
compute := libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))

Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqCpu := compute.Resources.Requests.Cpu().Value()
Expand Down Expand Up @@ -154,7 +155,7 @@ var _ = Describe("[sig-compute][Serial]CPU Hotplug", decorators.SigCompute, deco
}))

By("Ensuring the virt-launcher pod now has 400m CPU")
compute = tests.GetComputeContainerOfPod(tests.GetVmiPod(virtClient, vmi))
compute = libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))
Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqCpu = compute.Resources.Requests.Cpu().Value()
expCpu = resource.MustParse("400m")
Expand Down Expand Up @@ -204,7 +205,7 @@ var _ = Describe("[sig-compute][Serial]CPU Hotplug", decorators.SigCompute, deco
libwait.WaitForSuccessfulVMIStart(vmi)

By("Ensuring the compute container has 2 CPU")
compute := tests.GetComputeContainerOfPod(tests.GetVmiPod(virtClient, vmi))
compute := libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))

Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqCpu := compute.Resources.Requests.Cpu().Value()
Expand Down Expand Up @@ -251,7 +252,7 @@ var _ = Describe("[sig-compute][Serial]CPU Hotplug", decorators.SigCompute, deco
}))

By("Ensuring the virt-launcher pod now has 4 CPU")
compute = tests.GetComputeContainerOfPod(tests.GetVmiPod(virtClient, vmi))
compute = libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))

Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqCpu = compute.Resources.Requests.Cpu().Value()
Expand Down
5 changes: 3 additions & 2 deletions tests/hotplug/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"kubevirt.io/kubevirt/tests/decorators"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
. "kubevirt.io/kubevirt/tests/framework/matcher"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libwait"
)

Expand Down Expand Up @@ -102,7 +103,7 @@ var _ = Describe("[sig-compute][Serial]Memory Hotplug", decorators.SigCompute, d
migration.CreateMigrationPolicy(virtClient, migration.PreparePolicyAndVMIWithBandwidthLimitation(vmi, migrationBandwidthLimit))

By("Ensuring the compute container has at least 128Mi of memory")
compute := tests.GetComputeContainerOfPod(tests.GetVmiPod(virtClient, vmi))
compute := libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))

Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqMemory := compute.Resources.Requests.Memory().Value()
Expand Down Expand Up @@ -147,7 +148,7 @@ var _ = Describe("[sig-compute][Serial]Memory Hotplug", decorators.SigCompute, d
}, 240*time.Second, time.Second).Should(BeNumerically(">", guest.Value()))

By("Ensuring the virt-launcher pod now has at least more than 256Mi of memory")
compute = tests.GetComputeContainerOfPod(tests.GetVmiPod(virtClient, vmi))
compute = libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))
Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqMemory = compute.Resources.Requests.Memory().Value()
Expect(reqMemory).To(BeNumerically(">=", maxGuest.Value()))
Expand Down
1 change: 1 addition & 0 deletions tests/libpod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "go_default_library",
srcs = [
"blackhole.go",
"container.go",
"query.go",
"render.go",
"sort.go",
Expand Down
43 changes: 43 additions & 0 deletions tests/libpod/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright The KubeVirt Authors.
*
*/

package libpod

import (
"fmt"

v1 "k8s.io/api/core/v1"
)

const (
computeContainerName = "compute"
)

func LookupComputeContainer(pod *v1.Pod) *v1.Container {
return LookupContainer(pod, computeContainerName)
}

func LookupContainer(pod *v1.Pod, containerName string) *v1.Container {
for i := range pod.Spec.Containers {
if pod.Spec.Containers[i].Name == containerName {
return &pod.Spec.Containers[i]
}
}
panic(fmt.Errorf("could not find the %s container", containerName))
}
2 changes: 1 addition & 1 deletion tests/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3524,7 +3524,7 @@ func libvirtDomainIsPersistent(virtClient kubecli.KubevirtClient, vmi *v1.Virtua
stdout, stderr, err := exec.ExecuteCommandOnPodWithResults(
virtClient,
vmiPod,
tests.GetComputeContainerOfPod(vmiPod).Name,
libpod.LookupComputeContainer(vmiPod).Name,
[]string{"virsh", "--quiet", "list", "--persistent", "--name"},
)
if err != nil {
Expand Down
22 changes: 2 additions & 20 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,24 +393,6 @@ func GetPodCPUSet(pod *k8sv1.Pod) (output string, err error) {
return
}

func GetComputeContainerOfPod(pod *k8sv1.Pod) *k8sv1.Container {
return GetContainerOfPod(pod, "compute")
}

func GetContainerOfPod(pod *k8sv1.Pod, containerName string) *k8sv1.Container {
var computeContainer *k8sv1.Container
for _, container := range pod.Spec.Containers {
if container.Name == containerName {
computeContainer = &container
break
}
}
if computeContainer == nil {
util2.PanicOnError(fmt.Errorf("could not find the %s container", containerName))
}
return computeContainer
}

// NewRandomVirtualMachineInstanceWithDisk
//
// Deprecated: Use libvmi directly
Expand Down Expand Up @@ -908,7 +890,7 @@ func GetRunningVirtualMachineInstanceDomainXML(virtClient kubecli.KubevirtClient
stdout, stderr, err := exec.ExecuteCommandOnPodWithResults(
virtClient,
vmiPod,
GetComputeContainerOfPod(vmiPod).Name,
libpod.LookupComputeContainer(vmiPod).Name,
command,
)
if err != nil {
Expand All @@ -926,7 +908,7 @@ func LibvirtDomainIsPaused(virtClient kubecli.KubevirtClient, vmi *v1.VirtualMac
stdout, stderr, err := exec.ExecuteCommandOnPodWithResults(
virtClient,
vmiPod,
GetComputeContainerOfPod(vmiPod).Name,
libpod.LookupComputeContainer(vmiPod).Name,
[]string{"virsh", "--quiet", "domstate", vmi.Namespace + "_" + vmi.Name},
)
if err != nil {
Expand Down
17 changes: 9 additions & 8 deletions tests/vmi_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import (
cd "kubevirt.io/kubevirt/tests/containerdisk"
"kubevirt.io/kubevirt/tests/libdv"
"kubevirt.io/kubevirt/tests/libnode"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libstorage"
"kubevirt.io/kubevirt/tests/libvmi"
"kubevirt.io/kubevirt/tests/libwait"
Expand Down Expand Up @@ -1679,7 +1680,7 @@ var _ = Describe("[sig-compute]Configurations", decorators.SigCompute, func() {

getComputeMemoryRequest := func(vmi *virtv1.VirtualMachineInstance) resource.Quantity {
launcherPod := tests.GetPodByVirtualMachineInstance(vmi)
computeContainer := tests.GetComputeContainerOfPod(launcherPod)
computeContainer := libpod.LookupComputeContainer(launcherPod)
return computeContainer.Resources.Requests[kubev1.ResourceMemory]
}

Expand Down Expand Up @@ -1957,7 +1958,7 @@ var _ = Describe("[sig-compute]Configurations", decorators.SigCompute, func() {

readyPod, err := libvmi.GetPodByVirtualMachineInstance(runningVMI, testsuite.GetTestNamespace(vmi))
Expect(err).ToNot(HaveOccurred())
computeContainer := tests.GetComputeContainerOfPod(readyPod)
computeContainer := libpod.LookupComputeContainer(readyPod)
cpuRequest := computeContainer.Resources.Requests[kubev1.ResourceCPU]
Expect(cpuRequest.String()).To(Equal("500m"))
})
Expand All @@ -1968,7 +1969,7 @@ var _ = Describe("[sig-compute]Configurations", decorators.SigCompute, func() {

readyPod, err := libvmi.GetPodByVirtualMachineInstance(runningVMI, testsuite.GetTestNamespace(vmi))
Expect(err).ToNot(HaveOccurred())
computeContainer := tests.GetComputeContainerOfPod(readyPod)
computeContainer := libpod.LookupComputeContainer(readyPod)
cpuRequest := computeContainer.Resources.Requests[kubev1.ResourceCPU]
Expect(cpuRequest.String()).To(Equal("100m"))
})
Expand All @@ -1986,7 +1987,7 @@ var _ = Describe("[sig-compute]Configurations", decorators.SigCompute, func() {

readyPod, err := libvmi.GetPodByVirtualMachineInstance(runningVMI, testsuite.GetTestNamespace(vmi))
Expect(err).ToNot(HaveOccurred())
computeContainer := tests.GetComputeContainerOfPod(readyPod)
computeContainer := libpod.LookupComputeContainer(readyPod)
cpuRequest := computeContainer.Resources.Requests[kubev1.ResourceCPU]
Expect(cpuRequest.String()).To(Equal("800m"))
})
Expand All @@ -2010,7 +2011,7 @@ var _ = Describe("[sig-compute]Configurations", decorators.SigCompute, func() {
By("Ensuring no CPU limit is set")
readyPod, err := libvmi.GetPodByVirtualMachineInstance(runningVMI, testsuite.GetTestNamespace(vmi))
Expect(err).ToNot(HaveOccurred())
computeContainer := tests.GetComputeContainerOfPod(readyPod)
computeContainer := libpod.LookupComputeContainer(readyPod)
_, exists := computeContainer.Resources.Limits[kubev1.ResourceCPU]
Expect(exists).To(BeFalse(), "CPU limit set on the compute container when none was expected")
})
Expand All @@ -2031,7 +2032,7 @@ var _ = Describe("[sig-compute]Configurations", decorators.SigCompute, func() {
By("Ensuring the CPU limit is set to the correct value")
readyPod, err := libvmi.GetPodByVirtualMachineInstance(runningVMI, testsuite.GetTestNamespace(vmi))
Expect(err).ToNot(HaveOccurred())
computeContainer := tests.GetComputeContainerOfPod(readyPod)
computeContainer := libpod.LookupComputeContainer(readyPod)
limits, exists := computeContainer.Resources.Limits[kubev1.ResourceCPU]
Expect(exists).To(BeTrue(), "expected CPU limit not set on the compute container")
Expect(limits.String()).To(Equal("1"))
Expand All @@ -2049,7 +2050,7 @@ var _ = Describe("[sig-compute]Configurations", decorators.SigCompute, func() {
By("Ensuring no memory and cpu limits are set")
readyPod, err := libvmi.GetPodByVirtualMachineInstance(runningVMI, testsuite.GetTestNamespace(vmi))
Expect(err).ToNot(HaveOccurred())
computeContainer := tests.GetComputeContainerOfPod(readyPod)
computeContainer := libpod.LookupComputeContainer(readyPod)
_, exists := computeContainer.Resources.Limits[kubev1.ResourceMemory]
Expect(exists).To(BeFalse(), "Memory limits set on the compute container when none was expected")
_, exists = computeContainer.Resources.Limits[kubev1.ResourceCPU]
Expand Down Expand Up @@ -2106,7 +2107,7 @@ var _ = Describe("[sig-compute]Configurations", decorators.SigCompute, func() {
By("Ensuring the memory and cpu limits are set to the correct values")
readyPod, err := libvmi.GetPodByVirtualMachineInstance(runningVMI, testsuite.GetTestNamespace(vmi))
Expect(err).ToNot(HaveOccurred())
computeContainer := tests.GetComputeContainerOfPod(readyPod)
computeContainer := libpod.LookupComputeContainer(readyPod)
memLimits, exists := computeContainer.Resources.Limits[kubev1.ResourceMemory]
Expect(exists).To(BeTrue(), "expected memory limits set on the compute container")
Expect(memLimits.Value()).To(BeEquivalentTo(expectedLauncherMemLimits.Value()))
Expand Down
9 changes: 5 additions & 4 deletions tests/vmi_headless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/console"
cd "kubevirt.io/kubevirt/tests/containerdisk"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libvmi"
)

Expand Down Expand Up @@ -93,7 +94,7 @@ var _ = Describe("[rfe_id:609][sig-compute]VMIheadless", decorators.SigCompute,

readyPod, err := libvmi.GetPodByVirtualMachineInstance(vmi, testsuite.GetTestNamespace(vmi))
Expect(err).ToNot(HaveOccurred())
computeContainer := tests.GetComputeContainerOfPod(readyPod)
computeContainer := libpod.LookupComputeContainer(readyPod)

Expect(computeContainer.Resources.Requests.Memory().String()).To(Equal("100M"))
})
Expand All @@ -108,7 +109,7 @@ var _ = Describe("[rfe_id:609][sig-compute]VMIheadless", decorators.SigCompute,

readyPod, err := libvmi.GetPodByVirtualMachineInstance(vmi, testsuite.GetTestNamespace(vmi))
Expect(err).ToNot(HaveOccurred())
computeContainer := tests.GetComputeContainerOfPod(readyPod)
computeContainer := libpod.LookupComputeContainer(readyPod)

Expect(computeContainer.Resources.Requests.Memory().String()).ToNot(Equal("100M"))
})
Expand All @@ -121,11 +122,11 @@ var _ = Describe("[rfe_id:609][sig-compute]VMIheadless", decorators.SigCompute,

readyPod, err := libvmi.GetPodByVirtualMachineInstance(vmi, testsuite.GetTestNamespace(vmi))
Expect(err).ToNot(HaveOccurred())
computeContainer := tests.GetComputeContainerOfPod(readyPod)
computeContainer := libpod.LookupComputeContainer(readyPod)

normalReadyPod, err := libvmi.GetPodByVirtualMachineInstance(normalVmi, testsuite.GetTestNamespace(vmi))
Expect(err).ToNot(HaveOccurred())
normalComputeContainer := tests.GetComputeContainerOfPod(normalReadyPod)
normalComputeContainer := libpod.LookupComputeContainer(normalReadyPod)

memDiff := normalComputeContainer.Resources.Requests.Memory()
memDiff.Sub(*computeContainer.Resources.Requests.Memory())
Expand Down

0 comments on commit 766d99d

Please sign in to comment.