def install_package_precheck(packages): """If SSP is not enabled or the test is running in chroot (using test_that), packages installation should be skipped. The check does not raise exception so tests started by test_that or running in an Autotest setup with SSP disabled can continue. That assume the running environment, chroot or a machine, has the desired packages installed already. @param packages: A list of names of the packages to install. @return: True if package installation can continue. False if it should be skipped. """ if not SSP_ENABLED and not utils.is_in_container(): logging.info( 'Server-side packaging is not enabled. Install package %s ' 'is skipped.', packages) return False if server_utils.is_inside_chroot(): logging.info( 'Test is running inside chroot. Install package %s is ' 'skipped.', packages) return False if not utils.is_in_container(): raise error.ContainerError('Package installation is only supported ' 'when test is running inside container.') return True
def create_chameleon_host(dut, chameleon_args): """Create a ChameleonHost object. There three possible cases: 1) If the DUT is in Cros Lab and has a chameleon board, then create a ChameleonHost object pointing to the board. chameleon_args is ignored. 2) If not case 1) and chameleon_args is neither None nor empty, then create a ChameleonHost object using chameleon_args. 3) If neither case 1) or 2) applies, return None. @param dut: host name of the host that chameleon connects. It can be used to lookup the chameleon in test lab using naming convention. If dut is an IP address, it can not be used to lookup the chameleon in test lab. @param chameleon_args: A dictionary that contains args for creating a ChameleonHost object, e.g. {'chameleon_host': '172.11.11.112', 'chameleon_port': 9992}. @returns: A ChameleonHost object or None. """ if not utils.is_in_container(): is_moblab = utils.is_moblab() else: is_moblab = _CONFIG.get_config_value('SSP', 'is_moblab', type=bool, default=False) if not is_moblab: dut_is_hostname = not dnsname_mangler.is_ip_address(dut) if dut_is_hostname: chameleon_hostname = chameleon.make_chameleon_hostname(dut) if utils.host_is_in_lab_zone(chameleon_hostname): # Be more tolerant on chameleon in the lab because # we don't want dead chameleon blocks non-chameleon tests. if utils.ping(chameleon_hostname, deadline=3): logging.warning( 'Chameleon %s is not accessible. Please file a bug' ' to test lab', chameleon_hostname) return None return ChameleonHost(chameleon_host=chameleon_hostname) if chameleon_args: return ChameleonHost(**chameleon_args) else: return None else: afe = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10) hosts = afe.get_hosts(hostname=dut) if hosts and CHAMELEON_HOST_ATTR in hosts[0].attributes: return ChameleonHost( chameleon_host=hosts[0].attributes[CHAMELEON_HOST_ATTR], chameleon_port=hosts[0].attributes.get(CHAMELEON_PORT_ATTR, 9992)) else: return None
def run_once(self): """There is no body for this test. @raise: error.TestError if the test is not running inside container or any of the given packages failed to be installed. """ if not utils.is_in_container(): raise error.TestError('Install OS package is only supported in ' 'server-side packaging.') # Test OS package can be used. utils.run('sox --help') logging.info('Found sox executable.') # Test python module can be used. from selenium import webdriver logging.info('Found webdriver at %s', webdriver.__file__) return
def create_target_machine(machine, **kwargs): """Create the target machine which could be a testbed or a *Host. @param machine: A dict representing the test bed under test or a String representing the testbed hostname (for legacy caller support). If it is a machine dict, the 'hostname' key is required. Optional 'afe_host' key will pipe in afe_host from the autoserv runtime or the AFE. @param kwargs: Keyword args to pass to the testbed initialization. @returns: The target machine to be used for verify/repair. """ # For Brillo/Android devices connected to moblab, the `machine` name is # either `localhost` or `127.0.0.1`. It needs to be translated to the host # container IP if the code is running inside a container. This way, autoserv # can ssh to the moblab and run actual adb/fastboot commands. is_moblab = CONFIG.get_config_value('SSP', 'is_moblab', type=bool, default=False) hostname = machine['hostname'] if isinstance(machine, dict) else machine if (utils.is_in_container() and is_moblab and hostname in ['localhost', '127.0.0.1']): hostname = CONFIG.get_config_value('SSP', 'host_container_ip', type=str, default=None) if isinstance(machine, dict): machine['hostname'] = hostname else: machine = hostname logging.debug( 'Hostname of machine is converted to %s for the test to ' 'run inside a container.', hostname) # TODO(kevcheng): We'll want to have a smarter way of figuring out which # host to create (checking host labels). if server_utils.machine_is_testbed(machine): return create_testbed(machine, **kwargs) return create_host(machine, **kwargs)
def install_packages(packages=[], python_packages=[]): """Install the given package inside container. @param packages: A list of names of the packages to install. @param python_packages: A list of names of the python packages to install using pip. @raise error.ContainerError: If package is attempted to be installed outside a container. @raise error.CmdError: If the package doesn't exist or failed to install. """ if not install_package_precheck(packages or python_packages): return if not utils.is_in_container(): raise error.ContainerError('Package installation is only supported ' 'when test is running inside container.') # Always run apt-get update before installing any container. The base # container may have outdated cache. utils.run('sudo apt-get update') # Make sure the lists are not None for iteration. packages = [] if not packages else packages if python_packages: packages.extend(['python-pip', 'python-dev']) if packages: utils.run('sudo apt-get install %s -y --force-yes' % ' '.join(packages)) logging.debug('Packages are installed: %s.', packages) target_setting = '' # For containers running in Moblab, /usr/local/lib/python2.7/dist-packages/ # is a readonly mount from the host. Therefore, new python modules have to # be installed in /usr/lib/python2.7/dist-packages/ # Containers created in Moblab does not have autotest/site-packages folder. if not os.path.exists('/usr/local/autotest/site-packages'): target_setting = '--target="/usr/lib/python2.7/dist-packages/"' if python_packages: utils.run('sudo pip install %s %s' % (target_setting, ' '.join(python_packages))) logging.debug('Python packages are installed: %s.', python_packages)
def _get_standard_servo_args(dut_host): """Return servo data associated with a given DUT. This checks for the presence of servo host and port attached to the given `dut_host`. This data should be stored in the `_afe_host.attributes` field in the provided `dut_host` parameter. @param dut_host Instance of `Host` on which to find the servo attributes. @return A tuple of `servo_args` dict with host and an option port, plus an `is_in_lab` flag indicating whether this in the CrOS test lab, or some different environment. """ servo_args = None is_in_lab = False is_ssp_moblab = False if utils.is_in_container(): is_moblab = _CONFIG.get_config_value('SSP', 'is_moblab', type=bool, default=False) is_ssp_moblab = is_moblab else: is_moblab = utils.is_moblab() attrs = dut_host._afe_host.attributes if attrs and SERVO_HOST_ATTR in attrs: servo_host = attrs[SERVO_HOST_ATTR] if (is_ssp_moblab and servo_host in ['localhost', '127.0.0.1']): servo_host = _CONFIG.get_config_value('SSP', 'host_container_ip', type=str, default=None) servo_args = {SERVO_HOST_ATTR: servo_host} if SERVO_PORT_ATTR in attrs: try: servo_port = attrs[SERVO_PORT_ATTR] servo_args[SERVO_PORT_ATTR] = int(servo_port) except ValueError: logging.error('servo port is not an int: %s', servo_port) # Let's set the servo args to None since we're not creating # the ServoHost object with the proper port now. servo_args = None if SERVO_SERIAL_ATTR in attrs: servo_args[SERVO_SERIAL_ATTR] = attrs[SERVO_SERIAL_ATTR] is_in_lab = (not is_moblab and utils.host_is_in_lab_zone(servo_host)) # TODO(jrbarnette): This test to use the default lab servo hostname # is a legacy that we need only until every host in the DB has # proper attributes. elif (not is_moblab and not dnsname_mangler.is_ip_address(dut_host.hostname)): servo_host = make_servo_hostname(dut_host.hostname) is_in_lab = utils.host_is_in_lab_zone(servo_host) if is_in_lab: servo_args = {SERVO_HOST_ATTR: servo_host} if servo_args is not None: info = dut_host.host_info_store.get() if info.board: servo_args[SERVO_BOARD_ATTR] = _map_afe_board_to_servo_board( info.board) return servo_args, is_in_lab