Skip to content

Commit

Permalink
add case for chardev with log file
Browse files Browse the repository at this point in the history
  VIRT-296959:chardev with log file
Signed-off-by: nanli <[email protected]>
  • Loading branch information
nanli1 committed Jul 10, 2023
1 parent 7c1c7b6 commit 1d62e95
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
20 changes: 20 additions & 0 deletions libvirt/tests/cfg/chardev/log/chardev_with_log_file.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- chardev.log:
type = chardev_with_log_file
start_vm = 'no'
login_user = "EXAMPLE_USER"
login_passwd = "EXAMPLE_PWD"
log_file = "/var/log/libvirt/chardev_test.log"
variants:
- console:
chardev = "console"
check_cmd = "uname -r"
variants console_type:
- pty:
chardev_type = "pty"
device_dict = "{'type_name':'${chardev_type}','log_file': {'file': '${log_file}', 'append':'off'}}"
- unix:
chardev_type = 'unix'
source_mode = "bind"
source_path = "/tmp/foo"
access_cmd = "socat stdin unix-connect:${source_path}"
device_dict = "{'type_name':'${chardev_type}','log_file': {'file': '${log_file}', 'append':'off'}, 'sources': [{'attrs': {'path': '${source_path}', 'mode':'${source_mode}'}}]}"
93 changes: 93 additions & 0 deletions libvirt/tests/src/chardev/log/chardev_with_log_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Copyright Redhat
#
# SPDX-License-Identifier: GPL-2.0

# Author: Nan Li <[email protected]>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import aexpect
import os

from virttest import utils_test

from virttest.libvirt_xml import vm_xml
from virttest.utils_libvirt import libvirt_vmxml
from virttest.utils_test import libvirt


def run(test, params, env):
"""
Verify data send through chardev can be correctly logged into log file
"""

def setup_test():
"""
Prepare a vm with the *only* console device.
"""
vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
vmxml.remove_all_device_by_type("console")
vmxml.remove_all_device_by_type("serial")
libvirt_vmxml.modify_vm_device(
vmxml=vmxml, dev_type=chardev, dev_dict=device_dict)

def run_test():
"""
1) Connect to the device using suitable client
2) Login to the guest from client, run some commands
3) Check log file.
"""
test.log.info("TEST_STEP1: Connect to the device")
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: Check append value in guest.")
access_session = aexpect.ShellSession(access_cmd)
utils_test.libvirt.virsh_console_login(
access_session, login_user, login_passwd, debug=True, timeout=20)

access_session.cmd_output(check_cmd)
test.log.debug("Sent cmd: %s", check_cmd)

test.log.info("TEST_STEP3: Check log file")
vm_session = vm.wait_for_login()
kernel_version = vm_session.cmd_output(check_cmd)
test.log.debug("Get expected kernel version '%s' " % kernel_version)
vm_session.close()

libvirt.check_logfile("%s\n%s" % (check_cmd, kernel_version), log_file)

def teardown_test():
"""
Clean data.
"""
if vm.is_alive():
vm.destroy(gracefully=False)
bkxml.sync()

if os.path.exists(log_file):
os.remove(log_file)

vm_name = params.get("main_vm")
device_dict = eval(params.get('device_dict', '{}'))

access_cmd = params.get('access_cmd', "virsh console %s --force" % vm_name)
chardev = params.get('chardev')
log_file = params.get('log_file')
login_user = params.get('login_user')
login_passwd = params.get('login_passwd')
check_cmd = params.get('check_cmd')

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()

0 comments on commit 1d62e95

Please sign in to comment.