Exemple #1
0
def FindSymbolFiles(tempdir, paths):
  """Locate symbol files in |paths|

  This returns SymbolFile objects that contain file references which are valid
  after this exits. Those files may exist externally, or be created in the
  tempdir (say, when expanding tarballs). The caller must not consider
  SymbolFile's valid after tempdir is cleaned up.

  Args:
    tempdir: Path to use for temporary files.
    paths: A list of input paths to walk. Files are returned w/out any checks.
      Dirs are searched for files that end in ".sym". Urls are fetched and then
      processed. Tarballs are unpacked and walked.

  Yields:
    A SymbolFile for every symbol file found in paths.
  """
  cache_dir = path_util.GetCacheDir()
  common_path = os.path.join(cache_dir, constants.COMMON_CACHE)
  tar_cache = cache.TarballCache(common_path)

  for p in paths:
    o = urllib.parse.urlparse(p)
    if o.scheme:
      # Support globs of filenames.
      ctx = gs.GSContext()
      for gspath in ctx.LS(p):
        logging.info('processing files inside %s', gspath)
        o = urllib.parse.urlparse(gspath)
        key = ('%s%s' % (o.netloc, o.path)).split('/')
        # The common cache will not be LRU, removing the need to hold a read
        # lock on the cached gsutil.
        ref = tar_cache.Lookup(key)
        try:
          ref.SetDefault(gspath)
        except cros_build_lib.RunCommandError as e:
          logging.warning('ignoring %s\n%s', gspath, e)
          continue
        for sym in FindSymbolFiles(tempdir, [ref.path]):
          yield sym

    elif os.path.isdir(p):
      for root, _, files in os.walk(p):
        for f in files:
          if f.endswith('.sym'):
            # If p is '/tmp/foo' and filename is '/tmp/foo/bar/bar.sym',
            # display_path = 'bar/bar.sym'
            filename = os.path.join(root, f)
            yield SymbolFile(display_path=filename[len(p):].lstrip('/'),
                             file_name=filename)

    elif IsTarball(p):
      logging.info('processing files inside %s', p)
      tardir = tempfile.mkdtemp(dir=tempdir)
      cache.Untar(os.path.realpath(p), tardir)
      for sym in FindSymbolFiles(tardir, [tardir]):
        yield sym

    else:
      yield SymbolFile(display_path=p, file_name=p)
