def _StoreResults(self, experiment):
        if self._terminated:
            return
        results_directory = experiment.results_directory
        FileUtils().RmDir(results_directory)
        FileUtils().MkDirP(results_directory)
        self.l.LogOutput('Storing experiment file in %s.' % results_directory)
        experiment_file_path = os.path.join(results_directory,
                                            'experiment.exp')
        FileUtils().WriteFile(experiment_file_path, experiment.experiment_file)

        self.l.LogOutput('Storing results report in %s.' % results_directory)
        results_table_path = os.path.join(results_directory, 'results.html')
        report = HTMLResultsReport.FromExperiment(experiment).GetReport()
        if self.json_report:
            json_report = JSONResultsReport.FromExperiment(
                experiment, json_args={'indent': 2})
            _WriteJSONReportToFile(experiment, results_directory, json_report)

        FileUtils().WriteFile(results_table_path, report)

        self.l.LogOutput('Storing email message body in %s.' %
                         results_directory)
        msg_file_path = os.path.join(results_directory, 'msg_body.html')
        text_report = TextResultsReport.FromExperiment(experiment,
                                                       True).GetReport()
        text_report += ('\nResults are stored in %s.\n' %
                        experiment.results_directory)
        msg_body = "<pre style='font-size: 13px'>%s</pre>" % text_report
        FileUtils().WriteFile(msg_file_path, msg_body)

        self.l.LogOutput('Storing results of each benchmark run.')
        for benchmark_run in experiment.benchmark_runs:
            if benchmark_run.result:
                benchmark_run_name = filter(str.isalnum, benchmark_run.name)
                benchmark_run_path = os.path.join(results_directory,
                                                  benchmark_run_name)
                benchmark_run.result.CopyResultsTo(benchmark_run_path)
                benchmark_run.result.CleanUp(
                    benchmark_run.benchmark.rm_chroot_tmp)
示例#2
0
def VerifyChromeChecksum(chromeos_root, image, remote, log_level):
    cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
    rootfs_mp = tempfile.mkdtemp()
    stateful_mp = tempfile.mkdtemp()
    MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
    image_chrome_checksum = FileUtils().Md5File('%s/opt/google/chrome/chrome' %
                                                rootfs_mp,
                                                log_level=log_level)
    MountImage(chromeos_root,
               image,
               rootfs_mp,
               stateful_mp,
               log_level,
               unmount=True)

    command = 'md5sum /opt/google/chrome/chrome'
    [_, o, _] = cmd_executer.CrosRunCommandWOutput(command,
                                                   chromeos_root=chromeos_root,
                                                   machine=remote)
    device_chrome_checksum = o.split()[0]
    if image_chrome_checksum.strip() == device_chrome_checksum.strip():
        return True
    else:
        return False
示例#3
0
def CreateAutotestRuns(images,
                       autotests,
                       remote,
                       board,
                       exact_remote,
                       rerun,
                       rerun_if_failed,
                       main_chromeos_root=None):
    autotest_runs = []
    for image in images:
        logger.GetLogger().LogOutput('Computing md5sum of: %s' % image)
        image_checksum = FileUtils().Md5File(image)
        logger.GetLogger().LogOutput('md5sum %s: %s' % (image, image_checksum))
        ###    image_checksum = "abcdefghi"

        chromeos_root = main_chromeos_root
        if not main_chromeos_root:
            image_chromeos_root = os.path.join(os.path.dirname(image),
                                               '../../../../..')
            chromeos_root = CanonicalizeChromeOSRoot(image_chromeos_root)
            assert chromeos_root, 'chromeos_root: %s invalid' % image_chromeos_root
        else:
            chromeos_root = CanonicalizeChromeOSRoot(main_chromeos_root)
            assert chromeos_root, 'chromeos_root: %s invalid' % main_chromeos_root

        # We just need a single ChromeOS root in the MachineManagerSingleton. It is
        # needed because we can save re-image time by checking the image checksum at
        # the beginning and assigning autotests to machines appropriately.
        if not MachineManagerSingleton().chromeos_root:
            MachineManagerSingleton().chromeos_root = chromeos_root

        for autotest in autotests:
            for iteration in range(autotest.iterations):
                autotest_run = AutotestRun(autotest,
                                           chromeos_root=chromeos_root,
                                           chromeos_image=image,
                                           board=board,
                                           remote=remote,
                                           iteration=iteration,
                                           image_checksum=image_checksum,
                                           exact_remote=exact_remote,
                                           rerun=rerun,
                                           rerun_if_failed=rerun_if_failed)
                autotest_runs.append(autotest_run)
    return autotest_runs
