def setUp(self):
        self.router = router.Router()
        self.router.Register(build_api_test_pb2)

        self.input_file = os.path.join(self.tempdir, 'input.json')
        self.output_file = os.path.join(self.tempdir, 'output.json')

        self.chroot_dir = os.path.join(self.tempdir, 'chroot')
        chroot_tmp = os.path.join(self.chroot_dir, 'tmp')
        # Make the tmp dir for the re-exec inside chroot input/output files.
        osutils.SafeMakedirs(chroot_tmp)

        osutils.WriteFile(self.input_file,
                          self._INPUT_JSON_TEMPLATE % self.chroot_dir)
        osutils.WriteFile(self.output_file, '{}')

        self.subprocess_tempdir = os.path.join(self.chroot_dir, 'tempdir')
        osutils.SafeMakedirs(self.subprocess_tempdir)
        self.PatchObject(osutils.TempDir,
                         '__enter__',
                         return_value=self.subprocess_tempdir)
 def testReadsBoardToolchains(self, find_overlays_mock):
   """Tests that we correctly parse toolchain configs for an overlay stack."""
   # Create some fake overlays and put toolchain confs in a subset of them.
   overlays = [os.path.join(self.tempdir, 'overlay%d' % i) for i in range(3)]
   for overlay in overlays:
     osutils.SafeMakedirs(overlay)
   for overlay, contents in [(overlays[0], BASE_TOOLCHAIN_CONF),
                             (overlays[2], ADDITIONAL_TOOLCHAIN_CONF)]:
     osutils.WriteFile(os.path.join(overlay, 'toolchain.conf'), contents)
   find_overlays_mock.return_value = overlays
   actual_targets = toolchain.GetToolchainsForBoard('board_value')
   self.assertEqual(EXPECTED_TOOLCHAINS, actual_targets)
  def PerformStage(self):
    """Generate and upload CPE files."""
    buildroot = self._build_root
    board = self._current_board
    useflags = self._run.config.useflags

    logging.info('Generating CPE export.')
    result = commands.GenerateCPEExport(buildroot, board, useflags)

    logging.info('Writing CPE export to files for archive.')
    warnings_filename = os.path.join(self.archive_path,
                                     'cpe-warnings-chromeos-%s.txt' % board)
    results_filename = os.path.join(self.archive_path,
                                    'cpe-chromeos-%s.json' % board)

    osutils.WriteFile(warnings_filename, result.error)
    osutils.WriteFile(results_filename, result.output)

    logging.info('Uploading CPE files.')
    self.UploadArtifact(os.path.basename(warnings_filename), archive=False)
    self.UploadArtifact(os.path.basename(results_filename), archive=False)
Beispiel #4
0
 def testCacheInvalidTokenNotExists(self):
   """Test cache exists but invalid, token not exists."""
   invalid_cache = self.FAKE_CACHE.copy()
   invalid_cache['invalid'] = True
   osutils.WriteFile(self.token_cache_file, json.dumps(invalid_cache))
   msg = alerts.CreateEmail('fake subject', 'fake@localhost', 'fake msg')
   server = alerts.GmailServer(token_cache_file=self.token_cache_file,
                               token_json_file=self.token_json_file)
   ret = server.Send(msg)
   self.assertFalse(ret)
   invalid_cache = json.loads(osutils.ReadFile(self.token_cache_file))
   self.assertTrue(invalid_cache['invalid'])
