Example #1
0
    def attach_disk_test():
        """
        Attach-disk testcase.
        1.Attch a disk to guest.
        2.Perform domblkinfo operation.
        3.Detach the disk.

        :return: Command status and output.
        """
        try:
            source_file = open(test_disk_source, 'wb')
            source_file.seek((512 * 1024 * 1024) - 1)
            source_file.write(str(0))
            source_file.close()
            virsh.attach_disk(vm_name, test_disk_source, front_dev, debug=True)
            vm_ref = vm_name
            result_source = virsh.domblkinfo(vm_ref, test_disk_source,
                                             ignore_status=True, debug=True)
            status_source = result_source.exit_status
            output_source = result_source.stdout.strip()
            if driver == "qemu":
                result_target = virsh.domblkinfo(vm_ref, front_dev,
                                                 ignore_status=True, debug=True)
                status_target = result_target.exit_status
                output_target = result_target.stdout.strip()
            else:
                status_target = 0
                output_target = "Xen doesn't support domblkinfo target!"
            virsh.detach_disk(vm_name, front_dev, debug=True)
            return status_target, output_target, status_source, output_source
        except (error.CmdError, IOError):
            return 1, "", 1, ""
Example #2
0
    def attach_disk_test():
        """
        Attach-disk testcase.
        1.Attch a disk to guest.
        2.Perform domblkinfo operation.
        3.Detach the disk.

        :return: Command status and output.
        """
        try:
            with open(test_disk_source, 'wb') as source_file:
                source_file.seek((512 * 1024 * 1024) - 1)
                source_file.write(str(0).encode())
            virsh.attach_disk(vm_name, test_disk_source, front_dev, debug=True)
            vm_ref = vm_name
            result_source = virsh.domblkinfo(vm_ref, test_disk_source,
                                             ignore_status=True, debug=True)
            status_source = result_source.exit_status
            output_source = result_source.stdout.strip()
            if driver == "qemu":
                result_target = virsh.domblkinfo(vm_ref, front_dev,
                                                 ignore_status=True, debug=True)
                status_target = result_target.exit_status
                output_target = result_target.stdout.strip()
            else:
                status_target = 0
                output_target = "Xen doesn't support domblkinfo target!"
            virsh.detach_disk(vm_name, front_dev, debug=True)
            return status_target, output_target, status_source, output_source
        except (process.CmdError, IOError):
            return 1, "", 1, ""
Example #3
0
    def attach_disk_test(test_disk_source, front_dev):
        """
        Attach-disk testcase.
        1.Attach a disk to guest.
        2.Perform domblkinfo operation.
        3.Detach the disk.

        :param: test_disk_source disk source file path.
        :param: front_dev front end device name.
        :return: Command status and output.
        """
        try:
            disk_source = test_disk_source
            front_device = front_dev
            with open(disk_source, 'wb') as source_file:
                source_file.seek((512 * 1024 * 1024) - 1)
                source_file.write(str(0).encode())
            out = virsh.attach_disk(vm_name,
                                    disk_source,
                                    front_device,
                                    debug=True)
            utils_misc.wait_for(
                lambda: (out.stdout == "Disk attached successfully"), 10)
            vm_ref = vm_name
            if "--all" in extra:
                disk_source = ""
                vm_ref = "%s %s" % (vm_name, extra)
            result_source = virsh.domblkinfo(vm_ref,
                                             disk_source,
                                             ignore_status=True,
                                             debug=True)
            status_source = result_source.exit_status
            output_source = result_source.stdout.strip()
            if driver == "qemu":
                if "--all" in extra:
                    front_device = ""
                result_target = virsh.domblkinfo(vm_ref,
                                                 front_device,
                                                 ignore_status=True,
                                                 debug=True)
                status_target = result_target.exit_status
                output_target = result_target.stdout.strip()
            else:
                status_target = 0
                output_target = "Xen doesn't support domblkinfo target!"
            front_device = front_dev
            virsh.detach_disk(vm_name, front_device, debug=True)
            return status_target, output_target, status_source, output_source
        except (process.CmdError, IOError):
            return 1, "", 1, ""
