Skip to content

Commit

Permalink
Merge pull request #11710 from iholder101/clean_code/get_vmi_pod
Browse files Browse the repository at this point in the history
Remove utils' GetVmiPod() function and replace it with libpod functions
  • Loading branch information
kubevirt-bot committed May 5, 2024
2 parents a723532 + 61d317e commit 3763f9f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
13 changes: 9 additions & 4 deletions tests/hotplug/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ var _ = Describe("[sig-compute][Serial]CPU Hotplug", decorators.SigCompute, deco
vmi = libwait.WaitForSuccessfulVMIStart(vmi)

By("Ensuring the compute container has 200m CPU")
compute := libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))
compute, err := libpod.LookupComputeContainerFromVmi(vmi)
Expect(err).ToNot(HaveOccurred())

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

By("Ensuring the virt-launcher pod now has 400m CPU")
compute = libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))
compute, err = libpod.LookupComputeContainerFromVmi(vmi)
Expect(err).ToNot(HaveOccurred())

Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqCpu = compute.Resources.Requests.Cpu().Value()
expCpu = resource.MustParse("400m")
Expand Down Expand Up @@ -205,7 +208,8 @@ var _ = Describe("[sig-compute][Serial]CPU Hotplug", decorators.SigCompute, deco
vmi = libwait.WaitForSuccessfulVMIStart(vmi)

By("Ensuring the compute container has 2 CPU")
compute := libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))
compute, err := libpod.LookupComputeContainerFromVmi(vmi)
Expect(err).ToNot(HaveOccurred())

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

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

Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqCpu = compute.Resources.Requests.Cpu().Value()
Expand Down
6 changes: 4 additions & 2 deletions tests/hotplug/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ 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 := libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))
compute, err := libpod.LookupComputeContainerFromVmi(vmi)
Expect(err).ToNot(HaveOccurred())

Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqMemory := compute.Resources.Requests.Memory().Value()
Expand Down Expand Up @@ -152,7 +153,8 @@ 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 = libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))
compute, err = libpod.LookupComputeContainerFromVmi(vmi)
Expect(err).ToNot(HaveOccurred())
Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqMemory = compute.Resources.Requests.Memory().Value()
Expect(reqMemory).To(BeNumerically(">=", maxGuest.Value()))
Expand Down
15 changes: 15 additions & 0 deletions tests/libpod/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package libpod
import (
"fmt"

virtv1 "kubevirt.io/api/core/v1"

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

Expand All @@ -41,3 +43,16 @@ func LookupContainer(pod *v1.Pod, containerName string) *v1.Container {
}
panic(fmt.Errorf("could not find the %s container", containerName))
}

func LookupComputeContainerFromVmi(vmi *virtv1.VirtualMachineInstance) (*v1.Container, error) {
if vmi.Namespace == "" {
return nil, fmt.Errorf("vmi namespace is empty")
}

pod, err := GetPodByVirtualMachineInstance(vmi, vmi.Namespace)
if err != nil {
return nil, err
}

return LookupComputeContainer(pod), nil
}
14 changes: 4 additions & 10 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,22 +569,16 @@ func RemoveHostDiskImage(diskPath string, nodeName string) {
Expect(err).ToNot(HaveOccurred())
}

func GetVmiPod(virtClient kubecli.KubevirtClient, vmi *v1.VirtualMachineInstance) *k8sv1.Pod {
// RunCommandOnVmiPod runs specified command on the virt-launcher pod
func RunCommandOnVmiPod(vmi *v1.VirtualMachineInstance, command []string) string {
virtClient := kubevirt.Client()
pods, err := virtClient.CoreV1().Pods(testsuite.GetTestNamespace(vmi)).List(context.Background(), UnfinishedVMIPodSelector(vmi))
ExpectWithOffset(1, err).ToNot(HaveOccurred())
ExpectWithOffset(1, pods.Items).NotTo(BeEmpty())
vmiPod := pods.Items[0]

return &vmiPod
}

// RunCommandOnVmiPod runs specified command on the virt-launcher pod
func RunCommandOnVmiPod(vmi *v1.VirtualMachineInstance, command []string) string {
virtClient := kubevirt.Client()
vmiPod := GetVmiPod(virtClient, vmi)

output, err := exec.ExecuteCommandOnPod(
vmiPod,
&vmiPod,
"compute",
command,
)
Expand Down

0 comments on commit 3763f9f

Please sign in to comment.