Beispiel #5
0
  def _UploadSdkTarball(self, board_path, url_suffix, prepackaged,
                        toolchains_overlay_tarballs,
                        toolchains_overlay_upload_path,
                        toolchain_tarballs, toolchain_upload_path):
    """Upload a tarball of the sdk at the specified path to Google Storage.

    Args:
      board_path: The path to the board dir.
      url_suffix: The remote subdirectory where we should upload the packages.
      prepackaged: If given, a tarball that has been packaged outside of this
                   script and should be used.
      toolchains_overlay_tarballs: List of toolchains overlay tarball
          specifications to upload. Items take the form
          "toolchains_spec:/path/to/tarball".
      toolchains_overlay_upload_path: Path template under the bucket to place
          toolchains overlay tarballs.
      toolchain_tarballs: List of toolchain tarballs to upload.
      toolchain_upload_path: Path under the bucket to place toolchain tarballs.
    """
    remote_location = '%s/%s' % (self._upload_location.rstrip('/'), url_suffix)
    assert remote_location.startswith('gs://')
    boardname = os.path.basename(board_path.rstrip('/'))
    # We do not upload non SDK board tarballs,
    assert boardname == constants.CHROOT_BUILDER_BOARD
    assert prepackaged is not None

    version_str = self._version[len('chroot-'):]
    remote_tarfile = toolchain.GetSdkURL(
        for_gsutil=True, suburl='cros-sdk-%s.tar.xz' % (version_str,))
    # For SDK, also upload the manifest which is guaranteed to exist
    # by the builderstage.
    self._Upload(prepackaged + '.Manifest', remote_tarfile + '.Manifest')
    self._Upload(prepackaged, remote_tarfile)

    # Upload SDK toolchains overlays and toolchain tarballs, if given.
    for tarball_list, upload_path, qualifier_name in (
        (toolchains_overlay_tarballs, toolchains_overlay_upload_path,
         'toolchains'),
        (toolchain_tarballs, toolchain_upload_path, 'target')):
      for tarball_spec in tarball_list:
        qualifier_val, local_path = tarball_spec.split(':')
        suburl = upload_path % {qualifier_name: qualifier_val}
        remote_path = toolchain.GetSdkURL(for_gsutil=True, suburl=suburl)
        self._Upload(local_path, remote_path)

    # Finally, also update the pointer to the latest SDK on which polling
    # scripts rely.
    with osutils.TempDir() as tmpdir:
      pointerfile = os.path.join(tmpdir, 'cros-sdk-latest.conf')
      remote_pointerfile = toolchain.GetSdkURL(for_gsutil=True,
                                               suburl='cros-sdk-latest.conf')
      osutils.WriteFile(pointerfile, 'LATEST_SDK="%s"' % version_str)
      self._Upload(pointerfile, remote_pointerfile)
    def setUp(self):
        self.root = os.path.join(self.tempdir,
                                 'chroot/build/target/build/dev-install/')
        self.packages_file = os.path.join(self.root, 'package.installable')
        osutils.SafeMakedirs(self.root)
        package_file_content = """\
x11-apps/intel-gpu-tools-1.22
x11-libs/gdk-pixbuf-2.36.12-r1
x11-misc/read-edid-1.4.2
virtual/acl-0-r1
"""
        osutils.WriteFile(self.packages_file, package_file_content)
    def testMismatchAppId(self):
        """Tests that we correctly set the payload App ID to empty when mismatch."""
        self._payload_dir = self.tempdir

        with remote_access.ChromiumOSDeviceHandler(
                remote_access.TEST_IP) as device:
            CrOS_AU = auto_updater.ChromiumOSUpdater(device, None,
                                                     self._payload_dir)
            prop_file = os.path.join(self._payload_dir, 'payload.json')
            osutils.WriteFile(prop_file, '{"appid": "helloworld!"}')
            self.PatchObject(remote_access.ChromiumOSDevice,
                             'app_id',
                             return_value='different')
            CrOS_AU.ResolveAPPIDMismatchIfAny(prop_file)
            self.assertEqual(osutils.ReadFile(prop_file), '{"appid": ""}')

            osutils.WriteFile(prop_file, '{"appid": "same"}')
            self.PatchObject(remote_access.ChromiumOSDevice,
                             'app_id',
                             return_value='same')
            self.assertEqual(osutils.ReadFile(prop_file), '{"appid": "same"}')
