Example #1
0
 def StartupQuicServer(self, device):
     self._process = pexpect.spawn(QUIC_SERVER, [
         '--quic_in_memory_cache_dir=%s' % self._quic_server_doc_root,
         '--certificate_file=%s' % QUIC_CERT,
         '--key_file=%s' % QUIC_KEY,
         '--port=%d' % QUIC_PORT
     ])
     assert self._process != None
     # Wait for quic_server to start serving.
     waited_s = 0
     while subprocess.call([
             'lsof', '-i',
             'udp:%d' % QUIC_PORT, '-p',
             '%d' % self._process.pid
     ],
                           stdout=open(os.devnull, 'w')) != 0:
         sleep(0.1)
         waited_s += 0.1
         assert waited_s < 5, "quic_server failed to start after %fs" % waited_s
     # Push certificate to device.
     cert = open(QUIC_CERT, 'r').read()
     device_cert_path = os.path.join(device.GetExternalStoragePath(),
                                     CERT_PATH)
     device.RunShellCommand('mkdir -p %s' % device_cert_path)
     device.WriteFile(os.path.join(device_cert_path, QUIC_CERT_FILENAME),
                      cert)
Example #2
0
 def StartupQuicServer(self, device):
     # Chromium's presubmit checks aren't smart enough to understand
     # the redirect done in build/android/pylib/pexpect.py.
     # pylint: disable=no-member
     self._process = pexpect.spawn(QUIC_SERVER, [
         '--quic_in_memory_cache_dir=%s' % self._quic_server_doc_root,
         '--certificate_file=%s' % QUIC_CERT,
         '--key_file=%s' % QUIC_KEY,
         '--port=%d' % QUIC_PORT
     ])
     assert self._process != None
     # Wait for quic_server to start serving.
     waited_s = 0
     while subprocess.call([
             'lsof', '-i',
             'udp:%d' % QUIC_PORT, '-p',
             '%d' % self._process.pid
     ],
                           stdout=open(os.devnull, 'w')) != 0:
         sleep(0.1)
         waited_s += 0.1
         assert waited_s < 5, "quic_server failed to start after %fs" % waited_s
     # Push certificate to device.
     cert = open(QUIC_CERT, 'r').read()
     device_cert_path = posixpath.join(device.GetExternalStoragePath(),
                                       CERT_PATH)
     device.RunShellCommand(['mkdir', '-p', device_cert_path],
                            check_return=True)
     device.WriteFile(os.path.join(device_cert_path, QUIC_CERT_FILENAME),
                      cert)
def GetSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL):
    """Update the SDK to include the platform specified.

  Args:
    api_level: the Android API level to download
  """
    android_binary = os.path.join(constants.EMULATOR_SDK_ROOT, "sdk", "tools", "android")
    pattern = re.compile(r"\s*([0-9]+)- SDK Platform Android [\.,0-9]+, API %d.*" % api_level)
    # Example:
    #   2- SDK Platform Android 4.3, API 18, revision 2
    exit_code, stdout = cmd_helper.GetCmdStatusAndOutput([android_binary, "list", "sdk"])
    if exit_code != 0:
        raise Exception("'android list sdk' command return %d" % exit_code)
    for line in stdout.split("\n"):
        match = pattern.match(line)
        if match:
            index = match.group(1)
            print "package %s corresponds to platform level %d" % (index, api_level)
            # update sdk --no-ui --filter $INDEX
            update_command = [android_binary, "update", "sdk", "--no-ui", "--filter", index]
            update_command_str = " ".join(update_command)
            logging.info("running update command: %s" % update_command_str)
            update_process = pexpect.spawn(update_command_str)
            # TODO(andrewhayden): Do we need to bug the user about this?
            if update_process.expect("Do you accept the license") != 0:
                raise Exception("License agreement check failed")
            update_process.sendline("y")
            if update_process.expect("Done. 1 package installed.") == 0:
                print "Successfully installed platform for API level %d" % api_level
                return
            else:
                raise Exception("Failed to install platform update")
    raise Exception("Could not find android-%d update for the SDK!" % api_level)
Example #4
0
    def _CreateAVD(self):
        """Creates an AVD with the given name.

    Return avd_name.
    """

        if self.abi == 'arm':
            abi_option = 'armeabi-v7a'
        else:
            abi_option = 'x86'

        avd_command = [
            self.android,
            '--silent',
            'create',
            'avd',
            '--name',
            self.avd_name,
            '--abi',
            abi_option,
            '--target',
            API_TARGET,
            '--force',
        ]
        avd_cmd_str = ' '.join(avd_command)
        logging.info('Create AVD command: %s', avd_cmd_str)
        avd_process = pexpect.spawn(avd_cmd_str)

        # Instead of creating a custom profile, we overwrite config files.
        avd_process.expect('Do you wish to create a custom hardware profile')
        avd_process.sendline('no\n')
        avd_process.expect('Created AVD \'%s\'' % self.avd_name)

        # Setup test device as default Galaxy Nexus AVD
        avd_config_dir = os.path.join(constants.DIR_SOURCE_ROOT, 'build',
                                      'android', 'avd_configs')
        avd_config_ini = os.path.join(
            avd_config_dir, 'AVD_for_Galaxy_Nexus_by_Google_%s.avd' % self.abi,
            'config.ini')

        # Replace current configuration with default Galaxy Nexus config.
        avds_dir = os.path.join(os.path.expanduser('~'), '.android', 'avd')
        ini_file = os.path.join(avds_dir, '%s.ini' % self.avd_name)
        new_config_ini = os.path.join(avds_dir, '%s.avd' % self.avd_name,
                                      'config.ini')

        # Remove config files with defaults to replace with Google's GN settings.
        os.unlink(ini_file)
        os.unlink(new_config_ini)

        # Create new configuration files with Galaxy Nexus by Google settings.
        with open(ini_file, 'w') as new_ini:
            new_ini.write('avd.ini.encoding=ISO-8859-1\n')
            new_ini.write('target=%s\n' % API_TARGET)
            new_ini.write('path=%s/%s.avd\n' % (avds_dir, self.avd_name))
            new_ini.write('path.rel=avd/%s.avd\n' % self.avd_name)

        shutil.copy(avd_config_ini, new_config_ini)
        return self.avd_name
 def SpawnTestProcess(self, adb):
     args = [
         'adb', '-s',
         adb.GetDevice(), 'shell', 'sh',
         constants.TEST_EXECUTABLE_DIR + '/chrome_test_runner.sh'
     ]
     logging.info(args)
     return pexpect.spawn(args[0], args[1:], logfile=sys.stdout)