示例#4
0
 def Checksum(self):
     with self._lock:
         if not self._checksum:
             logger.GetLogger().LogOutput(
                 "Acquiring checksum for '%s'." % self.label.name)
             self._checksum = None
             if self.label.image_type != 'local':
                 raise RuntimeError(
                     'Called Checksum on non-local image!')
             if self.label.chromeos_image:
                 if os.path.exists(self.label.chromeos_image):
                     self._checksum = FileUtils().Md5File(
                         self.label.chromeos_image,
                         log_level=self.log_level)
                     logger.GetLogger().LogOutput(
                         'Computed checksum is '
                         ': %s' % self._checksum)
             if not self._checksum:
                 raise RuntimeError('Checksum computing error.')
             logger.GetLogger().LogOutput('Checksum is: %s' %
                                          self._checksum)
         return self._checksum
示例#5
0
    def __init__(self,
                 name,
                 chromeos_image,
                 autotest_path,
                 chromeos_root,
                 board,
                 remote,
                 image_args,
                 cache_dir,
                 cache_only,
                 log_level,
                 compiler,
                 chrome_src=None):

        self.image_type = self._GetImageType(chromeos_image)

        # Expand ~
        chromeos_root = os.path.expanduser(chromeos_root)
        if self.image_type == 'local':
            chromeos_image = os.path.expanduser(chromeos_image)

        self.name = name
        self.chromeos_image = chromeos_image
        self.autotest_path = autotest_path
        self.board = board
        self.remote = remote
        self.image_args = image_args
        self.cache_dir = cache_dir
        self.cache_only = cache_only
        self.log_level = log_level
        self.chrome_version = ''
        self.compiler = compiler

        if not chromeos_root:
            if self.image_type == 'local':
                chromeos_root = FileUtils().ChromeOSRootFromImage(
                    chromeos_image)
            if not chromeos_root:
                raise RuntimeError(
                    "No ChromeOS root given for label '%s' and could "
                    "not determine one from image path: '%s'." %
                    (name, chromeos_image))
        else:
            chromeos_root = FileUtils().CanonicalizeChromeOSRoot(chromeos_root)
            if not chromeos_root:
                raise RuntimeError(
                    "Invalid ChromeOS root given for label '%s': '%s'." %
                    (name, chromeos_root))

        self.chromeos_root = chromeos_root
        if not chrome_src:
            self.chrome_src = os.path.join(
                self.chromeos_root,
                '.cache/distfiles/target/chrome-src-internal')
            if not os.path.exists(self.chrome_src):
                self.chrome_src = os.path.join(
                    self.chromeos_root, '.cache/distfiles/target/chrome-src')
        else:
            chromeos_src = misc.CanonicalizePath(chrome_src)
            if not chromeos_src:
                raise RuntimeError(
                    "Invalid Chrome src given for label '%s': '%s'." %
                    (name, chrome_src))
            self.chrome_src = chromeos_src

        self._SetupChecksum()
        self.assertEqual(len(exp.labels), 2)
        self.assertEqual(exp.labels[1].chromeos_image, 'fake_image_path')
        self.assertEqual(exp.labels[1].autotest_path, 'fake_autotest_path')
        self.assertEqual(
            exp.remote,
            ['fake_chromeos_machine1.cros', 'fake_chromeos_machine2.cros'])

    def test_get_default_remotes(self):
        board_list = [
            'x86-alex', 'lumpy', 'elm', 'parrot', 'daisy', 'peach_pit',
            'peppy', 'squawks'
        ]

        ef = ExperimentFactory()
        self.assertRaises(Exception, ef.GetDefaultRemotes, 'bad-board')

        # Verify that we have entries for every board, and that we get three
        # machines back for each board.
        for b in board_list:
            remotes = ef.GetDefaultRemotes(b)
            if b == 'elm':
                self.assertEqual(len(remotes), 2)
            else:
                self.assertEqual(len(remotes), 3)