Beispiel #8
0
    def _MockParseWorkonVariables(self, fake_projects, fake_srcpaths,
                                  fake_localnames, fake_subdirs,
                                  fake_ebuild_contents):
        """Mock the necessary calls, call GetSourcePath()."""
        def _isdir(path):
            """Mock function for os.path.isdir"""
            if any(fake_srcpaths):
                if path == os.path.join(self.tempdir, 'src'):
                    return True

            for srcpath in fake_srcpaths:
                if srcpath:
                    if path == os.path.join(self.tempdir, 'src', srcpath):
                        return True
                else:
                    for localname, subdir in zip(fake_localnames,
                                                 fake_subdirs):
                        if path == os.path.join(self.tempdir, localname,
                                                subdir):
                            return False
                        elif path == os.path.join(self.tempdir, 'platform',
                                                  localname, subdir):
                            return True

            raise Exception('unhandled path: %s' % path)

        def _FindCheckoutFromPath(path):
            """Mock function for manifest.FindCheckoutFromPath"""
            for project, localname, subdir in zip(fake_projects,
                                                  fake_localnames,
                                                  fake_subdirs):
                if path == os.path.join(self.tempdir, 'platform', localname,
                                        subdir):
                    return {'name': project}
            return {}

        self.PatchObject(os.path, 'isdir', side_effect=_isdir)
        self.PatchObject(MANIFEST,
                         'FindCheckoutFromPath',
                         side_effect=_FindCheckoutFromPath)

        if not fake_srcpaths:
            fake_srcpaths = [''] * len(fake_projects)
        if not fake_projects:
            fake_projects = [''] * len(fake_srcpaths)

        # We need 'chromeos-base' here because it controls default _SUBDIR values.
        ebuild_path = os.path.join(self.tempdir, 'packages', 'chromeos-base',
                                   'package', 'package-9999.ebuild')
        osutils.WriteFile(ebuild_path, fake_ebuild_contents, makedirs=True)

        ebuild = portage_util.EBuild(ebuild_path)
        return ebuild.GetSourcePath(self.tempdir, MANIFEST)
 def testGetLastBuildStateGoodFile(self):
     """Tests GetLastBuildState on a good file."""
     osutils.SafeMakedirs(self.root)
     osutils.WriteFile(
         self.previous_build_state,
         '{"build_number": 1, "master_build_id": 3, "status": "pass"}')
     state = cbuildbot_launch.GetLastBuildState(self.root)
     self.assertEqual(
         state,
         build_summary.BuildSummary(build_number=1,
                                    master_build_id=3,
                                    status='pass'))
Beispiel #10
0
 def testCheckoutChromeLKGM(self):
     "Tests that we can read/obtain the old LKGM from mocked out SVN."
     # Write out an old lkgm file as if we got it from svn update.
     old_lkgm = '2098.0.0'
     osutils.WriteFile(self.lkgm_file, old_lkgm)
     self.committer.CheckoutChromeLKGM()
     self.assertTrue(self.committer._old_lkgm, old_lkgm)
     self.assertCommandContains([
         '%s/%s' % (gclient.CHROME_COMMITTER_URL,
                    os.path.dirname(constants.SVN_CHROME_LKGM))
     ])
     self.assertCommandContains([constants.CHROME_LKGM_FILE])
def _UpdateAlternatesDir(alternates_root, reference_maps, projects):
    for project in projects:
        alt_path = os.path.join(alternates_root, project)
        paths = []
        for k, v in reference_maps.iteritems():
            suffix = os.path.join('.repo', 'project-objects', project,
                                  'objects')
            if os.path.exists(os.path.join(k, suffix)):
                paths.append(os.path.join(v, suffix))

        osutils.SafeMakedirs(os.path.dirname(alt_path))
        osutils.WriteFile(alt_path, '%s\n' % ('\n'.join(paths), ), atomic=True)
