Exemplo n.º 1
0
def run(test, params, env):
    '''
    Test the command virsh pool-create-as

    (1) Call virsh pool-create-as
    (2) Call virsh -c remote_uri pool-create-as
    (3) Call virsh pool-create-as with an unexpected option
    '''

    # Run test case
    if not params.has_key('pool_name') or not params.has_key('pool_target'):
        logging.error("Please give a 'name' and 'target'")

    pool_options = params.get('pool_options', '')

    pool_name = params.get('pool_name')
    pool_type = params.get('pool_type')
    pool_target = params.get('pool_target')

    if not os.path.isdir(pool_target):
        if os.path.isfile(pool_target):
            logging.error('<target> must be a directory')
        else:
            os.makedirs(pool_target)

    logging.info('Creating a %s type pool %s', pool_type, pool_name)
    status = virsh.pool_create_as(pool_name,
                                  pool_type,
                                  pool_target,
                                  extra=pool_options,
                                  uri=virsh.canonical_uri())

    # Check status_error
    status_error = params.get('status_error')
    if status_error == 'yes':
        if status:
            raise error.TestFail("%d not a expected command return value" %
                                 status)
        else:
            logging.info("It's an expected error")
    elif status_error == 'no':
        result = virsh.pool_info(pool_name, uri=virsh.canonical_uri())
        if result.exit_status:
            raise error.TestFail('Failed to check pool information')
        else:
            logging.info('Pool %s is running', pool_name)
        if not status:
            raise error.TestFail('%d not a expected command return value' %
                                 status)
        else:
            logging.info('Succeed to create pool %s', pool_name)
    # Clean up
    libvirt_pool = libvirt_storage.StoragePool()
    if libvirt_pool.pool_exists(pool_name):
        libvirt_pool.delete_pool(pool_name)
Exemplo n.º 2
0
def run(test, params, env):
    '''
    Test the command virsh pool-create-as

    (1) Call virsh pool-create-as
    (2) Call virsh -c remote_uri pool-create-as
    (3) Call virsh pool-create-as with an unexpected option
    '''

    # Run test case
    if not params.has_key('pool_name') or not params.has_key('pool_target'):
        logging.error("Please give a 'name' and 'target'")

    pool_options = params.get('pool_options', '')

    pool_name = params.get('pool_name')
    pool_type = params.get('pool_type')
    pool_target = params.get('pool_target')

    if not os.path.isdir(pool_target):
        if os.path.isfile(pool_target):
            logging.error('<target> must be a directory')
        else:
            os.makedirs(pool_target)

    logging.info('Creating a %s type pool %s', pool_type, pool_name)
    status = virsh.pool_create_as(pool_name, pool_type, pool_target,
                                  extra=pool_options, uri=virsh.canonical_uri())

    # Check status_error
    status_error = params.get('status_error')
    if status_error == 'yes':
        if status:
            raise error.TestFail("%d not a expected command return value"
                                 % status)
        else:
            logging.info("It's an expected error")
    elif status_error == 'no':
        result = virsh.pool_info(pool_name, uri=virsh.canonical_uri())
        if result.exit_status:
            raise error.TestFail('Failed to check pool information')
        else:
            logging.info('Pool %s is running', pool_name)
        if not status:
            raise error.TestFail('%d not a expected command return value'
                                 % status)
        else:
            logging.info('Succeed to create pool %s', pool_name)
    # Clean up
    libvirt_pool = libvirt_storage.StoragePool()
    if libvirt_pool.pool_exists(pool_name):
        libvirt_pool.delete_pool(pool_name)
