Beispiel #1
0
def secret_validate(file=None, **virsh_dargs):
    """
    Test for schema secret
    """
    tmp_dir = data_dir.get_tmp_dir()
    volume_path = os.path.join(tmp_dir, "secret_volume")
    ephemeral = "no"
    private = "no"
    secret_xml_obj = SecretXML(ephemeral, private)

    status, uuid = commands.getstatusoutput("uuidgen")
    if status:
        raise error.TestNAError("Failed to generate valid uuid")

    secret_xml_obj.uuid = uuid
    secret_xml_obj.volume = volume_path
    secret_xml_obj.usage = "volume"

    secret_obj_xmlfile = os.path.join(SECRET_DIR, uuid + ".xml")
    cmd_result = virsh.secret_define(secret_xml_obj.xml, debug=True)

    cmd_result = virsh.secret_list(**virsh_dargs)
    libvirt.check_exit_status(cmd_result)
    try:
        uuid = re.findall(r"(\S+)\ +(\S+)[\ +\n]",
                          str(cmd_result.stdout))[1][0]
    except IndexError:
        raise error.TestError("Fail to get secret uuid")

    if uuid:
        try:
            virsh.secret_dumpxml(uuid, to_file=file, **virsh_dargs)
        except error.CmdError, e:
            raise error.TestError(str(e))
def secret_validate(file=None, **virsh_dargs):
    """
    Test for schema secret
    """
    tmp_dir = data_dir.get_tmp_dir()
    volume_path = os.path.join(tmp_dir, "secret_volume")
    ephemeral = "no"
    private = "no"
    secret_xml_obj = SecretXML(ephemeral, private)

    status, uuid = commands.getstatusoutput("uuidgen")
    if status:
        raise error.TestNAError("Failed to generate valid uuid")

    secret_xml_obj.uuid = uuid
    secret_xml_obj.volume = volume_path
    secret_xml_obj.usage = "volume"

    secret_obj_xmlfile = os.path.join(SECRET_DIR, uuid + ".xml")
    cmd_result = virsh.secret_define(secret_xml_obj.xml, debug=True)

    cmd_result = virsh.secret_list(**virsh_dargs)
    libvirt.check_exit_status(cmd_result)
    try:
        uuid = re.findall(r"(\S+)\ +(\S+)[\ +\n]", str(cmd_result.stdout))[1][0]
    except IndexError:
        raise error.TestError("Fail to get secret uuid")

    if uuid:
        try:
            virsh.secret_dumpxml(uuid, to_file=file, **virsh_dargs)
        except error.CmdError, e:
            raise error.TestError(str(e))
def secret_validate(file=None, **virsh_dargs):
    """
    Test for schema secret
    """
    cmd_result = virsh.secret_list(**virsh_dargs)
    libvirt.check_exit_status(cmd_result)
    try:
        uuid = re.findall(r"(\S+)\ +(\S+)[\ +\n]", str(cmd_result.stdout))[1][0]
    except IndexError:
        raise error.TestError("Fail to get secret uuid")

    if uuid:
        try:
            virsh.secret_dumpxml(uuid, to_file=file, **virsh_dargs)
        except error.CmdError, e:
            raise error.TestError(str(e))
def secret_validate(test, file=None, **virsh_dargs):
    """
    Test for schema secret
    """
    cmd_result = virsh.secret_list(**virsh_dargs)
    libvirt.check_exit_status(cmd_result)
    try:
        uuid = re.findall(r"(\S+)\ +(\S+)", str(cmd_result.stdout.strip()))[1][0]
    except IndexError:
        test.error("Fail to get secret uuid")

    if uuid:
        try:
            virsh.secret_dumpxml(uuid, to_file=file, **virsh_dargs)
        except process.CmdError as e:
            test.error(str(e))