Exemple #2
0
def main(argv):
  parser = GetParser()
  opts = parser.parse_args(argv)
  opts.Freeze()

  # Process list output quickly as it takes no privileges.
  if opts.list:
    print('\n'.join(sorted(opts.tests or FindTests((constants.CHROMITE_DIR,)))))
    return

  # Many of our tests require a valid chroot to run. Make sure it's created
  # before we block network access.
  chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR)
  if (not os.path.exists(chroot) and
      ChrootAvailable() and
      not cros_build_lib.IsInsideChroot()):
    cros_build_lib.RunCommand(['cros_sdk', '--create'])

  # This is a cheesy hack to make sure gsutil is populated in the cache before
  # we run tests. This is a partial workaround for crbug.com/468838.
  gs.GSContext.GetDefaultGSUtilBin()

  # Now let's run some tests.
  _ReExecuteIfNeeded([sys.argv[0]] + argv, opts.network)
  # A lot of pieces here expect to be run in the root of the chromite tree.
  # Make them happy.
  os.chdir(constants.CHROMITE_DIR)
  tests = opts.tests or FindTests()

  if opts.quick:
    SPECIAL_TESTS.update(SLOW_TESTS)

  global TIMING_CACHE_FILE  # pylint: disable=global-statement
  TIMING_CACHE_FILE = os.path.join(
      path_util.GetCacheDir(), constants.COMMON_CACHE, 'run_tests.cache.json')

  jobs = opts.jobs or multiprocessing.cpu_count()

  with cros_build_lib.ContextManagerStack() as stack:
    # If we're running outside the chroot, try to contain ourselves.
    if cgroups.Cgroup.IsSupported() and not cros_build_lib.IsInsideChroot():
      stack.Add(cgroups.SimpleContainChildren, 'run_tests')

    # Throw all the tests into a custom tempdir so that if we do CTRL+C, we can
    # quickly clean up all the files they might have left behind.
    stack.Add(osutils.TempDir, prefix='chromite.run_tests.', set_global=True,
              sudo_rm=True)

    with cros_build_lib.TimedSection() as timer:
      result = RunTests(
          tests, jobs=jobs, chroot_available=ChrootAvailable(),
          network=opts.network, dryrun=opts.dryrun, failfast=opts.failfast)

    if result:
      logging.info('All tests succeeded! (%s total)', timer.delta)
    else:
      return 1

  if not opts.network:
    logging.warning('Network tests skipped; use --network to run them')
    def _FindCacheDir(self):
        """Helper for deciding what cache directory to use.

    Returns:
      Returns a directory suitable for use with a DownloadCache.
    """
        return os.path.join(path_util.GetCacheDir(), 'paygen_cache')
    def testOutsideChrootOutbound(self, _):
        """Tests FromChroot() calls from outside the chroot."""
        self.PatchObject(os,
                         'getcwd',
                         return_value=self.FakeCwd(FAKE_SOURCE_PATH))
        self.SetChrootPath(constants.SOURCE_ROOT)
        resolver = path_util.ChrootPathResolver()

        # Case: source root path.
        self.assertEqual(
            os.path.join(constants.SOURCE_ROOT, 'some/path'),
            resolver.FromChroot(
                os.path.join(constants.CHROOT_SOURCE_ROOT, 'some/path')))

        # Case: cyclic source/chroot sub-path elimination.
        self.assertEqual(
            os.path.join(constants.SOURCE_ROOT, 'some/path'),
            resolver.FromChroot(
                os.path.join(constants.CHROOT_SOURCE_ROOT,
                             constants.DEFAULT_CHROOT_DIR,
                             constants.CHROOT_SOURCE_ROOT.lstrip(os.path.sep),
                             constants.DEFAULT_CHROOT_DIR,
                             constants.CHROOT_SOURCE_ROOT.lstrip(os.path.sep),
                             'some/path')))

        # Case: path inside the cache directory.
        self.assertEqual(
            os.path.join(path_util.GetCacheDir(), 'some/path'),
            resolver.FromChroot(
                os.path.join(constants.CHROOT_CACHE_ROOT, 'some/path')))

        # Case: non-rooted chroot paths.
        self.assertEqual(os.path.join(self.chroot_path, 'some/path'),
                         resolver.FromChroot('/some/path'))
Exemple #5
0
def CheckAndGetCIDBCreds(force_update=False, folder=None):
    """Check if CIDB creds exist, download creds if necessary."""
    cache_dir = path_util.GetCacheDir()
    dir_name = folder if folder is not None else 'cidb_creds'
    cidb_dir = os.path.join(cache_dir, dir_name)
    cidb_dir_lock = cidb_dir + '.lock'

    with locking.FileLock(cidb_dir_lock).write_lock():
        if os.path.exists(cidb_dir):
            if force_update:
                shutil.rmtree(cidb_dir, ignore_errors=True)
                logging.debug('Force updating CIDB creds. Deleted %s.',
                              cidb_dir)
            else:
                logging.debug('Using cached credentials %s', cidb_dir)
                return cidb_dir

        os.mkdir(cidb_dir)

        try:
            GetCIDBCreds(cidb_dir)
            return cidb_dir
        except Exception as e:
            if isinstance(e, gs.GSCommandError):
                logging.warning(
                    'Please check if the GS credentials is configured '
                    'correctly. Please note the permissions to fetch '
                    'these credentials are for Googlers only,')

            logging.error('Failed to get CIDB credentials. Deleting %s',
                          cidb_dir)
            shutil.rmtree(cidb_dir, ignore_errors=True)
            raise