Example #4
0
def execute_statistics_command(params):
    """
    Execute statistics command

    :param params: dict, used to setup the connection
    """
    vm_name = params.get("migrate_main_vm")
    disk_type = params.get("disk_type")

    vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
    disks = vmxml.get_disk_all_by_expr('type==%s' % disk_type, 'device==disk')
    logging.debug("disks: %s", disks)
    debug_kargs = {'ignore_status': False, 'debug': True}
    for disk in list(disks.values()):
        disk_source = disk.find('source').get('dev')
        disk_target = disk.find('target').get('dev')
        logging.debug("disk_source: %s", disk_source)
        logging.debug("disk_target: %s", disk_target)
        virsh.domblkstat(vm_name, disk_target, "", **debug_kargs)
        virsh.domblkinfo(vm_name, disk_source, **debug_kargs)
        virsh.domstats(vm_name, **debug_kargs)
        virsh.dommemstat(vm_name, **debug_kargs)
def check_slice_hot_operate(vm, params, test):
    """
    Check hot operation on disk with slice attribute

    :param vm: one object representing VM
    :param params: wrapped parameters in dictionary format
    :param test: test assert object
    """
    device_target = params.get("target_dev")
    try:
        session = vm.wait_for_login()
        _, output = session.cmd_status_output(
            "dd if=/dev/zero of=/dev/%s bs=10M count=1" % device_target)
        LOG.info("Fill contents in VM:\n%s", output)
        session.close()
    except Exception as e:
        LOG.error(str(e))
        session.close()

    domstats_raw_output = virsh.domstats(vm.name,
                                         "--block",
                                         ignore_status=False,
                                         debug=True).stdout_text.strip()
    domstats_dict = translate_raw_output_into_dict(domstats_raw_output)
    domblkinfo_raw_output = virsh.domblkinfo(vm.name,
                                             device_target,
                                             ignore_status=False,
                                             debug=True).stdout_text.strip()
    domblkinfo_dict = translate_raw_output_into_dict(domblkinfo_raw_output,
                                                     default_delimiter=":")

    # Verify allocation, capacity, and physical are the same from domstats and domblkinfo
    for key in ['Allocation', 'Capacity', 'Physical']:
        if domstats_dict["block.1.%s" % key.lower()] != domblkinfo_dict[key]:
            test.fail("The domstats value: %s is not equal to domblkinfo: %s" %
                      (domstats_dict["block.1.%s" % key.lower()],
                       domblkinfo_dict[key]))