def run(test, params, env):
    """
    Test command: secret-dumpxml <secret>

    Output attributes of a secret as an XML dump to stdout.
    """

    # MAIN TEST CODE ###
    # Process cartesian parameters
    status_error = ("yes" == params.get("status_error", "no"))
    secret_ref = params.get("secret_ref")

    if secret_ref == "secret_valid_uuid":
        # Generate valid uuid
        cmd = "uuidgen"
        status, uuid = commands.getstatusoutput(cmd)
        if status:
            raise error.TestNAError("Failed to generate valid uuid")

    # Get a full path of tmpfile, the tmpfile need not exist
    tmp_dir = data_dir.get_tmp_dir()
    volume_path = os.path.join(tmp_dir, "secret_volume")

    secret_xml = """
<secret ephemeral='no' private='yes'>
  <uuid>%s</uuid>
  <usage type='volume'>
    <volume>%s</volume>
  </usage>
</secret>
""" % (uuid, volume_path)

    # Write secret xml into a tmpfile
    tmp_file = tempfile.NamedTemporaryFile(prefix=("secret_xml_"),
                                           dir=tmp_dir)
    xmlfile = tmp_file.name
    tmp_file.close()

    fd = open(xmlfile, 'w')
    fd.write(secret_xml)
    fd.close()

    try:
        virsh.secret_define(xmlfile, debug=True)

        cmd_result = virsh.secret_dumpxml(uuid, debug=True)
        output = cmd_result.stdout.strip()
        if not status_error and cmd_result.exit_status:
            raise error.TestFail("Dumping the xml of secret object failed")

        match_string = "<uuid>%s</uuid>" % uuid
        if not re.search(match_string, output):
            raise error.TestFail("The secret xml is not valid")
    finally:
        #Cleanup
        virsh.secret_undefine(uuid, debug=True)

        if os.path.exists(xmlfile):
            os.remove(xmlfile)
def secret_validate(test, secret_volume, file=None, **virsh_dargs):
    """
    Test for schema secret
    """
    # Clean up dirty secrets in test environments if there are.
    libvirt_secret.clean_up_secrets()
    sec_params = {
        "sec_usage": "volume",
        "sec_volume": secret_volume,
        "sec_desc": "Test for schema secret."
    }
    sec_uuid = libvirt.create_secret(sec_params)
    if sec_uuid:
        try:
            virsh.secret_dumpxml(sec_uuid, to_file=file, **virsh_dargs)
        except process.CmdError as e:
            test.error(str(e))
def secret_validate(test, file=None, **virsh_dargs):
    """
    Test for schema secret
    """
    cmd_result = virsh.secret_list(**virsh_dargs)
    libvirt.check_exit_status(cmd_result)
    try:
        uuid = re.findall(r"(\S+)\ +(\S+)",
                          str(cmd_result.stdout.strip()))[1][0]
    except IndexError:
        test.error("Fail to get secret uuid")

    if uuid:
        try:
            virsh.secret_dumpxml(uuid, to_file=file, **virsh_dargs)
        except process.CmdError as e:
            test.error(str(e))
def run(test, params, env):
    """
    Test command: secret-dumpxml <secret>

    Output attributes of a secret as an XML dump to stdout.
    """

    # MAIN TEST CODE ###
    # Process cartesian parameters
    status_error = ("yes" == params.get("status_error", "no"))
    secret_ref = params.get("secret_ref")

    if secret_ref == "secret_valid_uuid":
        # Generate valid uuid
        cmd = "uuidgen"
        status, uuid = commands.getstatusoutput(cmd)
        if status:
            raise error.TestNAError("Failed to generate valid uuid")

    # Get a full path of tmpfile, the tmpfile need not exist
    tmp_dir = data_dir.get_tmp_dir()
    volume_path = os.path.join(tmp_dir, "secret_volume")

    secret_xml = """
<secret ephemeral='no' private='yes'>
  <uuid>%s</uuid>
  <usage type='volume'>
    <volume>%s</volume>
  </usage>
</secret>
""" % (uuid, volume_path)

    # Write secret xml into a tmpfile
    tmp_file = tempfile.NamedTemporaryFile(prefix=("secret_xml_"), dir=tmp_dir)
    xmlfile = tmp_file.name
    tmp_file.close()

    fd = open(xmlfile, 'w')
    fd.write(secret_xml)
    fd.close()

    try:
        virsh.secret_define(xmlfile, debug=True)

        cmd_result = virsh.secret_dumpxml(uuid, debug=True)
        output = cmd_result.stdout.strip()
        if not status_error and cmd_result.exit_status:
            raise error.TestFail("Dumping the xml of secret object failed")

        match_string = "<uuid>%s</uuid>" % uuid
        if not re.search(match_string, output):
            raise error.TestFail("The secret xml is not valid")
    finally:
        #Cleanup
        virsh.secret_undefine(uuid, debug=True)

        if os.path.exists(xmlfile):
            os.remove(xmlfile)
Beispiel #9
0
def run(test, params, env):
    """
    Test embedded qemu driver:
    1.Start guest by virt-qemu-run;
    2.Start guest with luks disk by virt-qemu-run;
    3.Options test for virt-qemu-run;
    """
    log = []

    def _logger(line):
        """
        Callback function to log embeddedqemu output.
        """
        log.append(line)

    def _check_log():
        """
        Check whether the output meets expectation
        """
        if re.findall(expected_pattern, '\n'.join(log)):
            return True
        else:
            return False

    def _confirm_terminate():
        """
        Confirm qemu process exits successfully after 'ctrl+c'
        """
        cmd = 'pgrep qemu | wc -l'
        output = int(process.run(cmd, shell=True).stdout_text.strip())
        if output == virt_qemu_run.qemu_pro_num:
            return True
        else:
            return False

    def add_luks_disk(secret_type, sec_encryption_uuid):
        # Add disk xml.
        vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)

        disk_xml = Disk(type_name=disk_type)
        disk_xml.device = "disk"
        disk_source = disk_xml.new_disk_source(**{"attrs": {'file': img_path}})
        disk_xml.driver = {"name": "qemu", "type": img_format, "cache": "none"}
        disk_xml.target = {"dev": disk_target, "bus": disk_bus}
        encryption_dict = {
            "encryption": 'luks',
            "secret": {
                "type": secret_type,
                "uuid": sec_encryption_uuid
            }
        }
        disk_source.encryption = disk_xml.new_encryption(**encryption_dict)
        disk_xml.source = disk_source
        logging.debug("disk xml is:\n%s" % disk_xml)
        vmxml.add_device(disk_xml)
        logging.debug("guest xml: %s", vmxml.xml)
        return vmxml

    arg_str = params.get("embedded_arg", "")
    expected_pattern = params.get("logger_pattern", "")
    root_dir = params.get("root_dir", "/var/tmp/virt_qemu_run_dir")
    config_path = params.get('expected_config_path', "")
    terminate_guest = params.get("terminate_guest", "no") == "yes"
    expected_secret = params.get("expected_secret", "no") == "yes"
    no_valuefile = params.get("no_valuefile", "no") == "yes"
    expected_root_dir = params.get("expected_root_dir", "no") == "yes"
    expected_help = params.get("expected_help", "no") == "yes"
    status_error = params.get("status_error", "no") == "yes"

    disk_target = params.get("disk_target", "vdd")
    disk_type = params.get("disk_type", "file")
    disk_bus = params.get("disk_bus", "virtio")
    img_path = params.get("sec_volume", "/var/lib/libvirt/images/luks.img")
    img_format = params.get("img_format", "qcow2")
    img_cap = params.get("img_cap", "1")

    secret_type = params.get("secret_type", "passphrase")
    secret_password = params.get("secret_password", "redhat")
    extra_luks_parameter = params.get("extra_parameter")
    secret_value_file = "/tmp/secret_value"
    secret_file = "/tmp/secret.xml"

    vm_name = params.get("main_vm")
    vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)

    virt_qemu_run = libvirt_embedded_qemu.EmbeddedQemuSession(
        logging_handler=_logger, )

    try:
        if expected_secret:
            libvirt.create_local_disk(disk_type="file",
                                      extra=extra_luks_parameter,
                                      path=img_path,
                                      size=int(img_cap),
                                      disk_format=img_format)
            libvirt_secret.clean_up_secrets()
            sec_params = {
                "sec_usage": "volume",
                "sec_volume": img_path,
                "sec_desc": "Secret for volume."
            }
            sec_uuid = libvirt.create_secret(sec_params)
            if sec_uuid:
                try:
                    virsh.secret_dumpxml(sec_uuid, to_file=secret_file)
                except process.CmdError as e:
                    test.error(str(e))

            process.run("echo -n %s > %s" %
                        (secret_password, secret_value_file),
                        shell=True)
            vmxml = add_luks_disk(secret_type, sec_uuid)
            if not no_valuefile:
                arg_str += ' -s %s,%s' % (secret_file, secret_value_file)
            else:
                arg_str += ' -s %s' % secret_file
        if expected_root_dir:
            arg_str += ' -r %s' % (root_dir)
        if not expected_help:
            arg_str += ' %s' % (vmxml.xml)

        logging.debug("Start virt-qemu-run process with options: %s", arg_str)
        virt_qemu_run.start(arg_str=arg_str, wait_for_working="yes")

        if expected_pattern:
            if not utils_misc.wait_for(lambda: _check_log(), 60, 10):
                test.fail('Expected output %s not found in log: \n%s' %
                          (expected_pattern, '\n'.join(log)))

        if log:
            logging.debug("virt-qemu-run log:")
            for line in log:
                logging.debug(line)

        if not expected_help and not status_error:
            if expected_pattern != "domain type":
                expected_pattern = "guest running"
                if not utils_misc.wait_for(lambda: _check_log(), 60, 20):
                    test.fail("Start qemu process unexpected failed")

    finally:
        virt_qemu_run.tail.sendcontrol('c')
        if not utils_misc.wait_for(lambda: _confirm_terminate, 60, 10):
            test.fail("process did not exit successfully")
        virt_qemu_run.exit()
        process.run('pkill -9 qemu-kvm', ignore_status=True, shell=True)
        libvirt_secret.clean_up_secrets()
        if os.path.exists(secret_value_file):
            os.remove(secret_value_file)
        if os.path.exists(secret_file):
            os.remove(secret_file)
        if os.path.exists(img_path):
            os.remove(img_path)
        if os.path.exists(root_dir):
            shutil.rmtree(root_dir, ignore_errors=False)
