Exemplo n.º 1
0
  def testCopyingSymlinks(self):
    in_dir = os.path.join(self.tempdir, 'input')
    in_dir_link = os.path.join(in_dir, 'link')
    in_dir_symlinks_dir = os.path.join(in_dir, 'holding_symlink')
    in_dir_symlinks_dir_link = os.path.join(in_dir_symlinks_dir, 'link')

    out_dir = os.path.join(self.tempdir, 'output')
    out_dir_link = os.path.join(out_dir, 'link')
    out_dir_symlinks_dir = os.path.join(out_dir, 'holding_symlink')
    out_dir_symlinks_dir_link = os.path.join(out_dir_symlinks_dir, 'link')

    # Create directories and symlinks appropriately.
    osutils.SafeMakedirsNonRoot(in_dir)
    osutils.SafeMakedirsNonRoot(in_dir_symlinks_dir)
    os.symlink(self.tempdir, in_dir_link)
    os.symlink(self.tempdir, in_dir_symlinks_dir_link)

    osutils.SafeMakedirsNonRoot(out_dir)

    # Actual execution.
    osutils.CopyDirContents(in_dir, out_dir, symlinks=True)

    # Assertions.
    self.assertTrue(os.path.islink(out_dir_link))
    self.assertTrue(os.path.islink(out_dir_symlinks_dir_link))
Exemplo n.º 2
0
    def __init__(self, cache_dir, cache_user=None):
        self._cache_dir = cache_dir
        self._cache_user = cache_user
        self.staging_dir = os.path.join(cache_dir, self._STAGING_DIR)

        osutils.SafeMakedirsNonRoot(self._cache_dir, user=self._cache_user)
        osutils.SafeMakedirsNonRoot(self.staging_dir, user=self._cache_user)
Exemplo n.º 3
0
 def testCopyEmptyDir(self):
   """Copy "empty" contents from a dir."""
   in_dir = os.path.join(self.tempdir, 'input')
   out_dir = os.path.join(self.tempdir, 'output')
   osutils.SafeMakedirsNonRoot(in_dir)
   osutils.SafeMakedirsNonRoot(out_dir)
   osutils.CopyDirContents(in_dir, out_dir)
Exemplo n.º 4
0
 def testDestinationDirNonEmptyAllowNonEmptySet(self):
   """Copying to a non-empty destination with allow_nonempty does not raise."""
   in_dir = os.path.join(self.tempdir, 'input')
   out_dir = os.path.join(self.tempdir, 'output')
   osutils.SafeMakedirsNonRoot(in_dir)
   osutils.SafeMakedirsNonRoot(out_dir)
   osutils.SafeMakedirsNonRoot(os.path.join(out_dir, 'blah'))
   osutils.CopyDirContents(in_dir, out_dir, allow_nonempty=True)
Exemplo n.º 5
0
 def testDestinationDirNonEmptyRaises(self):
   """Coping to a non-empty destination dir raises."""
   in_dir = os.path.join(self.tempdir, 'input')
   out_dir = os.path.join(self.tempdir, 'output')
   osutils.SafeMakedirsNonRoot(in_dir)
   osutils.SafeMakedirsNonRoot(out_dir)
   osutils.SafeMakedirsNonRoot(os.path.join(out_dir, 'blah'))
   with self.assertRaises(osutils.BadPathsException):
     osutils.CopyDirContents(in_dir, out_dir)
 def setUp(self):
     self.CreateMockOverlay('moblab-generic-vm')
     self._temp_chroot_prefix = os.path.join(self.tempdir, 'chroot')
     osutils.SafeMakedirsNonRoot(self._temp_chroot_prefix)
     self._temp_host_prefix = os.path.join(self.tempdir, 'host')
     osutils.SafeMakedirsNonRoot(self._temp_host_prefix)
     self.PatchObject(commands, 'UploadArchivedFile', autospec=True)
     self._Prepare()
     self.buildstore = FakeBuildStore()