Beispiel #12
0
  def testBasicRetry(self):
    # pylint: disable=E1101
    path = os.path.join(self.tempdir, 'script')
    paths = {
      'stop': os.path.join(self.tempdir, 'stop'),
      'store': os.path.join(self.tempdir, 'store')
    }
    osutils.WriteFile(path,
        "import sys\n"
        "val = int(open(%(store)r).read())\n"
        "stop_val = int(open(%(stop)r).read())\n"
        "open(%(store)r, 'w').write(str(val + 1))\n"
        "print val\n"
        "sys.exit(0 if val == stop_val else 1)\n" % paths)

    os.chmod(path, 0755)

    def _setup_counters(start, stop, sleep, sleep_cnt):
      self.mox.ResetAll()
      for i in xrange(sleep_cnt):
        time.sleep(sleep * (i + 1))
      self.mox.ReplayAll()

      osutils.WriteFile(paths['store'], str(start))
      osutils.WriteFile(paths['stop'], str(stop))

    self.mox.StubOutWithMock(time, 'sleep')
    self.mox.ReplayAll()

    _setup_counters(0, 0, 0, 0)
    command = ['python', path]
    kwds = {'redirect_stdout': True, 'print_cmd': False}

    self.assertEqual(cros_build_lib.RunCommand(command, **kwds).output, '0\n')

    func = cros_build_lib.RunCommandWithRetries

    _setup_counters(2, 2, 0, 0)
    self.assertEqual(func(0, command, sleep=0, **kwds).output, '2\n')
    self.mox.VerifyAll()

    _setup_counters(0, 2, 1, 2)
    self.assertEqual(func(2, command, sleep=1, **kwds).output, '2\n')
    self.mox.VerifyAll()

    _setup_counters(0, 1, 2, 1)
    self.assertEqual(func(1, command, sleep=2, **kwds).output, '1\n')
    self.mox.VerifyAll()

    _setup_counters(0, 3, 3, 2)
    self.assertRaises(cros_build_lib.RunCommandError,
                      func, 2, command, sleep=3, **kwds)
    self.mox.VerifyAll()
def _CreateWrapper(wrapper_path, template, **kwargs):
  """Creates a wrapper from a given template.

  Args:
    wrapper_path: path to the wrapper.
    template: wrapper template.
    kwargs: fields to be set in the template.
  """
  osutils.WriteFile(wrapper_path, template.format(**kwargs), makedirs=True,
                    sudo=True)
  cros_build_lib.SudoRunCommand(['chmod', '+x', wrapper_path], print_cmd=False,
                                redirect_stderr=True)
  def testMergeBotoConfig(self):
    boto_config = os.path.join(self.tempdir, 'boto.cfg')
    osutils.WriteFile(boto_config, '[S]\nk = v')
    os.environ['BOTO_CONFIG'] = boto_config

    with boto_compat.FixBotoCerts(strict=True):
      config = configparser.SafeConfigParser()
      config.read(os.environ['BOTO_CONFIG'])
      self.assertEqual(config.get('S', 'k'), 'v')
      self.assertTrue(config.has_option('Boto', 'ca_certificates_file'))

    self.assertEqual(os.environ['BOTO_CONFIG'], boto_config)
Beispiel #15
0
    def _CreateLogFile(self, name, timestamp):
        """Creates a log file for testing.

    Args:
      name (str): Log file name.
      timestamp (datetime): timestamp that is written to the file.
    """
        path = os.path.join(
            self.goma_log_dir, '%s.host.log.INFO.%s' %
            (name, timestamp.strftime('%Y%m%d-%H%M%S.%f')))
        osutils.WriteFile(
            path, timestamp.strftime('Log file created at: %Y/%m/%d %H:%M:%S'))
    def testValidateMoblabTestSuccessTestFailedRaises(self):
        """ValidateMoblabTestSuccess raises when logs indicate test failed."""
        os.makedirs(os.path.join(self.tempdir, 'debug'))
        osutils.WriteFile(
            os.path.join(self.tempdir, 'debug', 'test_that.INFO'), """
Some random stuff.
01/08 15:00:28.679 INFO  autoserv| [stderr] Suite job          [ PASSED ]
01/08 15:00:28.680 INFO  autoserv| [stderr] dummy_PassServer   [ FAILED ]
01/08 15:00:28.681 INFO  autoserv| [stderr]
01/08 15:00:28.681 INFO  autoserv| [stderr] Suite timings:""")
        with self.assertRaises(failures_lib.TestFailure):
            vm_test_stages.ValidateMoblabTestSuccess(self.tempdir)
