Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env_process: Destroy unrequested VMs via a Setuper #3979

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 UnrequestedVMHandler
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(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):
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 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
Loading