Example #6
0
  def _CreateAVD(self):
    """Creates an AVD with the given name.

    Return avd_name.
    """

    if self.abi == 'arm':
      abi_option = 'armeabi-v7a'
    else:
      abi_option = 'x86'

    avd_command = [
        self.android,
        '--silent',
        'create', 'avd',
        '--name', self.avd_name,
        '--abi', abi_option,
        '--target', API_TARGET,
        '--sdcard', SDCARD_SIZE,
        '--force',
    ]
    avd_cmd_str = ' '.join(avd_command)
    logging.info('Create AVD command: %s', avd_cmd_str)
    avd_process = pexpect.spawn(avd_cmd_str)

    # Instead of creating a custom profile, we overwrite config files.
    avd_process.expect('Do you wish to create a custom hardware profile')
    avd_process.sendline('no\n')
    avd_process.expect('Created AVD \'%s\'' % self.avd_name)

    # Setup test device as default Galaxy Nexus AVD
    avd_config_dir = os.path.join(constants.DIR_SOURCE_ROOT, 'build', 'android',
                                  'avd_configs')
    avd_config_ini = os.path.join(avd_config_dir,
                                  'AVD_for_Galaxy_Nexus_by_Google_%s.avd' %
                                  self.abi, 'config.ini')

    # Replace current configuration with default Galaxy Nexus config.
    avds_dir = os.path.join(os.path.expanduser('~'), '.android', 'avd')
    ini_file = os.path.join(avds_dir, '%s.ini' % self.avd_name)
    new_config_ini = os.path.join(avds_dir, '%s.avd' % self.avd_name,
                                  'config.ini')

    # Remove config files with defaults to replace with Google's GN settings.
    os.unlink(ini_file)
    os.unlink(new_config_ini)

    # Create new configuration files with Galaxy Nexus by Google settings.
    with open(ini_file, 'w') as new_ini:
      new_ini.write('avd.ini.encoding=ISO-8859-1\n')
      new_ini.write('target=%s\n' % API_TARGET)
      new_ini.write('path=%s/%s.avd\n' % (avds_dir, self.avd_name))
      new_ini.write('path.rel=avd/%s.avd\n' % self.avd_name)

    shutil.copy(avd_config_ini, new_config_ini)
    return self.avd_name
Example #7
0
    def RunTestsAndListResults(self):
        """Runs all the tests and checks for failures.

    Returns:
      A TestRunResults object.
    """
        args = ["adb", "-s", self.device, "shell", "sh", constants.TEST_EXECUTABLE_DIR + "/chrome_test_runner.sh"]
        logging.info(args)
        p = pexpect.spawn(args[0], args[1:], logfile=sys.stdout)
        return self._WatchTestOutput(p)