Beispiel #17
0
    def testGenerateHashes(self):
        """Test _GenerateHashes."""
        gen = self._GetStdGenerator()

        # Stub out the required functions.
        run_mock = self.PatchObject(paygen_payload_lib.PaygenPayload,
                                    '_RunGeneratorCmd')
        osutils.WriteFile(gen.payload_hash_file, 'payload')
        osutils.WriteFile(gen.metadata_hash_file, 'hash')

        # Run the test.
        self.assertEqual(gen._GenerateHashes(), (b'payload', b'hash'))

        # Check the expected function calls.
        cmd = [
            'delta_generator', '--in_file=' + gen.payload_file,
            '--signature_size=256',
            partial_mock.HasString('--out_hash_file='),
            partial_mock.HasString('--out_metadata_hash_file=')
        ]
        run_mock.assert_called_once_with(cmd)
Beispiel #18
0
 def testGitRepoHasChanges(self):
   """Tests that GitRepoHasChanges works correctly."""
   git.RunGit(self.tempdir,
              ['clone', '--depth=1', constants.CHROMITE_DIR, self.tempdir])
   # No changes yet as we just cloned the repo.
   self.assertFalse(portage_util.EBuild.GitRepoHasChanges(self.tempdir))
   # Update metadata but no real changes.
   osutils.Touch(os.path.join(self.tempdir, 'LICENSE'))
   self.assertFalse(portage_util.EBuild.GitRepoHasChanges(self.tempdir))
   # A real change.
   osutils.WriteFile(os.path.join(self.tempdir, 'LICENSE'), 'hi')
   self.assertTrue(portage_util.EBuild.GitRepoHasChanges(self.tempdir))
Beispiel #19
0
    def setUp(self):
        self.proc_mount = os.path.join(self.tempdir, 'mounts')
        osutils.WriteFile(
            self.proc_mount,
            r'''/dev/loop0 /mnt/dir_8 ext4 rw,relatime,data=ordered 0 0
/dev/loop2 /mnt/dir_1 ext4 rw,relatime,data=ordered 0 0
/dev/loop1 /mnt/dir_12 vfat rw 0 0
/dev/loop4 /mnt/dir_3 ext4 ro,relatime 0 0
weird\040system /mnt/weirdo unknown ro 0 0
tmpfs /mnt/spaced\040dir tmpfs ro 0 0
tmpfs /mnt/\134 tmpfs ro 0 0
''')
    def testGetPrebuiltsFiles(self):
        """GetPrebuiltsFiles returns all archives for all packages."""
        packages_content = """\
ARCH: amd64
URI: gs://foo_prebuilts

CPV: package/prebuilt_a

CPV: package/prebuilt_b
    """
        osutils.WriteFile(os.path.join(self.root, 'Packages'),
                          packages_content)
        osutils.WriteFile(os.path.join(self.root, 'package/prebuilt_a.tbz2'),
                          'a',
                          makedirs=True)
        osutils.WriteFile(os.path.join(self.root, 'package/prebuilt_b.tbz2'),
                          'b')

        actual = binhost.GetPrebuiltsFiles(self.root)
        expected = ['package/prebuilt_a.tbz2', 'package/prebuilt_b.tbz2']
        self.assertEqual(actual, expected)
Beispiel #21
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')
Beispiel #22
0
 def Save(self):
     """Save KeyVersions to disk if needed."""
     if self.saved:
         return
     keys = sorted(self._versions.keys())
     if 'name' in keys:
         keys.remove('name')
         keys.insert(0, 'name')
     lines = ['%s=%s' % (k, str(self._versions[k])) for k in keys]
     contents = '\n'.join(lines) + '\n'
     osutils.WriteFile(self._path, contents)
     self.saved = True
 def testGenerateHtmlIndexTuplePretty(self):
   """Verifies GenerateHtmlIndex gives us something pretty (input: tuple)"""
   index = os.path.join(self.tempdir, 'index.html')
   files = ('..|up', 'f.txt|MY FILE', 'm.log|MONKEY', 'b.bin|Yander',)
   commands.GenerateHtmlIndex(index, files)
   html = osutils.ReadFile(index)
   osutils.WriteFile('/tmp/foo.html', html)
   for f in files:
     a = f.split('|')
     # TODO(build): Use assertIn w/python-2.7.
     self.assertTrue('href="%s"' % a[0] in html)
     self.assertTrue('>%s</a>' % a[1] in html)
    def testBinaryMiscFiles(self):
        """Test for various binary file formats."""
        # A timezone file.
        some_timezone = os.path.join(self.tempdir, 'some_timezone')
        osutils.WriteFile(
            some_timezone,
            b'TZif2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
            b'\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
            b'\x00\x01\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00UTC\x00\x00\x00TZif2'
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
            b'\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
            b'\x01\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00UTC\x00\x00\x00\nUTC0\n',
            mode='wb')
        self.assertEqual('binary/tzfile',
                         filetype.FileTypeDecoder.DecodeFile(some_timezone))

        # A x86 boot sector with just nops.
        bootsec = os.path.join(self.tempdir, 'bootsec')
        osutils.WriteFile(bootsec, b'\x90' * 510 + b'\x55\xaa', mode='wb')
        self.assertEqual('binary/bootsector/x86',
                         filetype.FileTypeDecoder.DecodeFile(bootsec))