Exemple #6
0
    def GetDefaultGSUtilBin(cls, cache_dir=None):
        if cls.DEFAULT_GSUTIL_BIN is None:
            if cache_dir is None:
                cache_dir = path_util.GetCacheDir()
            if cache_dir is not None:
                common_path = os.path.join(cache_dir, constants.COMMON_CACHE)
                tar_cache = cache.TarballCache(common_path)
                key = (cls.GSUTIL_TAR, )
                # The common cache will not be LRU, removing the need to hold a read
                # lock on the cached gsutil.
                ref = tar_cache.Lookup(key)
                ref.SetDefault(cls.GSUTIL_URL)
                cls.DEFAULT_GSUTIL_BIN = os.path.join(ref.path, 'gsutil',
                                                      'gsutil')
            else:
                # Check if the default gsutil path for builders exists. If
                # not, try locating gsutil. If none exists, simply use 'gsutil'.
                gsutil_bin = cls.DEFAULT_GSUTIL_BUILDER_BIN
                if not os.path.exists(gsutil_bin):
                    gsutil_bin = osutils.Which('gsutil')
                if gsutil_bin is None:
                    gsutil_bin = 'gsutil'
                cls.DEFAULT_GSUTIL_BIN = gsutil_bin

        return cls.DEFAULT_GSUTIL_BIN
    def testOutsideChrootInbound(self, _):
        """Tests ToChroot() calls from outside the chroot."""
        for source_path, source_from_path_repo in itertools.product(
            (None, CUSTOM_SOURCE_PATH), (False, True)):
            if source_from_path_repo:
                actual_source_path = FAKE_REPO_PATH
            else:
                actual_source_path = source_path or constants.SOURCE_ROOT

            fake_cwd = self.FakeCwd(actual_source_path)
            self.PatchObject(os, 'getcwd', return_value=fake_cwd)
            self.SetChrootPath(actual_source_path)
            resolver = path_util.ChrootPathResolver(
                source_path=source_path,
                source_from_path_repo=source_from_path_repo)
            self.PatchObject(resolver, '_ReadChrootLink', return_value=None)
            source_rel_cwd = os.path.relpath(fake_cwd, actual_source_path)

            # Case: path inside the chroot space.
            self.assertEqual(
                '/some/path',
                resolver.ToChroot(os.path.join(self.chroot_path, 'some/path')))

            # Case: path inside the cache directory.
            self.assertEqual(
                os.path.join(constants.CHROOT_CACHE_ROOT, 'some/path'),
                resolver.ToChroot(
                    os.path.join(path_util.GetCacheDir(), 'some/path')))

            # Case: absolute path inside the source tree.
            if source_from_path_repo:
                self.assertEqual(
                    os.path.join(constants.CHROOT_SOURCE_ROOT, 'some/path'),
                    resolver.ToChroot(os.path.join(FAKE_REPO_PATH,
                                                   'some/path')))
            else:
                self.assertEqual(
                    os.path.join(constants.CHROOT_SOURCE_ROOT, 'some/path'),
                    resolver.ToChroot(
                        os.path.join(actual_source_path, 'some/path')))

            # Case: relative path inside the source tree.
            if source_from_path_repo:
                self.assertEqual(
                    os.path.join(constants.CHROOT_SOURCE_ROOT, source_rel_cwd,
                                 'some/path'), resolver.ToChroot('some/path'))
            else:
                self.assertEqual(
                    os.path.join(constants.CHROOT_SOURCE_ROOT, source_rel_cwd,
                                 'some/path'), resolver.ToChroot('some/path'))

            # Case: unreachable, path with improper source root prefix.
            with self.assertRaises(ValueError):
                resolver.ToChroot(
                    os.path.join(actual_source_path + '-foo', 'some/path'))

            # Case: unreachable (random).
            with self.assertRaises(ValueError):
                resolver.ToChroot('/some/path')
def FindCacheDir():
    """Helper for deciding what cache directory to use.

  Returns:
    Returns a directory suitable for use with a DownloadCache.
  """
    # Discover which directory to use for caching
    return os.path.join(path_util.GetCacheDir(), 'paygen_cache')
def _GetCipdBinary(pkg_name, bin_name, instance_id):
    """Returns a local path to the given binary fetched from cipd."""
    cache_dir = os.path.join(path_util.GetCacheDir(), 'cipd', 'packages')
    path = cipd.InstallPackage(cipd.GetCIPDFromCache(),
                               pkg_name,
                               instance_id,
                               destination=cache_dir)

    return os.path.join(path, bin_name)