Example #8
0
  def WaitForLogMatch(self, success_re, error_re, clear=False):
    """Blocks until a matching line is logged or a timeout occurs.

    Args:
      success_re: A compiled re to search each line for.
      error_re: A compiled re which, if found, terminates the search for
          |success_re|. If None is given, no error condition will be detected.
      clear: If True the existing logcat output will be cleared, defaults to
          false.

    Raises:
      pexpect.TIMEOUT upon the timeout specified by StartMonitoringLogcat().

    Returns:
      The re match object if |success_re| is matched first or None if |error_re|
      is matched first.
    """
    logging.info('<<< Waiting for logcat:' + str(success_re.pattern))
    t0 = time.time()
    while True:
      if not self._logcat:
        self.StartMonitoringLogcat(clear)
      try:
        while True:
          # Note this will block for upto the timeout _per log line_, so we need
          # to calculate the overall timeout remaining since t0.
          time_remaining = t0 + self._logcat.timeout - time.time()
          if time_remaining < 0: raise pexpect.TIMEOUT(self._logcat)
          self._logcat.expect(PEXPECT_LINE_RE, timeout=time_remaining)
          line = self._logcat.match.group(1)
          if error_re:
            error_match = error_re.search(line)
            if error_match:
              return None
          success_match = success_re.search(line)
          if success_match:
            return success_match
          logging.info('<<< Skipped Logcat Line:' + str(line))
      except pexpect.TIMEOUT:
        raise pexpect.TIMEOUT(
            'Timeout (%ds) exceeded waiting for pattern "%s" (tip: use -vv '
            'to debug)' %
            (self._logcat.timeout, success_re.pattern))
      except pexpect.EOF:
        # It seems that sometimes logcat can end unexpectedly. This seems
        # to happen during Chrome startup after a reboot followed by a cache
        # clean. I don't understand why this happens, but this code deals with
        # getting EOF in logcat.
        logging.critical('Found EOF in adb logcat. Restarting...')
        # Rerun spawn with original arguments. Note that self._logcat.args[0] is
        # the path of adb, so we don't want it in the arguments.
        self._logcat = pexpect.spawn('adb',
                                     self._logcat.args[1:],
                                     timeout=self._logcat.timeout,
                                     logfile=self._logcat.logfile)
 def _WatchFifo(self, timeout, logfile=None):
   for i in range(10):
     if self.adb.FileExistsOnDevice(self._GetFifo()):
       print 'Fifo created...'
       break
     time.sleep(i)
   else:
     raise Exception('Unable to find fifo on device %s ' % self._GetFifo())
   args = shlex.split(self.adb.Adb()._target_arg)
   args += ['shell', 'cat', self._GetFifo()]
   return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile)
 def _WatchFifo(self, timeout, logfile=None):
     for i in range(10):
         if self.adb.FileExistsOnDevice(self._GetFifo()):
             print "Fifo created..."
             break
         time.sleep(i)
     else:
         raise errors.DeviceUnresponsiveError("Unable to find fifo on device %s " % self._GetFifo())
     args = shlex.split(self.adb.Adb()._target_arg)
     args += ["shell", "cat", self._GetFifo()]
     return pexpect.spawn("adb", args, timeout=timeout, logfile=logfile)
Example #11
0
 def _WatchFifo(self, device, timeout, logfile=None):
   for i in range(100):
     if device.FileExists(self._GetFifo()):
       logging.info('Fifo created. Slept for %f secs', i * 0.5)
       break
     time.sleep(0.5)
   else:
     raise device_errors.DeviceUnreachableError(
         'Unable to find fifo on device %s ' % self._GetFifo())
   args = ['-s', device.adb.GetDeviceSerial(), 'shell', 'cat', self._GetFifo()]
   return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile)
  def RunTestsAndListResults(self):
    """Runs all the tests and checks for failures.

    Returns:
      A TestResults object.
    """
    args = ['adb', '-s', self.device, 'shell', 'sh',
            constants.TEST_EXECUTABLE_DIR + '/chrome_test_runner.sh']
    logging.info(args)
    p = pexpect.spawn(args[0], args[1:], logfile=sys.stdout)
    return self._WatchTestOutput(p)
 def _WatchFifo(self, adb, timeout, logfile=None):
     for i in range(10):
         if adb.FileExistsOnDevice(self._GetFifo()):
             logging.info('Fifo created.')
             break
         time.sleep(i)
     else:
         raise errors.DeviceUnresponsiveError(
             'Unable to find fifo on device %s ' % self._GetFifo())
     args = shlex.split(adb.Adb()._target_arg)
     args += ['shell', 'cat', self._GetFifo()]
     return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile)
 def _WatchFifo(self, device, timeout, logfile=None):
   for i in range(10):
     if device.old_interface.FileExistsOnDevice(self._GetFifo()):
       logging.info('Fifo created.')
       break
     time.sleep(i)
   else:
     raise adb_wrapper.DeviceUnreachableError(
         'Unable to find fifo on device %s ' % self._GetFifo())
   args = shlex.split(device.old_interface.Adb()._target_arg)
   args += ['shell', 'cat', self._GetFifo()]
   return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile)
Example #15
0
 def _WatchFifo(self, device, timeout, logfile=None):
   for i in range(10):
     if device.FileExists(self._GetFifo()):
       logging.info('Fifo created.')
       break
     time.sleep(i)
   else:
     raise device_errors.DeviceUnreachableError(
         'Unable to find fifo on device %s ' % self._GetFifo())
   args = shlex.split(device.old_interface.Adb()._target_arg)
   args += ['shell', 'cat', self._GetFifo()]
   return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile)
Example #16
0
 def _WatchFifo(self, device, timeout, logfile=None):
     for i in range(100):
         if device.FileExists(self._GetFifo()):
             logging.info('Fifo created. Slept for %f secs' % (i * 0.5))
             break
         time.sleep(0.5)
     else:
         raise device_errors.DeviceUnreachableError(
             'Unable to find fifo on device %s ' % self._GetFifo())
     args = [
         '-s',
         device.adb.GetDeviceSerial(), 'shell', 'cat',
         self._GetFifo()
     ]
     return pexpect.spawn('adb', args, timeout=timeout, logfile=logfile)
def UpdateSDK(api_level, package_name, package_pattern, timeout):
    """This function update SDK with a filter index.

  Args:
    api_level: the Android API level to download for.
    package_name: logging name of package that is being updated.
    package_pattern: the pattern to match the filter index from.
    timeout: the amount of time wait for update command.
  """
    android_binary = os.path.join(constants.ANDROID_SDK_ROOT, 'tools',
                                  'android')

    list_sdk_repo_command = [android_binary, 'list', 'sdk', '--all']

    exit_code, stdout = cmd_helper.GetCmdStatusAndOutput(list_sdk_repo_command)

    if exit_code != 0:
        raise Exception('\'android list sdk --all\' command return %d' %
                        exit_code)

    for line in stdout.split('\n'):
        match = package_pattern.match(line)
        if match:
            index = match.group(1)
            logging.info('package %s corresponds to %s with api level %d',
                         index, package_name, api_level)
            update_command = [
                android_binary, 'update', 'sdk', '--no-ui', '--all',
                '--filter', index
            ]
            update_command_str = ' '.join(update_command)
            logging.info('running update command: %s', update_command_str)
            update_process = pexpect.spawn(update_command_str)

            if update_process.expect('Do you accept the license') != 0:
                raise Exception('License agreement check failed')
            update_process.sendline('y')
            if update_process.expect('Done. 1 package installed.',
                                     timeout=timeout) == 0:
                logging.info('Successfully installed %s for API level %d',
                             package_name, api_level)
                return
            else:
                raise Exception('Failed to install platform update')
    raise Exception('Could not find android-%d update for the SDK!' %
                    api_level)
Example #18
0
 def StartupQuicServer(self, device):
   self._process = pexpect.spawn(QUIC_SERVER,
                                 ['--quic_in_memory_cache_dir=%s' %
                                     self._quic_server_doc_root,
                                  '--port=%d' % QUIC_PORT])
   assert self._process != None
   # Wait for quic_server to start serving.
   waited_s = 0
   while subprocess.call([QUIC_CLIENT,
                          '--host=%s' % GetServersHost(device),
                          '--port=%d' % QUIC_PORT,
                          'http://%s:%d/%s' % (GetServersHost(device),
                                               QUIC_PORT, SMALL_RESOURCE)],
                         stdout=open(os.devnull, 'w')) != 0:
     sleep(0.1)
     waited_s += 0.1
     assert waited_s < 5, "quic_server failed to start after %fs" % waited_s
Example #19
0
 def StartupQuicServer(self, device):
   self._process = pexpect.spawn(QUIC_SERVER,
                                 ['--quic_in_memory_cache_dir=%s' %
                                     self._quic_server_doc_root,
                                  '--port=%d' % QUIC_PORT])
   assert self._process != None
   # Wait for quic_server to start serving.
   waited_s = 0
   while subprocess.call([QUIC_CLIENT,
                          '--host=%s' % GetServersHost(device),
                          '--port=%d' % QUIC_PORT,
                          'http://%s:%d/%s' % (GetServersHost(device),
                                               QUIC_PORT, SMALL_RESOURCE)],
                         stdout=open(os.devnull, 'w')) != 0:
     sleep(0.1)
     waited_s += 0.1
     assert waited_s < 5, "quic_server failed to start after %fs" % waited_s
Example #20
0
def GetSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL):
    """Update the SDK to include the platform specified.

  Args:
    api_level: the Android API level to download
  """
    android_binary = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk', 'tools',
                                  'android')
    pattern = re.compile(
        '\s*([0-9]+)- SDK Platform Android [\.,0-9]+, API %d.*' % api_level)
    # Example:
    #   2- SDK Platform Android 4.3, API 18, revision 2
    exit_code, stdout = cmd_helper.GetCmdStatusAndOutput(
        [android_binary, 'list', 'sdk'])
    if exit_code != 0:
        raise Exception('\'android list sdk\' command return %d' % exit_code)
    for line in stdout.split('\n'):
        match = pattern.match(line)
        if match:
            index = match.group(1)
            print('package %s corresponds to platform level %d' %
                  (index, api_level))
            # update sdk --no-ui --filter $INDEX
            update_command = [
                android_binary, 'update', 'sdk', '--no-ui', '--filter', index
            ]
            update_command_str = ' '.join(update_command)
            logging.info('running update command: %s' % update_command_str)
            update_process = pexpect.spawn(update_command_str)
            # TODO(andrewhayden): Do we need to bug the user about this?
            if update_process.expect('Do you accept the license') != 0:
                raise Exception('License agreement check failed')
            update_process.sendline('y')
            if update_process.expect('Done. 1 package installed.') == 0:
                print('Successfully installed platform for API level %d' %
                      api_level)
                return
            else:
                raise Exception('Failed to install platform update')
    raise Exception('Could not find android-%d update for the SDK!' %
                    api_level)
def UpdateSDK(api_level, package_name, package_pattern, timeout):
  """This function update SDK with a filter index.

  Args:
    api_level: the Android API level to download for.
    package_name: logging name of package that is being updated.
    package_pattern: the pattern to match the filter index from.
    timeout: the amount of time wait for update command.
  """
  android_binary = os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'android')

  list_sdk_repo_command = [android_binary, 'list', 'sdk', '--all']

  exit_code, stdout = cmd_helper.GetCmdStatusAndOutput(list_sdk_repo_command)

  if exit_code != 0:
    raise Exception('\'android list sdk --all\' command return %d' % exit_code)

  for line in stdout.split('\n'):
    match = package_pattern.match(line)
    if match:
      index = match.group(1)
      logging.info('package %s corresponds to %s with api level %d',
                   index, package_name, api_level)
      update_command = [android_binary, 'update', 'sdk', '--no-ui', '--all',
                         '--filter', index]
      update_command_str = ' '.join(update_command)
      logging.info('running update command: %s', update_command_str)
      update_process = pexpect.spawn(update_command_str)

      if update_process.expect('Do you accept the license') != 0:
        raise Exception('License agreement check failed')
      update_process.sendline('y')
      if update_process.expect(
        'Done. 1 package installed.', timeout=timeout) == 0:
        logging.info('Successfully installed %s for API level %d',
                      package_name, api_level)
        return
      else:
        raise Exception('Failed to install platform update')
  raise Exception('Could not find android-%d update for the SDK!' % api_level)
