|
| 1 | +""" |
| 2 | +PCI Topology Stability Tests |
| 3 | +
|
| 4 | +STP: https://github.com/RedHatQE/openshift-virtualization-tests-design-docs/blob/main/stps/sig-virt/pci-topology-stability.md |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import logging |
| 10 | +from typing import TYPE_CHECKING |
| 11 | + |
| 12 | +import pytest |
| 13 | +from ocp_resources.template import Template |
| 14 | +from ocp_resources.virtual_machine_restore import VirtualMachineRestore |
| 15 | +from ocp_resources.virtual_machine_snapshot import VirtualMachineSnapshot |
| 16 | + |
| 17 | +from tests.os_params import RHEL_LATEST, RHEL_LATEST_LABELS |
| 18 | +from tests.virt.utils import get_pci_fingerprint |
| 19 | +from utilities.virt import ( |
| 20 | + VirtualMachineForTestsFromTemplate, |
| 21 | + migrate_vm_and_verify, |
| 22 | + restart_vm_wait_for_running_vm, |
| 23 | + running_vm, |
| 24 | +) |
| 25 | + |
| 26 | +if TYPE_CHECKING: |
| 27 | + from utilities.virt import VirtualMachineForTests |
| 28 | + |
| 29 | +LOGGER = logging.getLogger(__name__) |
| 30 | + |
| 31 | +pytestmark = [pytest.mark.rwx_default_storage, pytest.mark.data_collector_scope(scope="module")] |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(scope="class") |
| 35 | +def pci_topology_vm( |
| 36 | + request, |
| 37 | + namespace, |
| 38 | + unprivileged_client, |
| 39 | + golden_image_data_volume_template_for_test_scope_class, |
| 40 | + modern_cpu_for_migration, |
| 41 | +): |
| 42 | + with VirtualMachineForTestsFromTemplate( |
| 43 | + name=request.param["vm_name"], |
| 44 | + labels=Template.generate_template_labels(**request.param["template_labels"]), |
| 45 | + namespace=namespace.name, |
| 46 | + client=unprivileged_client, |
| 47 | + data_volume_template=golden_image_data_volume_template_for_test_scope_class, |
| 48 | + cpu_model=modern_cpu_for_migration, |
| 49 | + ) as vm: |
| 50 | + running_vm(vm=vm) |
| 51 | + yield vm |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture() |
| 55 | +def initial_pci_fingerprint(pci_topology_vm): |
| 56 | + return get_pci_fingerprint(vm=pci_topology_vm) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture() |
| 60 | +def restarted_pci_topology_vm(pci_topology_vm, initial_pci_fingerprint): |
| 61 | + restart_vm_wait_for_running_vm(vm=pci_topology_vm) |
| 62 | + return pci_topology_vm |
| 63 | + |
| 64 | + |
| 65 | +@pytest.fixture() |
| 66 | +def migrated_pci_topology_vm(admin_client, pci_topology_vm, initial_pci_fingerprint): |
| 67 | + migrate_vm_and_verify(vm=pci_topology_vm, client=admin_client, check_ssh_connectivity=True) |
| 68 | + return pci_topology_vm |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture() |
| 72 | +def snapshot_restored_pci_topology_vm(admin_client, pci_topology_vm, initial_pci_fingerprint): |
| 73 | + with VirtualMachineSnapshot( |
| 74 | + name=f"{pci_topology_vm.name}-snapshot", |
| 75 | + namespace=pci_topology_vm.namespace, |
| 76 | + vm_name=pci_topology_vm.name, |
| 77 | + ) as snapshot: |
| 78 | + snapshot.wait_snapshot_done() |
| 79 | + pci_topology_vm.stop(wait=True) |
| 80 | + LOGGER.info(f"Restoring VM {pci_topology_vm.name} from snapshot {snapshot.name}") |
| 81 | + with VirtualMachineRestore( |
| 82 | + client=admin_client, |
| 83 | + name=f"{pci_topology_vm.name}-restore", |
| 84 | + namespace=pci_topology_vm.namespace, |
| 85 | + vm_name=pci_topology_vm.name, |
| 86 | + snapshot_name=snapshot.name, |
| 87 | + ) as restore: |
| 88 | + restore.wait_restore_done() |
| 89 | + running_vm(vm=pci_topology_vm) |
| 90 | + return pci_topology_vm |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.parametrize( |
| 94 | + "golden_image_data_source_for_test_scope_class, pci_topology_vm", |
| 95 | + [ |
| 96 | + pytest.param( |
| 97 | + {"os_dict": RHEL_LATEST}, |
| 98 | + {"template_labels": RHEL_LATEST_LABELS, "vm_name": "pci-topology-vm"}, |
| 99 | + ), |
| 100 | + ], |
| 101 | + indirect=True, |
| 102 | +) |
| 103 | +class TestPCITopologyStability: |
| 104 | + """ |
| 105 | + Verify PCI device addresses remain stable across VM lifecycle operations. |
| 106 | +
|
| 107 | + Preconditions: |
| 108 | + - RHEL VM created from latest template with a modern CPU model for migration, |
| 109 | + started and running with SSH access |
| 110 | + - PCI fingerprint (MD5 of sorted guest PCI BDF addresses) captured before each operation |
| 111 | + """ |
| 112 | + |
| 113 | + @pytest.mark.polarion("CNV-16326") |
| 114 | + def test_pci_address_stability_on_restart( |
| 115 | + self, |
| 116 | + initial_pci_fingerprint: str, |
| 117 | + restarted_pci_topology_vm: VirtualMachineForTests, |
| 118 | + ): |
| 119 | + """ |
| 120 | + Steps: |
| 121 | + 1. Restart the VM and wait until it is running with SSH access |
| 122 | + 2. Capture PCI fingerprint from the guest |
| 123 | +
|
| 124 | + Expected: |
| 125 | + - Fingerprints match (PCI topology unchanged) |
| 126 | + """ |
| 127 | + fingerprint_after = get_pci_fingerprint(vm=restarted_pci_topology_vm) |
| 128 | + assert fingerprint_after == initial_pci_fingerprint, ( |
| 129 | + f"PCI topology changed after restart: before={initial_pci_fingerprint}, after={fingerprint_after}" |
| 130 | + ) |
| 131 | + |
| 132 | + @pytest.mark.polarion("CNV-16327") |
| 133 | + def test_pci_address_stability_on_migration( |
| 134 | + self, |
| 135 | + initial_pci_fingerprint: str, |
| 136 | + migrated_pci_topology_vm: VirtualMachineForTests, |
| 137 | + ): |
| 138 | + """ |
| 139 | + Steps: |
| 140 | + 1. Live-migrate the VM and verify SSH connectivity |
| 141 | + 2. Capture PCI fingerprint from the guest |
| 142 | +
|
| 143 | + Expected: |
| 144 | + - Fingerprints match (PCI topology unchanged) |
| 145 | + """ |
| 146 | + fingerprint_after = get_pci_fingerprint(vm=migrated_pci_topology_vm) |
| 147 | + assert fingerprint_after == initial_pci_fingerprint, ( |
| 148 | + f"PCI topology changed after migration: before={initial_pci_fingerprint}, after={fingerprint_after}" |
| 149 | + ) |
| 150 | + |
| 151 | + @pytest.mark.polarion("CNV-16328") |
| 152 | + @pytest.mark.usefixtures("skip_if_no_storage_class_for_snapshot") |
| 153 | + def test_pci_address_stability_on_snapshot_restore( |
| 154 | + self, |
| 155 | + initial_pci_fingerprint: str, |
| 156 | + snapshot_restored_pci_topology_vm: VirtualMachineForTests, |
| 157 | + ): |
| 158 | + """ |
| 159 | + Preconditions: |
| 160 | + - Storage class supports snapshots |
| 161 | +
|
| 162 | + Steps: |
| 163 | + 1. Create a VM snapshot and wait until it completes |
| 164 | + 2. Stop the VM |
| 165 | + 3. Restore the VM from the snapshot |
| 166 | + 4. Start the VM and wait until it is running with SSH access |
| 167 | + 5. Capture PCI fingerprint from the guest |
| 168 | +
|
| 169 | + Expected: |
| 170 | + - Fingerprints match (PCI topology unchanged) |
| 171 | + """ |
| 172 | + fingerprint_after = get_pci_fingerprint(vm=snapshot_restored_pci_topology_vm) |
| 173 | + assert fingerprint_after == initial_pci_fingerprint, ( |
| 174 | + f"PCI topology changed after snapshot restore: before={initial_pci_fingerprint}, after={fingerprint_after}" |
| 175 | + ) |
0 commit comments