def test_parse_machine_good(self):
     '''test that parse_machine() is outputting the correct data'''
     gooddata = (('host',                ('host', 'root', '', 22)),
                 ('host:21',             ('host', 'root', '', 21)),
                 ('user@host',           ('host', 'user', '', 22)),
                 ('user:pass@host',      ('host', 'user', 'pass', 22)),
                 ('user:pass@host:1234', ('host', 'user', 'pass', 1234)),
                )
     for machine, result in gooddata:
         self.assertEquals(utils.parse_machine(machine), result)
 def test_parse_machine_good(self):
     '''test that parse_machine() is outputting the correct data'''
     gooddata = (('host',                ('host', 'root', '', 22)),
                 ('host:21',             ('host', 'root', '', 21)),
                 ('user@host',           ('host', 'user', '', 22)),
                 ('user:pass@host',      ('host', 'user', 'pass', 22)),
                 ('user:pass@host:1234', ('host', 'user', 'pass', 1234)),
                )
     for machine, result in gooddata:
         self.assertEquals(utils.parse_machine(machine), result)
def create_host(machine, host_class=None, connectivity_class=None, **args):
    """Create a host object.

    This method mixes host classes that are needed into a new subclass
    and creates a instance of the new class.

    @param machine: A dict representing the device under test or a String
                    representing the DUT hostname (for legacy caller support).
                    If it is a machine dict, the 'hostname' key is required.
                    Optional 'host_attributes' key will pipe in host_attributes
                    from the autoserv runtime or the AFE.
    @param host_class: Host class to use, if None, will attempt to detect
                       the correct class.
    @param connectivity_class: Connectivity class to use, if None will decide
                               based off of hostname and config settings.
    @param args: Args that will be passed to the constructor of
                 the new host class.

    @returns: A host object which is an instance of the newly created
              host class.
    """
    hostname, host_attributes = server_utils.get_host_info_from_machine(
        machine)
    args['host_attributes'] = host_attributes
    ssh_user, ssh_pass, ssh_port, ssh_verbosity_flag, ssh_options = \
            _get_host_arguments()

    hostname, args['user'], args['password'], ssh_port = \
            server_utils.parse_machine(hostname, ssh_user, ssh_pass, ssh_port)
    args['ssh_verbosity_flag'] = ssh_verbosity_flag
    args['ssh_options'] = ssh_options
    args['port'] = ssh_port

    if not connectivity_class:
        connectivity_class = _choose_connectivity_class(hostname, ssh_port)
    host_attributes = args.get('host_attributes', {})
    host_class = host_class or OS_HOST_DICT.get(host_attributes.get('os_type'))
    if host_class:
        classes = [host_class, connectivity_class]
    else:
        classes = [
            _detect_host(connectivity_class, hostname, **args),
            connectivity_class
        ]

    # create a custom host class for this machine and return an instance of it
    host_class = type("%s_host" % hostname, tuple(classes), {})
    host_instance = host_class(hostname, **args)

    # call job_start if this is the first time this host is being used
    if hostname not in _started_hostnames:
        host_instance.job_start()
        _started_hostnames.add(hostname)

    return host_instance
def _get_host_arguments(machine):
    """Get parameters to construct a host object.

    There are currently 2 use cases for creating a host.
    1. Through the server_job, in which case the server_job injects
       the appropriate ssh parameters into our name space and they
       are available as the variables ssh_user, ssh_pass etc.
    2. Directly through factory.create_host, in which case we use
       the same defaults as used in the server job to create a host.

    @param machine: machine dict
    @return: A dictionary containing arguments for host specifically hostname,
              afe_host, user, password, port, ssh_verbosity_flag and
              ssh_options.
    """
    hostname, afe_host = server_utils.get_host_info_from_machine(machine)
    connection_pool = server_utils.get_connection_pool_from_machine(machine)
    host_info_store = host_info.get_store_from_machine(machine)
    info = host_info_store.get()

    g = globals()
    user = info.attributes.get('ssh_user', g.get('ssh_user', DEFAULT_SSH_USER))
    password = info.attributes.get('ssh_pass',
                                   g.get('ssh_pass', DEFAULT_SSH_PASS))
    port = info.attributes.get('ssh_port', g.get('ssh_port', DEFAULT_SSH_PORT))
    ssh_verbosity_flag = info.attributes.get(
        'ssh_verbosity_flag', g.get('ssh_verbosity_flag',
                                    DEFAULT_SSH_VERBOSITY))
    ssh_options = info.attributes.get(
        'ssh_options', g.get('ssh_options', DEFAULT_SSH_OPTIONS))

    hostname, user, password, port = server_utils.parse_machine(
        hostname, user, password, port)

    host_args = {
        'hostname': hostname,
        'afe_host': afe_host,
        'host_info_store': host_info_store,
        'user': user,
        'password': password,
        'port': int(port),
        'ssh_verbosity_flag': ssh_verbosity_flag,
        'ssh_options': ssh_options,
        'connection_pool': connection_pool,
    }
    return host_args