Example #22
0
 def StartupQuicServer(self, device):
   self._process = pexpect.spawn(QUIC_SERVER,
                                 ['--quic_in_memory_cache_dir=%s' %
                                     self._quic_server_doc_root,
                                  '--certificate_file=%s' % QUIC_CERT,
                                  '--key_file=%s' % QUIC_KEY,
                                  '--port=%d' % QUIC_PORT])
   assert self._process != None
   # Wait for quic_server to start serving.
   waited_s = 0
   while subprocess.call(['lsof', '-i', 'udp:%d' % QUIC_PORT, '-p',
                          '%d' % self._process.pid],
                         stdout=open(os.devnull, 'w')) != 0:
     sleep(0.1)
     waited_s += 0.1
     assert waited_s < 5, "quic_server failed to start after %fs" % waited_s
   # Push certificate to device.
   cert = open(QUIC_CERT, 'r').read()
   device_cert_path = os.path.join(device.GetExternalStoragePath(), CERT_PATH)
   device.RunShellCommand('mkdir -p %s' % device_cert_path)
   device.WriteFile(os.path.join(device_cert_path, QUIC_CERT_FILENAME), cert)
Example #23
0
  def StartupHttpServer(self):
    """Starts up a http server with specified document root and port."""
    # If we want a specific port, make sure no one else is listening on it.
    if self.fixed_port:
      self._KillProcessListeningOnPort(self.fixed_port)
    while True:
      if self.base_config_path:
        # Read the config
        with codecs.open(self.base_config_path, 'r', 'utf-8') as f:
          config_contents = f.read()
      else:
        config_contents = self._GetDefaultBaseConfig()
      if self.extra_config_contents:
        config_contents += self.extra_config_contents
      # Write out the config, filling in placeholders from the members of |self|
      with codecs.open(self.config_path, 'w', 'utf-8') as f:
        f.write(config_contents % self.__dict__)
      if (not os.path.exists(self.lighttpd_path) or
          not os.access(self.lighttpd_path, os.X_OK)):
        raise EnvironmentError(
            'Could not find lighttpd at %s.\n'
            'It may need to be installed (e.g. sudo apt-get install lighttpd)'
            % self.lighttpd_path)
      # pylint: disable=no-member
      self.process = pexpect.spawn(self.lighttpd_path,
                                   ['-D', '-f', self.config_path,
                                    '-m', self.lighttpd_module_path],
                                   cwd=self.temp_dir)
      client_error, server_error = self._TestServerConnection()
      if not client_error:
        assert int(open(self.pid_file, 'r').read()) == self.process.pid
        break
      self.process.close()

      if self.fixed_port or 'in use' not in server_error:
        print 'Client error:', client_error
        print 'Server error:', server_error
        return False
      self.port = self._GetRandomPort()
    return True
Example #24
0
    def StartupHttpServer(self):
        """Starts up a http server with specified document root and port."""
        # If we want a specific port, make sure no one else is listening on it.
        if self.fixed_port:
            self._KillProcessListeningOnPort(self.fixed_port)
        while True:
            if self.base_config_path:
                # Read the config
                with codecs.open(self.base_config_path, 'r', 'utf-8') as f:
                    config_contents = f.read()
            else:
                config_contents = self._GetDefaultBaseConfig()
            if self.extra_config_contents:
                config_contents += self.extra_config_contents
            # Write out the config, filling in placeholders from the members of |self|
            with codecs.open(self.config_path, 'w', 'utf-8') as f:
                f.write(config_contents % self.__dict__)
            if (not os.path.exists(self.lighttpd_path)
                    or not os.access(self.lighttpd_path, os.X_OK)):
                raise EnvironmentError(
                    'Could not find lighttpd at %s.\n'
                    'It may need to be installed (e.g. sudo apt-get install lighttpd)'
                    % self.lighttpd_path)
            # pylint: disable=no-member
            self.process = pexpect.spawn(self.lighttpd_path, [
                '-D', '-f', self.config_path, '-m', self.lighttpd_module_path
            ],
                                         cwd=self.temp_dir)
            client_error, server_error = self._TestServerConnection()
            if not client_error:
                assert int(open(self.pid_file, 'r').read()) == self.process.pid
                break
            self.process.close()

            if self.fixed_port or 'in use' not in server_error:
                print('Client error:', client_error)
                print('Server error:', server_error)
                return False
            self.port = self._GetRandomPort()
        return True
Example #25
0
 def StartupQuicServer(self, adb):
     self._process = pexpect.spawn(
         QUIC_SERVER, ["--quic_in_memory_cache_dir=%s" % self._quic_server_doc_root, "--port=%d" % QUIC_PORT]
     )
     assert self._process != None
     # Wait for quic_server to start serving.
     waited_s = 0
     while (
         subprocess.call(
             [
                 QUIC_CLIENT,
                 "--host=%s" % GetServersHost(adb),
                 "--port=%d" % QUIC_PORT,
                 "http://%s:%d/%s" % (GetServersHost(adb), QUIC_PORT, SMALL_RESOURCE),
             ],
             stdout=open(os.devnull, "w"),
         )
         != 0
     ):
         sleep(0.1)
         waited_s += 0.1
         assert waited_s < 5, "quic_server failed to start after %fs" % waited_s