Exemplo n.º 7
0
 def testCopyTree(self):
   """Copy from a dir that contains files."""
   in_dir = os.path.join(self.tempdir, 'input')
   out_dir = os.path.join(self.tempdir, 'output')
   osutils.SafeMakedirsNonRoot(in_dir)
   osutils.SafeMakedirsNonRoot(os.path.join(in_dir, 'a'))
   osutils.WriteFile(os.path.join(in_dir, 'a', 'b.txt'), 'bbb')
   osutils.SafeMakedirsNonRoot(out_dir)
   osutils.CopyDirContents(in_dir, out_dir)
   self.assertEqual(
       osutils.ReadFile(os.path.join(out_dir, 'a', 'b.txt')).strip(), 'bbb')
Exemplo n.º 8
0
    def Create(self,
               moblab_image_dir,
               dut_image_dir='',
               create_vm_images=True):
        """Create a new moblabvm setup.

    Args:
      moblab_image_dir: A directory containing the image for moblab. This
          directory should ideally contain all the files output by build_image
          because they are needed to manipulate the image properly.
      dut_image_dir: A directory containing the image for the sub-DUT. Only
          required if a DUT should be attached to the moblab VM. This directory
          should ideally contain all the files output by build_image because
          they are needed to manipulate the image properly.
      create_vm_images: If True, the source directories contain test images that
          need to be converted to vm images. If False, the source directories
          should already contain VM images to be used directly.
    """
        if self._config[_CONFIG_INITIALIZED]:
            raise SetupError('Cannot overwrite existing setup at %s' %
                             self.workspace)

        logging.notice('Initializing workspace in %s', self.workspace)
        logging.notice('This involves creating some VM images. '
                       'May take a few minutes.')

        logging.notice('Preparing moblab image...')
        moblab_dir = os.path.join(self.workspace, _WORKSPACE_MOBLAB_DIR)
        osutils.SafeMakedirsNonRoot(moblab_dir)
        if create_vm_images:
            self._config[_CONFIG_MOBLAB_IMAGE] = _CreateVMImage(
                moblab_image_dir, moblab_dir)
        else:
            self._config[_CONFIG_MOBLAB_IMAGE] = self._CopyVMImage(
                moblab_image_dir, moblab_dir)

        logging.notice('Generating moblab external disk...')
        moblab_disk = _CreateMoblabDisk(moblab_dir)
        self._config[_CONFIG_MOBLAB_DISK] = moblab_disk

        if dut_image_dir:
            logging.notice('Preparing dut image...')
            dut_dir = os.path.join(self.workspace, _WORKSPACE_DUT_DIR)
            osutils.SafeMakedirsNonRoot(dut_dir)
            if create_vm_images:
                self._config[_CONFIG_DUT_IMAGE] = _CreateVMImage(
                    dut_image_dir, dut_dir)
            else:
                self._config[_CONFIG_DUT_IMAGE] = self._CopyVMImage(
                    dut_image_dir, dut_dir)

        self._config[_CONFIG_INITIALIZED] = 'true'
        self._Persist()
        logging.notice('All Done!')
    def CreateStaticDirectory(cls, static_dir=DEFAULT_STATIC_DIR):
        """Creates |static_dir|.

    Args:
      static_dir: path to the static directory of the devserver instance.
    """
        osutils.SafeMakedirsNonRoot(static_dir)
Exemplo n.º 10
0
def CopyTestcaseToSysroot(src_testcase_path):
    """Copies a testcase into the sysroot.

  Copies a testcase into the sysroot. Doesn't copy if testcase is already in
  sysroot.

  Args:
    src_testcase_path: A path (in the chroot) to a testcase that will be copied
      into sysroot.

  Returns:
    The path in the sysroot that the testcase was copied to.
  """
    if SysrootPath.IsPathInSysroot(src_testcase_path):
        # Don't copy if |src_testcase_path| is already in sysroot. Just return it in
        # the format expected by the caller.
        return SysrootPath(src_testcase_path)

    dest_testcase_path = GetPathForCopy(TESTCASE_DIRECTORY_NAME,
                                        src_testcase_path)
    osutils.SafeMakedirsNonRoot(os.path.dirname(dest_testcase_path.chroot))
    osutils.SafeUnlink(dest_testcase_path.chroot)

    shutil.copy(src_testcase_path, dest_testcase_path.chroot)
    return dest_testcase_path