Exemplo n.º 3
0
def run_virsh_uri(test, params, env):
    """
    Test the command virsh uri

    (1) Call virsh uri
    (2) Call virsh -c remote_uri uri
    (3) Call virsh uri with an unexpected option
    (4) Call virsh uri with libvirtd service stop
    """

    connect_uri = libvirt_vm.normalize_connect_uri( params.get("connect_uri",
                                                               "default") )

    option = params.get("options")
    target_uri = params.get("target_uri")
    if target_uri:
        if target_uri.count('EXAMPLE.COM'):
            raise error.TestError('target_uri configuration set to sample value')
        logging.info("The target_uri: %s", target_uri)
        cmd = "virsh -c %s uri" % target_uri
    else:
        cmd = "virsh uri %s" % option

    # Prepare libvirtd service
    check_libvirtd = params.has_key("libvirtd")
    if check_libvirtd:
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            libvirt_vm.service_libvirtd_control("stop")

    # Run test case
    logging.info("The command: %s", cmd)
    try:
        uri_test = virsh.canonical_uri(option, uri=connect_uri,
                             ignore_status=False,
                             debug=True)
        status = 0 # good
    except error.CmdError:
        status = 1 # bad
        uri_test = ''

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    # Check status_error
    status_error = params.get("status_error")
    if status_error == "yes":
        if status == 0:
            raise error.TestFail("Command: %s  succeeded "
                                 "(incorrect command)" % cmd)
        else:
            logging.info("command: %s is a expected error", cmd)
    elif status_error == "no":
        if cmp(target_uri, uri_test) != 0:
            raise error.TestFail("Virsh cmd uri %s != %s." %
                                 (uri_test, target_uri))
        if status != 0:
            raise error.TestFail("Command: %s  failed "
                                 "(correct command)" % cmd)
Exemplo n.º 4
0
def run_virsh_uri(test, params, env):
    """
    Test the command virsh uri

    (1) Call virsh uri
    (2) Call virsh -c remote_uri uri
    (3) Call virsh uri with an unexpected option
    (4) Call virsh uri with libvirtd service stop
    """

    connect_uri = libvirt_vm.normalize_connect_uri(
        params.get("connect_uri", "default"))

    option = params.get("options")
    target_uri = params.get("target_uri")
    if target_uri:
        if target_uri.count('EXAMPLE.COM'):
            raise error.TestError(
                'target_uri configuration set to sample value')
        logging.info("The target_uri: %s", target_uri)
        cmd = "virsh -c %s uri" % target_uri
    else:
        cmd = "virsh uri %s" % option

    # Prepare libvirtd service
    check_libvirtd = params.has_key("libvirtd")
    if check_libvirtd:
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            libvirt_vm.service_libvirtd_control("stop")

    # Run test case
    logging.info("The command: %s", cmd)
    try:
        uri_test = virsh.canonical_uri(option,
                                       uri=connect_uri,
                                       ignore_status=False,
                                       debug=True)
        status = 0  # good
    except error.CmdError:
        status = 1  # bad
        uri_test = ''

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    # Check status_error
    status_error = params.get("status_error")
    if status_error == "yes":
        if status == 0:
            raise error.TestFail("Command: %s  succeeded "
                                 "(incorrect command)" % cmd)
        else:
            logging.info("command: %s is a expected error", cmd)
    elif status_error == "no":
        if cmp(target_uri, uri_test) != 0:
            raise error.TestFail("Virsh cmd uri %s != %s." %
                                 (uri_test, target_uri))
        if status != 0:
            raise error.TestFail("Command: %s  failed "
                                 "(correct command)" % cmd)
