def test_runs_directly_if_logdog_error(self, bs_result, bootstrap):
        bootstrap.return_value = logdog_bootstrap.BootstrapState(
            ['logdog_bootstrap'] + self.recipe_args, '/path/to/result.json')
        bs_result.side_effect = logdog_bootstrap.BootstrapError()

        # Return a different error code depending on whether we're bootstrapping so
        # we can assert that specifically the non-bootstrapped error code is the one
        # that is returned.
        def get_error_code(args, **_kw):
            if len(args) > 0 and args[0] == 'logdog_bootstrap':
                return (1, '')
            return (2, '')

        annotated_run._run_command.side_effect = get_error_code

        rv = annotated_run._exec_recipe(self.rt, self.opts, self.basedir,
                                        self.tdir, self.properties)
        self.assertEqual(rv, 2)

        bootstrap.assert_called_once()
        bs_result.assert_called_once()
        annotated_run._run_command.assert_has_calls([
            mock.call(['logdog_bootstrap'] + self.recipe_args, dry_run=False),
            mock.call(self.recipe_args, dry_run=False),
        ])
  def test_bootstrap_annotations(self):
    sio = StringIO.StringIO()
    stream = annotator.StructuredAnnotationStream(stream=sio)
    cfg = ldbs.Config(
        params=self.base,
        plat=None,
        host='example.com',
        prefix='foo/bar',
        tags=None,
        logdog_only=False,
        service_account_path=None,
    )
    bs = ldbs.BootstrapState(cfg, [], '/foo/bar')
    bs.annotate(stream)

    lines = [l for l in sio.getvalue().splitlines() if l]
    self.assertEqual(lines, [
        '@@@SEED_STEP LogDog Bootstrap@@@',
        '@@@STEP_CURSOR LogDog Bootstrap@@@',
        '@@@STEP_STARTED@@@',
        '@@@SET_BUILD_PROPERTY@logdog_project@"alpha"@@@',
        '@@@SET_BUILD_PROPERTY@logdog_prefix@"foo/bar"@@@',
        ('@@@SET_BUILD_PROPERTY@logdog_annotation_url@'
         '"logdog://example.com/alpha/foo/bar/+/recipes/annotations"@@@'),
        '@@@STEP_CURSOR LogDog Bootstrap@@@',
        '@@@STEP_CLOSED@@@',
    ])
  def test_get_bootstrap_result(self):
    mo = mock.mock_open(read_data='{"return_code": 1337}')
    with mock.patch('slave.logdog_bootstrap.open', mo, create=True):
      bs = ldbs.BootstrapState(None, [], '/foo/bar')
      self.assertEqual(bs.get_result(), 1337)

    mo = mock.mock_open(read_data='!!! NOT JSON? !!!')
    with mock.patch('slave.logdog_bootstrap.open', mo, create=True):
      bs = ldbs.BootstrapState(None, [], '/foo/bar')
      self.assertRaises(ldbs.BootstrapError, bs.get_result)

    mo = mock.mock_open(read_data='{"invalid": "json"}')
    with mock.patch('slave.logdog_bootstrap.open', mo, create=True):
      bs = ldbs.BootstrapState(None, [], '/foo/bar')
      self.assertRaises(ldbs.BootstrapError, bs.get_result)

    mo = mock.mock_open()
    with mock.patch('slave.logdog_bootstrap.open', mo, create=True):
      mo.side_effect = IOError('Test not found')
      self.assertRaises(ldbs.BootstrapError, bs.get_result)
    def test_exec_with_logdog_bootstrap(self, bs_result, bootstrap):
        bootstrap.return_value = logdog_bootstrap.BootstrapState(
            ['logdog_bootstrap'] + self.recipe_args, '/path/to/result.json')
        bootstrap.return_value.get_result.return_value = 13
        annotated_run._run_command.return_value = (13, '')

        rv = annotated_run._exec_recipe(self.rt, self.opts, self.basedir,
                                        self.tdir, self.properties)

        self.assertEqual(rv, 13)
        annotated_run._run_command.assert_called_once_with(
            ['logdog_bootstrap'] + self.recipe_args, dry_run=False)
        self._assertRecipeProperties(self.properties)