Example #6
0
def run(test, params, env):
    """
    Test command: virsh domblkinfo.
    1.Prepare test environment.
    2.Get vm's driver.
    3.According to driver perform virsh domblkinfo operation.
    4.Recover test environment.
    5.Confirm the test result.
    """
    def attach_disk_test(test_disk_source, front_dev):
        """
        Attach-disk testcase.
        1.Attch a disk to guest.
        2.Perform domblkinfo operation.
        3.Detach the disk.

        :param: test_disk_source disk source file path.
        :param: front_dev front end device name.
        :return: Command status and output.
        """
        try:
            disk_source = test_disk_source
            front_device = front_dev
            with open(disk_source, 'wb') as source_file:
                source_file.seek((512 * 1024 * 1024) - 1)
                source_file.write(str(0).encode())
            virsh.attach_disk(vm_name, disk_source, front_device, debug=True)
            vm_ref = vm_name
            if "--all" in extra:
                disk_source = ""
                vm_ref = "%s %s" % (vm_name, extra)
            result_source = virsh.domblkinfo(vm_ref,
                                             disk_source,
                                             ignore_status=True,
                                             debug=True)
            status_source = result_source.exit_status
            output_source = result_source.stdout.strip()
            if driver == "qemu":
                if "--all" in extra:
                    front_device = ""
                result_target = virsh.domblkinfo(vm_ref,
                                                 front_device,
                                                 ignore_status=True,
                                                 debug=True)
                status_target = result_target.exit_status
                output_target = result_target.stdout.strip()
            else:
                status_target = 0
                output_target = "Xen doesn't support domblkinfo target!"
            front_device = front_dev
            virsh.detach_disk(vm_name, front_device, debug=True)
            return status_target, output_target, status_source, output_source
        except (process.CmdError, IOError):
            return 1, "", 1, ""

    def check_disk_info():
        """
        Ckeck virsh domblkinfo output.
        """
        if driver == "qemu" and output_source.strip() != output_target.strip():
            test.fail("Command domblkinfo target/source"
                      " got different information!")
        if output_source != "":
            lines = output_source.splitlines()
            if "--human" in extra and not any(
                    re.findall(r'GiB|MiB', lines[0], re.IGNORECASE)):
                test.fail("Command domblkinfo human output is wrong")
            if "--all" in extra:
                blocklist = vm_xml.VMXML.get_disk_blk(vm_name)
                if not all(
                        re.findall(r''.join(block), output_source,
                                   re.IGNORECASE) for block in blocklist):
                    test.fail("Command domblkinfo --all output is wrong")
                return
            if disk_size_check:
                capacity_cols = lines[0].split(":")
                if "--human" in extra:
                    size = float(capacity_cols[1].strip().split(" ")[0])
                else:
                    size = int(capacity_cols[1].strip())
                if disk_size != size:
                    test.fail("Command domblkinfo output is wrong! "
                              "'%d' != '%d'" % (disk_size, size))
        else:
            test.fail("Command domblkinfo has no output!")

    vm_name = params.get("main_vm")
    vm = env.get_vm(vm_name)

    # Get all parameters from configuration.
    vm_ref = params.get("domblkinfo_vm_ref")
    device = params.get("domblkinfo_device", "yes")
    front_dev = params.get("domblkinfo_front_dev", "vdd")
    extra = params.get("domblkinfo_extra", "")
    status_error = params.get("status_error", "no")
    test_attach_disk = os.path.join(test.virtdir, "tmp.img")

    domid = vm.get_id()
    domuuid = vm.get_uuid()
    driver = virsh.driver()

    blklist = vm_xml.VMXML.get_disk_blk(vm_name)
    sourcelist = vm_xml.VMXML.get_disk_source(vm_name)
    test_disk_target = blklist[0]
    test_disk_source = sourcelist[0].find('source').get('file')
    test_disk_format = sourcelist[0].find('driver').get('type')

    disk_size_check = False
    if test_disk_format == "raw":
        disk_size_check = True
    if device == "no":
        test_disk_target = ""
        test_disk_source = ""

    if vm_ref == "id":
        vm_ref = domid
    elif vm_ref == "hex_id":
        vm_ref = hex(int(domid))
    elif vm_ref.find("invalid") != -1:
        vm_ref = params.get(vm_ref)
    elif vm_ref == "name":
        vm_ref = "%s %s" % (vm_name, extra)
    elif vm_ref == "uuid":
        vm_ref = domuuid

    if any(re.findall(
            r'--all|--human', extra,
            re.IGNORECASE)) and not libvirt_version.version_compare(4, 5, 0):
        test.cancel(
            "--all and --human options are supported until libvirt 4.5.0 version"
        )

    if vm_ref == "test_attach_disk":
        test_disk_source = test_attach_disk
        disk_size_check = True
        (status_target, output_target, status_source,
         output_source) = attach_disk_test(test_disk_source, front_dev)
    else:
        result_source = virsh.domblkinfo(vm_ref,
                                         test_disk_source,
                                         ignore_status=True,
                                         debug=True)
        status_source = result_source.exit_status
        output_source = result_source.stdout.strip()
        if driver == "qemu":
            result_target = virsh.domblkinfo(vm_ref,
                                             test_disk_target,
                                             ignore_status=True,
                                             debug=True)
            status_target = result_target.exit_status
            output_target = result_target.stdout.strip()
        else:
            status_target = 0
            output_target = "xen doesn't support domblkinfo target!"
    disk_size = 0
    if os.path.exists(test_disk_source):
        disk_size = os.path.getsize(test_disk_source)

    # Recover enviremont
    if os.path.exists(test_attach_disk):
        os.remove(test_attach_disk)

    # Check status_error
    if status_error == "yes":
        if status_target == 0 or status_source == 0:
            test.fail("Run successfully with wrong command!")
    elif status_error == "no":
        if status_target != 0 or status_source != 0:
            test.fail("Run failed with right command")
        # Check source information.
        check_disk_info()
    else:
        test.fail("The status_error must be 'yes' or 'no'!")