Exemplo n.º 5
0
def run(test, params, env):
    """
    Test command: virsh connect.
    """
    def unix_transport_setup():
        """
        Setup a unix connect to local libvirtd.
        """
        shutil.copy(libvirtd_conf_path, libvirtd_conf_bak_path)

        with open(libvirtd_conf_path, 'r') as libvirtdconf_file:
            line_list = libvirtdconf_file.readlines()
        conf_dict = {r'auth_unix_rw\s*=': 'auth_unix_rw="none"\n', }
        for key in conf_dict:
            pattern = key
            conf_line = conf_dict[key]
            flag = False
            for index in range(len(line_list)):
                line = line_list[index]
                if not re.search(pattern, line):
                    continue
                else:
                    line_list[index] = conf_line
                    flag = True
                    break
            if not flag:
                line_list.append(conf_line)

        with open(libvirtd_conf_path, 'w') as libvirtdconf_file:
            libvirtdconf_file.writelines(line_list)

        # restart libvirtd service
        utils_libvirtd.libvirtd_restart()

    def unix_transport_recover():
        """
        Recover the libvirtd on local.
        """
        if os.path.exists(libvirtd_conf_bak_path):
            shutil.copy(libvirtd_conf_bak_path, libvirtd_conf_path)
            utils_libvirtd.libvirtd_restart()

    # get the params from subtests.
    # params for general.
    connect_arg = params.get("connect_arg", "")
    connect_opt = params.get("connect_opt", "")
    status_error = params.get("status_error", "no")

    # params for transport connect.
    local_ip = params.get("local_ip", "ENTER.YOUR.LOCAL.IP")
    local_pwd = params.get("local_pwd", "ENTER.YOUR.LOCAL.ROOT.PASSWORD")
    transport_type = params.get("connect_transport_type", "local")
    transport = params.get("connect_transport", "ssh")
    client_ip = local_ip
    client_pwd = local_pwd
    server_ip = local_ip
    server_pwd = local_pwd

    # params special for tls connect.
    server_cn = params.get("connect_server_cn", "TLSServer")
    client_cn = params.get("connect_client_cn", "TLSClient")

    # params special for tcp connect.
    tcp_port = params.get("tcp_port", '16509')

    # params special for unix transport.
    libvirtd_conf_path = '/etc/libvirt/libvirtd.conf'
    libvirtd_conf_bak_path = '%s/libvirtd.conf.bak' % data_dir.get_tmp_dir()

    # check the config
    if (connect_arg == "transport" and
            transport_type == "remote" and
            local_ip.count("ENTER")):
        test.cancel("Parameter local_ip is not configured"
                    "in remote test.")
    if (connect_arg == "transport" and
            transport_type == "remote" and
            local_pwd.count("ENTER")):
        test.cancel("Parameter local_pwd is not configured"
                    "in remote test.")
    # In Ubuntu libvirt_lxc available in /usr/lib/libvirt
    if (connect_arg.count("lxc") and
            (not (os.path.exists("/usr/libexec/libvirt_lxc") or
                  os.path.exists("/usr/lib/libvirt/libvirt_lxc")))):
        test.cancel("Connect test of lxc:/// is not suggested on "
                    "the host with no lxc driver.")
    if connect_arg.count("xen") and (not os.path.exists("/var/run/xend")):
        test.cancel("Connect test of xen:/// is not suggested on "
                    "the host with no xen driver.")
    if connect_arg.count("qemu") and (not os.path.exists("/dev/kvm")):
        test.cancel("Connect test of qemu:/// is not suggested"
                    "on the host with no qemu driver.")

    if connect_arg == "transport":
        canonical_uri_type = virsh.driver()

        if transport == "ssh":
            ssh_connection = utils_conn.SSHConnection(server_ip=server_ip,
                                                      server_pwd=server_pwd,
                                                      client_ip=client_ip,
                                                      client_pwd=client_pwd)
            try:
                ssh_connection.conn_check()
            except utils_conn.ConnectionError:
                ssh_connection.conn_setup()
                ssh_connection.conn_check()

            connect_uri = libvirt_vm.get_uri_with_transport(
                uri_type=canonical_uri_type,
                transport=transport, dest_ip=server_ip)
        elif transport == "tls":
            tls_connection = utils_conn.TLSConnection(server_ip=server_ip,
                                                      server_pwd=server_pwd,
                                                      client_ip=client_ip,
                                                      client_pwd=client_pwd,
                                                      server_cn=server_cn,
                                                      client_cn=client_cn)
            tls_connection.conn_setup()

            connect_uri = libvirt_vm.get_uri_with_transport(
                uri_type=canonical_uri_type,
                transport=transport, dest_ip=server_cn)
        elif transport == "tcp":
            tcp_connection = utils_conn.TCPConnection(server_ip=server_ip,
                                                      server_pwd=server_pwd,
                                                      tcp_port=tcp_port)
            tcp_connection.conn_setup()

            connect_uri = libvirt_vm.get_uri_with_transport(
                uri_type=canonical_uri_type,
                transport=transport,
                dest_ip="%s:%s"
                % (server_ip, tcp_port))
        elif transport == "unix":
            unix_transport_setup()
            connect_uri = libvirt_vm.get_uri_with_transport(
                uri_type=canonical_uri_type,
                transport=transport,
                dest_ip="")
        else:
            test.cancel("Configuration of transport=%s is "
                        "not recognized." % transport)
    else:
        connect_uri = connect_arg

    try:
        try:
            uri = do_virsh_connect(connect_uri, connect_opt)
            # connect successfully
            if status_error == "yes":
                test.fail("Connect successfully in the "
                          "case expected to fail.")
            # get the expect uri when connect argument is ""
            if connect_uri == "":
                connect_uri = virsh.canonical_uri().split()[-1]

            logging.debug("expected uri is: %s", connect_uri)
            logging.debug("actual uri after connect is: %s", uri)
            if not uri == connect_uri:
                test.fail("Command exit normally but the uri is "
                          "not setted as expected.")
        except process.CmdError as detail:
            if status_error == "no":
                test.fail("Connect failed in the case expected"
                          "to success.\n"
                          "Error: %s" % detail)
    finally:
        if transport == "unix":
            unix_transport_recover()
        if transport == "tcp":
            tcp_connection.conn_recover()
        if transport == "tls":
            tls_connection.conn_recover()
Exemplo n.º 6
0
def run(test, params, env):
    """
    Test the command virsh uri

    (1) Call virsh uri
    (2) Call virsh -c remote_uri uri
    (3) Call virsh uri with an unexpected option
    (4) Call virsh uri with libvirtd service stop
    """

    connect_uri = libvirt_vm.normalize_connect_uri(
        params.get("connect_uri", "default"))

    option = params.get("virsh_uri_options")
    unprivileged_user = params.get('unprivileged_user')

    remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM")
    remote_pwd = params.get("remote_pwd", None)
    remote_user = params.get("remote_user", "root")

    # Forming the uri using the api
    target_uri = params.get("target_uri")
    remote_ref = params.get("uri_remote_ref", "")

    if remote_ref:
        if target_uri.count('EXAMPLE.COM'):
            test.cancel('target_uri configuration set to sample value')
        logging.info("The target_uri: %s", target_uri)
        cmd = "virsh -c %s uri" % target_uri
    else:
        cmd = "virsh uri %s" % option

    # Prepare libvirtd service
    check_libvirtd = "libvirtd" in list(params.keys())
    if check_libvirtd:
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            utils_libvirtd.libvirtd_stop()

    # Run test case
    logging.info("The command: %s", cmd)

    # setup autologin for ssh to remote machine to execute commands
    if remote_ref:
        ssh_key.setup_ssh_key(remote_ip, remote_user, remote_pwd)

    if unprivileged_user:
        if process.run("id %s" % unprivileged_user,
                       ignore_status=True).exit_status != 0:
            process.run("useradd %s" % unprivileged_user)
    try:
        if remote_ref == "remote" or unprivileged_user:
            connect_uri = target_uri
        uri_test = virsh.canonical_uri(option,
                                       unprivileged_user=unprivileged_user,
                                       uri=connect_uri,
                                       ignore_status=False,
                                       debug=True)
        status = 0  # good
    except process.CmdError:
        status = 1  # bad
        uri_test = ''
        if unprivileged_user:
            process.run("userdel %s" % unprivileged_user)

    # Recover libvirtd service start
    if libvirtd == "off":
        utils_libvirtd.libvirtd_start()

    # Check status_error
    status_error = params.get("status_error", "no")
    if status_error == "yes":
        if status == 0:
            if libvirtd == "off" and libvirt_version.version_compare(5, 6, 0):
                logging.info(
                    "From libvirt version 5.6.0 libvirtd is restarted "
                    "and command should succeed.")
            else:
                test.fail("Command: %s  succeeded "
                          "(incorrect command)" % cmd)
        else:
            logging.info("command: %s is a expected error", cmd)
    elif status_error == "no":
        if target_uri != uri_test:
            test.fail("Virsh cmd uri %s != %s." % (uri_test, target_uri))
        if status != 0:
            test.fail("Command: %s  failed " "(correct command)" % cmd)
Exemplo n.º 7
0
def run(test, params, env):
    """
    Test command: virsh connect.
    """
    def unix_transport_setup():
        """
        Setup a unix connect to local libvirtd.
        """
        shutil.copy(libvirtd_conf_path, libvirtd_conf_bak_path)

        with open(libvirtd_conf_path, 'r') as libvirtdconf_file:
            line_list = libvirtdconf_file.readlines()
        conf_dict = {
            r'auth_unix_rw\s*=': 'auth_unix_rw="none"\n',
        }
        for key in conf_dict:
            pattern = key
            conf_line = conf_dict[key]
            flag = False
            for index in range(len(line_list)):
                line = line_list[index]
                if not re.search(pattern, line):
                    continue
                else:
                    line_list[index] = conf_line
                    flag = True
                    break
            if not flag:
                line_list.append(conf_line)

        with open(libvirtd_conf_path, 'w') as libvirtdconf_file:
            libvirtdconf_file.writelines(line_list)

        # restart libvirtd service
        utils_libvirtd.libvirtd_restart()

    def unix_transport_recover():
        """
        Recover the libvirtd on local.
        """
        if os.path.exists(libvirtd_conf_bak_path):
            shutil.copy(libvirtd_conf_bak_path, libvirtd_conf_path)
            utils_libvirtd.libvirtd_restart()

    # get the params from subtests.
    # params for general.
    connect_arg = params.get("connect_arg", "")
    connect_opt = params.get("connect_opt", "")
    status_error = params.get("status_error", "no")

    # params for transport connect.
    local_ip = params.get("local_ip", "ENTER.YOUR.LOCAL.IP")
    local_pwd = params.get("local_pwd", "ENTER.YOUR.LOCAL.ROOT.PASSWORD")
    transport_type = params.get("connect_transport_type", "local")
    transport = params.get("connect_transport", "ssh")
    client_ip = local_ip
    client_pwd = local_pwd
    server_ip = local_ip
    server_pwd = local_pwd

    # params special for tls connect.
    server_cn = params.get("connect_server_cn", "TLSServer")
    client_cn = params.get("connect_client_cn", "TLSClient")

    # params special for tcp connect.
    tcp_port = params.get("tcp_port", '16509')

    # params special for unix transport.
    libvirtd_conf_path = '/etc/libvirt/libvirtd.conf'
    libvirtd_conf_bak_path = '%s/libvirtd.conf.bak' % data_dir.get_tmp_dir()

    # special params for test connection alive
    alive = params.get('alive', None)

    if alive:
        check_virsh_connect_alive(test, params)
        return

    # check the config
    if (connect_arg == "transport" and transport_type == "remote"
            and local_ip.count("ENTER")):
        test.cancel("Parameter local_ip is not configured" "in remote test.")
    if (connect_arg == "transport" and transport_type == "remote"
            and local_pwd.count("ENTER")):
        test.cancel("Parameter local_pwd is not configured" "in remote test.")
    # In Ubuntu libvirt_lxc available in /usr/lib/libvirt
    if (connect_arg.count("lxc")
            and (not (os.path.exists("/usr/libexec/libvirt_lxc")
                      or os.path.exists("/usr/lib/libvirt/libvirt_lxc")))):
        test.cancel("Connect test of lxc:/// is not suggested on "
                    "the host with no lxc driver.")
    if connect_arg.count("xen") and (not os.path.exists("/var/run/xend")):
        test.cancel("Connect test of xen:/// is not suggested on "
                    "the host with no xen driver.")
    if connect_arg.count("qemu") and (not os.path.exists("/dev/kvm")):
        test.cancel("Connect test of qemu:/// is not suggested"
                    "on the host with no qemu driver.")

    if connect_arg == "transport":
        canonical_uri_type = virsh.driver()

        if transport == "ssh":
            ssh_connection = utils_conn.SSHConnection(server_ip=server_ip,
                                                      server_pwd=server_pwd,
                                                      client_ip=client_ip,
                                                      client_pwd=client_pwd)
            try:
                ssh_connection.conn_check()
            except utils_conn.ConnectionError:
                ssh_connection.conn_setup()
                ssh_connection.conn_check()

            connect_uri = libvirt_vm.get_uri_with_transport(
                uri_type=canonical_uri_type,
                transport=transport,
                dest_ip=server_ip)
        elif transport == "tls":
            tls_connection = utils_conn.TLSConnection(server_ip=server_ip,
                                                      server_pwd=server_pwd,
                                                      client_ip=client_ip,
                                                      client_pwd=client_pwd,
                                                      server_cn=server_cn,
                                                      client_cn=client_cn,
                                                      special_cn='yes')
            tls_connection.conn_setup()

            connect_uri = libvirt_vm.get_uri_with_transport(
                uri_type=canonical_uri_type,
                transport=transport,
                dest_ip=server_cn)
        elif transport == "tcp":
            tcp_connection = utils_conn.TCPConnection(server_ip=server_ip,
                                                      server_pwd=server_pwd,
                                                      tcp_port=tcp_port)
            tcp_connection.conn_setup()

            connect_uri = libvirt_vm.get_uri_with_transport(
                uri_type=canonical_uri_type,
                transport=transport,
                dest_ip="%s:%s" % (server_ip, tcp_port))
        elif transport == "unix":
            unix_transport_setup()
            connect_uri = libvirt_vm.get_uri_with_transport(
                uri_type=canonical_uri_type, transport=transport, dest_ip="")
        else:
            test.cancel("Configuration of transport=%s is "
                        "not recognized." % transport)
    else:
        connect_uri = connect_arg

    try:
        try:
            uri = do_virsh_connect(connect_uri, connect_opt)
            # connect successfully
            if status_error == "yes":
                test.fail("Connect successfully in the "
                          "case expected to fail.")
            # get the expect uri when connect argument is ""
            if connect_uri == "":
                connect_uri = virsh.canonical_uri().split()[-1]

            logging.debug("expected uri is: %s", connect_uri)
            logging.debug("actual uri after connect is: %s", uri)
            if not uri == connect_uri:
                test.fail("Command exit normally but the uri is "
                          "not set as expected.")
        except process.CmdError as detail:
            if status_error == "no":
                test.fail("Connect failed in the case expected"
                          "to success.\n"
                          "Error: %s" % detail)
    finally:
        if transport == "unix":
            unix_transport_recover()
        if transport == "tcp":
            tcp_connection.conn_recover()
        if transport == "tls":
            tls_connection.conn_recover()
