This patch adds testcases for virt-edit.
Signed-off-by: Yu Mingfei <[email protected]>
---
client/tests/libvirt/tests/virt_edit.py | 278 +++++++++++++++++++++++++++++++
1 files changed, 278 insertions(+), 0 deletions(-)
create mode 100644 client/tests/libvirt/tests/virt_edit.py
diff --git a/client/tests/libvirt/tests/virt_edit.py
b/client/tests/libvirt/tests/virt_edit.py
new file mode 100644
index 0000000..1d4a00d
--- /dev/null
+++ b/client/tests/libvirt/tests/virt_edit.py
@@ -0,0 +1,278 @@
+import logging, time, os, sys, shutil, re
+from autotest.client.shared import utils, error
+from autotest.client.virt import virt_vm, virt_remote, virt_env_process,
libvirt_vm, aexpect, libvirt_xml_utils
+from xml.dom.minidom import parse, parseString
+
+
+def cmd_virt_edit(dom_disk_object, filename, option="", more_args="",
change_lang="no", uri=""):
+ """
+ Create command of virt-edit.
+
+ @param dom_disk_object:your virt-edit object,a dom name or a disk path
+ @param filename:the file to edit in dom_disk_object
+ @param option:virt-edit option
+ @param more_args:additional option after dom_disk_object
+ @param change_lang:add LANG=C before cmd or not
+ @param uri:if using libvirt, connect to the given URI.
+ @return:return created cmd string
+ """
+ cmd = ""
+ if change_lang == "yes":
+ cmd = "LANG=C "
+
+ if uri == "":
+ cmd += "virt-edit "
+ else:
+ cmd += "virt-edit -c %s " % uri
+ if option != "":
+ cmd += "%s " % option
+ if dom_disk_object != "":
+ cmd += "%s " % dom_disk_object
+ if filename != "":
+ cmd += "%s " % filename
+ if more_args != "":
+ cmd += "%s " % more_args
+ return cmd
+
+
+def virt_edit(vm_ref, filename, params):
+ """
+ According params dict to make virt-edit command for test through
func(cmd_virt_edit).
+
+ @param vm_ref:virt-edit object's type,it can be useful or not.
+ @param filename:the file name to be edited.
+ @param params:params dict of your test target
+ @return:return created cmd string
+ """
+ change_lang = params.get("change_lang")
+ uri = params.get("uri")
+ option = params.get("option")
+ use_disk_name = params.get("use_disk_name")
+ more_args = params.get("more_args")
+ if vm_ref == "right_name":
+ if use_disk_name == "yes":
+ dom_disk_object = params.get("disk_name")
+ utils.run("dd if=/dev/zero of=%s bs=512M count=2" %
dom_disk_object)
+ else:
+ dom_disk_object = params.get("new_vm_name")
+ elif vm_ref == "valid_name":
+ dom_disk_object = params.get("valid_name")
+ elif vm_ref == "uuid":
+ if params.has_key("dom_uuid"):
+ dom_disk_object = params.get("dom_uuid")
+ else:
+ logging.info("Get dom uuid failed.")
+ dom_disk_object = ""
+ elif vm_ref == "dom_disk":
+ if params.has_key("dom_disk"):
+ dom_disk_object = params.get("dom_disk")
+ else:
+ logging.info("Get dom disk failed.")
+ dom_disk_object = ""
+ else:
+ dom_disk_object = ""
+
+ cmd = cmd_virt_edit(dom_disk_object, filename, option, more_args,
change_lang, uri)
+ logging.info("virt-edit command: %s" % cmd)
+ return cmd
+
+
+def exec_virt_edit(cmd):
+ cmd_result = utils.run(cmd, timeout=30, ignore_status=True)
+ logging.info("Output: %s", cmd_result.stdout.strip())
+ logging.info("Error: %s", cmd_result.stderr.strip())
+ logging.info("Status: %d", cmd_result.exit_status)
+ return cmd_result.exit_status
+
+
+def result_analysis(status, error_opposite="no"):
+ """
+ According status return a result of PASS(True) or FAIL(False).
+
+ @param status: status of command.
+ @param error_opposite(True or False):status good to PASS or bad to PASS.
+ @return:test fail or pass(False or True)
+ """
+ return (status == 0) ^ (error_opposite == "yes")
+
+
+def clean_up(vm, undefine_vm="no"):
+ """
+ Undefine vm if undefine_vm is yes.
+ @param vm:vm object
+ @param undefine_vm:undefine vm or not
+ """
+ if undefine_vm == "yes":
+ vm.remove()
+
+
+def run_virt_edit(test, params, env):
+ """
+ Test of virt-edit.
+
+ 1)Start a domain and check its IP according domain's os type
+ 2)According case in subtest.cfg, create a new vm or not
+ 3)Check domain's state,start it or not
+ 4)Get virt-edit's object and file to edit
+ 5)Execute command virt-edit
+ 6)Check virt-edit's object to get result
+ 7)According result to PASS or FAIL this case
+
+ @param test: kvm test object
+ @param params: Dictionary with the test parameters
+ @param env: Dictionary with test environment.
+ """
+ vm_ref = params.get("vm_ref")
+ libvirtd_restart = params.get("libvirtd_restart")
+ fail_flag = 0
+ file_names_type = params.get("file_names_type")
+ addition_edit = params.get("addition_edit")
+ foo_line = params.get("foo_line")
+ create_new_vm = params.get("create_new_vm")
+ vm = env.get_vm(params["main_vm"])
+ shutdown_later = params.get("shutdown_later")
+ wait_time = int(params.get("wait_time"))
+ undefine_vm = params.get("undefine_vm")
+ vm_name = params.get("main_vm")
+
+ vm.verify_alive()
+ time.sleep(5)
+
+ #check domain ip, if domain can not be connected ,end test
+ if params.get("os_type") != "windows":
+ vm.wait_for_login(timeout=wait_time)
+ vm_ip = vm.get_address(index = 0)
+ logging.info("VM IP: %s" % vm_ip)
+ ping_result = utils.run("ping -c 2 %s" % vm_ip, ignore_status = True)
+ if ping_result.exit_status != 0:
+ raise error.TestError("Pinging VM %s has no responding." % vm_name)
+ vm.destroy()
+
+ #create new domain if needed.
+ new_dom_name = params.get("new_vm_name")
+ domain = libvirt_xml_utils.create_new_vm(vm, new_dom_name, params)
+ params["dom_uuid"] = domain.get_uuid()
+ domain.start()
+ time.sleep(10)
+ domain.verify_alive()
+ if params.get("os_type") != "windows":
+ domain.wait_for_login(timeout=wait_time)
+
+ #if test during domain is runing,shutdown_later is yes.
+ if shutdown_later == "no":
+ domain.destroy()
+ time.sleep(10)
+
+ if vm_ref == "dom_disk":
+ get_disk_cmd = "domblklist %s" % params.get("new_vm_name")
+ disk_list = libvirt_vm.virsh_cmd(get_disk_cmd).stdout.split('\n')
+ logging.info("Disks on %s:\n%s", params.get("new_vm_name"), disk_list)
+ disk_path = ""
+ for disk in disk_list:
+ disk_name = disk.split(' ')[0]
+ if re.search("[v|h|s]da", disk_name):
+ disk_path = disk.split(' ')[-1]
+ break
+ params["dom_disk"] = disk_path.strip()
+ logging.info("disk to edit:%s", params["dom_disk"])
+
+ #get files to edit in domain or disk
+ if file_names_type == "":
+ file_names = [""]
+ elif file_names_type == "valid_file_name":
+ file_names = ["/etc/foo"]
+ else:
+ if params.get("os_type") == "windows":
+ file_names = params.get("win_file_names").split(" ")
+ else:
+ file_names = params.get("linux_file_names").split(" ")
+
+ #try to edit files
+ for file_name in file_names:
+ cmd = virt_edit(vm_ref, file_name, params)
+ #if libvirtd_restart is true,stop libvirt now and start later
+ if libvirtd_restart == "yes":
+ libvirt_vm.libvirtd_stop()
+ time.sleep(5)
+ if addition_edit == "yes":
+ try:
+ session = aexpect.ShellSession(cmd)
+ time.sleep(15)
+ session.send('Go')
+ time.sleep(5)
+ session.send('%s' % foo_line)
+ time.sleep(5)
+ session.send('\x1b')
+ time.sleep(5)
+ session.send('ZZ')
+ time.sleep(5)
+ logging.info("virt-edit to add %s success", foo_line)
+ except Exception, e:
+ fail_flag = 1
+ logging.info("virt-edit returned invalid: add %s failed: %s",
foo_line, e)
+ else:
+ status = exec_virt_edit(cmd)
+ status_error = params.get("status_error")
+ result = result_analysis(status, status_error)
+ if result:
+ logging.info("virt-edit test pass, result is expected.")
+ else:
+ fail_flag = 1
+ logging.info("virt-edit returned is not expected.")
+ if libvirtd_restart == "yes":
+ libvirt_vm.libvirtd_start()
+ time.sleep(5)
+ if shutdown_later == "yes":
+ domain.destroy()
+
+ if params.get("use_disk_name") == "yes":
+ utils.run("rm -f %s" % params.get("disk_name"))
+
+ if fail_flag != 0:
+ clean_up(domain, undefine_vm)
+ raise error.TestFail("Test Failed.")
+ else:
+ if params.get("os_type") == "windows":
+ clean_up(domain, undefine_vm)
+ else:
+ if addition_edit == "yes":
+ domain.start()
+ time.sleep(10)
+ dom_state = domain.state()
+ oper_output = ""
+ if dom_state != "running":
+ fail_flag = 1
+ logging.info("vm guest can not be started.Test Fail.")
+ else:
+ try:
+ domain.wait_for_login(timeout=wait_time)
+ passwd = params.get("password")
+ session = virt_remote.remote_login("ssh", vm_ip,
+ "22", "root", passwd, "#")
+ time.sleep(5)
+ oper_output = session.cmd_output('cat /etc/hosts',
+ internal_timeout=10)
+ logging.info("\n%s", oper_output)
+ time.sleep(5)
+ session.cmd('cp -f /etc/hosts /etc/hosts.bk')
+ time.sleep(5)
+ session.cmd('sed -e \'/%s/d\' /etc/hosts.bk >
/etc/hosts' % foo_line)
+ time.sleep(5)
+ session.cmd('rm -f /etc/hosts.bk')
+ time.sleep(5)
+ session.close()
+ except Exception, e:
+ fail_flag = 1
+ logging.info("vm guest can not be accessed.Test
Fail:%s", e)
+ domain.destroy()
+ if not re.search(("%s" % foo_line), oper_output):
+ fail_flag = 1
+ logging.info("Edit file fail.Test Fail.")
+ clean_up(domain, undefine_vm)
+ else:
+ if undefine_vm == "yes":
+ clean_up(domain, undefine_vm)
+ if libvirtd_restart == "yes" and undefine_vm == "yes":
+ libvirt_vm.virsh_undefine(new_dom_name)
+ if fail_flag != 0:
+ raise error.TestFail("Test Failed.")
-- 1.7.1
_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest