Skip to content

Commit

Permalink
env_process: Destroy unrequested VMs via a Setuper
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bgartzi committed Aug 27, 2024
1 parent b490b81 commit 6dc943d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
24 changes: 2 additions & 22 deletions virttest/env_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
CheckRunningAsRoot,
)
from virttest.test_setup.storage import StorageConfig
from virttest.test_setup.vms import DestroyUnrequestedVMs
from virttest.utils_version import VersionInterval

utils_libvirtd = lazy_import("virttest.utils_libvirtd")
Expand Down Expand Up @@ -1046,34 +1047,13 @@ def preprocess(test, params, env):
_setup_manager.register(FirewalldService)
_setup_manager.register(IPSniffer)
_setup_manager.register(MigrationEnvSetup)
_setup_manager.register(DestroyUnrequestedVMs)
_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):
Expand Down
34 changes: 34 additions & 0 deletions virttest/test_setup/vms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import logging

from virttest import virt_vm
from virttest.test_setup.core import Setuper

LOG = logging.getLogger(__name__)


class DestroyUnrequestedVMs(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

0 comments on commit 6dc943d

Please sign in to comment.