Exemplo n.º 8
0
def run(test, params, env):
    """
    Test the command virsh uri

    (1) Call virsh uri
    (2) Call virsh -c remote_uri uri
    (3) Call virsh uri with an unexpected option
    (4) Call virsh uri with libvirtd service stop
    """

    connect_uri = libvirt_vm.normalize_connect_uri(
        params.get("connect_uri", "default"))

    option = params.get("virsh_uri_options")

    remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM")
    remote_pwd = params.get("remote_pwd", None)
    remote_user = params.get("remote_user", "root")

    # Forming the uri using the api
    target_uri = params.get("target_uri")
    remote_ref = params.get("uri_remote_ref", "")

    if remote_ref:
        if target_uri.count('EXAMPLE.COM'):
            raise exceptions.TestSkipError(
                'target_uri configuration set to sample value')
        logging.info("The target_uri: %s", target_uri)
        cmd = "virsh -c %s uri" % target_uri
    else:
        cmd = "virsh uri %s" % option

    # Prepare libvirtd service
    check_libvirtd = "libvirtd" in params.keys()
    if check_libvirtd:
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            utils_libvirtd.libvirtd_stop()

    # Run test case
    logging.info("The command: %s", cmd)

    # setup autologin for ssh to remote machine to execute commands
    config_opt = ["StrictHostKeyChecking=no"]
    if remote_ref:
        ssh_key.setup_remote_ssh_key(remote_ip,
                                     remote_user,
                                     remote_pwd,
                                     config_options=config_opt)

    try:
        if remote_ref == "remote":
            connect_uri = target_uri
        uri_test = virsh.canonical_uri(option,
                                       uri=connect_uri,
                                       ignore_status=False,
                                       debug=True)
        status = 0  # good
    except process.CmdError:
        status = 1  # bad
        uri_test = ''

    # Recover libvirtd service start
    if libvirtd == "off":
        utils_libvirtd.libvirtd_start()

    # Check status_error
    status_error = params.get("status_error")
    if status_error == "yes":
        if status == 0:
            raise exceptions.TestFail("Command: %s  succeeded "
                                      "(incorrect command)" % cmd)
        else:
            logging.info("command: %s is a expected error", cmd)
    elif status_error == "no":
        if cmp(target_uri, uri_test) != 0:
            raise exceptions.TestFail("Virsh cmd uri %s != %s." %
                                      (uri_test, target_uri))
        if status != 0:
            raise exceptions.TestFail("Command: %s  failed "
                                      "(correct command)" % cmd)