Beispiel #25
0
    def SaveLicenseDump(self, save_file):
        """Save PackageInfo contents to a YAML file.

    This is used to cache license results between the emerge hook phase and
    credits page generation.

    Args:
      save_file: File to save the yaml contents into.
    """
        logging.debug('Saving license to %s', save_file)
        yaml_dump = self.__dict__.items()
        osutils.WriteFile(save_file, yaml.dump(yaml_dump), makedirs=True)
def _WriteLinesToFile(path, lines, line_prefix, line_suffix):
    """Write a set of lines to a file, adding prefixes, suffixes and newlines.

  Args:
    path: path to file.
    lines: iterable of lines to write.
    line_prefix: string to prefix each line with.
    line_suffix: string to append to each line before a newline.
  """
    contents = ''.join(
        ['%s%s%s\n' % (line_prefix, line, line_suffix) for line in lines])
    osutils.WriteFile(path, contents, makedirs=True)
  def testBasicRetry(self):
    path = os.path.join(self.tempdir, 'script')
    paths = {
        'stop': os.path.join(self.tempdir, 'stop'),
        'store': os.path.join(self.tempdir, 'store'),
    }
    osutils.WriteFile(
        path,
        'from __future__ import print_function\n'
        'import sys\n'
        'val = int(open(%(store)r).read())\n'
        'stop_val = int(open(%(stop)r).read())\n'
        "open(%(store)r, 'w').write(str(val + 1))\n"
        'print(val)\n'
        'sys.exit(0 if val == stop_val else 1)\n' % paths)

    os.chmod(path, 0o755)

    def _SetupCounters(start, stop):
      sleep_mock.reset_mock()
      osutils.WriteFile(paths['store'], str(start))
      osutils.WriteFile(paths['stop'], str(stop))

    def _AssertCounters(sleep, sleep_cnt):
      calls = [mock.call(float(sleep * (x + 1))) for x in range(sleep_cnt)]
      sleep_mock.assert_has_calls(calls)

    sleep_mock = self.PatchObject(time, 'sleep')

    _SetupCounters(0, 0)
    command = [sys.executable, path]
    kwargs = {'stdout': True, 'print_cmd': False}
    self.assertEqual(cros_build_lib.run(command, **kwargs).output, b'0\n')
    _AssertCounters(0, 0)

    func = retry_util.RunCommandWithRetries

    _SetupCounters(2, 2)
    self.assertEqual(func(0, command, sleep=0, **kwargs).output, b'2\n')
    _AssertCounters(0, 0)

    _SetupCounters(0, 2)
    self.assertEqual(func(2, command, sleep=1, **kwargs).output, b'2\n')
    _AssertCounters(1, 2)

    _SetupCounters(0, 1)
    self.assertEqual(func(1, command, sleep=2, **kwargs).output, b'1\n')
    _AssertCounters(2, 1)

    _SetupCounters(0, 3)
    with self.assertRaises(cros_build_lib.RunCommandError):
      func(2, command, sleep=3, **kwargs)
    _AssertCounters(3, 2)