def SymbolFinder(tempdir, paths):
    """Locate symbol files in |paths|

  Args:
    tempdir: Path to use for temporary files (caller will clean up).
    paths: A list of input paths to walk. Files are returned w/out any checks.
      Dirs are searched for files that end in ".sym". Urls are fetched and then
      processed. Tarballs are unpacked and walked.

  Returns:
    Yield every viable sym file.
  """
    cache_dir = path_util.GetCacheDir()
    common_path = os.path.join(cache_dir, constants.COMMON_CACHE)
    tar_cache = cache.TarballCache(common_path)

    for p in paths:
        # Pylint is confused about members of ParseResult.

        o = urlparse.urlparse(p)
        if o.scheme:  # pylint: disable=E1101
            # Support globs of filenames.
            ctx = gs.GSContext()
            for p in ctx.LS(p):
                logging.info('processing files inside %s', p)
                o = urlparse.urlparse(p)
                key = ('%s%s' % (o.netloc, o.path)).split('/')  # pylint: disable=E1101
                # The common cache will not be LRU, removing the need to hold a read
                # lock on the cached gsutil.
                ref = tar_cache.Lookup(key)
                try:
                    ref.SetDefault(p)
                except cros_build_lib.RunCommandError as e:
                    logging.warning('ignoring %s\n%s', p, e)
                    continue
                for p in SymbolFinder(tempdir, [ref.path]):
                    yield p

        elif os.path.isdir(p):
            for root, _, files in os.walk(p):
                for f in files:
                    if f.endswith('.sym'):
                        yield os.path.join(root, f)

        elif IsTarball(p):
            logging.info('processing files inside %s', p)
            tardir = tempfile.mkdtemp(dir=tempdir)
            cache.Untar(os.path.realpath(p), tardir)
            for p in SymbolFinder(tardir, [tardir]):
                yield p

        else:
            yield p
Exemple #11
0
  def _Flash(self):
    """Flash device."""
    if not self.flash:
      return

    if self.xbuddy:
      xbuddy_path = self.xbuddy.format(board=self._device.board)
    else:
      version = xbuddy.LATEST
      if path_util.DetermineCheckout().type != path_util.CHECKOUT_TYPE_REPO:
        # Try flashing to the full version of the board used in the Simple
        # Chrome SDK if it's present in the cache. Otherwise default to using
        # latest.
        cache = self.cache_dir or path_util.GetCacheDir()
        version = cros_chrome_sdk.SDKFetcher.GetCachedFullVersion(
            cache, self._device.board) or version
      suffix = ''
      if self.public_image:
        suffix = '-full'
      xbuddy_path = 'xbuddy://remote/%s%s/%s' % (
          self._device.board, suffix, version)

    # Skip the flash if the device is already running the requested version.
    device_version = self._device.remote.version
    _, _, requested_version, _ = xbuddy.XBuddy.InterpretPath(xbuddy_path)
    # Split on the first "-" when comparing versions since xbuddy requires
    # the RX- prefix, but the device may not advertise it.
    if xbuddy.LATEST not in requested_version:
      if (requested_version == device_version or
          ('-' in requested_version and
           requested_version.split('-', 1)[1] == device_version)):
        logging.info(
            'Skipping the flash. Device running %s when %s was requested',
            device_version, xbuddy_path)
        return

    device_name = 'ssh://' + self._device.device
    if self._device.ssh_port:
      device_name += ':' + str(self._device.ssh_port)
    flash_cmd = [
        os.path.join(constants.CHROMITE_BIN_DIR, 'cros'),
        'flash',
        device_name,
        xbuddy_path,
        '--board', self._device.board,
        '--disable-rootfs-verification',
        '--clobber-stateful',
    ]
    cros_build_lib.run(flash_cmd, dryrun=self.dryrun)
Exemple #12
0
def GetCIPDFromCache(instance_id=CIPD_INSTANCE_ID):
    """Checks the cache, downloading CIPD if it is missing.

  Args:
    instance_id: The version of CIPD to download. Default CIPD_INSTANCE_ID

  Returns:
    Path to the CIPD binary.
  """
    cache_dir = os.path.join(path_util.GetCacheDir(), 'cipd')
    bin_cache = CipdCache(cache_dir)
    key = (instance_id, )
    ref = bin_cache.Lookup(key)
    ref.SetDefault('cipd://' + instance_id)
    return ref.path
Exemple #13
0
def _GetAuthUtil(instance_id='latest'):
    """Returns a path to the authutil binary.

  This will download and install the authutil package if it is not already
  deployed.

  Args:
    instance_id: The instance-id of the package to install. Defaults to 'latest'

  Returns:
    the path to the authutil binary.
  """
    cache_dir = os.path.join(path_util.GetCacheDir(), 'cipd/packages')
    path = cipd.InstallPackage(cipd.GetCIPDFromCache(),
                               'infra/tools/authutil/linux-amd64',
                               instance_id,
                               destination=cache_dir)

    return os.path.join(path, 'authutil')