Beispiel #10
0
 def get_info(self, name):
     infos = {}
     infos['uuid'] = name
     infos['xml'] = virsh.secret_dumpxml(name).stdout.splitlines()
     return infos
def run(test, params, env):
    """
    Test command: secret-dumpxml <secret>

    Output attributes of a secret as an XML dump to stdout.
    """

    # MAIN TEST CODE ###
    # Process cartesian parameters
    status_error = ("yes" == params.get("status_error", "no"))
    secret_ref = params.get("secret_ref")

    # acl polkit params
    uri = params.get("virsh_uri")
    unprivileged_user = params.get('unprivileged_user')
    if unprivileged_user:
        if unprivileged_user.count('EXAMPLE'):
            unprivileged_user = '******'

    if not libvirt_version.version_compare(1, 1, 1):
        if params.get('setup_libvirt_polkit') == 'yes':
            raise error.TestNAError("API acl test not supported in current"
                                    " libvirt version.")

    virsh_dargs = {'debug': True}
    if params.get('setup_libvirt_polkit') == 'yes':
        virsh_dargs['unprivileged_user'] = unprivileged_user
        virsh_dargs['uri'] = uri

    if secret_ref == "secret_valid_uuid":
        # Generate valid uuid
        cmd = "uuidgen"
        status, uuid = commands.getstatusoutput(cmd)
        if status:
            raise error.TestNAError("Failed to generate valid uuid")

    # Get a full path of tmpfile, the tmpfile need not exist
    tmp_dir = data_dir.get_tmp_dir()
    volume_path = os.path.join(tmp_dir, "secret_volume")

    secret_xml = """
<secret ephemeral='no' private='yes'>
  <uuid>%s</uuid>
  <usage type='volume'>
    <volume>%s</volume>
  </usage>
</secret>
""" % (uuid, volume_path)

    # Write secret xml into a tmpfile
    tmp_file = tempfile.NamedTemporaryFile(prefix=("secret_xml_"), dir=tmp_dir)
    xmlfile = tmp_file.name
    tmp_file.close()

    fd = open(xmlfile, 'w')
    fd.write(secret_xml)
    fd.close()

    try:
        virsh.secret_define(xmlfile, debug=True)

        cmd_result = virsh.secret_dumpxml(uuid, **virsh_dargs)
        output = cmd_result.stdout.strip()
        if not status_error and cmd_result.exit_status:
            raise error.TestFail("Dumping the xml of secret object failed")

        match_string = "<uuid>%s</uuid>" % uuid
        if not re.search(match_string, output):
            raise error.TestFail("The secret xml is not valid")
    finally:
        #Cleanup
        virsh.secret_undefine(uuid, debug=True)

        if os.path.exists(xmlfile):
            os.remove(xmlfile)