Example #7
0
def run_virsh_domblkinfo(test, params, env):
    """
    Test command: virsh domblkinfo.
    1.Prepare test environment.
    2.Get vm's driver.
    3.According to driver perform virsh domblkinfo operation.
    4.Recover test environment.
    5.Confirm the test result.
    """
    def attach_disk_test():
        """
        Attach-disk testcase.
        1.Attch a disk to guest.
        2.Perform domblkinfo operation.
        3.Detach the disk.

        :return: Command status and output.
        """
        try:
            source_file = open(test_disk_source, 'wb')
            source_file.seek((512 * 1024 * 1024) - 1)
            source_file.write(str(0))
            source_file.close()
            virsh.attach_disk(vm_name, test_disk_source, front_dev, debug=True)
            vm_ref = vm_name
            result_source = virsh.domblkinfo(vm_ref,
                                             test_disk_source,
                                             ignore_status=True,
                                             debug=True)
            status_source = result_source.exit_status
            output_source = result_source.stdout.strip()
            if driver == "qemu":
                result_target = virsh.domblkinfo(vm_ref,
                                                 front_dev,
                                                 ignore_status=True,
                                                 debug=True)
                status_target = result_target.exit_status
                output_target = result_target.stdout.strip()
            else:
                status_target = 0
                output_target = "Xen doesn't support domblkinfo target!"
            virsh.detach_disk(vm_name, front_dev, debug=True)
            return status_target, output_target, status_source, output_source
        except (error.CmdError, IOError):
            return 1, "", 1, ""

    def check_disk_info():
        """
        Ckeck virsh domblkinfo output.
        """
        if driver == "qemu" and output_source.strip() != output_target.strip():
            raise error.TestFail("Command domblkinfo target/source"
                                 " got different information!")
        if output_source != "":
            lines = output_source.splitlines()
            capacity_cols = lines[0].split(":")
            size = int(capacity_cols[1].strip())
            if disk_size != size and disk_size_check:
                raise error.TestFail("Command domblkinfo output is wrong! "
                                     "'%d' != '%d'" % (disk_size, size))
        else:
            raise error.TestFail("Command domblkinfo has no output!")

    vm_name = params.get("main_vm")
    vm = env.get_vm(vm_name)

    # Get all parameters from configuration.
    vm_ref = params.get("domblkinfo_vm_ref")
    device = params.get("domblkinfo_device", "yes")
    front_dev = params.get("domblkinfo_front_dev", "vdd")
    extra = params.get("domblkinfo_extra", "")
    status_error = params.get("status_error", "no")
    test_attach_disk = os.path.join(test.virtdir, "tmp.img")

    domid = vm.get_id()
    domuuid = vm.get_uuid()
    driver = virsh.driver()

    blklist = vm_xml.VMXML.get_disk_blk(vm_name)
    sourcelist = vm_xml.VMXML.get_disk_source(vm_name)
    test_disk_target = blklist[0]
    test_disk_source = sourcelist[0].find('source').get('file')
    test_disk_format = sourcelist[0].find('driver').get('type')

    disk_size_check = False
    if test_disk_format == "raw":
        disk_size_check = True
    if device == "no":
        test_disk_target = ""
        test_disk_source = ""

    if vm_ref == "id":
        vm_ref = domid
    elif vm_ref == "hex_id":
        vm_ref = hex(int(domid))
    elif vm_ref.find("invalid") != -1:
        vm_ref = params.get(vm_ref)
    elif vm_ref == "name":
        vm_ref = "%s %s" % (vm_name, extra)
    elif vm_ref == "uuid":
        vm_ref = domuuid

    if vm_ref == "test_attach_disk":
        test_disk_source = test_attach_disk
        disk_size_check = True
        (status_target, output_target, status_source,
         output_source) = attach_disk_test()
    else:
        result_source = virsh.domblkinfo(vm_ref,
                                         test_disk_source,
                                         ignore_status=True,
                                         debug=True)
        status_source = result_source.exit_status
        output_source = result_source.stdout.strip()
        if driver == "qemu":
            result_target = virsh.domblkinfo(vm_ref,
                                             test_disk_target,
                                             ignore_status=True,
                                             debug=True)
            status_target = result_target.exit_status
            output_target = result_target.stdout.strip()
        else:
            status_target = 0
            output_target = "xen doesn't support domblkinfo target!"
    disk_size = 0
    if os.path.exists(test_disk_source):
        disk_size = os.path.getsize(test_disk_source)

    # Recover enviremont
    if os.path.exists(test_attach_disk):
        os.remove(test_attach_disk)

    # Check status_error
    if status_error == "yes":
        if status_target == 0 or status_source == 0:
            raise error.TestFail("Run successfully with wrong command!")
    elif status_error == "no":
        if status_target != 0 or status_source != 0:
            raise error.TestFail("Run failed with right command")
        # Check source information.
        check_disk_info()
    else:
        raise error.TestFail("The status_error must be 'yes' or 'no'!")
