| 1 | # SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | # |
| 3 | # Migration test base class |
| 4 | # |
| 5 | # Copyright (c) 2019 Red Hat, Inc. |
| 6 | # |
| 7 | # Authors: |
| 8 | # Cleber Rosa <crosa@redhat.com> |
| 9 | # Caio Carrara <ccarrara@redhat.com> |
| 10 | # |
| 11 | # This work is licensed under the terms of the GNU GPL, version 2 or |
| 12 | # later. See the COPYING file in the top-level directory. |
| 13 | |
| 14 | import time |
| 15 | |
| 16 | from qemu_test import QemuSystemTest, which |
| 17 | from qemu_test.ports import Ports |
| 18 | |
| 19 | |
| 20 | class MigrationTest(QemuSystemTest): |
| 21 | |
| 22 | timeout = 10 |
| 23 | |
| 24 | @staticmethod |
| 25 | def migration_finished(vm): |
| 26 | return vm.cmd('query-migrate')['status'] in ('completed', 'failed') |
| 27 | |
| 28 | def assert_migration(self, src_vm, dst_vm): |
| 29 | |
| 30 | end = time.monotonic() + self.timeout |
| 31 | while time.monotonic() < end and not self.migration_finished(src_vm): |
| 32 | time.sleep(0.1) |
| 33 | |
| 34 | end = time.monotonic() + self.timeout |
| 35 | while time.monotonic() < end and not self.migration_finished(dst_vm): |
| 36 | time.sleep(0.1) |
| 37 | |
| 38 | self.assertEqual(src_vm.cmd('query-migrate')['status'], 'completed') |
| 39 | self.assertEqual(dst_vm.cmd('query-migrate')['status'], 'completed') |
| 40 | self.assertEqual(dst_vm.cmd('query-status')['status'], 'running') |
| 41 | self.assertEqual(src_vm.cmd('query-status')['status'],'postmigrate') |
| 42 | |
| 43 | # Can be overridden by subclasses to configure both source/dest VMs. |
| 44 | def configure_machine(self, vm): |
| 45 | vm.add_args('-nodefaults') |
| 46 | |
| 47 | # Can be overridden by subclasses to prepare the source VM before |
| 48 | # migration, e.g. by running some workload inside the source VM |
| 49 | # to see if it continues to run properly after migration. |
| 50 | def launch_source_vm(self, vm): |
| 51 | vm.launch() |
| 52 | |
| 53 | # Can be overridden by subclasses to check the destination VM after |
| 54 | # migration, e.g. by checking if the workload is still running after |
| 55 | # migration. |
| 56 | def assert_dest_vm(self, vm): |
| 57 | pass |
| 58 | |
| 59 | def migrate_vms(self, dst_uri, src_uri, dst_vm, src_vm): |
| 60 | dst_vm.qmp('migrate-incoming', uri=dst_uri) |
| 61 | src_vm.qmp('migrate', uri=src_uri) |
| 62 | self.assert_migration(src_vm, dst_vm) |
| 63 | self.assert_dest_vm(dst_vm) |
| 64 | |
| 65 | def migrate(self, dst_uri, src_uri=None): |
| 66 | dst_vm = self.get_vm("dst-qemu") |
| 67 | dst_vm.add_args('-incoming', 'defer') |
| 68 | self.configure_machine(dst_vm) |
| 69 | dst_vm.launch() |
| 70 | |
| 71 | src_vm = self.get_vm(name="src-qemu") |
| 72 | self.configure_machine(src_vm) |
| 73 | self.launch_source_vm(src_vm) |
| 74 | |
| 75 | if src_uri is None: |
| 76 | src_uri = dst_uri |
| 77 | |
| 78 | self.migrate_vms(dst_uri, src_uri, dst_vm, src_vm) |
| 79 | |
| 80 | def _get_free_port(self, ports): |
| 81 | port = ports.find_free_port() |
| 82 | if port is None: |
| 83 | self.skipTest('Failed to find a free port') |
| 84 | return port |
| 85 | |
| 86 | def migration_with_tcp_localhost_vms(self, dst_vm, src_vm): |
| 87 | with Ports() as ports: |
| 88 | uri = 'tcp:localhost:%u' % self._get_free_port(ports) |
| 89 | self.migrate_vms(uri, uri, dst_vm, src_vm) |
| 90 | |
| 91 | def migration_with_tcp_localhost(self): |
| 92 | with Ports() as ports: |
| 93 | dst_uri = 'tcp:localhost:%u' % self._get_free_port(ports) |
| 94 | self.migrate(dst_uri) |
| 95 | |
| 96 | def migration_with_unix(self): |
| 97 | dst_uri = 'unix:%s/migration.sock' % self.socket_dir().name |
| 98 | self.migrate(dst_uri) |
| 99 | |
| 100 | def migration_with_exec(self): |
| 101 | if not which('socat'): |
| 102 | self.skipTest('socat is not available') |
| 103 | with Ports() as ports: |
| 104 | free_port = self._get_free_port(ports) |
| 105 | dst_uri = 'exec:socat TCP-LISTEN:%u -' % free_port |
| 106 | src_uri = 'exec:socat - TCP:localhost:%u,forever' % free_port |
| 107 | self.migrate(dst_uri, src_uri) |