Exemple #14
0
    def _GetDefaultVersion(self):
        """Get default full SDK version.

    For non-chrome, use 'latest'. For chrome, look up the
    full version in the misc cache.
    """
        if path_util.DetermineCheckout(
        ).type != path_util.CHECKOUT_TYPE_GCLIENT:
            return 'latest'

        board = self.options.board or flash.GetDefaultBoard()
        if not board:
            raise flash.FlashError('Must specify board.')

        full_version = cros_chrome_sdk.SDKFetcher.GetCachedFullVersion(
            self.options.cache_dir or path_util.GetCacheDir(),
            board) or 'latest'
        logging.notice('CrOS SDK version: %s', full_version)
        return full_version
    def testOutsideChrootOutbound(self, _):
        """Tests FromChroot() calls from outside the chroot."""
        self.PatchObject(os,
                         'getcwd',
                         return_value=self.FakeCwd(FAKE_SOURCE_PATH))
        self.SetChrootPath(constants.SOURCE_ROOT)
        resolver = path_util.ChrootPathResolver()
        # These two patches are only necessary or have any affect on the test when
        # the test is run inside of a symlinked chroot. The _ReadChrootLink patch
        # ensures it runs as if it is not in a symlinked chroot. The realpath
        # patch is necessary to make it actually behave as if that's the case.
        # In both instances the effective return value are as if it was not in a
        # symlinked chroot.
        # TODO(saklein) Rewrite these tests so this isn't necessary.
        self.PatchObject(resolver, '_ReadChrootLink', return_value=None)
        self.PatchObject(os.path, 'realpath', side_effect=lambda x: x)

        # Case: source root path.
        self.assertEqual(
            os.path.join(constants.SOURCE_ROOT, 'some/path'),
            resolver.FromChroot(
                os.path.join(constants.CHROOT_SOURCE_ROOT, 'some/path')))

        # Case: cyclic source/chroot sub-path elimination.
        self.assertEqual(
            os.path.join(constants.SOURCE_ROOT, 'some/path'),
            resolver.FromChroot(
                os.path.join(constants.CHROOT_SOURCE_ROOT,
                             constants.DEFAULT_CHROOT_DIR,
                             constants.CHROOT_SOURCE_ROOT.lstrip(os.path.sep),
                             constants.DEFAULT_CHROOT_DIR,
                             constants.CHROOT_SOURCE_ROOT.lstrip(os.path.sep),
                             'some/path')))

        # Case: path inside the cache directory.
        self.assertEqual(
            os.path.join(path_util.GetCacheDir(), 'some/path'),
            resolver.FromChroot(
                os.path.join(constants.CHROOT_CACHE_ROOT, 'some/path')))

        # Case: non-rooted chroot paths.
        self.assertEqual(os.path.join(self.chroot_path, 'some/path'),
                         resolver.FromChroot('/some/path'))
Exemple #16
0
def GetLuciAuth(
    instance_id='git_revision:fd059ace316e4dbcaa5afdcec9ed4a855c4f3c65'):
  """Returns a path to the luci-auth binary.

  This will download and install the luci-auth package if it is not already
  deployed.

  Args:
    instance_id: The instance-id of the package to install. Defaults to
    'git_revision:fd059ace316e4dbcaa5afdcec9ed4a855c4f3c65' which is the last
    SHA1 version.

  Returns:
    the path to the luci-auth binary.
  """
  cache_dir = os.path.join(path_util.GetCacheDir(), 'cipd/packages')
  path = cipd.InstallPackage(
      cipd.GetCIPDFromCache(),
      'infra/tools/luci-auth/linux-amd64',
      instance_id,
      destination=cache_dir)

  return os.path.join(path, 'luci-auth')
