From 86786d9d380994a000d172ce1cce2a751f6d42dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Be=C3=B1at=20Gartzia=20Arruabarrena?= Date: Wed, 20 Nov 2024 18:43:03 +0100 Subject: [PATCH] env_process: Refactor {pre,post}process_vm_off_hook execution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Write a Setuper in charge of running the pre and post process hooks for when vms go off. Register it in env_process's _setup_manager instead of calling the hooks in-situ. Signed-off-by: BeƱat Gartzia Arruabarrena --- virttest/env_process.py | 21 +++++---------------- virttest/test_setup/vms.py | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/virttest/env_process.py b/virttest/env_process.py index 2ab6925a1b..395acf9e1f 100644 --- a/virttest/env_process.py +++ b/virttest/env_process.py @@ -62,7 +62,7 @@ ) from virttest.test_setup.storage import StorageConfig from virttest.test_setup.verify import VerifyHostDMesg -from virttest.test_setup.vms import UnrequestedVMHandler +from virttest.test_setup.vms import ProcessVMOff, UnrequestedVMHandler from virttest.utils_version import VersionInterval utils_libvirtd = lazy_import("virttest.utils_libvirtd") @@ -1005,10 +1005,6 @@ def preprocess(test, params, env): """ error_context.context("preprocessing") - # Run this hook before any network setup stage and vm creation. - if callable(preprocess_vm_off_hook): - preprocess_vm_off_hook(test, params, env) # pylint: disable=E1102 - # Add migrate_vms to vms migrate_vms = params.objects("migrate_vms") if migrate_vms: @@ -1016,6 +1012,10 @@ def preprocess(test, params, env): params["vms"] = " ".join(vms) _setup_manager.initialize(test, params, env) + # Keep ProcessVMOff registered first. That way VM Off hooks will be first + # and last running during pre/postprocess. That way vms will be actually + # off to ensure data is written to disk. + _setup_manager.register(ProcessVMOff) _setup_manager.register(ResetQemuGCov) _setup_manager.register(VerifyHostDMesg) _setup_manager.register(SwitchSMTOff) @@ -1758,17 +1758,6 @@ def postprocess(test, params, env): err += "\n".join(_setup_manager.do_cleanup()) - # Run this hook after any vms are actually off to ensure data is - # written to disk. - if callable(postprocess_vm_off_hook): - try: - postprocess_vm_off_hook(test, params, env) # pylint: disable=E1102 - except Exception as details: - err += "\nPostprocessing dead vm hook: %s" % str(details).replace( - "\\n", "\n " - ) - LOG.error(details) - if err: raise RuntimeError("Failures occurred while postprocess:\n%s" % err) elif _post_hugepages_surp > _pre_hugepages_surp: diff --git a/virttest/test_setup/vms.py b/virttest/test_setup/vms.py index d022dcac0a..4bd52c2d94 100644 --- a/virttest/test_setup/vms.py +++ b/virttest/test_setup/vms.py @@ -1,6 +1,6 @@ import logging -from virttest import virt_vm +from virttest import env_process, virt_vm from virttest.test_setup.core import Setuper LOG = logging.getLogger(__name__) @@ -32,3 +32,24 @@ def setup(self): def cleanup(self): pass + + +class ProcessVMOff(Setuper): + def setup(self): + # Run this hook before any network setup stage and vm creation. + if callable(env_process.preprocess_vm_off_hook): + env_process.preprocess_vm_off_hook( + self.test, self.params, self.env + ) # pylint: disable=E1102 + + def cleanup(self): + if callable(env_process.postprocess_vm_off_hook): + try: + env_process.postprocess_vm_off_hook( + self.test, self.params, self.env + ) # pylint: disable=E1102 + except Exception as details: + raise Exception( + "\nPostprocessing dead vm hook: %s" + % str(details).replace("\\n", "\n ") + ) from None