Signed-off-by: Yu Mingfei<[email protected]>
---
 client/tests/libvirt/tests/virt_edit.py |  230 +++++++++++++++++++++++++++++++
 1 files changed, 230 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..15dc61e
--- /dev/null
+++ b/client/tests/libvirt/tests/virt_edit.py
@@ -0,0 +1,230 @@
+import logging, time, re
+from autotest.client.shared import utils, error
+from autotest.client.virt import remote, libvirt_vm, aexpect, libvirt_xml
+
+
+def cmd_virt_edit(option_vm="", option_file="", prefix_option="", 
suffix_option=""):
+    """
+    Create command of virt-edit.
+
+    @param option_dom_disk:virt-edit object,a dom name or a disk path
+    @param option_file:the  file to edit
+    @param prefix_option:additional prefix option
+    @param suffix_option:additional suffix option
+    @return:return created cmd string
+    """
+    cmd = "virt-edit"
+    if prefix_option != "":
+        cmd += " %s" % prefix_option
+    if option_vm != "":
+        cmd += " %s" % option_vm
+    if option_file != "":
+        cmd += " %s" % option_file
+    if suffix_option != "":
+        cmd += " %s" % suffix_option
+    return cmd
+
+
+def exec_virt_edit(cmd):
+    cmd_result = utils.run(cmd, timeout=30, ignore_status=True)
+    logging.info("Status:%s", cmd_result.exit_status)
+    logging.info("Output:%s", cmd_result.stdout.strip())
+    logging.info("Error:%s", cmd_result.stderr.strip())
+    return cmd_result.exit_status
+
+
+def add_foo_line(cmd, foo_line):
+    """
+    Execute virt-edit command and add a foo_line to the end of file.
+    """
+    session = aexpect.ShellSession(cmd)
+    time.sleep(15)
+    logging.info("Output: %s", session.get_output())
+    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)
+    return True
+
+
+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, rename vm or not.
+    3)Check domain's state,shutdown vm or not.
+    4)Get virt-edit's object and create cmd to virt-edit.
+    5)Execute command virt-edit
+    6)Check 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.
+    """
+    os_type = params.get("windows", "linux")
+    vm_name = params.get("main_vm", "vm1")
+    vm = env.get_vm(params["main_vm"])
+    wait_time = int(params.get("wait_time", "30"))
+
+    #Rename vm if it is needed.
+    #virt-edit can edit a guest's disk by its name,
+    #It is necessary to verify different type name.
+    new_vm_name = params.get("new_vm_name", "")
+    escape_name = params.get("escape_name", "no")
+    if new_vm_name != "":
+        libvirt_xml.vm_rename(vm_name, new_vm_name)
+        if escape_name == "yes":
+            test_vm_name = "\\" + new_vm_name
+        else:
+            test_vm_name = new_vm_name
+        vm.name = test_vm_name
+    else:
+        test_vm_name = vm_name
+
+    #check domain ip
+    #if domain is linux and can not be connected ,end test
+    #because test will log in to check command's result.
+    if os_type != "windows":
+        if not vm.is_alive():
+            vm.start()
+        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:
+            if new_vm_name != "":
+                libvirt_xml.vm_rename(test_vm_name, vm_name)
+                vm.name = vm_name
+            raise error.TestError("Pinging VM %s has no response." % vm_name)
+
+    #All failures are stored for envionment's cleanup.
+    fail_flag = 0
+
+    vm_uuid = vm.get_uuid()
+
+    #if we need to test during vm is runing,vm_running is yes.
+    vm_running = params.get("vm_running", "no")
+    if vm_running != "yes":
+        vm.destroy()
+        vm.wait_for_shutdown(count=30)
+
+    vm_ref = params.get("vm_ref", "")
+    if vm_ref == "vm_disk":
+        get_disk_cmd = "domblklist %s" % test_vm_name
+        disk_list = libvirt_vm.virsh_cmd(get_disk_cmd).stdout.split('\n')
+        logging.info("Disks on %s:\n%s", test_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
+        vm_ref = disk_path.strip()
+        logging.info("disk to edit:%s", vm_ref)
+    elif vm_ref == "vm_name":
+        vm_ref = test_vm_name
+    elif vm_ref == "vm_uuid":
+        vm_ref = vm_uuid
+    elif vm_ref == "created_img":
+        temp_file = "%s/foo.img" % params.get("image_dir", "/tmp")
+        created_img = params.get("created_img", temp_file)
+        vm_ref = created_img
+        utils.run("dd if=/dev/zero of=%s bs=512M count=2" % created_img)
+
+    #get file to edit in domain or disk
+    file_ref = params.get("exist_file", "exist_file")
+    addition_edit = params.get("addition_edit", "no")
+    foo_line = params.get("foo_line", "#foo")
+    if file_ref == "exist_file":
+        if os_type == "windows":
+            file_ref = params.get("windows_file", "")
+        else:
+            file_ref = params.get("linux_file", "")
+
+    #Stop libvirtd if libvirtd is off.
+    libvirtd = params.get("libvirtd", "on")
+    if libvirtd == "off":
+        libvirt_vm.libvirtd_stop()
+
+    #try to edit files, and add a foo_line as you need.
+    prefix_option = params.get("prefix_option", "")
+    suffix_option = params.get("suffix_option", "")
+    vm_status_error = params.get("vm_status_error", "no")
+    file_status_error = params.get("file_status_error", "no")
+    addition_status_error = params.get("addition_status_error", "no")
+    result_good = (vm_status_error == "no") and (file_status_error == "no") \
+                             and (addition_status_error == "no")
+    cmd = cmd_virt_edit(vm_ref, file_ref, prefix_option, suffix_option)
+    if addition_edit == "yes" and result_good is True:
+        try:
+            logging.info("Virt-edit command:%s", cmd)
+            add_foo_line(cmd, foo_line)
+        except Exception, e:
+            fail_flag = 1
+            logging.info("virt-edit returned invalid, add %s failed:\n%s", 
foo_line, e)
+    else:
+        status = exec_virt_edit(cmd)
+        if status:
+            logging.info("virt-edit test pass, result is expected.")
+        else:
+            fail_flag = 1
+            logging.info("virt-edit returned is not expected.")
+
+    #Recover libvirtd if libvirtd is off.
+    if libvirtd == "off":
+        libvirt_vm.libvirtd_start()
+
+    if params.get("vm_ref") == "created_img":
+        utils.run("rm -f %s" % created_img)
+
+    if fail_flag != 0:
+        #Recover modified vm
+        if new_vm_name != "":
+            libvirt_xml.vm_rename(test_vm_name, vm_name)
+            vm.name = vm_name
+        raise error.TestFail("Command executed failed, Test Failed.")
+    else:
+        #login vm to check whether foo_line has been added if os is linux
+        if os_type == "linux" and addition_edit == "yes" and result_good is 
True:
+            vm.start()
+            cat_file = ""
+            if not vm.is_alive():
+                fail_flag = 1
+                logging.info("vm can not be started when check result.Test 
Fail.")
+            else:
+                try:
+                    vm.wait_for_login(timeout=wait_time)
+                    passwd = params.get("password")
+                    session = remote.remote_login("ssh", vm_ip,
+                                                   "22", "root", passwd, "#")
+                    time.sleep(5)
+                    cat_file = session.cmd_output('cat /etc/hosts', 
internal_timeout=10)
+                    logging.info("\n%s", cat_file)
+                    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)
+            vm.destroy()
+            if not re.search(foo_line, cat_file):
+                fail_flag = 1
+                logging.info("Edit file fail, do not find %s.Test Fail.", 
foo_line)
+
+        if new_vm_name != "":
+            libvirt_xml.vm_rename(test_vm_name, vm_name)
+            vm.name = vm_name
+        if fail_flag != 0:
+            raise error.TestFail("Check result failed,Test Failed.")
-- 1.7.1

--
Best Regards
Yu Mingfei

_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel

Reply via email to