Skip to content

Commit

Permalink
tests/utils.go: Get rid of RunVMI
Browse files Browse the repository at this point in the history
This function was giving the false impression of allowing the caller to
wait for the VMI to be running by allowing to supply a timeout.

In reality this function only created a VMI and was using a nonsensical
Eventually loop around the Create call.

Get rid of RunVMI by replacing all its usages with direct calls to the
kubevirt client.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
  • Loading branch information
0xFelix committed Jan 22, 2024
1 parent 973e70a commit f1e004f
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ format:
fmt: format

lint:
if [ $$(wc -l < tests/utils.go) -gt 2100 ]; then echo >&2 "do not make tests/utils longer"; exit 1; fi
if [ $$(wc -l < tests/utils.go) -gt 2027 ]; then echo >&2 "do not make tests/utils longer"; exit 1; fi

hack/dockerized "golangci-lint run --timeout 20m --verbose \
pkg/instancetype/... \
Expand Down
9 changes: 5 additions & 4 deletions tests/launchsecurity/sev.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,14 @@ var _ = Describe("[sig-compute]AMD Secure Encrypted Virtualization (SEV)", decor
expectedSEVMeasurementInfo v1.SEVMeasurementInfo
)

vmi := newSEVFedora(false, libvmi.WithSEVAttestation())
vmi = tests.RunVMI(vmi, 30)
Eventually(ThisVMI(vmi), 60).Should(BeInPhase(v1.Scheduled))

virtClient, err := kubecli.GetKubevirtClient()
Expect(err).ToNot(HaveOccurred())

vmi := newSEVFedora(false, libvmi.WithSEVAttestation())
vmi, err = virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Create(context.Background(), vmi)
Expect(err).ToNot(HaveOccurred())
Eventually(ThisVMI(vmi), 60).Should(BeInPhase(v1.Scheduled))