if __name__ == '__main__':
    FileUtils.Configure(True)
    test_flag.SetTestMode(True)
    unittest.main()
示例#7
0
def DoImage(argv):
    """Image ChromeOS."""

    parser = argparse.ArgumentParser()
    parser.add_argument('-c',
                        '--chromeos_root',
                        dest='chromeos_root',
                        help='Target directory for ChromeOS installation.')
    parser.add_argument('-r', '--remote', dest='remote', help='Target device.')
    parser.add_argument('-i',
                        '--image',
                        dest='image',
                        help='Image binary file.')
    parser.add_argument('-b',
                        '--board',
                        dest='board',
                        help='Target board override.')
    parser.add_argument('-f',
                        '--force',
                        dest='force',
                        action='store_true',
                        default=False,
                        help='Force an image even if it is non-test.')
    parser.add_argument('-n',
                        '--no_lock',
                        dest='no_lock',
                        default=False,
                        action='store_true',
                        help='Do not attempt to lock remote before imaging.  '
                        'This option should only be used in cases where the '
                        'exclusive lock has already been acquired (e.g. in '
                        'a script that calls this one).')
    parser.add_argument('-l',
                        '--logging_level',
                        dest='log_level',
                        default='verbose',
                        help='Amount of logging to be used. Valid levels are '
                        "'quiet', 'average', and 'verbose'.")
    parser.add_argument('-a', '--image_args', dest='image_args')

    options = parser.parse_args(argv[1:])

    if not options.log_level in command_executer.LOG_LEVEL:
        Usage(parser,
              "--logging_level must be 'quiet', 'average' or 'verbose'")
    else:
        log_level = options.log_level

    # Common initializations
    cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
    l = logger.GetLogger()

    if options.chromeos_root is None:
        Usage(parser, '--chromeos_root must be set')

    if options.remote is None:
        Usage(parser, '--remote must be set')

    options.chromeos_root = os.path.expanduser(options.chromeos_root)

    if options.board is None:
        board = cmd_executer.CrosLearnBoard(options.chromeos_root,
                                            options.remote)
    else:
        board = options.board

    if options.image is None:
        images_dir = misc.GetImageDir(options.chromeos_root, board)
        image = os.path.join(images_dir, 'latest', 'chromiumos_test_image.bin')
        if not os.path.exists(image):
            image = os.path.join(images_dir, 'latest', 'chromiumos_image.bin')
        is_xbuddy_image = False
    else:
        image = options.image
        is_xbuddy_image = image.startswith('xbuddy://')
        if not is_xbuddy_image:
            image = os.path.expanduser(image)

    if not is_xbuddy_image:
        image = os.path.realpath(image)

    if not os.path.exists(image) and not is_xbuddy_image:
        Usage(parser, 'Image file: ' + image + ' does not exist!')

    try:
        should_unlock = False
        if not options.no_lock:
            try:
                _ = locks.AcquireLock(list(options.remote.split()),
                                      options.chromeos_root)
                should_unlock = True
            except Exception as e:
                raise RuntimeError('Error acquiring machine: %s' % str(e))

        reimage = False
        local_image = False
        if not is_xbuddy_image:
            local_image = True
            image_checksum = FileUtils().Md5File(image, log_level=log_level)

            command = 'cat ' + checksum_file
            ret, device_checksum, _ = cmd_executer.CrosRunCommandWOutput(
                command,
                chromeos_root=options.chromeos_root,
                machine=options.remote)

            device_checksum = device_checksum.strip()
            image_checksum = str(image_checksum)

            l.LogOutput('Image checksum: ' + image_checksum)
            l.LogOutput('Device checksum: ' + device_checksum)

            if image_checksum != device_checksum:
                [found,
                 located_image] = LocateOrCopyImage(options.chromeos_root,
                                                    image,
                                                    board=board)

                reimage = True
                l.LogOutput('Checksums do not match. Re-imaging...')

                is_test_image = IsImageModdedForTest(options.chromeos_root,
                                                     located_image, log_level)

                if not is_test_image and not options.force:
                    logger.GetLogger().LogFatal(
                        'Have to pass --force to image a '
                        'non-test image!')
        else:
            reimage = True
            found = True
            l.LogOutput('Using non-local image; Re-imaging...')

        if reimage:
            # If the device has /tmp mounted as noexec, image_to_live.sh can fail.
            command = 'mount -o remount,rw,exec /tmp'
            cmd_executer.CrosRunCommand(command,
                                        chromeos_root=options.chromeos_root,
                                        machine=options.remote)

            real_src_dir = os.path.join(
                os.path.realpath(options.chromeos_root), 'src')
            real_chroot_dir = os.path.join(
                os.path.realpath(options.chromeos_root), 'chroot')
            if local_image:
                if located_image.find(real_src_dir) != 0:
                    if located_image.find(real_chroot_dir) != 0:
                        raise RuntimeError(
                            'Located image: %s not in chromeos_root: %s' %
                            (located_image, options.chromeos_root))
                    else:
                        chroot_image = located_image[len(real_chroot_dir):]
                else:
                    chroot_image = os.path.join(
                        '~/trunk/src',
                        located_image[len(real_src_dir):].lstrip('/'))

            # Check to see if cros flash will work for the remote machine.
            CheckForCrosFlash(options.chromeos_root, options.remote, log_level)

            # Disable the annoying chromebook beeps after reboot.
            DisableCrosBeeps(options.chromeos_root, options.remote, log_level)

            cros_flash_args = [
                'cros', 'flash',
                '--board=%s' % board, '--clobber-stateful', options.remote
            ]
            if local_image:
                cros_flash_args.append(chroot_image)
            else:
                cros_flash_args.append(image)

            command = ' '.join(cros_flash_args)

            # Workaround for crosbug.com/35684.
            os.chmod(misc.GetChromeOSKeyFile(options.chromeos_root), 0600)

            if log_level == 'average':
                cmd_executer.SetLogLevel('verbose')
            retries = 0
            while True:
                if log_level == 'quiet':
                    l.LogOutput('CMD : %s' % command)
                ret = cmd_executer.ChrootRunCommand(options.chromeos_root,
                                                    command,
                                                    command_timeout=1800)
                if ret == 0 or retries >= 2:
                    break
                retries += 1
                if log_level == 'quiet':
                    l.LogOutput('Imaging failed. Retry # %d.' % retries)

            if log_level == 'average':
                cmd_executer.SetLogLevel(log_level)

            if found == False:
                temp_dir = os.path.dirname(located_image)
                l.LogOutput('Deleting temp image dir: %s' % temp_dir)
                shutil.rmtree(temp_dir)

            logger.GetLogger().LogFatalIf(ret, 'Image command failed')

            # Unfortunately cros_image_to_target.py sometimes returns early when the
            # machine isn't fully up yet.
            ret = EnsureMachineUp(options.chromeos_root, options.remote,
                                  log_level)

            # If this is a non-local image, then the ret returned from
            # EnsureMachineUp is the one that will be returned by this function;
            # in that case, make sure the value in 'ret' is appropriate.
            if not local_image and ret == True:
                ret = 0
            else:
                ret = 1

            if local_image:
                if log_level == 'average':
                    l.LogOutput('Verifying image.')
                command = 'echo %s > %s && chmod -w %s' % (
                    image_checksum, checksum_file, checksum_file)
                ret = cmd_executer.CrosRunCommand(
                    command,
                    chromeos_root=options.chromeos_root,
                    machine=options.remote)
                logger.GetLogger().LogFatalIf(ret, 'Writing checksum failed.')

                successfully_imaged = VerifyChromeChecksum(
                    options.chromeos_root, image, options.remote, log_level)
                logger.GetLogger().LogFatalIf(not successfully_imaged,
                                              'Image verification failed!')
                TryRemountPartitionAsRW(options.chromeos_root, options.remote,
                                        log_level)
        else:
            l.LogOutput('Checksums match. Skipping reimage')
        return ret
    finally:
        if should_unlock:
            locks.ReleaseLock(list(options.remote.split()),
                              options.chromeos_root)