Example #8
0
def run(test, params, env):
    """
    Test command: virsh domblklist.
    1.Prepare test environment.
    2.Run domblklist and check
    3.Do attach disk and rerun domblklist with check
    4.Clean test environment.
    """
    def domblklist_test():
        """
        Run domblklist and check result, raise error if check fail.
        """
        disk_info_list = []
        output_disk_info = {}
        output_disk_info_list = []
        result = virsh.domblklist(vm_ref,
                                  options,
                                  ignore_status=True,
                                  debug=True)
        status = result.exit_status
        output = result.stdout.strip()

        # Check status_error
        if status_error == "yes":
            if status == 0:
                test.fail("Run successfully with wrong command!")
        elif status_error == "no":
            if status == 1:
                test.fail("Run failed with right command")
            # Check disk information.
            disk_info = get_disk_info(vm_name, options)
            logging.debug("The disk info dict from xml is: %s" % disk_info)

            output_list = output.split('\n')
            for i in range(2, len(output_list)):
                output_disk_info[i - 2] = output_list[i].split()
            logging.debug("The disk info dict from command output is: %s" %
                          output_disk_info)

            for (k, v) in list(iteritems(output_disk_info)):
                output_disk_info_list.append(v)

            if "--details" in options:
                for (k, v) in list(iteritems(disk_info)):
                    disk_info_list.append(v)
            else:
                for (k, v) in list(iteritems(disk_info)):
                    disk_info_list.append(v[2:])

            disk_info_list.sort()
            logging.debug("The disk info list from xml is: %s" %
                          disk_info_list)
            output_disk_info_list.sort()
            logging.debug("The disk info list from command output is: %s" %
                          output_disk_info_list)

            if disk_info_list != output_disk_info_list:
                test.fail("The output did not match with disk"
                          " info from xml")

    vm_name = params.get("main_vm")
    vm = env.get_vm(vm_name)

    # Get all parameters from configuration.
    vm_ref = params.get("domblklist_vm_ref")
    options = params.get("domblklist_options", "")
    info_options = params.get("info_options", "")
    status_error = params.get("status_error", "no")
    front_dev = params.get("domblkinfo_front_dev", "vdd")
    test_attach_disk = os.path.join(test.virtdir, "tmp.img")
    domblkinfo = params.get("domblkinfo", "no")
    extra = ""

    domid = vm.get_id()
    domuuid = vm.get_uuid()
    vm_state = vm.state()

    if vm_ref == "id":
        vm_ref = domid
    elif vm_ref == "hex_id":
        vm_ref = hex(int(domid))
    elif vm_ref.find("invalid") != -1:
        vm_ref = params.get(vm_ref)
    elif vm_ref == "name":
        vm_ref = vm_name
    elif vm_ref == "uuid":
        vm_ref = domuuid

    # run domblklist and check
    domblklist_test()

    # Test domblkinfo function as well
    if domblkinfo == "yes":
        ret = virsh.domblklist(vm_ref, options, ignore_status=True, debug=True)
        target_disks = re.findall(r"[v,s]d[a-z]", ret.stdout)
        if info_options == "":
            check_list = ["Capacity", "Allocation", "Physical"]
            ret2 = virsh.domblkinfo(vm_ref, target_disks[0])
        elif info_options == "--human":
            check_list = ["Capacity", "Allocation", "Physical", "GiB"]
            cmd = "virsh domblkinfo %s %s %s" % (vm_ref, target_disks[0],
                                                 info_options)
            ret2 = process.run(cmd, shell=True, ignore_status=True)
        for check in check_list:
            if not re.search(check, results_stdout_52lts(ret2)):
                test.fail("Cmd domblkinfo run failed")

    if status_error == "no":
        try:
            # attach disk and check
            with open(test_attach_disk, 'wb') as source_file:
                source_file.seek((512 * 1024 * 1024) - 1)
                source_file.write(str(0).encode())
            # since bug 1049529, --config will work with detach when
            # domain is running, so change it back using --config here
            if "--inactive" in options or vm_state == "shut off":
                extra = "--config"
            virsh.attach_disk(vm_name,
                              test_attach_disk,
                              front_dev,
                              extra,
                              debug=True)
            domblklist_test()
        finally:
            virsh.detach_disk(vm_name, front_dev, extra, debug=True)
            if os.path.exists(test_attach_disk):
                os.remove(test_attach_disk)