def run(test, params, env):
    """
    Test command: secret-dumpxml <secret>

    Output attributes of a secret as an XML dump to stdout.
    """

    # MAIN TEST CODE ###
    # Process cartesian parameters
    status_error = ("yes" == params.get("status_error", "no"))
    secret_ref = params.get("secret_ref")

    # acl polkit params
    uri = params.get("virsh_uri")
    unprivileged_user = params.get('unprivileged_user')
    if unprivileged_user:
        if unprivileged_user.count('EXAMPLE'):
            unprivileged_user = '******'

    if not libvirt_version.version_compare(1, 1, 1):
        if params.get('setup_libvirt_polkit') == 'yes':
            raise error.TestNAError("API acl test not supported in current"
                                    " libvirt version.")

    virsh_dargs = {'debug': True}
    if params.get('setup_libvirt_polkit') == 'yes':
        virsh_dargs['unprivileged_user'] = unprivileged_user
        virsh_dargs['uri'] = uri

    if secret_ref == "secret_valid_uuid":
        # Generate valid uuid
        cmd = "uuidgen"
        status, uuid = commands.getstatusoutput(cmd)
        if status:
            raise error.TestNAError("Failed to generate valid uuid")

    # Get a full path of tmpfile, the tmpfile need not exist
    tmp_dir = data_dir.get_tmp_dir()
    volume_path = os.path.join(tmp_dir, "secret_volume")

    secret_xml = """
<secret ephemeral='no' private='yes'>
  <uuid>%s</uuid>
  <usage type='volume'>
    <volume>%s</volume>
  </usage>
</secret>
""" % (uuid, volume_path)

    # Write secret xml into a tmpfile
    tmp_file = tempfile.NamedTemporaryFile(prefix=("secret_xml_"),
                                           dir=tmp_dir)
    xmlfile = tmp_file.name
    tmp_file.close()

    fd = open(xmlfile, 'w')
    fd.write(secret_xml)
    fd.close()

    try:
        virsh.secret_define(xmlfile, debug=True)

        cmd_result = virsh.secret_dumpxml(uuid, **virsh_dargs)
        output = cmd_result.stdout.strip()
        if not status_error and cmd_result.exit_status:
            raise error.TestFail("Dumping the xml of secret object failed")

        match_string = "<uuid>%s</uuid>" % uuid
        if not re.search(match_string, output):
            raise error.TestFail("The secret xml is not valid")
    finally:
        #Cleanup
        virsh.secret_undefine(uuid, debug=True)

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