Exemplo n.º 9
0
def run(test, params, env):
    """
    Test the command virsh uri

    (1) Call virsh uri
    (2) Call virsh -c remote_uri uri
    (3) Call virsh uri with an unexpected option
    (4) Call virsh uri with libvirtd service stop
    """

    connect_uri = libvirt_vm.normalize_connect_uri(params.get("connect_uri",
                                                              "default"))

    option = params.get("virsh_uri_options")

    remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM")
    remote_pwd = params.get("remote_pwd", None)
    remote_user = params.get("remote_user", "root")

    # Forming the uri using the api
    target_uri = params.get("target_uri")
    remote_ref = params.get("uri_remote_ref", "")

    if remote_ref:
        if target_uri.count('EXAMPLE.COM'):
            raise exceptions.TestSkipError(
                'target_uri configuration set to sample value')
        logging.info("The target_uri: %s", target_uri)
        cmd = "virsh -c %s uri" % target_uri
    else:
        cmd = "virsh uri %s" % option

    # Prepare libvirtd service
    check_libvirtd = "libvirtd" in list(params.keys())
    if check_libvirtd:
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            utils_libvirtd.libvirtd_stop()

    # Run test case
    logging.info("The command: %s", cmd)

    # setup autologin for ssh to remote machine to execute commands
    if remote_ref:
        ssh_key.setup_ssh_key(remote_ip, remote_user, remote_pwd)

    try:
        if remote_ref == "remote":
            connect_uri = target_uri
        uri_test = virsh.canonical_uri(option, uri=connect_uri,
                                       ignore_status=False,
                                       debug=True)
        status = 0  # good
    except process.CmdError:
        status = 1  # bad
        uri_test = ''

    # Recover libvirtd service start
    if libvirtd == "off":
        utils_libvirtd.libvirtd_start()

    # Check status_error
    status_error = params.get("status_error")
    if status_error == "yes":
        if status == 0:
            raise exceptions.TestFail("Command: %s  succeeded "
                                      "(incorrect command)" % cmd)
        else:
            logging.info("command: %s is a expected error", cmd)
    elif status_error == "no":
        if target_uri != uri_test:
            raise exceptions.TestFail("Virsh cmd uri %s != %s." %
                                      (uri_test, target_uri))
        if status != 0:
            raise exceptions.TestFail("Command: %s  failed "
                                      "(correct command)" % cmd)