Example #26
0
def GetSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL):
  """Update the SDK to include the platform specified.

  Args:
    api_level: the Android API level to download
  """
  android_binary = os.path.join(constants.EMULATOR_SDK_ROOT,
                                'sdk', 'tools', 'android')
  pattern = re.compile('\s*([0-9]+)- SDK Platform Android [\.,0-9]+, API %d.*' %
                       api_level)
  # Example:
  #   2- SDK Platform Android 4.3, API 18, revision 2
  exit_code, stdout = cmd_helper.GetCmdStatusAndOutput(
      [android_binary, 'list', 'sdk'])
  if exit_code != 0:
    raise Exception('\'android list sdk\' command return %d' % exit_code)
  for line in stdout.split('\n'):
    match = pattern.match(line)
    if match:
      index = match.group(1)
      print('package %s corresponds to platform level %d' % (index, api_level))
      # update sdk --no-ui --filter $INDEX
      update_command = [android_binary,
                        'update', 'sdk', '--no-ui', '--filter', index]
      update_command_str = ' '.join(update_command)
      logging.info('running update command: %s' % update_command_str)
      update_process = pexpect.spawn(update_command_str)
      # TODO(andrewhayden): Do we need to bug the user about this?
      if update_process.expect('Do you accept the license') != 0:
        raise Exception('License agreement check failed')
      update_process.sendline('y')
      if update_process.expect('Done. 1 package installed.') == 0:
        print('Successfully installed platform for API level %d' % api_level)
        return
      else:
        raise Exception('Failed to install platform update')
  raise Exception('Could not find android-%d update for the SDK!' % api_level)
Example #27
0
  def StartMonitoringLogcat(self, clear=True, timeout=10, logfile=None,
                            filters=None):
    """Starts monitoring the output of logcat, for use with WaitForLogMatch.

    Args:
      clear: If True the existing logcat output will be cleared, to avoiding
             matching historical output lurking in the log.
      timeout: How long WaitForLogMatch will wait for the given match
      filters: A list of logcat filters to be used.
    """
    if clear:
      self.RunShellCommand('logcat -c')
    args = []
    if self._adb._target_arg:
      args += shlex.split(self._adb._target_arg)
    args += ['logcat', '-v', 'threadtime']
    if filters:
      args.extend(filters)
    else:
      args.append('*:v')

    if logfile:
      logfile = NewLineNormalizer(logfile)

    # Spawn logcat and syncronize with it.
    for _ in range(4):
      self._logcat = pexpect.spawn('adb', args, timeout=timeout,
                                   logfile=logfile)
      self.RunShellCommand('log startup_sync')
      if self._logcat.expect(['startup_sync', pexpect.EOF,
                              pexpect.TIMEOUT]) == 0:
        break
      self._logcat.close(force=True)
    else:
      logging.critical('Error reading from logcat: ' + str(self._logcat.match))
      sys.exit(1)
Example #28
0
    def CreateAVD(self, api_level):
        """Creates an AVD with the given name.

    Args:
      api_level: the api level of the image

    Return avd_name.
    """

        if self.abi == "arm":
            abi_option = "armeabi-v7a"
        elif self.abi == "mips":
            abi_option = "mips"
        else:
            abi_option = "x86"

        api_target = "android-%s" % api_level

        avd_command = [
            self.android,
            "--silent",
            "create",
            "avd",
            "--name",
            self.avd_name,
            "--abi",
            abi_option,
            "--target",
            api_target,
            "--sdcard",
            SDCARD_SIZE,
            "--force",
        ]
        avd_cmd_str = " ".join(avd_command)
        logging.info("Create AVD command: %s", avd_cmd_str)
        avd_process = pexpect.spawn(avd_cmd_str)

        # Instead of creating a custom profile, we overwrite config files.
        avd_process.expect("Do you wish to create a custom hardware profile")
        avd_process.sendline("no\n")
        avd_process.expect("Created AVD '%s'" % self.avd_name)

        # Replace current configuration with default Galaxy Nexus config.
        avds_dir = os.path.join(os.path.expanduser("~"), ".android", "avd")
        ini_file = os.path.join(avds_dir, "%s.ini" % self.avd_name)
        new_config_ini = os.path.join(avds_dir, "%s.avd" % self.avd_name, "config.ini")

        # Remove config files with defaults to replace with Google's GN settings.
        os.unlink(ini_file)
        os.unlink(new_config_ini)

        # Create new configuration files with Galaxy Nexus by Google settings.
        with open(ini_file, "w") as new_ini:
            new_ini.write("avd.ini.encoding=ISO-8859-1\n")
            new_ini.write("target=%s\n" % api_target)
            new_ini.write("path=%s/%s.avd\n" % (avds_dir, self.avd_name))
            new_ini.write("path.rel=avd/%s.avd\n" % self.avd_name)

        custom_config = CONFIG_TEMPLATE
        replacements = CONFIG_REPLACEMENTS[self.abi]
        for key in replacements:
            custom_config = custom_config.replace(key, replacements[key])
        custom_config = custom_config.replace("{api.level}", str(api_level))

        with open(new_config_ini, "w") as new_config_ini:
            new_config_ini.write(custom_config)

        return self.avd_name