Beispiel #28
0
    def _WriteToMoblabDiskImage(self, target_path, lines):
        """Write a file in the moblab external disk.

    Args:
      target_path: Path within the mounted disk to write. This path will be
          overwritten.
      lines: An iterator of lines to write.
    """
        with self.MountedMoblabDiskContext() as disk_dir:
            osutils.WriteFile(os.path.join(disk_dir, target_path),
                              lines,
                              sudo=True)
Beispiel #29
0
    def RunCommand(self, cmd, **kwargs):
        """Executes a shell command on the device with output captured by default.

    Also sets environment variables using dictionary provided by
    keyword argument |extra_env|.

    Args:
      cmd: command to run. See RemoteAccess.RemoteSh documentation.
      **kwargs: keyword arguments to pass along with cmd. See
        RemoteAccess.RemoteSh documentation.
    """
        # Handle setting environment variables on the device by copying
        # and sourcing a temporary environment file.
        extra_env = kwargs.pop('extra_env', None)
        if extra_env:
            remote_sudo = kwargs.pop('remote_sudo', False)
            if remote_sudo and self.GetAgent().username == ROOT_ACCOUNT:
                remote_sudo = False

            new_cmd = []
            flat_vars = [
                '%s=%s' % (k, cros_build_lib.ShellQuote(v))
                for k, v in extra_env.iteritems()
            ]

            # If the vars are too large for the command line, do it indirectly.
            # We pick 32k somewhat arbitrarily -- the kernel should accept this
            # and rarely should remote commands get near that size.
            ARG_MAX = 32 * 1024

            # What the command line would generally look like on the remote.
            cmdline = ' '.join(flat_vars + cmd)
            if len(cmdline) > ARG_MAX:
                env_list = ['export %s' % x for x in flat_vars]
                with tempfile.NamedTemporaryFile(dir=self.tempdir.tempdir,
                                                 prefix='env') as f:
                    logging.debug('Environment variables: %s',
                                  ' '.join(env_list))
                    osutils.WriteFile(f.name, '\n'.join(env_list))
                    self.CopyToWorkDir(f.name)
                    env_file = os.path.join(self.work_dir,
                                            os.path.basename(f.name))
                    new_cmd += ['.', '%s;' % env_file]
                    if remote_sudo:
                        new_cmd += ['sudo', '-E']
            else:
                if remote_sudo:
                    new_cmd += ['sudo']
                new_cmd += flat_vars

            cmd = new_cmd + cmd

        return self.BaseRunCommand(cmd, **kwargs)
Beispiel #30
0
    def RegisterBinfmt(self):
        """Make sure qemu has been registered as a format handler

    Prep the binfmt handler. First mount if needed, then unregister any bad
    mappings, and then register our mapping.

    There may still be some race conditions here where one script
    de-registers and another script starts executing before it gets
    re-registered, however it should be rare.
    """
        if not os.path.exists(self._BINFMT_REGISTER_PATH):
            osutils.Mount('binfmt_misc', self._BINFMT_PATH, 'binfmt_misc', 0)

        if os.path.exists(self.binfmt_path):
            interp = b'\ninterpreter %s\n' % (self.GetFullInterpPath(
                self.build_path).encode('utf-8'), )
            data = b'\n' + osutils.ReadFile(self.binfmt_path, mode='rb')
            if interp not in data:
                logging.info('recreating binfmt config: %s', self.binfmt_path)
                logging.debug('config was missing line: %s', interp.strip())
                logging.debug('existing config:\n%s', data.strip())
                try:
                    osutils.WriteFile(self.binfmt_path, '-1')
                except IOError as e:
                    logging.error('unregistering config failed: %s: %s',
                                  self.binfmt_path, e)
                    return

        if not os.path.exists(self.binfmt_path):
            register = self.GetRegisterBinfmtStr(self.arch, self.name,
                                                 self.build_path)
            try:
                osutils.WriteFile(self._BINFMT_REGISTER_PATH,
                                  register,
                                  mode='wb')
            except IOError as e:
                logging.error('binfmt register attempt failed: %s: %s',
                              self._BINFMT_REGISTER_PATH, e)
                logging.error('register data (len:%i): %s', len(register),
                              register)