Exemple #17
0
  def GetParser():
    """Parse a list of args.

    Args:
      argv: list of command line arguments.

    Returns:
      List of parsed opts.
    """
    parser = device.Device.GetParser()
    parser.add_argument('--start', action='store_true', default=False,
                        help='Start the VM.')
    parser.add_argument('--stop', action='store_true', default=False,
                        help='Stop the VM.')
    parser.add_argument('--image-path', type='path',
                        help='Path to VM image to launch with --start.')
    parser.add_argument('--image-format', default=VM.IMAGE_FORMAT,
                        help='Format of the VM image (raw, qcow2, ...).')
    parser.add_argument('--qemu-path', type='path',
                        help='Path of qemu binary to launch with --start.')
    parser.add_argument('--qemu-m', type=str, default='8G',
                        help='Memory argument that will be passed to qemu.')
    parser.add_argument('--qemu-smp', type=int, default='0',
                        help='SMP argument that will be passed to qemu. (0 '
                             'means auto-detection.)')
    # TODO(pwang): replace SandyBridge to Haswell-noTSX once lab machine
    # running VMTest all migrate to GCE.
    parser.add_argument('--qemu-cpu', type=str,
                        default='SandyBridge,-invpcid,-tsc-deadline',
                        help='CPU argument that will be passed to qemu.')
    parser.add_argument('--qemu-bios-path', type='path',
                        help='Path of directory with qemu bios files.')
    parser.add_argument('--qemu-hostfwd', action='append',
                        help='Ports to forward from the VM to the host in the '
                        'QEMU hostfwd format, eg tcp:127.0.0.1:12345-:54321 to '
                        'forward port 54321 on the VM to 12345 on the host.')
    parser.add_argument('--qemu-args', action='append',
                        help='Additional args to pass to qemu.')
    parser.add_argument('--copy-on-write', action='store_true', default=False,
                        help='Generates a temporary copy-on-write image backed '
                             'by the normal boot image. All filesystem changes '
                             'will instead be reflected in the temporary '
                             'image.')
    parser.add_argument('--qemu-img-path', type='path',
                        help='Path to qemu-img binary used to create temporary '
                             'copy-on-write images.')
    parser.add_argument('--disable-kvm', dest='enable_kvm',
                        action='store_false', default=None,
                        help='Disable KVM, use software emulation.')
    parser.add_argument('--no-display', dest='display',
                        action='store_false', default=True,
                        help='Do not display video output.')
    parser.add_argument('--ssh-port', type=int,
                        help='ssh port to communicate with VM.')
    parser.add_argument('--chroot-path', type='path',
                        default=os.path.join(constants.SOURCE_ROOT,
                                             constants.DEFAULT_CHROOT_DIR))
    parser.add_argument('--cache-dir', type='path',
                        default=path_util.GetCacheDir(),
                        help='Cache directory to use.')
    parser.add_argument('--vm-dir', type='path',
                        help='Temp VM directory to use.')
    return parser
Exemple #18
0
    def GetParser():
        """Parse a list of args.

    Args:
      argv: list of command line arguments.

    Returns:
      List of parsed opts.
    """
        parser = commandline.ArgumentParser(description=__doc__)
        parser.add_argument('--start',
                            action='store_true',
                            default=False,
                            help='Start the VM.')
        parser.add_argument('--stop',
                            action='store_true',
                            default=False,
                            help='Stop the VM.')
        parser.add_argument('--image-path',
                            type=str,
                            help='Path to VM image to launch with --start.')
        parser.add_argument('--image-format',
                            default=VM.IMAGE_FORMAT,
                            help='Format of the VM image (raw, qcow2, ...).')
        parser.add_argument('--qemu-path',
                            type=str,
                            help='Path of qemu binary to launch with --start.')
        parser.add_argument(
            '--qemu-m',
            type=str,
            default='8G',
            help='Memory argument that will be passed to qemu.')
        parser.add_argument(
            '--qemu-smp',
            type=int,
            default='0',
            help='SMP argument that will be passed to qemu. (0 '
            'means auto-detection.)')
        # TODO(pwang): replace SandyBridge to Haswell-noTSX once lab machine
        # running VMTest all migrate to GCE.
        parser.add_argument('--qemu-cpu',
                            type=str,
                            default='SandyBridge,-invpcid,-tsc-deadline',
                            help='CPU argument that will be passed to qemu.')
        parser.add_argument('--qemu-bios-path',
                            type=str,
                            help='Path of directory with qemu bios files.')
        parser.add_argument('--disable-kvm',
                            dest='enable_kvm',
                            action='store_false',
                            default=True,
                            help='Disable KVM, use software emulation.')
        parser.add_argument('--no-display',
                            dest='display',
                            action='store_false',
                            default=True,
                            help='Do not display video output.')
        parser.add_argument('--ssh-port',
                            type=int,
                            default=VM.SSH_PORT,
                            help='ssh port to communicate with VM.')
        sdk_board_env = os.environ.get(
            cros_chrome_sdk.SDKFetcher.SDK_BOARD_ENV)
        parser.add_argument('--board',
                            default=sdk_board_env,
                            help='Board to use.')
        parser.add_argument('--cache-dir',
                            type=str,
                            default=path_util.GetCacheDir(),
                            help='Cache directory to use.')
        parser.add_argument('--vm-dir',
                            type=str,
                            help='Temp VM directory to use.')
        parser.add_argument('--dry-run',
                            action='store_true',
                            default=False,
                            help='dry run for debugging.')
        parser.add_argument('--cmd',
                            action='store_true',
                            default=False,
                            help='Run a command in the VM.')
        parser.add_argument('args',
                            nargs=argparse.REMAINDER,
                            help='Command to run in the VM.')
        return parser
