|
| 1 | +from enum import Enum |
| 2 | +import unittest |
| 3 | + |
| 4 | +# Following import statement allows for proper python IDE support and proper |
| 5 | +# nix build support. The duplicate listing of imported functions is a bit |
| 6 | +# unfortunate, but it seems to be the best compromise. This way the python IDE |
| 7 | +# support works out of the box in VSCode and IntelliJ without requiring |
| 8 | +# additional IDE configuration. |
| 9 | +try: |
| 10 | + from ..test_helper.test_helper import ( # type: ignore |
| 11 | + LibvirtTestsBase, |
| 12 | + hotplug, |
| 13 | + initialComputeVMSetup, |
| 14 | + initialControllerVMSetup, |
| 15 | + wait_for_ssh, |
| 16 | + ) |
| 17 | +except Exception: |
| 18 | + from test_helper import ( |
| 19 | + LibvirtTestsBase, |
| 20 | + hotplug, |
| 21 | + initialComputeVMSetup, |
| 22 | + initialControllerVMSetup, |
| 23 | + wait_for_ssh, |
| 24 | + ) |
| 25 | + |
| 26 | +# pyright: reportPossiblyUnboundVariable=false |
| 27 | + |
| 28 | +# Following is required to allow proper linting of the python code in IDEs. |
| 29 | +# Because certain functions like start_all() and certain objects like computeVM |
| 30 | +# or other machines are added by Nix, we need to provide certain stub objects |
| 31 | +# in order to allow the IDE to lint the python code successfully. |
| 32 | +if "start_all" not in globals(): |
| 33 | + from ..test_helper.test_helper.nixos_test_stubs import ( # type: ignore |
| 34 | + Machine, |
| 35 | + computeVM, |
| 36 | + controllerVM, |
| 37 | + start_all, |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +# Config options for single connection/multiple connections |
| 42 | +class Connections(Enum): |
| 43 | + SINGLE_CONNECTION = "" |
| 44 | + MULTIPLE_CONNECTIONS = "--parallel --parallel-connections 4" |
| 45 | + |
| 46 | + |
| 47 | +# Config options for tls/no tls |
| 48 | +class Tls(Enum): |
| 49 | + WITH_TLS = "--tls" |
| 50 | + WITHOUT_TLS = "" |
| 51 | + |
| 52 | + |
| 53 | +def test_migration( |
| 54 | + sender: Machine, receiver: Machine, connections: Connections, tls: Tls |
| 55 | +): |
| 56 | + """ |
| 57 | + The test implementation itself. This test |
| 58 | + - starts a VM |
| 59 | + - hotplugs an additional NIC |
| 60 | + - hotplgs a disk |
| 61 | + - executes the configured migration |
| 62 | + - checks that SSH via both NICs works |
| 63 | + - checks that unplugging the disk works |
| 64 | + """ |
| 65 | + sender.succeed("virsh define /etc/domain-chv.xml") |
| 66 | + sender.succeed("virsh start testvm") |
| 67 | + |
| 68 | + wait_for_ssh(sender) |
| 69 | + |
| 70 | + # Attach some devices |
| 71 | + hotplug(sender, "virsh attach-device testvm /etc/new_interface.xml") |
| 72 | + wait_for_ssh(sender, ip="192.168.2.2") |
| 73 | + |
| 74 | + sender.succeed("qemu-img create -f raw /nfs-root/disk.img 100M") |
| 75 | + sender.succeed("chmod 0666 /nfs-root/disk.img") |
| 76 | + |
| 77 | + sender.succeed("virsh list | grep testvm") |
| 78 | + receiver.fail("virsh list | grep testvm") |
| 79 | + |
| 80 | + hotplug( |
| 81 | + sender, |
| 82 | + "virsh attach-disk --domain testvm --target vdb --persistent --source /var/lib/libvirt/storage-pools/nfs-share/disk.img", |
| 83 | + ) |
| 84 | + |
| 85 | + # Assemble the migration command |
| 86 | + dst_host = receiver.name |
| 87 | + migration_command = f"virsh migrate --domain testvm --desturi ch+tcp://{dst_host}/session --persistent --live --p2p {tls.value} {connections.value}" |
| 88 | + |
| 89 | + sender.succeed(migration_command) |
| 90 | + |
| 91 | + wait_for_ssh(receiver) |
| 92 | + wait_for_ssh(receiver, ip="192.168.2.2") |
| 93 | + hotplug(receiver, "virsh detach-disk --domain testvm --target vdb") |
| 94 | + |
| 95 | + sender.fail("virsh list | grep testvm") |
| 96 | + receiver.succeed("virsh list | grep testvm") |
| 97 | + |
| 98 | + |
| 99 | +class LibvirtTests(LibvirtTestsBase): # type: ignore |
| 100 | + def __init__(self, methodName): |
| 101 | + super().__init__(methodName, controllerVM, computeVM) |
| 102 | + |
| 103 | + @classmethod |
| 104 | + def setUpClass(cls): |
| 105 | + start_all() |
| 106 | + initialControllerVMSetup(controllerVM) |
| 107 | + initialComputeVMSetup(computeVM) |
| 108 | + |
| 109 | + def test_migrate_single_connection_no_tls(self): |
| 110 | + """Migration with a single connection and no TLS.""" |
| 111 | + test_migration( |
| 112 | + controllerVM, |
| 113 | + computeVM, |
| 114 | + Connections.SINGLE_CONNECTION, |
| 115 | + Tls.WITHOUT_TLS, |
| 116 | + ) |
| 117 | + |
| 118 | + def test_migrate_multiple_connections_no_tls(self): |
| 119 | + """Migration with multiple connections and no TLS.""" |
| 120 | + test_migration( |
| 121 | + controllerVM, computeVM, Connections.MULTIPLE_CONNECTIONS, Tls.WITHOUT_TLS |
| 122 | + ) |
| 123 | + |
| 124 | + def test_migrate_single_connection_with_tls(self): |
| 125 | + """Migration with a single connection and TLS.""" |
| 126 | + test_migration( |
| 127 | + controllerVM, computeVM, Connections.SINGLE_CONNECTION, Tls.WITH_TLS |
| 128 | + ) |
| 129 | + |
| 130 | + def test_migrate_multiple_connections_with_tls(self): |
| 131 | + """Migration with multiple connections and TLS.""" |
| 132 | + test_migration( |
| 133 | + controllerVM, computeVM, Connections.MULTIPLE_CONNECTIONS, Tls.WITH_TLS |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +def suite(): |
| 138 | + # Test cases sorted in alphabetical order. |
| 139 | + testcases = [ |
| 140 | + LibvirtTests.test_migrate_multiple_connections_no_tls, |
| 141 | + LibvirtTests.test_migrate_multiple_connections_with_tls, |
| 142 | + LibvirtTests.test_migrate_single_connection_no_tls, |
| 143 | + LibvirtTests.test_migrate_single_connection_with_tls, |
| 144 | + ] |
| 145 | + |
| 146 | + suite = unittest.TestSuite() |
| 147 | + for testcaseMethod in testcases: |
| 148 | + suite.addTest(LibvirtTests(testcaseMethod.__name__)) |
| 149 | + return suite |
| 150 | + |
| 151 | + |
| 152 | +runner = unittest.TextTestRunner() |
| 153 | +if not runner.run(suite()).wasSuccessful(): |
| 154 | + raise Exception("Test Run unsuccessful") |
0 commit comments