From 05147907cf52899170a2eb5938ff61e2db0f5603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Be=C3=B1at=20Gartzia=20Arruabarrena?= Date: Tue, 27 Aug 2024 09:34:12 +0200 Subject: [PATCH] env_process: Destroy unrequested VMs via a Setuper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor the piece of code that destroyed the unrequested VMs (if asked so) into a setuper. Import that class into env_process and register it into the setup_manager. Signed-off-by: BeƱat Gartzia Arruabarrena --- virttest/env_process.py | 24 ++---------------------- virttest/test_setup/vms.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 virttest/test_setup/vms.py diff --git a/virttest/env_process.py b/virttest/env_process.py index d51000f1ba..62d5b0673b 100644 --- a/virttest/env_process.py +++ b/virttest/env_process.py @@ -60,6 +60,7 @@ CheckRunningAsRoot, ) from virttest.test_setup.storage import StorageConfig +from virttest.test_setup.vms import UnrequestedVMHandler from virttest.utils_version import VersionInterval utils_libvirtd = lazy_import("virttest.utils_libvirtd") @@ -1046,34 +1047,13 @@ def preprocess(test, params, env): _setup_manager.register(FirewalldService) _setup_manager.register(IPSniffer) _setup_manager.register(MigrationEnvSetup) + _setup_manager.register(UnrequestedVMHandler) _setup_manager.do_setup() vm_type = params.get("vm_type") base_dir = data_dir.get_data_dir() - # Destroy and remove VMs that are no longer needed in the environment or - # leave them untouched if they have to be disregarded only for this test - requested_vms = params.objects("vms") - keep_unrequested_vms = params.get_boolean("keep_unrequested_vms", False) - kill_unrequested_vms_gracefully = params.get_boolean( - "kill_unrequested_vms_gracefully", True - ) - for key in list(env.keys()): - vm = env[key] - if not isinstance(vm, virt_vm.BaseVM): - continue - if vm.name not in requested_vms: - if keep_unrequested_vms: - LOG.debug( - "The vm %s is registered in the env and disregarded " - "in the current test", - vm.name, - ) - else: - vm.destroy(gracefully=kill_unrequested_vms_gracefully) - del env[key] - global KVM_MODULE_HANDLERS kvm_modules = arch.get_kvm_module_list() for module in reversed(kvm_modules): diff --git a/virttest/test_setup/vms.py b/virttest/test_setup/vms.py new file mode 100644 index 0000000000..d022dcac0a --- /dev/null +++ b/virttest/test_setup/vms.py @@ -0,0 +1,34 @@ +import logging + +from virttest import virt_vm +from virttest.test_setup.core import Setuper + +LOG = logging.getLogger(__name__) + + +class UnrequestedVMHandler(Setuper): + def setup(self): + # Destroy and remove VMs that are no longer needed in the environment or + # leave them untouched if they have to be disregarded only for this test + requested_vms = self.params.objects("vms") + keep_unrequested_vms = self.params.get_boolean("keep_unrequested_vms", False) + kill_unrequested_vms_gracefully = self.params.get_boolean( + "kill_unrequested_vms_gracefully", True + ) + for key in list(self.env.keys()): + vm = self.env[key] + if not isinstance(vm, virt_vm.BaseVM): + continue + if vm.name not in requested_vms: + if keep_unrequested_vms: + LOG.debug( + "The vm %s is registered in the env and disregarded " + "in the current test", + vm.name, + ) + else: + vm.destroy(gracefully=kill_unrequested_vms_gracefully) + del self.env[key] + + def cleanup(self): + pass