Exemplo n.º 11
0
def PrepareMoblabVmImageCache(vms, builder, payload_dirs):
    """Preload the given payloads into the moblab VM image cache.

  Args:
    vms (MoblabVm): The Moblab VM.
    builder (str): The builder path, used to name the cache dir.
    payload_dirs (list[str]): List of payload directories to load.

  Returns:
    str: Absolute path to the image cache path.
  """
    with vms.MountedMoblabDiskContext() as disk_dir:
        image_cache_root = os.path.join(disk_dir, 'static/prefetched')
        # If by any chance this path exists, the permission bits are surely
        # nonsense, since 'moblab' user doesn't exist on the host system.
        osutils.RmDir(image_cache_root, ignore_missing=True, sudo=True)

        image_cache_dir = os.path.join(image_cache_root, builder)
        osutils.SafeMakedirsNonRoot(image_cache_dir)
        for payload_dir in payload_dirs:
            osutils.CopyDirContents(payload_dir,
                                    image_cache_dir,
                                    allow_nonempty=True)

    image_cache_rel_dir = image_cache_dir[len(disk_dir):].strip('/')
    return os.path.join('/', 'mnt/moblab', image_cache_rel_dir)
Exemplo n.º 12
0
    def Run(self):
        """The main handler of this CLI."""
        self.options.Freeze()
        cmd = self.options.moblabvm_command
        if cmd == 'create':
            # Allow the workspace to not exist. This makes the CLI uniform across the
            # 'cros moblabvm' commands, without burdening the user with a mkdir before
            # 'create'
            osutils.SafeMakedirsNonRoot(self.options.workspace)

        if not os.path.isdir(self.options.workspace):
            cros_build_lib.Die('Workspace directory %s does not exist!',
                               self.options.workspace)

        vm = moblab_vm.MoblabVm(self.options.workspace)
        if cmd == 'create':
            if self.options.clean:
                vm.Destroy()
            vm.Create(self.options.moblab_image_dir,
                      self.options.dut_image_dir,
                      create_vm_images=not self.options.source_vm_images)
        elif cmd == 'start':
            if self.options.restart:
                vm.Stop()
            vm.Start()
            _Describe(vm)
        elif cmd == 'stop':
            vm.Stop()
        elif cmd == 'destroy':
            vm.Destroy()
        elif cmd == 'describe':
            _Describe(vm)
        else:
            assert False, 'Unrecognized command %s!' % cmd
Exemplo n.º 13
0
def PrepareImage(path, content, domain=None):
    """Prepares a recovery image for OOBE autoconfiguration.

  Args:
    path: Path to the recovery image.
    content: The content of the OOBE autoconfiguration.
    domain: Which domain to enroll to.
  """
    with osutils.TempDir() as tmp, \
      image_lib.LoopbackPartitions(path, tmp) as image:
        stateful_mnt = image.Mount((constants.CROS_PART_STATEFUL, ),
                                   mount_opts=('rw', ))[0]

        # /stateful/unencrypted may not exist at this point in time on the
        # recovery image, so create it root-owned here.
        unencrypted = os.path.join(stateful_mnt, 'unencrypted')
        osutils.SafeMakedirs(unencrypted, mode=0o755, sudo=True)

        # The OOBE autoconfig directory must be owned by the chronos user so
        # that we can delete the config file from it from Chrome.
        oobe_autoconf = os.path.join(unencrypted, _OOBE_DIRECTORY)
        osutils.SafeMakedirsNonRoot(oobe_autoconf, user='******')

        # Create the config file to be owned by the chronos user, and write the
        # given data into it.
        config = os.path.join(oobe_autoconf, _CONFIG_PATH)
        osutils.WriteFile(config, content, sudo=True)
        cros_build_lib.sudo_run(['chown', 'chronos:chronos', config])

        # If we have a plaintext domain name, write it.
        if domain:
            domain_path = os.path.join(oobe_autoconf, _DOMAIN_PATH)
            osutils.WriteFile(domain_path, SanitizeDomain(domain), sudo=True)
            cros_build_lib.sudo_run(['chown', 'chronos:chronos', domain_path])