Exemple #5
0
def create_host(hostname,
                auto_monitor=True,
                follow_paths=None,
                pattern_paths=None,
                netconsole=False,
                **args):
    # by default assume we're using SSH support
    if SSH_ENGINE == 'paramiko':
        from autotest_lib.server.hosts import paramiko_host
        classes = [paramiko_host.ParamikoHost]
    elif SSH_ENGINE == 'raw_ssh':
        classes = [ssh_host.SSHHost, ssh_host.AsyncSSHMixin]
    else:
        raise error.AutoServError(
            "Unknown SSH engine %s. Please verify the "
            "value of the configuration key 'ssh_engine' "
            "on autotest's global_config.ini file." % SSH_ENGINE)

    # by default mix in run_test support
    classes.append(autotest_remote.AutotestHostMixin)

    # if the user really wants to use netconsole, let them
    if netconsole:
        classes.append(netconsole.NetconsoleHost)

    if auto_monitor:
        # use serial console support if it's available
        conmux_args = {}
        for key in ("conmux_server", "conmux_attach"):
            if key in args:
                conmux_args[key] = args[key]
        if serial.SerialHost.host_is_supported(hostname, **conmux_args):
            classes.append(serial.SerialHost)
        else:
            # no serial available, fall back to direct dmesg logging
            if follow_paths is None:
                follow_paths = [DEFAULT_FOLLOW_PATH]
            else:
                follow_paths = list(follow_paths) + [DEFAULT_FOLLOW_PATH]

            if pattern_paths is None:
                pattern_paths = [DEFAULT_PATTERNS_PATH]
            else:
                pattern_paths = (list(pattern_paths) + [DEFAULT_PATTERNS_PATH])

            logfile_monitor_class = logfile_monitor.NewLogfileMonitorMixin(
                follow_paths, pattern_paths)
            classes.append(logfile_monitor_class)

    elif follow_paths:
        logfile_monitor_class = logfile_monitor.NewLogfileMonitorMixin(
            follow_paths, pattern_paths)
        classes.append(logfile_monitor_class)

    # do any site-specific processing of the classes list
    site_factory.postprocess_classes(classes,
                                     hostname,
                                     auto_monitor=auto_monitor,
                                     **args)

    hostname, args['user'], args['password'], args['port'] = \
            server_utils.parse_machine(hostname, ssh_user, ssh_pass, ssh_port)

    # create a custom host class for this machine and return an instance of it
    host_class = type("%s_host" % hostname, tuple(classes), {})
    host_instance = host_class(hostname, **args)

    # call job_start if this is the first time this host is being used
    if hostname not in _started_hostnames:
        host_instance.job_start()
        _started_hostnames.add(hostname)

    return host_instance
Exemple #6
0
 def test_parse_machine_override(self):
     '''Test that parse_machine() defaults can be overridden'''
     self.assertEquals(utils.parse_machine('host', 'bob', 'foo', 1234),
                       ('host', 'bob', 'foo', 1234))
 def test_parse_machine_override(self):
     '''Test that parse_machine() defaults can be overridden'''
     self.assertEquals(utils.parse_machine('host', 'bob', 'foo', 1234),
                       ('host', 'bob', 'foo', 1234))
Exemple #8
0
def create_host(
    hostname, auto_monitor=True, follow_paths=None, pattern_paths=None,
    netconsole=False, **args):
    # by default assume we're using SSH support
    if SSH_ENGINE == 'paramiko':
        from autotest_lib.server.hosts import paramiko_host
        classes = [paramiko_host.ParamikoHost]
    elif SSH_ENGINE == 'raw_ssh':
        classes = [ssh_host.SSHHost, ssh_host.AsyncSSHMixin]
    else:
        raise error.AutoServError("Unknown SSH engine %s. Please verify the "
                                  "value of the configuration key 'ssh_engine' "
                                  "on autotest's global_config.ini file." %
                                  SSH_ENGINE)

    # by default mix in run_test support
    classes.append(autotest_remote.AutotestHostMixin)

    # if the user really wants to use netconsole, let them
    if netconsole:
        classes.append(netconsole.NetconsoleHost)

    if auto_monitor:
        # use serial console support if it's available
        conmux_args = {}
        for key in ("conmux_server", "conmux_attach"):
            if key in args:
                conmux_args[key] = args[key]
        if serial.SerialHost.host_is_supported(hostname, **conmux_args):
            classes.append(serial.SerialHost)
        else:
            # no serial available, fall back to direct dmesg logging
            if follow_paths is None:
                follow_paths = [DEFAULT_FOLLOW_PATH]
            else:
                follow_paths = list(follow_paths) + [DEFAULT_FOLLOW_PATH]

            if pattern_paths is None:
                pattern_paths = [DEFAULT_PATTERNS_PATH]
            else:
                pattern_paths = (
                    list(pattern_paths) + [DEFAULT_PATTERNS_PATH])

            logfile_monitor_class = logfile_monitor.NewLogfileMonitorMixin(
                follow_paths, pattern_paths)
            classes.append(logfile_monitor_class)

    elif follow_paths:
        logfile_monitor_class = logfile_monitor.NewLogfileMonitorMixin(
            follow_paths, pattern_paths)
        classes.append(logfile_monitor_class)

    # do any site-specific processing of the classes list
    site_factory.postprocess_classes(classes, hostname,
                                     auto_monitor=auto_monitor, **args)

    hostname, args['user'], args['password'], args['port'] = \
            server_utils.parse_machine(hostname, ssh_user, ssh_pass, ssh_port)

    # create a custom host class for this machine and return an instance of it
    host_class = type("%s_host" % hostname, tuple(classes), {})
    host_instance = host_class(hostname, **args)

    # call job_start if this is the first time this host is being used
    if hostname not in _started_hostnames:
        host_instance.job_start()
        _started_hostnames.add(hostname)

    return host_instance