On 04/09/2013 11:30 PM, Lucas Meneghel Rodrigues wrote:
Hi Yu, thanks for the test! I have some problems with it, mainly the
fact that it won't work on the JeOS. Some suggestions:
1) See if our JeOS really needs this configuration, I'd think it doesn't
2) Skip attempts to configure grub or inittab if it's not applicable.
Ok, I'll consider the implement of grub2.
And sending with pull request.
Thanks
Yu
So, from your 6 patch series, I'm taking the first 4, then I'm marking
these 2 last ones as superseded. Thanks!
On Fri, Mar 8, 2013 at 3:56 AM, Yu Mingfei <[email protected]
<mailto:[email protected]>> wrote:
Signed-off-by: Yu Mingfei <[email protected]
<mailto:[email protected]>>
---
libvirt/tests/virsh_console.py | 237
+++++++++++++++++++++++++++++++++++++++++
1 file changed, 237 insertions(+)
create mode 100644 libvirt/tests/virsh_console.py
diff --git a/libvirt/tests/virsh_console.py
b/libvirt/tests/virsh_console.py
new file mode 100644
index 0000000..bea8ffa
--- /dev/null
+++ b/libvirt/tests/virsh_console.py
@@ -0,0 +1,237 @@
+import re, logging
+from autotest.client.shared import error
+from virttest import aexpect
+from virttest.libvirt_xml import vm_xml, xcepts
+
+
+def xml_console_config(vm_name, serial_type='pty',
+ serial_port='0', serial_path=None):
+ """
+ Check the primary serial and set it to pty.
+ """
+ vm_xml.VMXML.set_primary_serial(vm_name, serial_type,
+ serial_port, serial_path)
+
+
+def xml_console_recover(vmxml):
+ """
+ Recover older xml config with backup vmxml.
+ """
+ try:
+ vmxml.undefine()
+ vmxml.define()
+ return True
+ except xcepts.LibvirtXMLError, detail:
+ logging.error("Recover older serial failed:%s.", detail)
+ return False
+
+
+def vm_console_config(vm, dev='ttyS0', speed='115200'):
+ """
+ Login to config vm for virsh console.
+ Three step:
+ 1)Add dev to /etc/securetty to support 'root' for virsh console
+ 2)Add kernel console option:
+ e.g. console=ttyS0,115200
+ 3)Config init process for RHEL5,4,3 and others.
+ (No need this on RHEL6)
+ e.g. S0:2345:respawn:/sbin/mingetty ttyS0
+ """
+ if not vm.is_alive():
+ vm.start()
+ session = vm.wait_for_login()
+
+ # Step 1
+ try:
+ status, output = session.cmd_status_output(
+ "cat /etc/securetty | grep %s" % dev)
+ if status:
+ session.cmd("echo %s>> /etc/securetty" % dev)
+ except Exception, detail:
+ session.close()
+ logging.error("Config /etc/securetty failed:%s", detail)
+ return False
+
+ # Step 2
+ # TODO: Support grub2
+ try:
+ status, output = session.cmd_status_output("cat
/etc/grub.conf |"
+ " grep
console=%s" % dev)
+ if status:
+ session.cmd("rm -f /etc/grub.conf.bak")
+ session.cmd("cp -f /etc/grub.conf /etc/grub.conf.bak")
+ version = session.cmd("uname -r").strip()
+ kernel_console = " console=%s,%s" % (dev, speed)
+ session.cmd("sed -e \'s/vmlinuz-%s.*/&%s/g\'
/etc/grub.conf.bak"
+ "> /etc/grub.conf" % (version,
kernel_console))
+ except Exception, detail:
+ session.close()
+ logging.error("Config /etc/grub.conf failed:%s", detail)
+ return False
Obviously this doesn't support grub2, and it should be skipped in case
there's no /etc/grub.conf.
+ # Step 3
+ try:
+ status, output = session.cmd_status_output("cat
/etc/inittab | "
+ "grep \'2345:respawn:.*\s%s\'" % dev)
+ if status:
+ status, output = session.cmd_status_output(
+ "grep \'6:2345:respawn:/sbin/mingetty\'
/etc/inittab")
+ if not status:
+ session.cmd("rm -f /etc/inittab.bak")
+ session.cmd("cp -f /etc/inittab /etc/inittab.bak")
+ init_console_str =
"co:2345:respawn:/sbin/mingetty ttyS0"
+ session.cmd("sed -e \'/6:2345:respawn:/a\\%s\'
/etc/init"
+ "tab.bak> /etc/inittab" %
init_console_str)
+ except Exception, detail:
+ session.close()
+ logging.error("Config /etc/inittab failed:%s", detail)
+ return False
+
+ return True
Inittab doesn't exist on systemd systems, so this check should be
skipped in case there inittab file contains "systemd" inside of it.
+
+def vm_console_cleanup(vm):
+ """
+ Cleanup console config.
+ """
For this function, same comments about the setup stage apply.
+ if not vm.is_alive():
+ vm.start()
+ session = vm.wait_for_login()
+
+ try:
+ # recover&delete /etc/grub.conf.bak
+ status, output = session.cmd("ls /etc/grub.conf.bak")
+ if not status:
+ session.cmd("mv -f /etc/grub.conf.bak /etc/conf.cfg")
+
+ # recover&delete /etc/inittab.bak
+ status, output = session.cmd("ls /etc/inittab.bak")
+ if not status:
+ session.cmd("mv -f /etc/inittab.bak /etc/inittab")
+ except Exception, detail:
+ # Just for warning
+ logging.error("Cleanup kernel or init failed:%s.", detail)
+ return False
+
+ session.close()
+ return True
+
+
+def verify_virsh_console(session, user, passwd, debug=False):
+ """
+ Run commands in console session.
+ """
+ log = ""
+ console_cmd = "cat /proc/cpuinfo"
+ try:
+ while True:
+ match, text = session.read_until_last_line_matches(
+ [r"[E|e]scape character is", r"login:",
+ r"[P|p]assword:", session.prompt],
+ timeout=10, internal_timeout=0.5)
+
+ if match == 0:
+ if debug:
+ logging.debug("Got '^]', sending '\\n'")
+ session.sendline()
+ elif match == 1:
+ if debug:
+ logging.debug("Got 'login:', sending '%s'", user)
+ session.sendline("%s" % user)
+ elif match == 2:
+ if debug:
+ logging.debug("Got 'Password:', sending
'%s'", passwd)
+ session.sendline("%s" % passwd)
+ elif match == 3:
+ if debug:
+ logging.debug("Got Shell prompt -- logged in")
+ break
+
+ status, output = session.cmd_status_output(console_cmd)
+ logging.info <http://logging.info>("output of command:\n%s", output)
+ session.close()
+ except Exception, detail: # Just need to know command
executed failed.;)
+ log = session.get_output()
+ logging.error("Verify virsh console failed:\n%s\n%s",
detail, log)
+ session.close()
+ return False
+
+ if not re.search("processor", output):
+ logging.error("Verify virsh console failed: Result does
not match.")
+ return False
+
+ return True
+
+
+def run_virsh_console(test, params, env):
+ """
+ Test command: virsh console.
+ """
+ os_type = params.get("os_type")
+ if os_type == "windows":
+ raise error.TestNAError("SKIP:Do not support Windows.")
+
+ # Get parameters for test
+ vm_name = params.get("main_vm")
+ vm = env.get_vm(vm_name)
+
+ vm_ref = params.get("virsh_console_vm_ref", "domname")
+ vm_state = params.get("virsh_console_vm_state", "running")
+ login_user = params.get("console_login_user", "root")
+ if login_user == "root":
+ login_passwd = params.get("password")
+ else:
+ login_passwd = params.get("console_password_not_root")
+ status_error = params.get("status_error", "yes")
+ domuuid = vm.get_uuid()
+ domid = ""
+
+ # A backup of original vm
+ vmxml_backup = vm_xml.VMXML.new_from_dumpxml(vm_name)
+ if vm.is_alive():
+ vm.destroy()
+ xml_console_config(vm_name)
+ vm_console_config(vm)
+ vm.destroy()
+
+ # Prepare vm state for test
+ if vm_state != "shutoff":
+ vm.start()
+ vm.wait_for_login()
+ domid = vm.get_id()
+ if vm_state == "paused":
+ vm.pause()
+
+ if vm_ref == "domname":
+ vm_ref = vm_name
+ elif vm_ref == "domid":
+ vm_ref = domid
+ elif vm_ref == "domuuid":
+ vm_ref = domuuid
+ elif domid and vm_ref == "hex_id":
+ vm_ref = hex(int(domid))
+
+ # Run command
+ command = "virsh console %s" % vm_ref
+ session = aexpect.ShellSession(command)
+
+ status = verify_virsh_console(session, login_user,
+ login_passwd, debug=True) == False
+
+ # Recover state of vm.
+ if vm_state == "paused":
+ vm.resume()
+
+ # Recover vm
+ vm_console_cleanup(vm)
+ if vm.is_alive():
+ vm.destroy()
+ xml_console_recover(vmxml_backup)
+
+ # Check result
+ if status_error == "yes":
+ if not status:
+ raise error.TestFail("Run successful with wrong
command!")
+ elif status_error == "no":
+ if status:
+ raise error.TestFail("Run failed with right command!")
--
1.7.11.7
_______________________________________________
Virt-test-devel mailing list
[email protected] <mailto:[email protected]>
https://www.redhat.com/mailman/listinfo/virt-test-devel
--
Lucas
--
Best Regards
Yu Mingfei
_______________________________________________
Virt-test-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virt-test-devel