Exemplo n.º 14
0
 def _LockForKey(self, key, suffix='.lock'):
     """Returns an unacquired lock associated with a key."""
     key_path = self._GetKeyPath(key)
     osutils.SafeMakedirsNonRoot(os.path.dirname(key_path))
     lock_path = os.path.join(self._cache_dir, os.path.dirname(key_path),
                              os.path.basename(key_path) + suffix)
     return locking.FileLock(lock_path)
Exemplo n.º 15
0
 def testSourceDirDoesNotExistRaises(self):
   """Coping from a non-existent source dir raises."""
   in_dir = os.path.join(self.tempdir, 'input')
   out_dir = os.path.join(self.tempdir, 'output')
   osutils.SafeMakedirsNonRoot(out_dir)
   with self.assertRaises(osutils.BadPathsException):
     osutils.CopyDirContents(in_dir, out_dir)
Exemplo n.º 16
0
 def _Insert(self, key, path):
   """Insert a file or a directory into the cache at a given key."""
   self._Remove(key)
   key_path = self.GetKeyPath(key)
   osutils.SafeMakedirsNonRoot(os.path.dirname(key_path),
                               user=self._cache_user)
   shutil.move(path, key_path)
Exemplo n.º 17
0
def main(argv):
    parser = _GetParser()
    options = parser.parse_args(argv)
    options.Freeze()

    with osutils.TempDir() as tempdir:
        # This is rsync in 'archive' mode, but symlinks are followed to copy actual
        # files/directories.
        rsync_cmd = ['rsync', '-qrLgotD', '--exclude=\'*/*.pyc\'']
        chromite_dir = os.path.dirname(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

        cmd = rsync_cmd + [
            'chromite/appengine/',
            tempdir,
            '--exclude=google_appengine_*',
        ]
        cros_build_lib.RunCommand(cmd, cwd=os.path.dirname(chromite_dir))

        cmd = rsync_cmd + [
            'chromite',
            os.path.join(tempdir, 'cq_stats'),
            '--exclude=appengine',
            '--exclude=third_party',
            '--exclude=ssh_keys',
            '--exclude=contrib',
            '--exclude=.git',
            '--exclude=venv',
        ]
        cros_build_lib.RunCommand(cmd, cwd=os.path.dirname(chromite_dir))

        target_creds_dir = os.path.join(tempdir, 'cq_stats',
                                        CREDENTIALS_SUBDIR)
        osutils.SafeMakedirsNonRoot(target_creds_dir)
        cmd = rsync_cmd + [
            options.cidb_creds_dir + '/',
            target_creds_dir,
        ]
        cros_build_lib.RunCommand(cmd, cwd=os.path.dirname(chromite_dir))

        osutils.WriteFile(
            os.path.join(tempdir, 'cq_stats', 'cq_stats',
                         'deploy_settings.py'), _GetDeploySettings(options))

        # update the instance we're updating.
        # Use absolute path. Let's not update sourcedir by mistake.
        app_yaml_path = os.path.join(tempdir, 'cq_stats', 'app.yaml')
        regex = (r's/^application:[ \t]*[a-zA-Z0-9_-\.:]\+[ \t]*$'
                 '/application: %s/')
        cmd = [
            'sed',
            '-i',
            '-e',
            regex % APP_INSTANCE_NAME[options.instance],
            app_yaml_path,
        ]
        cros_build_lib.RunCommand(cmd, cwd=tempdir)

        _DeployApp(tempdir, options)
Exemplo n.º 18
0
    def PerformStage(self):
        test_root_in_chroot = commands.CreateTestRoot(self._build_root)
        test_root = path_util.FromChrootPath(test_root_in_chroot)
        results_dir = os.path.join(test_root, 'results')
        work_dir = os.path.join(test_root, 'workdir')
        osutils.SafeMakedirsNonRoot(results_dir)
        osutils.SafeMakedirsNonRoot(work_dir)

        try:
            self._PerformStage(work_dir, results_dir)
        except:
            logging.error(
                _ERROR_MSG %
                dict(test_name='MoblabVMTest', test_results='directory'))
            raise
        finally:
            self._ArchiveTestResults(results_dir)
Exemplo n.º 19
0
def DownloadFuzzerCorpus(fuzzer, dest_directory=None):
    """Downloads a corpus and returns its path.

  Downloads a corpus to a subdirectory of dest_directory if specified and
  returns path on the filesystem of the corpus. Asks users to authenticate
  if permission to read from bucket is denied.

  Args:
    fuzzer: The name of the fuzzer who's corpus we want to download.
    dest_directory: The directory to download the corpus to.

  Returns:
    The path to the downloaded corpus.

  Raises:
    gs.NoSuchKey: A corpus for the fuzzer doesn't exist.
    gs.GSCommandError: The corpus failed to download for another reason.
  """
    if not fuzzer.startswith('chromeos_'):
        # ClusterFuzz internally appends "chromeos_" to chromeos targets' names.
        # Therefore we must do so in order to find the corpus.
        fuzzer = 'chromeos_%s' % fuzzer

    if dest_directory is None:
        dest_directory = GetScriptStoragePath(CORPUS_DIRECTORY_NAME).chroot
    osutils.SafeMakedirsNonRoot(dest_directory)

    clusterfuzz_gcs_corpus_bucket = 'chromeos-corpus'
    suburl = 'libfuzzer/%s' % fuzzer
    gcs_path = gs.GetGsURL(clusterfuzz_gcs_corpus_bucket,
                           for_gsutil=True,
                           public=False,
                           suburl=suburl)

    dest_path = os.path.join(dest_directory, fuzzer)

    try:
        logging.info('Downloading corpus to %s.', dest_path)
        ctx = gs.GSContext()
        ctx.Copy(gcs_path,
                 dest_directory,
                 recursive=True,
                 parallel=True,
                 debug_level=logging.DEBUG)
        logging.info('Finished downloading corpus.')
    except gs.GSNoSuchKey as exception:
        logging.error('Corpus for fuzzer: %s does not exist.', fuzzer)
        raise exception
    # Try to authenticate if we were denied permission to access the corpus.
    except gs.GSCommandError as exception:
        logging.error(
            'gsutil failed to download the corpus. You may need to log in. See:\n'
            'https://chromium.googlesource.com/chromiumos/docs/+/master/gsutil.md'
            '#setup\n'
            'for instructions on doing this.')
        raise exception

    return dest_path
Exemplo n.º 20
0
    def setUp(self):
        self.chroot_path = os.path.join(self.tempdir, 'chroot')
        osutils.SafeMakedirsNonRoot(self.chroot_path)
        self.chroot_img = self.chroot_path + '.img'

        self._MOUNT = [
            'sudo', '--', 'mount', '-text4', '-onoatime',
            partial_mock.Ignore(), self.chroot_path
        ]
Exemplo n.º 21
0
    def SetUp(self):
        """Sets up devices in the sysroot's /dev.

    Creates /dev/null, /dev/random, and /dev/urandom. If they already exist then
    recreates them.
    """
        self.CleanUp()
        osutils.SafeMakedirsNonRoot(self.dev_path_chroot)
        for device, mknod_params in self.DEVICE_MKNOD_PARAMS.items():
            device_path = self._GetDevicePath(device)
            self._MakeCharDevice(device_path, *mknod_params)
Exemplo n.º 22
0
  def testSafeMakedirsNoSudoRootOwnedDirs(self):
    """Test that we can recover some root owned directories."""
    self.ExpectRootOwnedFiles()
    root_owned_prefix = os.path.join(self.tempdir, 'root_owned_prefix')
    root_owned_dir = os.path.join(root_owned_prefix, 'root_owned_dir')
    non_root_dir = os.path.join(root_owned_prefix, 'non_root_dir')
    self.assertTrue(osutils.SafeMakedirs(root_owned_dir, sudo=True))
    self.assertExists(root_owned_prefix)
    self.assertEqual(os.stat(root_owned_prefix).st_uid, 0)
    self.assertExists(root_owned_dir)
    self.assertEqual(os.stat(root_owned_dir).st_uid, 0)

    # Test that we can reclaim a root-owned dir.
    # Note, return value is False because the directory already exists.
    self.assertFalse(osutils.SafeMakedirsNonRoot(root_owned_dir))
    self.assertNotEqual(os.stat(root_owned_dir).st_uid, 0)

    # Test that we can create a non-root directory in a root-path.
    self.assertTrue(osutils.SafeMakedirsNonRoot(non_root_dir))
    self.assertNotEqual(os.stat(non_root_dir).st_uid, 0)
Exemplo n.º 23
0
  def setUp(self):
    with sudo.SudoKeepAlive():
      # Create just enough of a chroot to fool cros_sdk into accepting it.
      self.chroot = os.path.join(self.tempdir, 'chroot')
      cros_sdk_lib.MountChroot(self.chroot, create=True)
      logging.debug('Chroot mounted on %s', self.chroot)

      chroot_etc = os.path.join(self.chroot, 'etc')
      osutils.SafeMakedirsNonRoot(chroot_etc)

      self.chroot_version_file = os.path.join(chroot_etc, 'cros_chroot_version')
      osutils.Touch(self.chroot_version_file, makedirs=True)
Exemplo n.º 24
0
    def testNotCopyingSymlinks(self):
        # Create temporary to symlink against.
        tmp_file = os.path.join(self.tempdir, 'a.txt')
        osutils.WriteFile(tmp_file, 'aaa')
        tmp_subdir = os.path.join(self.tempdir, 'tmp')
        osutils.SafeMakedirsNonRoot(tmp_subdir)
        tmp_subdir_file = os.path.join(tmp_subdir, 'b.txt')
        osutils.WriteFile(tmp_subdir_file, 'bbb')

        in_dir = os.path.join(self.tempdir, 'input')
        in_dir_link = os.path.join(in_dir, 'link')
        in_dir_symlinks_dir = os.path.join(in_dir, 'holding_symlink')
        in_dir_symlinks_dir_link = os.path.join(in_dir_symlinks_dir, 'link')

        out_dir = os.path.join(self.tempdir, 'output')
        out_dir_file = os.path.join(out_dir, 'link')
        out_dir_symlinks_dir = os.path.join(out_dir, 'holding_symlink')
        out_dir_symlinks_dir_subdir = os.path.join(out_dir_symlinks_dir,
                                                   'link')

        # Create directories and symlinks appropriately.
        osutils.SafeMakedirsNonRoot(in_dir)
        osutils.SafeMakedirsNonRoot(in_dir_symlinks_dir)
        os.symlink(tmp_file, in_dir_link)
        os.symlink(tmp_subdir, in_dir_symlinks_dir_link)

        osutils.SafeMakedirsNonRoot(out_dir)

        # Actual execution.
        osutils.CopyDirContents(in_dir, out_dir, symlinks=False)

        # Assertions.
        self.assertFalse(os.path.islink(out_dir_file))
        self.assertFalse(os.path.islink(out_dir_symlinks_dir_subdir))
        self.assertTrue(filecmp.cmp(out_dir_file, tmp_file))
        self.assertTrue(
            filecmp.cmp(os.path.join(out_dir_symlinks_dir_subdir, 'b.txt'),
                        tmp_subdir_file))
Exemplo n.º 25
0
    def testSuccessfulCreateNoCreateVm(self):
        """A successful call to .Create w/o create_vm_images."""
        self._mock_create_moblab_disk.return_value = self.moblab_disk_path
        osutils.SafeMakedirsNonRoot(self.moblab_from_path)
        osutils.WriteFile(
            os.path.join(self.moblab_from_path, 'chromiumos_qemu_image.bin'),
            'fake_image')

        vmsetup = moblab_vm.MoblabVm(self.workspace)
        vmsetup.Create(self.moblab_from_path, create_vm_images=False)
        self.assertEqual(self._mock_create_vm_image.call_count, 0)
        self._mock_create_moblab_disk.assert_called_once_with(
            self.moblab_workdir_path)
        self.assertTrue(vmsetup.initialized)
        self.assertFalse(vmsetup.running)
        self.assertFalse(vmsetup.dut_running)
Exemplo n.º 26
0
    def Run(self):
        """Perform the cros stage command."""
        logging.info('Attempting to stage: %s as Image: %s at Location: %s',
                     self.options.image, self.staged_image_name,
                     self.options.remote)
        osutils.SafeMakedirsNonRoot(flash.DEVSERVER_STATIC_DIR)

        with osutils.TempDir() as tempdir:
            if self._remote_image:
                self._DownloadPayloads(tempdir)
            else:
                self._GeneratePayloads(tempdir)
            self._GenerateTestBits(tempdir)
            if self._remote_is_moblab:
                self._StageOnMoblab(tempdir)
            else:
                self._StageOnGS(tempdir)
Exemplo n.º 27
0
    def _PerformStage(self, workdir, results_dir):
        """Actually performs this stage.

    Args:
      workdir: The workspace directory to use for all temporary files.
      results_dir: The directory to use to drop test results into.
    """
        dut_target_image = self._SubDutTargetImage()
        osutils.SafeMakedirsNonRoot(self._Workspace(workdir))
        vms = moblab_vm.MoblabVm(self._Workspace(workdir))
        try:
            r = ' reached %s test run timeout.' % self
            with timeout_util.Timeout(self._PERFORM_TIMEOUT_S,
                                      reason_message=r):
                start_time = datetime.datetime.now()
                vms.Create(self.GetImageDirSymlink(),
                           self.GetImageDirSymlink())
                payload_dir = self._GenerateTestArtifactsInMoblabDisk(vms)
                vms.Start()

                elapsed = (datetime.datetime.now() -
                           start_time).total_seconds()
                RunMoblabTests(
                    moblab_board=self._current_board,
                    moblab_ip=vms.moblab_ssh_port,
                    dut_target_image=dut_target_image,
                    results_dir=results_dir,
                    local_image_cache=payload_dir,
                    timeout_m=(self._PERFORM_TIMEOUT_S - elapsed) // 60,
                )

            vms.Stop()
            ValidateMoblabTestSuccess(results_dir)
        except:
            # Ignore errors while arhiving images, but re-raise the original error.
            try:
                vms.Stop()
                self._ArchiveMoblabVMWorkspace(self._Workspace(workdir))
            except Exception as e:
                logging.error(
                    'Failed to archive VM images after test failure: %s', e)
            raise
        finally:
            vms.Destroy()
Exemplo n.º 28
0
    def _GenerateTestArtifactsInMoblabDisk(self, vms):
        """Generates test artifacts into devserver cache directory in moblab's disk.

    Args:
      vms: A moblab_vm.MoblabVm instance that has been Createed but not Started.

    Returns:
      The absolute path inside moblab VM where the image cache is located.
    """
        with vms.MountedMoblabDiskContext() as disk_dir:
            # If by any chance this path exists, the permission bits are surely
            # nonsense, since 'moblab' user doesn't exist on the host system.
            osutils.RmDir(os.path.join(disk_dir, _MOBLAB_PAYLOAD_CACHE_DIR),
                          ignore_missing=True,
                          sudo=True)
            payloads_dir = os.path.join(disk_dir, _MOBLAB_PAYLOAD_CACHE_DIR,
                                        self._SubDutTargetImage())
            # moblab VM will chown this folder upon boot, so once again permission
            # bits from the host don't matter.
            osutils.SafeMakedirsNonRoot(payloads_dir)
            target_image_path = os.path.join(self.GetImageDirSymlink(),
                                             constants.TEST_IMAGE_BIN)
            commands.GeneratePayloads(target_image_path=target_image_path,
                                      archive_dir=payloads_dir,
                                      full=True,
                                      delta=False,
                                      stateful=True,
                                      dlc=False)
            commands.GenerateQuickProvisionPayloads(
                target_image_path=target_image_path, archive_dir=payloads_dir)
            cwd = os.path.abspath(
                os.path.join(self._build_root, 'chroot', 'build',
                             self._current_board,
                             constants.AUTOTEST_BUILD_PATH, '..'))
            logging.debug(
                'Running BuildAutotestTarballsForHWTest root %s cwd %s target %s',
                self._build_root, cwd, payloads_dir)
            commands.BuildAutotestTarballsForHWTest(self._build_root, cwd,
                                                    payloads_dir)
        return os.path.join(_MOBLAB_STATIC_MOUNT_PATH,
                            _MOBLAB_PAYLOAD_CACHE_DIR)
Exemplo n.º 29
0
  def testUnmountTree(self):
    with osutils.TempDir(prefix='chromite.test.osutils') as tempdir:
      # Mount the dir and verify it worked.
      st_before = os.stat(tempdir)
      osutils.MountTmpfsDir(tempdir)
      st_after = os.stat(tempdir)
      self.assertNotEqual(st_before.st_dev, st_after.st_dev)

      # Mount an inner dir the same way.
      tempdir2 = os.path.join(tempdir, 'inner')
      osutils.SafeMakedirsNonRoot(tempdir2)
      st_before2 = os.stat(tempdir2)
      osutils.MountTmpfsDir(tempdir2)
      st_after2 = os.stat(tempdir2)
      self.assertNotEqual(st_before2.st_dev, st_after2.st_dev)

      # Unmount the whole tree and verify it worked.
      osutils.UmountTree(tempdir)
      st_umount = os.stat(tempdir)
      self.assertNotExists(tempdir2)
      self.assertEqual(st_before.st_dev, st_umount.st_dev)
Exemplo n.º 30
0
    def Install(self):
        """Installs (sets up) an LLVM binary in the sysroot.

    Sets up an llvm binary in the sysroot so that it can be run there.
    """
        # Create a directory for installing |binary| and all of its dependencies in
        # the sysroot.
        binary_rel_path = ['usr', 'bin', self.binary]
        binary_chroot_path = os.path.join('/', *binary_rel_path)
        if not os.path.exists(binary_chroot_path):
            logging.warning('Cannot copy %s, file does not exist in chroot.',
                            binary_chroot_path)
            logging.warning('Functionality provided by %s will be missing.',
                            binary_chroot_path)
            return

        osutils.SafeMakedirsNonRoot(self.install_dir)

        # Copy the binary and everything needed to run it into the sysroot.
        cmd = [
            self.LDDTREE_SCRIPT_PATH,
            '-v',
            '--generate-wrappers',
            '--root',
            '/',
            '--copy-to-tree',
            self.install_dir,
            binary_chroot_path,
        ]
        sudo_run(cmd)

        # Create a symlink to the copy of the binary (we can't do lddtree in
        # self.binary_dir_path). Note that symlink should be relative so that it
        # will be valid when chrooted into the sysroot.
        rel_path = os.path.relpath(self.install_dir, self.binary_dir_path)
        link_path = os.path.join(rel_path, *binary_rel_path)
        osutils.SafeSymlink(link_path, self.binary_chroot_dest_path, sudo=True)