By("Querying virsh nodesevinfo")
nodeSevInfo := tests.RunCommandOnVmiPod(vmi, []string{"virsh", "nodesevinfo"})
Expect(nodeSevInfo).ToNot(BeEmpty())
Expand Down
3 changes: 2 additions & 1 deletion tests/monitoring/vm_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ var _ = Describe("[Serial][sig-monitoring]VM Monitoring", Serial, decorators.Sig
It("should fire VMCannotBeEvicted alert", func() {
By("starting non-migratable VMI with eviction strategy set to LiveMigrate ")
vmi := libvmi.NewAlpine(libvmi.WithEvictionStrategy(v1.EvictionStrategyLiveMigrate))
vmi = tests.RunVMI(vmi, 240)
vmi, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Create(context.Background(), vmi)
Expect(err).ToNot(HaveOccurred())

By("waiting for VMCannotBeEvicted alert")
verifyAlertExist(virtClient, "VMCannotBeEvicted")
Expand Down
5 changes: 3 additions & 2 deletions tests/network/vmi_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ var _ = SIGDescribe("[crit:high][arm64][vendor:cnv-qe@redhat.com][level:componen
v1.SetDefaults_NetworkInterface(bridgeVMI)
Expect(bridgeVMI.Spec.Domain.Devices.Interfaces).NotTo(BeEmpty())

By("starting a VMI with bridged network on a node")
bridgeVMI = tests.RunVMI(bridgeVMI, 40)
By("creating a VMI with bridged network on a node")
bridgeVMI, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(bridgeVMI)).Create(context.Background(), bridgeVMI)
Expect(err).ToNot(HaveOccurred())

// Start a VirtualMachineInstance with bridged networking
By("Waiting the VirtualMachineInstance start")
Expand Down
8 changes: 6 additions & 2 deletions tests/network/vmi_networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,9 @@ var _ = SIGDescribe("[rfe_id:694][crit:medium][vendor:cnv-qe@redhat.com][level:c
PrivateOptions: []v1.DHCPPrivateOptions{{Option: 240, Value: "private.options.kubevirt.io"}},
}

dhcpVMI = libwait.WaitUntilVMIReady(tests.RunVMI(dhcpVMI, 40), console.LoginToFedora)
dhcpVMI, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(dhcpVMI)).Create(context.Background(), dhcpVMI)
Expect(err).ToNot(HaveOccurred())
dhcpVMI = libwait.WaitUntilVMIReady(dhcpVMI, console.LoginToFedora)

err = console.SafeExpectBatch(dhcpVMI, []expect.Batcher{
&expect.BSnd{S: "\n"},
Expand Down Expand Up @@ -615,7 +617,9 @@ var _ = SIGDescribe("[rfe_id:694][crit:medium][vendor:cnv-qe@redhat.com][level:c
Nameservers: []string{"8.8.8.8", "4.2.2.1", DNSNameserverWithLeadingZeros},
Searches: []string{"example.com"},
}
dnsVMI = libwait.WaitUntilVMIReady(tests.RunVMI(dnsVMI, 40), console.LoginToCirros)
dnsVMI, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(dnsVMI)).Create(context.Background(), dnsVMI)
Expect(err).ToNot(HaveOccurred())
dnsVMI = libwait.WaitUntilVMIReady(dnsVMI, console.LoginToCirros)
err = console.SafeExpectBatch(dnsVMI, []expect.Batcher{
&expect.BSnd{S: "\n"},
&expect.BExp{R: console.PromptExpression},
Expand Down
6 changes: 3 additions & 3 deletions tests/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2943,11 +2943,11 @@ spec:
tests.UpdateKubeVirtConfigValueAndWait(kv.Spec.Configuration)

By("Checking launcher seccomp policy")
vmi := libvmi.NewCirros()
vmi = tests.RunVMI(vmi, 60)
vmi, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(nil)).Create(context.Background(), libvmi.NewCirros())
Expect(err).ToNot(HaveOccurred())
fetchVMI := matcher.ThisVMI(vmi)
psaRelatedErrorDetected := false
err := wait.PollImmediate(time.Second, 30*time.Second, func() (done bool, err error) {
err = wait.PollImmediate(time.Second, 30*time.Second, func() (done bool, err error) {
vmi, err := fetchVMI()
if err != nil {
return done, err
Expand Down
4 changes: 3 additions & 1 deletion tests/storage/datavolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ var _ = SIGDescribe("DataVolume Integration", func() {
dataVolume, err = virtClient.CdiClient().CdiV1beta1().DataVolumes(testsuite.GetTestNamespace(nil)).Create(context.Background(), dataVolume, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())

vmi = tests.RunVMI(vmi, 60)
vmi, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Create(context.Background(), vmi)
Expect(err).ToNot(HaveOccurred())

vmis = append(vmis, vmi)
dvs = append(dvs, dataVolume)
}
Expand Down
8 changes: 4 additions & 4 deletions tests/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,8 @@ var _ = SIGDescribe("Storage", func() {
libvmi.WithNamespace(testsuite.NamespacePrivileged),
)
vmi.Spec.Volumes[0].HostDisk.Capacity = resource.MustParse(strconv.Itoa(int(float64(diskSize) * 1.2)))
tests.RunVMI(vmi, 30)
vmi, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Create(context.Background(), vmi)
Expect(err).ToNot(HaveOccurred())

By("Checking events")
objectEventWatcher := watcher.New(vmi).SinceWatchedObjectResourceVersion().Timeout(time.Duration(120) * time.Second)
Expand Down Expand Up @@ -972,9 +973,8 @@ var _ = SIGDescribe("Storage", func() {
libvmi.WithResourceMemory("128Mi"),
libvmi.WithPersistentVolumeClaim("disk0", pvcName),
)
vmi = tests.RunVMI(vmi, 10)

virtClient := kubevirt.Client()
vmi, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Create(context.Background(), vmi)
Expect(err).ToNot(HaveOccurred())

Eventually(func() bool {
vmi, err = virtClient.VirtualMachineInstance(vmi.Namespace).Get(context.Background(), vmi.Name, &metav1.GetOptions{})
Expand Down
26 changes: 9 additions & 17 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,21 +236,10 @@ func DeleteSecret(name, namespace string) {
util2.PanicOnError(err)
}
}
func RunVMI(vmi *v1.VirtualMachineInstance, timeout int) *v1.VirtualMachineInstance {
By("Starting a VirtualMachineInstance")
virtCli := kubevirt.Client()

var obj *v1.VirtualMachineInstance
var err error
Eventually(func() error {
obj, err = virtCli.VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Create(context.Background(), vmi)
return err
}, timeout, 1*time.Second).ShouldNot(HaveOccurred())
return obj
}

func RunVMIAndExpectLaunch(vmi *v1.VirtualMachineInstance, timeout int) *v1.VirtualMachineInstance {
vmi = RunVMI(vmi, timeout)
vmi, err := kubevirt.Client().VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Create(context.Background(), vmi)
Expect(err).ToNot(HaveOccurred())
By(waitingVMInstanceStart)
return libwait.WaitForVMIPhase(vmi,
[]v1.VirtualMachineInstancePhase{v1.Running},
Expand All @@ -259,7 +248,8 @@ func RunVMIAndExpectLaunch(vmi *v1.VirtualMachineInstance, timeout int) *v1.Virt
}

func RunVMIAndExpectLaunchWithDataVolume(vmi *v1.VirtualMachineInstance, dv *cdiv1.DataVolume, timeout int) *v1.VirtualMachineInstance {
vmi = RunVMI(vmi, timeout)
vmi, err := kubevirt.Client().VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Create(context.Background(), vmi)
Expect(err).ToNot(HaveOccurred())
By("Waiting until the DataVolume is ready")
libstorage.EventuallyDV(dv, timeout, HaveSucceeded())
By(waitingVMInstanceStart)
Expand All @@ -272,9 +262,10 @@ func RunVMIAndExpectLaunchWithDataVolume(vmi *v1.VirtualMachineInstance, dv *cdi
}

func RunVMIAndExpectLaunchIgnoreWarnings(vmi *v1.VirtualMachineInstance, timeout int) *v1.VirtualMachineInstance {
obj := RunVMI(vmi, timeout)
vmi, err := kubevirt.Client().VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Create(context.Background(), vmi)
Expect(err).ToNot(HaveOccurred())
By(waitingVMInstanceStart)
return libwait.WaitForSuccessfulVMIStart(obj,
return libwait.WaitForSuccessfulVMIStart(vmi,
libwait.WithFailOnWarnings(false),
libwait.WithTimeout(timeout),
)
Expand All @@ -286,7 +277,8 @@ func RunVMIAndExpectScheduling(vmi *v1.VirtualMachineInstance, timeout int) *v1.
}

func RunVMIAndExpectSchedulingWithWarningPolicy(vmi *v1.VirtualMachineInstance, timeout int, wp watcher.WarningsPolicy) *v1.VirtualMachineInstance {
vmi = RunVMI(vmi, timeout)
vmi, err := kubevirt.Client().VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Create(context.Background(), vmi)
Expect(err).ToNot(HaveOccurred())
By("Waiting until the VirtualMachineInstance will be scheduled")
return libwait.WaitForVMIPhase(vmi,
[]v1.VirtualMachineInstancePhase{v1.Scheduling, v1.Scheduled, v1.Running},
Expand Down
5 changes: 3 additions & 2 deletions tests/vmi_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,9 @@ var _ = Describe("[sig-compute]Configurations", decorators.SigCompute, func() {
guestMemory := resource.MustParse("4096M")
vmi.Spec.Domain.Memory = &v1.Memory{Guest: &guestMemory}
vmi.Spec.Domain.Resources = v1.ResourceRequirements{}
runningVMI := tests.RunVMI(vmi, 30)
Expect(runningVMI.Spec.Domain.Resources.Requests.Memory().String()).To(Equal("2048M"))
vmi, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Create(context.Background(), vmi)
Expect(err).ToNot(HaveOccurred())
Expect(vmi.Spec.Domain.Resources.Requests.Memory().String()).To(Equal("2048M"))
})
})

Expand Down

0 comments on commit f1e004f

Please sign in to comment.