Ejemplo n.º 1
0
  def test_enqueue_callback_task_if_needed(self, enqueue_task_async):
    build = model.Build(
        id=1,
        bucket='chromium',
        create_time=datetime.datetime(2017, 1, 1),
        pubsub_callback=model.PubSubCallback(
            topic='projects/example/topic/buildbucket',
            user_data='hello',
            auth_token='secret',
        ),
    )

    @ndb.transactional
    def txn():
      build.put()
      events.on_build_completing_async(build).get_result()
    txn()

    enqueue_task_async.assert_called_with(
        'backend-default',
        '/internal/task/buildbucket/notify/1',
        json.dumps({
          'topic': 'projects/example/topic/buildbucket',
          'message': {
            'build_id': '1',
            'user_data': 'hello',
          },
          'attrs': {
            'build_id': '1',
            'auth_token': 'secret',
          },
        }, sort_keys=True),
        model.BUILD_TIMEOUT.total_seconds())
Ejemplo n.º 2
0
 def test_put(self, add):
     self.test_build.tags = ['owner:ivan']
     add.return_value = self.test_build
     req = {
         'client_operation_id': '42',
         'bucket': self.test_build.bucket,
         'tags': self.test_build.tags,
         'pubsub_callback': {
             'topic': 'projects/foo/topic/bar',
             'user_data': 'hello',
             'auth_token': 'secret',
         }
     }
     resp = self.call_api('put', req).json_body
     add.assert_called_once_with(
         service.BuildRequest(
             bucket=self.test_build.bucket,
             tags=req['tags'],
             client_operation_id='42',
             pubsub_callback=model.PubSubCallback(
                 topic='projects/foo/topic/bar',
                 user_data='hello',
                 auth_token='secret',
             ),
             retry_of=None,
         ))
     self.assertEqual(resp['build']['id'], str(self.test_build.key.id()))
     self.assertEqual(resp['build']['bucket'], req['bucket'])
     self.assertEqual(resp['build']['tags'], req['tags'])
Ejemplo n.º 3
0
    def test_retry(self, retry):
        build = model.Build(
            bucket='chromium',
            parameters={'builder_name': 'debug'},
            tags=['a:b'],
            retry_of=2,
        )
        build.put()
        retry.return_value = build

        req = {
            'id': build.key.id(),
            'client_operation_id': '42',
            'pubsub_callback': {
                'topic': 'projects/foo/topic/bar',
                'user_data': 'hello',
                'auth_token': 'secret',
            }
        }
        resp = self.call_api('retry', req).json_body
        retry.assert_called_once_with(
            build.key.id(),
            client_operation_id='42',
            lease_expiration_date=None,
            pubsub_callback=model.PubSubCallback(
                topic='projects/foo/topic/bar',
                user_data='hello',
                auth_token='secret',
            ),
        )
        self.assertEqual(resp['build']['id'], str(build.key.id()))
        self.assertEqual(resp['build']['bucket'], build.bucket)
        self.assertEqual(json.loads(resp['build']['parameters_json']),
                         build.parameters)
        self.assertEqual(resp['build']['retry_of'], '2')
Ejemplo n.º 4
0
def pubsub_callback_from_message(msg):
    if msg is None:
        return None
    return model.PubSubCallback(
        topic=msg.topic,
        user_data=msg.user_data,
        auth_token=msg.auth_token,
    )
Ejemplo n.º 5
0
 def callback_test(self, build):
     with mock.patch('notifications.enqueue_notifications_async',
                     autospec=True):
         notifications.enqueue_notifications_async.return_value = future(
             None)
         build.pubsub_callback = model.PubSubCallback(
             topic='projects/example/topics/buildbucket',
             user_data='hello',
             auth_token='secret',
         )
         build.put()
         yield
         build = build.key.get()
         notifications.enqueue_notifications_async.assert_called_with(build)
Ejemplo n.º 6
0
    def create_build_async(self, build_id, settings, builder_cfg, created_by,
                           now):
        """Converts the request to a build.

    Assumes self is valid.
    """
        sbr = self.schedule_build_request

        build_proto = yield self.create_build_proto_async(
            build_id, settings, builder_cfg, created_by, now)
        build = model.Build(
            id=build_id,
            proto=build_proto,
            tags=[
                buildtags.unparse(k, v)
                for k, v in sorted(self.compute_tag_set(sbr))
            ],
            parameters=copy.deepcopy(self.parameters or {}),
            created_by=created_by,
            create_time=now,
            never_leased=self.lease_expiration_date is None,
            retry_of=self.retry_of,
        )

        if sbr.builder.builder:  # pragma: no branch
            build.parameters[model.BUILDER_PARAMETER] = sbr.builder.builder

        build.parameters[model.PROPERTIES_PARAMETER] = bbutil.struct_to_dict(
            sbr.properties)

        if sbr.HasField('notify'):
            build.pubsub_callback = model.PubSubCallback(
                topic=sbr.notify.pubsub_topic,
                auth_token=self.pubsub_callback_auth_token,
                user_data=sbr.notify.user_data.decode('utf-8'),
            )

        if self.lease_expiration_date is not None:
            build.lease_expiration_date = self.lease_expiration_date
            build.leasee = created_by
            build.regenerate_lease_key()

        raise ndb.Return(build)
Ejemplo n.º 7
0
  def test_pubsub_callback(self):
    build = test_util.build(id=1)
    build.pubsub_callback = model.PubSubCallback(
        topic='projects/example/topics/buildbucket',
        user_data='hello',
        auth_token='secret',
    )

    out_props = model.BuildOutputProperties(
        key=model.BuildOutputProperties.key_for(build.key),
    )
    out_props.serialize(bbutil.dict_to_struct({'a': 'b'}))

    @ndb.transactional
    def txn():
      build.put()
      out_props.put()
      notifications.enqueue_notifications_async(build).get_result()

    txn()

    build = build.key.get()
    global_task_payload = {
        'id': 1,
        'mode': 'global',
    }
    callback_task_payload = {
        'id': 1,
        'mode': 'callback',
    }
    tq.enqueue_async.assert_called_with(
        'backend-default', [
            {
                'url': '/internal/task/buildbucket/notify/1',
                'payload': global_task_payload,
                'retry_options': {
                    'task_age_limit': model.BUILD_TIMEOUT.total_seconds(),
                },
            },
            {
                'url': '/internal/task/buildbucket/notify/1',
                'payload': callback_task_payload,
                'retry_options': {
                    'task_age_limit': model.BUILD_TIMEOUT.total_seconds(),
                },
            },
        ]
    )

    self.app.post_json(
        '/internal/task/buildbucket/notify/1',
        params=global_task_payload,
        headers={'X-AppEngine-QueueName': 'backend-default'}
    )
    pubsub.publish.assert_called_with(
        'projects/testbed-test/topics/builds',
        json.dumps({
            'build': api_common.build_to_dict(build, out_props),
            'hostname': 'buildbucket.example.com',
        },
                   sort_keys=True),
        {'build_id': '1'},
    )

    self.app.post_json(
        '/internal/task/buildbucket/notify/1',
        params=callback_task_payload,
        headers={'X-AppEngine-QueueName': 'backend-default'}
    )
    pubsub.publish.assert_called_with(
        'projects/example/topics/buildbucket',
        json.dumps({
            'build': api_common.build_to_dict(build, out_props),
            'hostname': 'buildbucket.example.com',
            'user_data': 'hello',
        },
                   sort_keys=True),
        {
            'build_id': '1',
            'auth_token': 'secret',
        },
    )