Example #29
0
 def SpawnTestProcess(self, device):
   args = ['adb', '-s', device.old_interface.GetDevice(), 'shell', 'sh',
           constants.TEST_EXECUTABLE_DIR + '/chrome_test_runner.sh']
   logging.info(args)
   return pexpect.spawn(args[0], args[1:], logfile=sys.stdout)
Example #30
0
    def CreateAVD(self, api_level):
        """Creates an AVD with the given name.

    Args:
      api_level: the api level of the image

    Return avd_name.
    """

        if self.abi == 'arm':
            abi_option = 'armeabi-v7a'
        elif self.abi == 'mips':
            abi_option = 'mips'
        else:
            abi_option = 'x86'

        api_target = 'android-%s' % api_level

        avd_command = [
            self.android,
            '--silent',
            'create',
            'avd',
            '--name',
            self.avd_name,
            '--abi',
            abi_option,
            '--target',
            api_target,
            '--sdcard',
            self.sdcard_size,
            '--force',
        ]
        avd_cmd_str = ' '.join(avd_command)
        logging.info('Create AVD command: %s', avd_cmd_str)
        avd_process = pexpect.spawn(avd_cmd_str)

        # Instead of creating a custom profile, we overwrite config files.
        avd_process.expect('Do you wish to create a custom hardware profile')
        avd_process.sendline('no\n')
        avd_process.expect('Created AVD \'%s\'' % self.avd_name)

        # Replace current configuration with default Galaxy Nexus config.
        ini_file = os.path.join(BASE_AVD_DIR, '%s.ini' % self.avd_name)
        new_config_ini = os.path.join(BASE_AVD_DIR, '%s.avd' % self.avd_name,
                                      'config.ini')

        # Remove config files with defaults to replace with Google's GN settings.
        os.unlink(ini_file)
        os.unlink(new_config_ini)

        # Create new configuration files with Galaxy Nexus by Google settings.
        with open(ini_file, 'w') as new_ini:
            new_ini.write('avd.ini.encoding=ISO-8859-1\n')
            new_ini.write('target=%s\n' % api_target)
            new_ini.write('path=%s/%s.avd\n' % (BASE_AVD_DIR, self.avd_name))
            new_ini.write('path.rel=avd/%s.avd\n' % self.avd_name)

        custom_config = CONFIG_TEMPLATE
        replacements = CONFIG_REPLACEMENTS[self.abi]
        for key in replacements:
            custom_config = custom_config.replace(key, replacements[key])
        custom_config = custom_config.replace('{api.level}', str(api_level))
        custom_config = custom_config.replace('{sdcard.size}',
                                              self.sdcard_size)
        custom_config.replace('{gpu}', 'no' if self.headless else 'yes')

        with open(new_config_ini, 'w') as new_config_ini:
            new_config_ini.write(custom_config)

        return self.avd_name