Exemple #19
0
def main(argv):
    parser = GetParser()
    opts = parser.parse_args(argv)
    opts.Freeze()

    # Process list output quickly as it takes no privileges.
    if opts.list:
        tests = set(opts.tests or FindTests((constants.CHROMITE_DIR, )))
        print('\n'.join(sorted(tests)))
        return

    # Many of our tests require a valid chroot to run. Make sure it's created
    # before we block network access.
    chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR)
    if (not os.path.exists(chroot) and ChrootAvailable()
            and not cros_build_lib.IsInsideChroot()):
        cros_build_lib.run(['cros_sdk', '--create'])

    # This is a cheesy hack to make sure gsutil is populated in the cache before
    # we run tests. This is a partial workaround for crbug.com/468838.
    gs.GSContext.GetDefaultGSUtilBin()

    # Now let's run some tests.
    _ReExecuteIfNeeded([sys.argv[0]] + argv, opts.network)
    # A lot of pieces here expect to be run in the root of the chromite tree.
    # Make them happy.
    os.chdir(constants.CHROMITE_DIR)
    tests = opts.tests or FindTests()

    # Clear python caches now that we're root, in the right dir, but before we
    # run any tests.
    ClearPythonCacheFiles()
    if opts.clear_pycache:
        return

    # Sanity check the environment.  https://crbug.com/1015450
    st = os.stat('/')
    if st.st_mode & 0o7777 != 0o755:
        cros_build_lib.Die('The root directory has broken permissions: %o\n'
                           'Fix with: sudo chmod 755 /' % (st.st_mode, ))
    if st.st_uid or st.st_gid:
        cros_build_lib.Die('The root directory has broken ownership: %i:%i'
                           ' (should be 0:0)\nFix with: sudo chown 0:0 /' %
                           (st.st_uid, st.st_gid))

    # Sanity check the settings to avoid bitrot.
    CheckStaleSettings()

    if opts.quick:
        SPECIAL_TESTS.update(SLOW_TESTS)

    global TIMING_CACHE_FILE  # pylint: disable=global-statement
    TIMING_CACHE_FILE = os.path.join(path_util.GetCacheDir(),
                                     constants.COMMON_CACHE,
                                     'run_tests.cache.json')

    jobs = opts.jobs or multiprocessing.cpu_count()

    with cros_build_lib.ContextManagerStack() as stack:
        # If we're running outside the chroot, try to contain ourselves.
        if cgroups.Cgroup.IsSupported(
        ) and not cros_build_lib.IsInsideChroot():
            stack.Add(cgroups.SimpleContainChildren, 'run_tests')

        # Throw all the tests into a custom tempdir so that if we do CTRL+C, we can
        # quickly clean up all the files they might have left behind.
        stack.Add(osutils.TempDir,
                  prefix='chromite.run_tests.',
                  set_global=True,
                  sudo_rm=True)

        with cros_build_lib.TimedSection() as timer:
            result = RunTests(tests,
                              jobs=jobs,
                              chroot_available=ChrootAvailable(),
                              network=opts.network,
                              dryrun=opts.dryrun,
                              failfast=opts.failfast,
                              pyver=opts.pyver)

        if result:
            logging.info('All tests succeeded! (%s total)', timer.delta)
        else:
            return 1

    if not opts.network:
        logging.warning('Network tests skipped; use --network to run them')