Example #9
0
def run(test, params, env):
    """
    Test command: virsh domblkinfo.
    1.Prepare test environment.
    2.Get vm's driver.
    3.According to driver perform virsh domblkinfo operation.
    4.Recover test environment.
    5.Confirm the test result.
    """

    def attach_disk_test():
        """
        Attach-disk testcase.
        1.Attch a disk to guest.
        2.Perform domblkinfo operation.
        3.Detach the disk.

        :return: Command status and output.
        """
        try:
            source_file = open(test_disk_source, 'wb')
            source_file.seek((512 * 1024 * 1024) - 1)
            source_file.write(str(0))
            source_file.close()
            virsh.attach_disk(vm_name, test_disk_source, front_dev, debug=True)
            vm_ref = vm_name
            result_source = virsh.domblkinfo(vm_ref, test_disk_source,
                                             ignore_status=True, debug=True)
            status_source = result_source.exit_status
            output_source = result_source.stdout.strip()
            if driver == "qemu":
                result_target = virsh.domblkinfo(vm_ref, front_dev,
                                                 ignore_status=True, debug=True)
                status_target = result_target.exit_status
                output_target = result_target.stdout.strip()
            else:
                status_target = 0
                output_target = "Xen doesn't support domblkinfo target!"
            virsh.detach_disk(vm_name, front_dev, debug=True)
            return status_target, output_target, status_source, output_source
        except (error.CmdError, IOError):
            return 1, "", 1, ""

    def check_disk_info():
        """
        Ckeck virsh domblkinfo output.
        """
        if driver == "qemu" and output_source.strip() != output_target.strip():
            raise error.TestFail("Command domblkinfo target/source"
                                 " got different information!")
        if output_source != "":
            lines = output_source.splitlines()
            capacity_cols = lines[0].split(":")
            size = int(capacity_cols[1].strip())
            if disk_size != size and disk_size_check:
                raise error.TestFail("Command domblkinfo output is wrong! "
                                     "'%d' != '%d'" % (disk_size, size))
        else:
            raise error.TestFail("Command domblkinfo has no output!")

    vm_name = params.get("main_vm")
    vm = env.get_vm(vm_name)

    # Get all parameters from configuration.
    vm_ref = params.get("domblkinfo_vm_ref")
    device = params.get("domblkinfo_device", "yes")
    front_dev = params.get("domblkinfo_front_dev", "vdd")
    extra = params.get("domblkinfo_extra", "")
    status_error = params.get("status_error", "no")
    test_attach_disk = os.path.join(test.virtdir, "tmp.img")

    domid = vm.get_id()
    domuuid = vm.get_uuid()
    driver = virsh.driver()

    blklist = vm_xml.VMXML.get_disk_blk(vm_name)
    sourcelist = vm_xml.VMXML.get_disk_source(vm_name)
    test_disk_target = blklist[0]
    test_disk_source = sourcelist[0].find('source').get('file')
    test_disk_format = sourcelist[0].find('driver').get('type')

    disk_size_check = False
    if test_disk_format == "raw":
        disk_size_check = True
    if device == "no":
        test_disk_target = ""
        test_disk_source = ""

    if vm_ref == "id":
        vm_ref = domid
    elif vm_ref == "hex_id":
        vm_ref = hex(int(domid))
    elif vm_ref.find("invalid") != -1:
        vm_ref = params.get(vm_ref)
    elif vm_ref == "name":
        vm_ref = "%s %s" % (vm_name, extra)
    elif vm_ref == "uuid":
        vm_ref = domuuid

    if vm_ref == "test_attach_disk":
        test_disk_source = test_attach_disk
        disk_size_check = True
        (status_target, output_target,
         status_source, output_source) = attach_disk_test()
    else:
        result_source = virsh.domblkinfo(vm_ref, test_disk_source,
                                         ignore_status=True, debug=True)
        status_source = result_source.exit_status
        output_source = result_source.stdout.strip()
        if driver == "qemu":
            result_target = virsh.domblkinfo(vm_ref, test_disk_target,
                                             ignore_status=True, debug=True)
            status_target = result_target.exit_status
            output_target = result_target.stdout.strip()
        else:
            status_target = 0
            output_target = "xen doesn't support domblkinfo target!"
    disk_size = 0
    if os.path.exists(test_disk_source):
        disk_size = os.path.getsize(test_disk_source)

    # Recover enviremont
    if os.path.exists(test_attach_disk):
        os.remove(test_attach_disk)

    # Check status_error
    if status_error == "yes":
        if status_target == 0 or status_source == 0:
            raise error.TestFail("Run successfully with wrong command!")
    elif status_error == "no":
        if status_target != 0 or status_source != 0:
            raise error.TestFail("Run failed with right command")
        # Check source information.
        check_disk_info()
    else:
        raise error.TestFail("The status_error must be 'yes' or 'no'!")
