From 2300e0cc420267bee5fe7895c0d21a864f511146 Mon Sep 17 00:00:00 2001 From: nanli Date: Thu, 15 Jun 2023 12:25:40 +0800 Subject: [PATCH] add case for chardev release console VIRT-296962: release virsh console when guest shut down Signed-off-by: nanli --- .../release_console.cfg | 34 ++++++ .../release_console.py | 107 ++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 libvirt/tests/cfg/chardev/virsh_console_operation/release_console.cfg create mode 100644 libvirt/tests/src/chardev/virsh_console_operation/release_console.py diff --git a/libvirt/tests/cfg/chardev/virsh_console_operation/release_console.cfg b/libvirt/tests/cfg/chardev/virsh_console_operation/release_console.cfg new file mode 100644 index 0000000000..465e9bf1ef --- /dev/null +++ b/libvirt/tests/cfg/chardev/virsh_console_operation/release_console.cfg @@ -0,0 +1,34 @@ +- chardev.release_console: + type = release_console + start_vm = 'no' + chardev_type = "pty" + release_cmd = "shutdown -h now" + port = 0 + variants dev: + - console: + chardev = "console" + access_cmd = "virsh console %s" + expected_msg = "Shell process terminated" + variants: + - virtio_target: + target_type = "virtio" + device_dict = "{'type_name':'${chardev_type}','target_type':'${target_type}', 'target_port':'${port}'}" + - serial: + chardev = "serial" + access_cmd = "console %s" + expected_msg = "1.*%s.*running" + variants: + - pci_target: + no s390-virtio + target_type = "pci-serial" + device_dict = "{'type_name':'${chardev_type}','target_type':'${target_type}', 'target_port':'${port}'}" + - isa_target: + target_type = isa-serial + target_model = isa-serial + aarch64: + target_type = system-serial + target_model = pl011 + s390x: + target_type = sclp-serial + target_model = sclpconsole + device_dict = "{'type_name':'${chardev_type}','target_model':'${target_model}','target_type':'${target_type}', 'target_port':'${port}'}" diff --git a/libvirt/tests/src/chardev/virsh_console_operation/release_console.py b/libvirt/tests/src/chardev/virsh_console_operation/release_console.py new file mode 100644 index 0000000000..30b99aae6b --- /dev/null +++ b/libvirt/tests/src/chardev/virsh_console_operation/release_console.py @@ -0,0 +1,107 @@ +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# Copyright Redhat +# +# SPDX-License-Identifier: GPL-2.0 + +# Author: Nan Li +# +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +import aexpect +import re + +from virttest import virsh +from virttest import utils_test + +from virttest.libvirt_xml import vm_xml +from virttest.utils_libvirt import libvirt_vmxml + + +def run(test, params, env): + """ + Test virsh console client can release console and return to host + when guest shutdown. + """ + + def setup_test(): + """ + Make sure remove all other serial/console devices before test. + """ + test.log.info("Setup env: Remove console and serial") + vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) + vmxml.remove_all_device_by_type('console') + vmxml.remove_all_device_by_type('serial') + vmxml.sync() + + def run_test(): + """ + 1) Start guest with chardev. + 2) Connect to vm . + 3) Shutdown and console is closed. + """ + test.log.info("TEST_STEP1: Add chardev and start guest") + vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) + libvirt_vmxml.modify_vm_device( + vmxml=vmxml, dev_type=chardev, dev_dict=device_dict) + vm.start() + vm.wait_for_login().close() + vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) + test.log.debug("The vmxml after start vm is:\n %s", vmxml) + + test.log.info("TEST_STEP2: Connect to vm and check shutdown works.") + if dev == "console": + # Check with 'virsh console vm_name' + access_session = aexpect.ShellSession(access_cmd % vm_name) + utils_test.libvirt.virsh_console_login( + access_session, params.get('username'), params.get('password'), + debug=True) + try: + access_session.cmd_output(release_cmd) + test.log.debug("Sent cmd: '%s' in console" % release_cmd) + except (aexpect.ShellError, aexpect.ExpectError) as detail: + if expected_msg not in str(detail): + test.fail('Expect shell terminated, but found %s' % detail) + else: + test.log.debug("Got '%s' in '%s' " % (expected_msg, detail)) + + elif dev == "serial": + # Check with 'virsh' and 'console vm_name' + access_session = virsh.VirshSession(virsh_exec=virsh.VIRSH_EXEC) + access_session.sendline(access_cmd % vm_name) + utils_test.libvirt.virsh_console_login( + access_session, params.get('username'), params.get('password'), + debug=True) + + result = access_session.cmd_output('list') + # dom_output = virsh.dom_list("--all", debug=True).stdout.strip() + if not re.search(expected_msg % vm_name, result): + test.fail('Expect "%s", but found "%s"' % (expected_msg % vm_name, result)) + else: + test.log.debug("Got '%s' in '%s' " % (expected_msg % vm_name, result)) + + def teardown_test(): + """ + Clean data. + """ + if vm.is_alive(): + vm.destroy(gracefully=False) + bkxml.sync() + + vm_name = params.get("main_vm") + dev = params.get('dev') + release_cmd = params.get('release_cmd') + expected_msg = params.get('expected_msg') + chardev = params.get('chardev') + access_cmd = params.get('access_cmd') + device_dict = eval(params.get('device_dict', '{}')) + + vm = env.get_vm(vm_name) + vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) + bkxml = vmxml.copy() + + try: + setup_test() + run_test() + + finally: + teardown_test()