Exemplo n.º 5
0
    def test_exec_with_logdog_failed(self, rt_tempdir, _install_cipd_packages,
                                     _logdog_bootstrap_result, bootstrap):

        args = self.recipe_remote_args + ['--'] + self.recipe_args
        cfg = self._default_namedtuple(logdog_bootstrap.Config)._replace(
            params=self._default_namedtuple(logdog_bootstrap.Params)._replace(
                project="project", ),
            prefix="prefix",
            host="example.com",
        )
        bootstrap.return_value = logdog_bootstrap.BootstrapState(
            cfg, ['logdog_bootstrap'] + args, '/path/to/result.json')
        bootstrap.return_value.get_result.return_value = 0

        remote_run._call.return_value = 0
        rt_tempdir.side_effect = [self.tempdir, self.build_data_dir]
        self.recipe_result = {
            'failure': {
                'timeout': True,
            }
        }
        self._write_recipe_result()

        rv = remote_run._exec_recipe(self.opts, self.rt, self.stream,
                                     self.basedir, self.buildbot_build_dir)
        self.assertEqual(rv, 255)

        remote_run._call.assert_called_once_with(bootstrap.return_value.cmd)
        self.assertEqual([
            l for l in self.stream_output.getvalue().splitlines() if l
        ], [
            '@@@SEED_STEP LogDog Bootstrap@@@',
            '@@@STEP_CURSOR LogDog Bootstrap@@@',
            '@@@STEP_STARTED@@@',
            '@@@SET_BUILD_PROPERTY@logdog_project@"project"@@@',
            '@@@SET_BUILD_PROPERTY@logdog_prefix@"prefix"@@@',
            ('@@@SET_BUILD_PROPERTY@logdog_annotation_url@'
             '"logdog://example.com/project/prefix/+/recipes/annotations"@@@'),
            '@@@STEP_CURSOR LogDog Bootstrap@@@',
            '@@@STEP_CLOSED@@@',
        ])
    def test_exec_with_logdog_bootstrap_logdog_only(self, bs_result,
                                                    bootstrap):
        cfg = self._default_namedtuple(logdog_bootstrap.Config)._replace(
            params=self._default_namedtuple(logdog_bootstrap.Params)._replace(
                project="project", ),
            prefix="prefix",
            host="example.com",
            logdog_only=True,
        )
        bootstrap.return_value = logdog_bootstrap.BootstrapState(
            cfg, ['logdog_bootstrap'] + self.recipe_args,
            '/path/to/result.json')
        bootstrap.return_value.get_result.return_value = 13
        annotated_run._run_command.return_value = (13, '')
        self._writeRecipeResult({})

        rv = annotated_run._exec_recipe(self.rt, self.opts, self.stream,
                                        self.basedir, self.tdir,
                                        self.properties)

        self.assertEqual(rv, 13)
        annotated_run._run_command.assert_called_once_with(
            ['logdog_bootstrap'] + self.recipe_args, dry_run=False)
        self._assertRecipeProperties(self.properties)

        self.assertEqual([
            l for l in self.stream_output.getvalue().splitlines() if l
        ], [
            '@@@SEED_STEP LogDog Bootstrap@@@',
            '@@@STEP_CURSOR LogDog Bootstrap@@@',
            '@@@STEP_STARTED@@@',
            '@@@SET_BUILD_PROPERTY@logdog_project@"project"@@@',
            '@@@SET_BUILD_PROPERTY@logdog_prefix@"prefix"@@@',
            ('@@@SET_BUILD_PROPERTY@log_location@'
             '"logdog://example.com/project/prefix/+/recipes/annotations"@@@'),
            '@@@STEP_CURSOR LogDog Bootstrap@@@',
            '@@@STEP_CLOSED@@@',
        ])