Example #10
0
def run(test, params, env):
    """
    Test command: virsh domblklist.
    1.Prepare test environment.
    2.Run domblklist and check
    3.Do attach disk and rerun domblklist with check
    4.Clean test environment.
    """

    def domblklist_test():
        """
        Run domblklist and check result, raise error if check fail.
        """
        disk_info_list = []
        output_disk_info = {}
        output_disk_info_list = []
        result = virsh.domblklist(vm_ref, options,
                                  ignore_status=True, debug=True)
        status = result.exit_status
        output = result.stdout.strip()

        # Check status_error
        if status_error == "yes":
            if status == 0:
                test.fail("Run successfully with wrong command!")
        elif status_error == "no":
            if status == 1:
                test.fail("Run failed with right command")
            # Check disk information.
            disk_info = get_disk_info(vm_name, options)
            logging.debug("The disk info dict from xml is: %s" % disk_info)

            output_list = output.split('\n')
            for i in range(2, len(output_list)):
                output_disk_info[i - 2] = output_list[i].split()
            logging.debug("The disk info dict from command output is: %s"
                          % output_disk_info)

            for (k, v) in list(iteritems(output_disk_info)):
                output_disk_info_list.append(v)

            if "--details" in options:
                for (k, v) in list(iteritems(disk_info)):
                    disk_info_list.append(v)
            else:
                for (k, v) in list(iteritems(disk_info)):
                    disk_info_list.append(v[2:])

            disk_info_list.sort()
            logging.debug("The disk info list from xml is: %s" % disk_info_list)
            output_disk_info_list.sort()
            logging.debug("The disk info list from command output is: %s"
                          % output_disk_info_list)

            if disk_info_list != output_disk_info_list:
                test.fail("The output did not match with disk"
                          " info from xml")

    vm_name = params.get("main_vm")
    vm = env.get_vm(vm_name)

    # Get all parameters from configuration.
    vm_ref = params.get("domblklist_vm_ref")
    options = params.get("domblklist_options", "")
    info_options = params.get("info_options", "")
    status_error = params.get("status_error", "no")
    front_dev = params.get("domblkinfo_front_dev", "vdd")
    test_attach_disk = os.path.join(test.virtdir, "tmp.img")
    domblkinfo = params.get("domblkinfo", "no")
    extra = ""

    domid = vm.get_id()
    domuuid = vm.get_uuid()
    vm_state = vm.state()

    if vm_ref == "id":
        vm_ref = domid
    elif vm_ref == "hex_id":
        vm_ref = hex(int(domid))
    elif vm_ref.find("invalid") != -1:
        vm_ref = params.get(vm_ref)
    elif vm_ref == "name":
        vm_ref = vm_name
    elif vm_ref == "uuid":
        vm_ref = domuuid

    # run domblklist and check
    domblklist_test()

    # Test domblkinfo function as well
    if domblkinfo == "yes":
        ret = virsh.domblklist(vm_ref, options,
                               ignore_status=True, debug=True)
        target_disks = re.findall(r"[v,s]d[a-z]", ret.stdout)
        if info_options == "":
            check_list = ["Capacity", "Allocation", "Physical"]
            ret2 = virsh.domblkinfo(vm_ref, target_disks[0])
        elif info_options == "--human":
            check_list = ["Capacity", "Allocation", "Physical", "GiB"]
            cmd = "virsh domblkinfo %s %s %s" % (vm_ref, target_disks[0], info_options)
            ret2 = process.run(cmd, shell=True, ignore_status=True)
        for check in check_list:
            if not re.search(check, results_stdout_52lts(ret2)):
                test.fail("Cmd domblkinfo run failed")

    if status_error == "no":
        try:
            # attach disk and check
            with open(test_attach_disk, 'wb') as source_file:
                source_file.seek((512 * 1024 * 1024) - 1)
                source_file.write(str(0).encode())
            # since bug 1049529, --config will work with detach when
            # domain is running, so change it back using --config here
            if "--inactive" in options or vm_state == "shut off":
                extra = "--config"
            virsh.attach_disk(vm_name, test_attach_disk, front_dev, extra,
                              debug=True)
            domblklist_test()
        finally:
            virsh.detach_disk(vm_name, front_dev, extra, debug=True)
            if os.path.exists(test_attach_disk):
                os.remove(test_attach_disk)