def _connect_adb(self):
        """Sets up ADB connection to the ARC container."""
        logging.info('Setting up adb connection.')
        # Generate and push keys for adb.
        # TODO(elijahtaylor): Extract this code to arc_common and de-duplicate
        # code in arc.py on the client side tests.
        key_path = os.path.join(self.tmpdir, 'test_key')
        pubkey_path = key_path + '.pub'
        self._run('adb', verbose=True, args=('keygen', pipes.quote(key_path)))
        with open(pubkey_path, 'r') as f:
            self._write_android_file(_ANDROID_ADB_KEYS_PATH, f.read())
        self._android_shell('restorecon ' +
                            pipes.quote(_ANDROID_ADB_KEYS_PATH))
        os.environ['ADB_VENDOR_KEYS'] = key_path

        # Kill existing adb server to ensure that the env var is picked up.
        self._run('adb', verbose=True, args=('kill-server', ))

        # This starts adbd.
        self._android_shell('setprop sys.usb.config mtp,adb')

        # Also let it be automatically started upon reboot.
        self._android_shell('setprop persist.sys.usb.config mtp,adb')

        # adbd may take some time to come up. Repeatedly try to connect to adb.
        utils.poll_for_condition(
            lambda: self._try_adb_connect(),
            exception=error.TestFail('Error: Failed to set up adb connection'),
            timeout=_ADB_READY_TIMEOUT_SECONDS,
            sleep_interval=_ADB_POLLING_INTERVAL_SECONDS)

        logging.info('Successfully setup adb connection.')
    def maybe_start(self, timeout=5):
        """Starts the background process to run multiplex ssh connection.

        If there already is a background process running, this does nothing.
        If there is a stale process or a stale socket, first clean them up,
        then create a background process.

        @param timeout: timeout in seconds (default 5) to wait for master ssh
                        connection to be established. If timeout is reached, a
                        warning message is logged, but no other action is
                        taken.
        """
        # Multiple processes might try in parallel to clean up the old master
        # ssh connection and create a new one, therefore use a lock to protect
        # against race conditions.
        with self._lock:
            # If a previously started master SSH connection is not running
            # anymore, it needs to be cleaned up and then restarted.
            if (self._master_job
                    and (not os.path.exists(self._socket_path)
                         or self._master_job.sp.poll() is not None)):
                logging.info('Master ssh connection to %s is down.',
                             self._hostname)
                self._close_internal()

            # Start a new master SSH connection.
            if not self._master_job:
                # Create a shared socket in a temp location.
                self._master_tempdir = autotemp.tempdir(unique_id='ssh-master',
                                                        dir='/tmp')

                # Start the master SSH connection in the background.
                master_cmd = _MASTER_SSH_COMMAND_TEMPLATE % {
                    'hostname': self._hostname,
                    'user': self._user,
                    'port': self._port,
                    'socket': self._socket_path,
                }
                logging.info('Starting master ssh connection \'%s\'',
                             master_cmd)
                self._master_job = utils.BgJob(master_cmd,
                                               nickname='master-ssh',
                                               stdout_tee=utils.DEVNULL,
                                               stderr_tee=utils.DEVNULL,
                                               unjoinable=True)

                # To prevent a race between the master ssh connection
                # startup and its first attempted use, wait for socket file to
                # exist before returning.
                try:
                    utils.poll_for_condition(
                        condition=lambda: os.path.exists(self._socket_path),
                        timeout=timeout,
                        sleep_interval=0.2,
                        desc='Wait for a socket file to exist')
                # log the issue if it fails, but don't throw an exception
                except utils.TimeoutError:
                    logging.info('Timed out waiting for master-ssh connection '
                                 'to be established.')
Exemple #3
0
    def _check_rlz_vpd_settings_post_ping(self):
        """Checks that rlz related vpd settings are correct after the test."""
        def should_send_rlz_ping():
            return int(
                self._host.run('vpd -i RW_VPD -g '
                               'should_send_rlz_ping').stdout)

        utils.poll_for_condition(lambda: should_send_rlz_ping() == 0,
                                 timeout=60)

        result = self._host.run('vpd -i RW_VPD -g rlz_embargo_end_date',
                                ignore_status=True)
        if result.exit_status == 0:
            raise error.TestFail('rlz_embargo_end_date still present in vpd.')
    def _wait_for_arc_boot(self):
        """Wait until ARC is fully booted.

        Tests for the presence of the intent helper app to determine whether ARC
        has finished booting.
        """
        def _intent_helper_running():
            result = self._run('adb',
                               args=('shell', 'pgrep', '-f',
                                     'org.chromium.arc.intent_helper'))
            return bool(result.stdout)

        utils.poll_for_condition(
            _intent_helper_running,
            exception=error.TestFail(
                'Error: Timed out waiting for intent helper.'),
            timeout=_ARC_READY_TIMEOUT_SECONDS,
            sleep_interval=_ARC_POLLING_INTERVAL_SECONDS)