def testInvalidContentPulled(self): subscription = """ subscriptions: { name: "Missing Email" bug_labels: ["Some-Label"] bug_components: ["Some>Component"] patterns: [{glob: "project/**"}] }""" invalid_content = """ { "configs": [ { "url": "https://example.com/p/s/+/0123456789abcde/chromeperf-sheriff.cfg", "content": "%s", "content_hash": "v1:somehash", "config_set": "projects/project", "revision": "0123456789abcdef" } ] }""" % (base64.standard_b64encode(bytearray(subscription, 'utf-8')).decode(), ) app = service.CreateApp({ 'environ': { 'GOOGLE_CLOUD_PROJECT': 'chromeperf', 'GAE_SERVICE': 'sheriff-config', }, 'datastore_client': datastore.Client(credentials=credentials.AnonymousCredentials(), project='chromeperf'), 'http': HttpMockSequenceWithDiscovery([({ 'status': '200' }, invalid_content), ({ 'status': '200' }, self.sample_config), ({ 'status': '200' }, invalid_content)]), }) client = app.test_client() # Step 1: Get an invalid config. response = client.get('/configs/update', headers={'X-Forwarded-Proto': 'https'}) self.assertEqual(response.status_code, 500) # Step 2: Get a config that's valid. response = client.get('/configs/update', headers={'X-Forwarded-Proto': 'https'}) self.assertEqual(response.status_code, 200) self.AssertProjectConfigSet1Holds(client, 200) self.AssertProjectConfigSet2Holds(client, 200) # Step 3: Get a config that's invalid, but ensure that the valid config # holds. response = client.get('/configs/update', headers={'X-Forwarded-Proto': 'https'}) self.assertEqual(response.status_code, 500) self.AssertProjectConfigSet1Holds(client, 200) self.AssertProjectConfigSet2Holds(client, 200)
def setUp(self): self.app = service.CreateApp({ 'environ': { 'GOOGLE_CLOUD_PROJECT': 'chromeperf', 'GAE_SERVICE': 'sheriff-config' }, 'http': HttpMockSequenceWithDiscovery([]) }) self.client = self.app.test_client()
def testListPrivate(self): configs = [ ('', '', sheriff_pb2.Subscription( name='Private', visibility=sheriff_pb2.Subscription.INTERNAL_ONLY, )), ('', '', sheriff_pb2.Subscription( name='Public', visibility=sheriff_pb2.Subscription.PUBLIC, )), ] http = HttpMockSequenceWithDiscovery([ ({ 'status': '200' }, '{ "is_member": true }'), ({ 'status': '200' }, '{ "is_member": false }'), ]) _ = service_client.CreateServiceClient( 'https://luci-config.appspot.com/_ah/api', 'config', 'v1', http=http) auth_client = service_client.CreateServiceClient( 'https://chrome-infra-auth.appspot.com/_ah/api', 'auth', 'v1', http=http) request = sheriff_config_pb2.ListRequest(identity_email='foo@bar1') configs = match_policy.FilterSubscriptionsByIdentity( auth_client, request, configs) self.assertEqual(['Private', 'Public'], [s.name for _, _, s in configs]) request = sheriff_config_pb2.ListRequest(identity_email='foo@bar2') configs = match_policy.FilterSubscriptionsByIdentity( auth_client, request, configs) self.assertEqual(['Public'], [s.name for _, _, s in configs]) request = sheriff_config_pb2.ListRequest() configs = match_policy.FilterSubscriptionsByIdentity( auth_client, request, configs) self.assertEqual(['Public'], [s.name for _, _, s in configs])
def setUp(self): with open('tests/sample-configs-get_project_configs.json' ) as sample_config_file: self.sample_config = sample_config_file.read() self.app = service.CreateApp({ 'environ': { 'GOOGLE_CLOUD_PROJECT': 'chromeperf', 'GAE_SERVICE': 'sheriff-config', }, 'datastore_client': datastore.Client(credentials=credentials.AnonymousCredentials(), project='chromeperf'), 'http': HttpMockSequenceWithDiscovery([({ 'status': '200' }, self.sample_config)]), })
def testPollAndEmptyConfigs(self): app = service.CreateApp({ 'environ': { 'GOOGLE_CLOUD_PROJECT': 'chromeperf', 'GAE_SERVICE': 'sheriff-config', }, 'datastore_client': datastore.Client(credentials=credentials.AnonymousCredentials(), project='chromeperf'), 'http': HttpMockSequenceWithDiscovery([({ 'status': '200' }, '{}')]) }) client = app.test_client() response = client.get('/configs/update', headers={'X-Forwarded-Proto': 'https'}) self.assertEqual(response.status_code, 200)
def testPollConfigAddsAndRemoves(self): with open('tests/sample-configs-get_project_configs_reduced.json' ) as sample_config_file: sample_config_reduced = sample_config_file.read() app = service.CreateApp({ 'environ': { 'GOOGLE_CLOUD_PROJECT': 'chromeperf', 'GAE_SERVICE': 'sheriff-config', }, 'datastore_client': datastore.Client(credentials=credentials.AnonymousCredentials(), project='chromeperf'), 'http': HttpMockSequenceWithDiscovery([({ 'status': '200' }, self.sample_config), ({ 'status': '200' }, sample_config_reduced)]), }) # Step 1: Get one configuration with two config sets. client = app.test_client() response = client.get('/configs/update', headers={'X-Forwarded-Proto': 'https'}) self.assertEqual(response.status_code, 200) self.AssertProjectConfigSet1Holds(client, 200) self.AssertProjectConfigSet2Holds(client, 200) # Step 2: Get another configuration, this time with just one config set. response = client.get('/configs/update', headers={'X-Forwarded-Proto': 'https'}) self.assertEqual(response.status_code, 200) # Update doesn't take effect because of caching self.AssertProjectConfigSet1Holds(client, 200) self.AssertProjectConfigSet2Holds(client, 200) # mocking utils.Time to invalid caching with mock.patch('utils.Time') as mock_time: mock_time.method.return_value = (time.time() + 60) self.AssertProjectConfigSet1Holds(client, 404) self.AssertProjectConfigSet2Holds(client, 200)
def testFindingAllSheriffConfigs(self): with open('tests/configs-projects-empty.json' ) as configs_get_projects_file: configs_get_projects_response = configs_get_projects_file.read() http = HttpMockSequenceWithDiscovery([ ({ 'status': '200' }, configs_get_projects_response), ]) service = service_client.CreateServiceClient( 'https://luci-config.appspot.com/_ah/api', 'config', 'v1', http=http) _ = service_client.CreateServiceClient( 'https://chrome-infra-auth.appspot.com/_ah/api', 'auth', 'v1', http=http) self.assertEqual({}, luci_config.FindAllSheriffConfigs(service))
def testListSubscriptions(self): app = service.CreateApp({ 'environ': { 'GOOGLE_CLOUD_PROJECT': 'chromeperf', 'GAE_SERVICE': 'sheriff-config', }, 'datastore_client': datastore.Client(credentials=credentials.AnonymousCredentials(), project='chromeperf'), 'http': HttpMockSequenceWithDiscovery([({ 'status': '200' }, self.sample_config), ({ 'status': '200' }, '{ "is_member": true }'), ({ 'status': '200' }, '{ "is_member": false }')]), }) client = app.test_client() response = client.get('/configs/update', headers={'X-Forwarded-Proto': 'https'}) self.assertEqual(response.status_code, 200) response = client.post('/subscriptions/list', json={'identity_email': '*****@*****.**'}, headers={'X-Forwarded-Proto': 'https'}) self.assertEqual(response.status_code, 200) self.assertDictEqual( response.get_json(), { 'subscriptions': [{ 'config_set': 'projects/project', 'revision': '0123456789abcdef', 'subscription': { 'name': 'Config 1', 'contact_email': '*****@*****.**', 'bug_labels': ['Some-Label'], 'bug_components': ['Some>Component'], 'auto_triage': { 'enable': False }, 'auto_bisection': { 'enable': False }, 'rules': {}, } }, { 'config_set': 'projects/project', 'revision': '0123456789abcdef', 'subscription': { 'name': 'Config 2', 'contact_email': '*****@*****.**', 'bug_labels': ['Some-Label'], 'bug_components': ['Some>Component'], 'auto_triage': { 'enable': False }, 'auto_bisection': { 'enable': False }, 'rules': {}, } }, { 'config_set': 'projects/other_project', 'revision': '0123456789abcdff', 'subscription': { 'name': 'Expected 1', 'monorail_project_id': 'non-chromium', 'contact_email': '*****@*****.**', 'bug_labels': ['Some-Label'], 'bug_components': ['Some>Component'], 'auto_triage': { 'enable': False }, 'auto_bisection': { 'enable': False }, 'rules': {}, } }] }) response = client.post('/subscriptions/list', json={'identity_email': '*****@*****.**'}, headers={'X-Forwarded-Proto': 'https'}) self.assertEqual(response.status_code, 200) self.assertDictEqual(response.get_json(), {})
def testListSubscriptions(self): app = service.CreateApp({ 'environ': { 'GOOGLE_CLOUD_PROJECT': 'chromeperf', 'GAE_SERVICE': 'sheriff-config', }, 'datastore_client': datastore.Client(credentials=credentials.AnonymousCredentials(), project='chromeperf'), 'http': HttpMockSequenceWithDiscovery([({ 'status': '200' }, self.sample_config), ({ 'status': '200' }, '{ "is_member": true }'), ({ 'status': '200' }, '{ "is_member": false }')]), }) client = app.test_client() response = client.get('/configs/update', headers={'X-Forwarded-Proto': 'https'}) self.assertEqual(response.status_code, 200) response = client.post('/subscriptions/list', json={'identity_email': '*****@*****.**'}, headers={'X-Forwarded-Proto': 'https'}) self.assertEqual(response.status_code, 200) self.assertDictEqual( response.get_json(), { 'subscriptions': [{ 'config_set': 'projects/project', 'revision': '0123456789abcdef', 'subscription': { 'name': 'Config 1', 'notification_email': '*****@*****.**', 'bug_labels': ['Some-Label'], 'bug_components': ['Some>Component'], 'patterns': [{ 'glob': 'project/**' }] } }, { 'config_set': 'projects/project', 'revision': '0123456789abcdef', 'subscription': { 'name': 'Config 2', 'notification_email': '*****@*****.**', 'bug_labels': ['Some-Label'], 'bug_components': ['Some>Component'], 'patterns': [{ 'regex': '^project/platform/.*/memory_peak$' }] } }, { 'config_set': 'projects/other_project', 'revision': '0123456789abcdff', 'subscription': { 'name': 'Expected 1', 'notification_email': '*****@*****.**', 'bug_labels': ['Some-Label'], 'bug_components': ['Some>Component'], 'patterns': [{ 'glob': 'Master/Bot/Test/Metric/Something' }] } }] }) response = client.post('/subscriptions/list', json={'identity_email': '*****@*****.**'}, headers={'X-Forwarded-Proto': 'https'}) self.assertEqual(response.status_code, 200) self.assertDictEqual(response.get_json(), {})