From 7843da3bafbb65beae797b7bdf419bc10d945d45 Mon Sep 17 00:00:00 2001 From: Ram Lavi Date: Sun, 24 Nov 2024 10:36:41 +0200 Subject: [PATCH] dbg: Check potential bug Signed-off-by: Ram Lavi --- test/e2e/persistentips_test.go | 16 +++++++++++++ test/env/getter.go | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/test/e2e/persistentips_test.go b/test/e2e/persistentips_test.go index 303d8bee..0cca9a93 100644 --- a/test/e2e/persistentips_test.go +++ b/test/e2e/persistentips_test.go @@ -120,6 +120,10 @@ var _ = DescribeTableSubtree("Persistent IPs", func(params testParams) { vmiIPsBeforeMigration := testenv.GetIPsFromVMIStatus(vmi, params.networkInterfaceName) Expect(vmiIPsBeforeMigration).NotTo(BeEmpty()) + virtLauncherPod, err := testenv.VirtualMachineInstancePod(vmi) + Expect(err).NotTo(HaveOccurred()) + fmt.Printf("\nRAM B4 Migration vmi %s virtLauncherPod %s Annotations %+v\n", vmi.Name, virtLauncherPod.Name, virtLauncherPod.Annotations) + testenv.LiveMigrateVirtualMachine(td.Namespace, vm.Name) testenv.CheckLiveMigrationSucceeded(td.Namespace, vm.Name) @@ -129,6 +133,10 @@ var _ = DescribeTableSubtree("Persistent IPs", func(params testParams) { WithTimeout(5 * time.Minute). Should(testenv.ContainConditionVMIReady()) + virtLauncherPod, err = testenv.VirtualMachineInstancePod(vmi) + Expect(err).NotTo(HaveOccurred()) + fmt.Printf("\nRAM After Migration vmi %s virtLauncherPod %s Annotations %+v\n", vmi.Name, virtLauncherPod.Name, virtLauncherPod.Annotations) + Expect(testenv.ThisVMI(vmi)()).Should(testenv.MatchIPsAtInterfaceByName(params.networkInterfaceName, ConsistOf(vmiIPsBeforeMigration))) }) @@ -285,9 +293,17 @@ var _ = DescribeTableSubtree("Persistent IPs", func(params testParams) { vmiIPsBeforeMigration := testenv.GetIPsFromVMIStatus(vmi, params.networkInterfaceName) Expect(vmiIPsBeforeMigration).NotTo(BeEmpty()) + virtLauncherPod, err := testenv.VirtualMachineInstancePod(vmi) + Expect(err).NotTo(HaveOccurred()) + fmt.Printf("\nRAM B4 Migration vmi %s virtLauncherPod %s Annotations %+v\n", vmi.Name, virtLauncherPod.Name, virtLauncherPod.Annotations) + testenv.LiveMigrateVirtualMachine(td.Namespace, vmi.Name) testenv.CheckLiveMigrationSucceeded(td.Namespace, vmi.Name) + virtLauncherPod, err = testenv.VirtualMachineInstancePod(vmi) + Expect(err).NotTo(HaveOccurred()) + fmt.Printf("\nRAM After Migration vmi %s virtLauncherPod %s Annotations %+v\n", vmi.Name, virtLauncherPod.Name, virtLauncherPod.Annotations) + Expect(testenv.ThisVMI(vmi)()).Should(testenv.MatchIPsAtInterfaceByName(params.networkInterfaceName, ConsistOf(vmiIPsBeforeMigration))) }) diff --git a/test/env/getter.go b/test/env/getter.go index c95bde35..f52afa1e 100644 --- a/test/env/getter.go +++ b/test/env/getter.go @@ -2,7 +2,9 @@ package env import ( "context" + "fmt" + corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" @@ -46,3 +48,43 @@ func GetIPsFromVMIStatus(vmi *kubevirtv1.VirtualMachineInstance, networkInterfac } return ifaceStatus.IPs } + +func VirtualMachineInstancePod(vmi *kubevirtv1.VirtualMachineInstance) (*corev1.Pod, error) { + pod, err := lookupPodBySelector(vmi.Namespace, vmiLabelSelector(vmi), vmiFieldSelector(vmi)) + if err != nil { + return nil, fmt.Errorf("failed to find pod for VMI %s (%s)", vmi.Name, string(vmi.GetUID())) + } + return pod, nil +} + +func lookupPodBySelector(namespace string, labelSelector, fieldSelector map[string]string) (*corev1.Pod, error) { + pods := &corev1.PodList{} + err := Client.List( + context.Background(), + pods, + client.InNamespace(namespace), + client.MatchingLabels(labelSelector), + client.MatchingFields(fieldSelector)) + if err != nil { + return nil, err + } + + if len(pods.Items) == 0 { + return nil, fmt.Errorf("failed to lookup pod with labels %v, fields %v in namespace %s", labelSelector, fieldSelector, namespace) + } + + return &pods.Items[0], nil +} + +func vmiLabelSelector(vmi *kubevirtv1.VirtualMachineInstance) map[string]string { + return map[string]string{kubevirtv1.CreatedByLabel: string(vmi.GetUID())} +} + +func vmiFieldSelector(vmi *kubevirtv1.VirtualMachineInstance) map[string]string { + fieldSelectors := map[string]string{} + if vmi.Status.Phase == kubevirtv1.Running { + const podPhase = "status.phase" + fieldSelectors[podPhase] = string(corev1.PodRunning) + } + return fieldSelectors +}