示例#1
0
  def testContextManagerWithException(self):
    """Tests that we emit metrics if the timed method raised something."""
    with self.assertRaises(AssertionError):
      with metrics.SecondsInstanceTimer('fooname', fields={'foo': 'bar'}):
        assert False

    self._mockMetric.set.assert_called_with(mock.ANY, fields={'foo': 'bar'})
示例#2
0
 def testContextManagerIgnoresInvalidField(self):
     """Test that we ignore fields that are set with no default."""
     with metrics.SecondsInstanceTimer('fooname', fields={'foo':
                                                          'bar'}) as c:
         c['qux'] = 'qwert'
     self._mockMetric.set.assert_called_with(mock.ANY,
                                             fields={'foo': 'bar'})
示例#3
0
 def testContextManagerWithUpdate(self):
     """Tests that timing context manager with a field update emits metric."""
     with metrics.SecondsInstanceTimer('fooname', fields={'foo':
                                                          'bar'}) as c:
         c['foo'] = 'qux'
     self._mockMetric.set.assert_called_with(mock.ANY,
                                             fields={'foo': 'qux'})
示例#4
0
 def testContextManagerWithoutUpdate(self):
     """Tests that the default value for fields is used when not updated."""
     # pylint: disable=unused-variable
     with metrics.SecondsInstanceTimer('fooname', fields={'foo':
                                                          'bar'}) as c:
         pass
     self._mockMetric.set.assert_called_with(mock.ANY,
                                             fields={'foo': 'bar'})
示例#5
0
 def testContextManager(self):
   """Test that timing context manager emits a metric."""
   with metrics.SecondsInstanceTimer('fooname'):
     pass
   self.assertEqual(metrics.FloatMetric.call_count, 1)
   self.assertEqual(self._mockMetric.set.call_count, 1)
def _main(options, argv):
    """main method of script.

  Args:
    options: preparsed options object for the build.
    argv: All command line arguments to pass as list of strings.

  Returns:
    Return code of cbuildbot as an integer.
  """
    branchname = options.branch or 'master'
    root = options.buildroot
    buildroot = os.path.join(root, 'repository')
    workspace = os.path.join(root, 'workspace')
    depot_tools_path = os.path.join(buildroot, constants.DEPOT_TOOLS_SUBPATH)

    # Does the entire build pass or fail.
    with metrics.Presence(METRIC_ACTIVE), \
         metrics.SuccessCounter(METRIC_COMPLETED) as s_fields:

        # Preliminary set, mostly command line parsing.
        with metrics.SuccessCounter(METRIC_INVOKED):
            if options.enable_buildbot_tags:
                logging.EnableBuildbotMarkers()
            ConfigureGlobalEnvironment()

        # Prepare the buildroot with source for the build.
        with metrics.SuccessCounter(METRIC_PREP):
            manifest_url = config_lib.GetSiteParams().MANIFEST_INT_URL
            repo = repository.RepoRepository(
                manifest_url,
                buildroot,
                branch=branchname,
                git_cache_dir=options.git_cache_dir)
            previous_build_state = GetLastBuildState(root)

            # Clean up the buildroot to a safe state.
            with metrics.SecondsTimer(METRIC_CLEAN):
                build_state = GetCurrentBuildState(options, branchname)
                CleanBuildRoot(root, repo, options.cache_dir, build_state)

            # Get a checkout close enough to the branch that cbuildbot can handle it.
            if options.sync:
                with metrics.SecondsTimer(METRIC_INITIAL):
                    InitialCheckout(repo)

        # Run cbuildbot inside the full ChromeOS checkout, on the specified branch.
        with metrics.SecondsTimer(METRIC_CBUILDBOT), \
             metrics.SecondsInstanceTimer(METRIC_CBUILDBOT_INSTANCE):
            if previous_build_state.is_valid():
                argv.append('--previous-build-state')
                argv.append(
                    base64.b64encode(previous_build_state.to_json().encode(
                        'utf-8')).decode('utf-8'))
            argv.extend(['--workspace', workspace])

            if not options.cache_dir_specified:
                argv.extend(['--cache-dir', options.cache_dir])

            result = Cbuildbot(buildroot, depot_tools_path, argv)
            s_fields['success'] = (result == 0)

            build_state.status = (constants.BUILDER_STATUS_PASSED if result
                                  == 0 else constants.BUILDER_STATUS_FAILED)
            SetLastBuildState(root, build_state)

            with metrics.SecondsTimer(METRIC_CHROOT_CLEANUP):
                CleanupChroot(buildroot)

            return result