def test_logs_and_prefix(self): ctx = validation_context.Context() self.assertFalse(ctx.result().has_errors) ctx.error('hello %s', 'world') self.assertTrue(ctx.result().has_errors) with ctx.prefix('prefix %d ', 3): ctx.warning('warning %s', 2) with ctx.prefix('unicode %s', u'\xf0\x9f\x90\xb1 '): ctx.error('no cat') self.assertEqual( ctx.result(), validation_context.Result(messages=[ validation_context.Message(severity=logging.ERROR, text='hello world'), validation_context.Message(severity=logging.WARNING, text='prefix 3 warning 2'), validation_context.Message( severity=logging.ERROR, text=u'unicode \xf0\x9f\x90\xb1 no cat'), ], ), )
def test_validate_buildbucket_cfg_unsorted(self): self.cfg_validation_test( parse_cfg(''' buckets { name: "c" } buckets { name: "b" } buckets { name: "a" } '''), [ validation_context.Message( severity=logging.WARNING, text='Bucket b: out of order', ), validation_context.Message( severity=logging.WARNING, text='Bucket a: out of order', ), ], )
def test_validate_buildbucket_cfg_unsorted(self): self.cfg_validation_test( parse_cfg(''' buckets { name: "b" } buckets { name: "a" } '''), [ validation_context.Message( severity=logging.WARNING, text='Buckets are not sorted by name'), ])
def test_validate_buildbucket_cfg_unsorted(self): self.cfg_validation_test( project_config_pb2.BuildbucketCfg(buckets=[ project_config_pb2.Bucket(name='b'), project_config_pb2.Bucket(name='a') ]), [ validation_context.Message( severity=logging.WARNING, text='Buckets are not sorted by name'), ])
def test_validation_by_service_async(self): cfg = '# a config' cfg_b64 = base64.b64encode(cfg) self.services = [ service_config_pb2.Service(id='a'), service_config_pb2.Service(id='b'), service_config_pb2.Service(id='c'), ] @ndb.tasklet def get_metadata_async(service_id): if service_id == 'a': raise ndb.Return( service_config_pb2.ServiceDynamicMetadata( validation=service_config_pb2.Validator( patterns=[ service_config_pb2.ConfigPattern( config_set='services/foo', path='bar.cfg', ) ], url='https://bar.verifier', ))) if service_id == 'b': raise ndb.Return( service_config_pb2.ServiceDynamicMetadata( validation=service_config_pb2.Validator( patterns=[ service_config_pb2.ConfigPattern( config_set=r'regex:projects/[^/]+', path=r'regex:.+\.cfg', ) ], url='https://bar2.verifier', ))) if service_id == 'c': raise ndb.Return( service_config_pb2.ServiceDynamicMetadata( validation=service_config_pb2.Validator( patterns=[ service_config_pb2.ConfigPattern( config_set=r'regex:.+', path=r'regex:.+', ) ], url='https://ultimate.verifier', ))) return None self.mock(services, 'get_metadata_async', mock.Mock()) services.get_metadata_async.side_effect = get_metadata_async @ndb.tasklet def json_request_async(url, **kwargs): raise ndb.Return({ 'messages': [{ 'text': 'OK from %s' % url, # default severity }], }) self.mock(net, 'json_request_async', mock.Mock(side_effect=json_request_async)) ############################################################################ result = validation.validate_config('services/foo', 'bar.cfg', cfg) self.assertEqual(result.messages, [ validation_context.Message(text='OK from https://bar.verifier', severity=logging.INFO), validation_context.Message( text='OK from https://ultimate.verifier', severity=logging.INFO) ]) net.json_request_async.assert_any_call( 'https://bar.verifier', method='POST', payload={ 'config_set': 'services/foo', 'path': 'bar.cfg', 'content': cfg_b64, }, scopes=net.EMAIL_SCOPE, ) net.json_request_async.assert_any_call( 'https://ultimate.verifier', method='POST', payload={ 'config_set': 'services/foo', 'path': 'bar.cfg', 'content': cfg_b64, }, scopes=net.EMAIL_SCOPE, ) ############################################################################ result = validation.validate_config('projects/foo', 'bar.cfg', cfg) self.assertEqual(result.messages, [ validation_context.Message(text='OK from https://bar2.verifier', severity=logging.INFO), validation_context.Message( text='OK from https://ultimate.verifier', severity=logging.INFO) ]) net.json_request_async.assert_any_call( 'https://bar2.verifier', method='POST', payload={ 'config_set': 'projects/foo', 'path': 'bar.cfg', 'content': cfg_b64, }, scopes=net.EMAIL_SCOPE, ) net.json_request_async.assert_any_call( 'https://ultimate.verifier', method='POST', payload={ 'config_set': 'projects/foo', 'path': 'bar.cfg', 'content': cfg_b64, }, scopes=net.EMAIL_SCOPE, ) ############################################################################ # Error found net.json_request_async.side_effect = None net.json_request_async.return_value = ndb.Future() net.json_request_async.return_value.set_result( {'messages': [{ 'text': 'error', 'severity': 'ERROR' }]}) result = validation.validate_config('projects/baz/refs/x', 'qux.cfg', cfg) self.assertEqual( result.messages, [validation_context.Message(text='error', severity=logging.ERROR)]) ############################################################################ # Less-expected responses res = {'messages': [{'severity': 'invalid severity'}, {}, []]} net.json_request_async.return_value = ndb.Future() net.json_request_async.return_value.set_result(res) result = validation.validate_config('projects/baz/refs/x', 'qux.cfg', cfg) self.assertEqual( result.messages, [ validation_context.Message( severity=logging.CRITICAL, text=( 'Error during external validation: invalid response: ' 'unexpected message severity: invalid severity\n' 'url: https://ultimate.verifier\n' 'config_set: projects/baz/refs/x\n' 'path: qux.cfg\n' 'response: %r' % res), ), validation_context.Message(severity=logging.INFO, text=''), validation_context.Message( severity=logging.CRITICAL, text=( 'Error during external validation: invalid response: ' 'message is not a dict: []\n' 'url: https://ultimate.verifier\n' 'config_set: projects/baz/refs/x\n' 'path: qux.cfg\n' 'response: %r' % res), ), ], )
def errmsg(text): return validation_context.Message(severity=logging.ERROR, text=text)