Example #31
0
  def __init__(self, adb, port_pairs, tool, host_name, build_type):
    """Forwards TCP ports on the device back to the host.

    Works like adb forward, but in reverse.

    Args:
      adb: Instance of AndroidCommands for talking to the device.
      port_pairs: A list of tuples (device_port, host_port) to forward. Note
                 that you can specify 0 as a device_port, in which case a
                 port will by dynamically assigned on the device. You can
                 get the number of the assigned port using the
                 DevicePortForHostPort method.
      tool: Tool class to use to get wrapper, if necessary, for executing the
            forwarder (see valgrind_tools.py).
      host_name: Address to forward to, must be addressable from the
                 host machine. Usually use loopback '127.0.0.1'.
      build_type: 'Release' or 'Debug'.

    Raises:
      Exception on failure to forward the port.
    """
    self._adb = adb
    self._host_to_device_port_map = dict()
    self._host_process = None
    self._device_process = None
    self._adb_forward_process = None

    self._host_adb_control_port = ports.AllocateTestServerPort()
    if not self._host_adb_control_port:
      raise Exception('Failed to allocate a TCP port in the host machine.')
    adb.PushIfNeeded(
        os.path.join(constants.CHROME_DIR, 'out', build_type,
                     'device_forwarder'),
        Forwarder._DEVICE_FORWARDER_PATH)
    self._host_forwarder_path = os.path.join(constants.CHROME_DIR,
                                             'out',
                                             build_type,
                                             'host_forwarder')
    forward_string = ['%d:%d:%s' %
                      (device, host, host_name) for device, host in port_pairs]
    logging.info('Forwarding ports: %s', forward_string)
    timeout_sec = 5
    host_pattern = 'host_forwarder.*' + ' '.join(forward_string)
    # TODO(felipeg): Rather than using a blocking kill() here, the device
    # forwarder could try to bind the Unix Domain Socket until it succeeds or
    # while it fails because the socket is already bound (with appropriate
    # timeout handling obviously).
    self._KillHostForwarderBlocking(host_pattern, timeout_sec)
    self._KillDeviceForwarderBlocking(timeout_sec)
    self._adb_forward_process = pexpect.spawn(
        'adb', ['-s',
                adb._adb.GetSerialNumber(),
                'forward',
                'tcp:%s' % self._host_adb_control_port,
                'localabstract:%s' % Forwarder._DEVICE_ADB_CONTROL_PORT])
    self._device_process = pexpect.spawn(
        'adb', ['-s',
                adb._adb.GetSerialNumber(),
                'shell',
                '%s %s -D --adb_sock=%s' % (
                    tool.GetUtilWrapper(),
                    Forwarder._DEVICE_FORWARDER_PATH,
                    Forwarder._DEVICE_ADB_CONTROL_PORT)])

    device_success_re = re.compile('Starting Device Forwarder.')
    device_failure_re = re.compile('.*:ERROR:(.*)')
    index = self._device_process.expect([device_success_re,
                                         device_failure_re,
                                         pexpect.EOF,
                                         pexpect.TIMEOUT],
                                        Forwarder._TIMEOUT_SECS)
    if index == 1:
      # Failure
      error_msg = str(self._device_process.match.group(1))
      logging.error(self._device_process.before)
      self._CloseProcess()
      raise Exception('Failed to start Device Forwarder with Error: %s' %
                      error_msg)
    elif index == 2:
      logging.error(self._device_process.before)
      self._CloseProcess()
      raise Exception('Unexpected EOF while trying to start Device Forwarder.')
    elif index == 3:
      logging.error(self._device_process.before)
      self._CloseProcess()
      raise Exception('Timeout while trying start Device Forwarder')

    self._host_process = pexpect.spawn(self._host_forwarder_path,
                                       ['--adb_port=%s' % (
                                           self._host_adb_control_port)] +
                                       forward_string)

    # Read the output of the command to determine which device ports where
    # forwarded to which host ports (necessary if
    host_success_re = re.compile('Forwarding device port (\d+) to host (\d+):')
    host_failure_re = re.compile('Couldn\'t start forwarder server for port '
                                 'spec: (\d+):(\d+)')
    for pair in port_pairs:
      index = self._host_process.expect([host_success_re,
                                         host_failure_re,
                                         pexpect.EOF,
                                         pexpect.TIMEOUT],
                                        Forwarder._TIMEOUT_SECS)
      if index == 0:
        # Success
        device_port = int(self._host_process.match.group(1))
        host_port = int(self._host_process.match.group(2))
        self._host_to_device_port_map[host_port] = device_port
        logging.info("Forwarding device port: %d to host port: %d." %
                     (device_port, host_port))
      elif index == 1:
        # Failure
        device_port = int(self._host_process.match.group(1))
        host_port = int(self._host_process.match.group(2))
        self._CloseProcess()
        raise Exception('Failed to forward port %d to %d' % (device_port,
                                                             host_port))
      elif index == 2:
        logging.error(self._host_process.before)
        self._CloseProcess()
        raise Exception('Unexpected EOF while trying to forward ports %s' %
                        port_pairs)
      elif index == 3:
        logging.error(self._host_process.before)
        self._CloseProcess()
        raise Exception('Timeout while trying to forward ports %s' % port_pairs)
Example #32
0
  def CreateAVD(self, api_level):
    """Creates an AVD with the given name.

    Args:
      api_level: the api level of the image

    Return avd_name.
    """

    if self.abi == 'arm':
      abi_option = 'armeabi-v7a'
    elif self.abi == 'mips':
      abi_option = 'mips'
    else:
      abi_option = 'x86'

    api_target = 'android-%s' % api_level

    avd_command = [
        self.android,
        '--silent',
        'create', 'avd',
        '--name', self.avd_name,
        '--abi', abi_option,
        '--target', api_target,
        '--sdcard', SDCARD_SIZE,
        '--force',
    ]
    avd_cmd_str = ' '.join(avd_command)
    logging.info('Create AVD command: %s', avd_cmd_str)
    avd_process = pexpect.spawn(avd_cmd_str)

    # Instead of creating a custom profile, we overwrite config files.
    avd_process.expect('Do you wish to create a custom hardware profile')
    avd_process.sendline('no\n')
    avd_process.expect('Created AVD \'%s\'' % self.avd_name)

    # Replace current configuration with default Galaxy Nexus config.
    avds_dir = os.path.join(os.path.expanduser('~'), '.android', 'avd')
    ini_file = os.path.join(avds_dir, '%s.ini' % self.avd_name)
    new_config_ini = os.path.join(avds_dir, '%s.avd' % self.avd_name,
                                  'config.ini')

    # Remove config files with defaults to replace with Google's GN settings.
    os.unlink(ini_file)
    os.unlink(new_config_ini)

    # Create new configuration files with Galaxy Nexus by Google settings.
    with open(ini_file, 'w') as new_ini:
      new_ini.write('avd.ini.encoding=ISO-8859-1\n')
      new_ini.write('target=%s\n' % api_target)
      new_ini.write('path=%s/%s.avd\n' % (avds_dir, self.avd_name))
      new_ini.write('path.rel=avd/%s.avd\n' % self.avd_name)

    custom_config = CONFIG_TEMPLATE
    replacements = CONFIG_REPLACEMENTS[self.abi]
    for key in replacements:
      custom_config = custom_config.replace(key, replacements[key])
    custom_config = custom_config.replace('{api.level}', str(api_level))

    with open(new_config_ini, 'w') as new_config_ini:
      new_config_ini.write(custom_config)

    return self.avd_name
Example #33
0
 def SpawnTestProcess(self, device):
     args = ["adb", "-s", str(device), "shell", "sh", constants.TEST_EXECUTABLE_DIR + "/chrome_test_runner.sh"]
     logging.info(args)
     return pexpect.spawn(args[0], args[1:], logfile=sys.stdout)