| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The LUCI Authors. All rights reserved. | 2 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
| 5 | 5 |
| 6 import logging | 6 import logging |
| 7 import sys | 7 import sys |
| 8 import unittest | 8 import unittest |
| 9 | 9 |
| 10 import test_env | 10 import test_env |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 TEST_CONFIG = bots_pb2.BotsCfg( | 22 TEST_CONFIG = bots_pb2.BotsCfg( |
| 23 trusted_dimensions=['pool'], | 23 trusted_dimensions=['pool'], |
| 24 bot_group=[ | 24 bot_group=[ |
| 25 bots_pb2.BotGroup( | 25 bots_pb2.BotGroup( |
| 26 bot_id=['bot1', 'bot{2..3}'], | 26 bot_id=['bot1', 'bot{2..3}'], |
| 27 auth=bots_pb2.BotAuth(require_luci_machine_token=True), | 27 auth=bots_pb2.BotAuth(require_luci_machine_token=True), |
| 28 owners=['owner@example.com'], | 28 owners=['owner@example.com'], |
| 29 dimensions=['pool:A', 'pool:B', 'other:D'], | 29 dimensions=['pool:A', 'pool:B', 'other:D'], |
| 30 ), | 30 ), |
| 31 # This group includes an injected bot_config. |
| 31 bots_pb2.BotGroup( | 32 bots_pb2.BotGroup( |
| 32 bot_id=['other_bot'], | 33 bot_id=['other_bot'], |
| 33 bot_id_prefix=['bot'], | 34 bot_id_prefix=['bot'], |
| 34 machine_type=[bots_pb2.MachineType(lease_duration_secs=1, name='mt', | 35 machine_type=[bots_pb2.MachineType(lease_duration_secs=1, name='mt', |
| 35 mp_dimensions=['k:v'], target_size=1)], | 36 mp_dimensions=['k:v'], target_size=1)], |
| 36 auth=bots_pb2.BotAuth(require_service_account='a@example.com')), | 37 auth=bots_pb2.BotAuth(require_service_account='a@example.com'), |
| 38 bot_config_script='foo.py'), |
| 37 bots_pb2.BotGroup( | 39 bots_pb2.BotGroup( |
| 38 auth=bots_pb2.BotAuth(ip_whitelist='bots'), | 40 auth=bots_pb2.BotAuth(ip_whitelist='bots'), |
| 39 dimensions=['pool:default']), | 41 dimensions=['pool:default']), |
| 40 ], | 42 ], |
| 41 ) | 43 ) |
| 42 | 44 |
| 43 | 45 |
| 44 EXPECTED_GROUP_1 = bot_groups_config._make_bot_group_config( | 46 EXPECTED_GROUP_1 = bot_groups_config._make_bot_group_config( |
| 45 require_luci_machine_token=True, | 47 require_luci_machine_token=True, |
| 46 require_service_account=u'', | 48 require_service_account=u'', |
| 47 ip_whitelist=u'', | 49 ip_whitelist=u'', |
| 48 owners=(u'owner@example.com',), | 50 owners=(u'owner@example.com',), |
| 49 dimensions={u'pool': [u'A', u'B'], u'other': [u'D']}) | 51 dimensions={u'pool': [u'A', u'B'], u'other': [u'D']}, |
| 52 bot_config_script='', |
| 53 bot_config_script_content='') |
| 50 | 54 |
| 51 EXPECTED_GROUP_2 = bot_groups_config._make_bot_group_config( | 55 EXPECTED_GROUP_2 = bot_groups_config._make_bot_group_config( |
| 52 require_luci_machine_token=False, | 56 require_luci_machine_token=False, |
| 53 require_service_account=u'a@example.com', | 57 require_service_account=u'a@example.com', |
| 54 ip_whitelist=u'', | 58 ip_whitelist=u'', |
| 55 owners=(), | 59 owners=(), |
| 56 dimensions={u'pool': []}) | 60 dimensions={u'pool': []}, |
| 61 bot_config_script='foo.py', |
| 62 bot_config_script_content='print "Hi"') |
| 57 | 63 |
| 58 EXPECTED_GROUP_3 = bot_groups_config._make_bot_group_config( | 64 EXPECTED_GROUP_3 = bot_groups_config._make_bot_group_config( |
| 59 require_luci_machine_token=False, | 65 require_luci_machine_token=False, |
| 60 require_service_account=u'', | 66 require_service_account=u'', |
| 61 ip_whitelist=u'bots', | 67 ip_whitelist=u'bots', |
| 62 owners=(), | 68 owners=(), |
| 63 dimensions={u'pool': [u'default']}) | 69 dimensions={u'pool': [u'default']}, |
| 70 bot_config_script='', |
| 71 bot_config_script_content='') |
| 64 | 72 |
| 65 | 73 |
| 66 DEFAULT_AUTH_CFG = bots_pb2.BotAuth(ip_whitelist='bots') | 74 DEFAULT_AUTH_CFG = bots_pb2.BotAuth(ip_whitelist='bots') |
| 67 | 75 |
| 68 | 76 |
| 69 class BotGroupsConfigTest(test_case.TestCase): | 77 class BotGroupsConfigTest(test_case.TestCase): |
| 70 def validator_test(self, cfg, messages): | 78 def validator_test(self, cfg, messages): |
| 71 ctx = validation.Context() | 79 ctx = validation.Context() |
| 72 bot_groups_config.validate_settings(cfg, ctx) | 80 bot_groups_config.validate_settings(cfg, ctx) |
| 73 self.assertEquals(ctx.result().messages, [ | 81 self.assertEquals(ctx.result().messages, [ |
| 74 validation.Message(severity=logging.ERROR, text=m) | 82 validation.Message(severity=logging.ERROR, text=m) |
| 75 for m in messages | 83 for m in messages |
| 76 ]) | 84 ]) |
| 77 | 85 |
| 78 def mock_config(self, cfg): | 86 def mock_config(self, cfg): |
| 79 def get_self_config_mock(path, cls, **_kwargs): | 87 def get_self_config_mock(path, cls=None, **kwargs): |
| 80 self.assertEquals('bots.cfg', path) | 88 self.assertEqual({'store_last_good': True}, kwargs) |
| 81 self.assertEquals(cls, bots_pb2.BotsCfg) | 89 if path == 'bots.cfg': |
| 82 return None, cfg | 90 self.assertEqual(cls, bots_pb2.BotsCfg) |
| 91 return '123', cfg |
| 92 self.assertEqual('scripts/foo.py', path) |
| 93 return '123', 'print "Hi"' |
| 94 |
| 83 self.mock(config, 'get_self_config', get_self_config_mock) | 95 self.mock(config, 'get_self_config', get_self_config_mock) |
| 84 utils.clear_cache(bot_groups_config._fetch_bot_groups) | 96 utils.clear_cache(bot_groups_config._fetch_bot_groups) |
| 85 | 97 |
| 86 def test_version(self): | 98 def test_version(self): |
| 87 self.assertEqual('hash:4564ed4cc34544', EXPECTED_GROUP_1.version) | 99 self.assertEqual('hash:be689c69320bcf', EXPECTED_GROUP_1.version) |
| 88 self.assertEqual('hash:18206c33fffaa8', EXPECTED_GROUP_2.version) | 100 self.assertEqual('hash:97cfccc0d3f7d0', EXPECTED_GROUP_2.version) |
| 89 | 101 |
| 90 def test_expand_bot_id_expr_success(self): | 102 def test_expand_bot_id_expr_success(self): |
| 91 def check(expected, expr): | 103 def check(expected, expr): |
| 92 self.assertEquals( | 104 self.assertEquals( |
| 93 expected, list(bot_groups_config._expand_bot_id_expr(expr))) | 105 expected, list(bot_groups_config._expand_bot_id_expr(expr))) |
| 94 check(['abc'], 'abc') | 106 check(['abc'], 'abc') |
| 95 check(['abc1def', 'abc2def'], 'abc{1,2}def') | 107 check(['abc1def', 'abc2def'], 'abc{1,2}def') |
| 96 check(['abc1def', 'abc2def', 'abc3def'], 'abc{1..3}def') | 108 check(['abc1def', 'abc2def', 'abc3def'], 'abc{1..3}def') |
| 97 | 109 |
| 98 def test_expand_bot_id_expr_fail(self): | 110 def test_expand_bot_id_expr_fail(self): |
| (...skipping 10 matching lines...) Expand all Loading... |
| 109 check_fail('}def') | 121 check_fail('}def') |
| 110 check_fail('1}def') | 122 check_fail('1}def') |
| 111 check_fail('abc{1..}') | 123 check_fail('abc{1..}') |
| 112 check_fail('abc{..2}') | 124 check_fail('abc{..2}') |
| 113 | 125 |
| 114 def test_fetch_bot_groups(self): | 126 def test_fetch_bot_groups(self): |
| 115 self.mock_config(TEST_CONFIG) | 127 self.mock_config(TEST_CONFIG) |
| 116 cfg = bot_groups_config._fetch_bot_groups() | 128 cfg = bot_groups_config._fetch_bot_groups() |
| 117 | 129 |
| 118 self.assertEquals({ | 130 self.assertEquals({ |
| 119 'bot1': EXPECTED_GROUP_1, | 131 u'bot1': EXPECTED_GROUP_1, |
| 120 'bot2': EXPECTED_GROUP_1, | 132 u'bot2': EXPECTED_GROUP_1, |
| 121 'bot3': EXPECTED_GROUP_1, | 133 u'bot3': EXPECTED_GROUP_1, |
| 122 'other_bot': EXPECTED_GROUP_2, | 134 u'other_bot': EXPECTED_GROUP_2, |
| 123 }, cfg.direct_matches) | 135 }, cfg.direct_matches) |
| 124 self.assertEquals([('bot', EXPECTED_GROUP_2)], cfg.prefix_matches) | 136 self.assertEquals([('bot', EXPECTED_GROUP_2)], cfg.prefix_matches) |
| 125 self.assertEquals(EXPECTED_GROUP_3, cfg.default_group) | 137 self.assertEquals(EXPECTED_GROUP_3, cfg.default_group) |
| 126 | 138 |
| 127 def test_get_bot_group_config(self): | 139 def test_get_bot_group_config(self): |
| 128 self.mock_config(TEST_CONFIG) | 140 self.mock_config(TEST_CONFIG) |
| 129 self.assertEquals( | 141 self.assertEquals( |
| 130 EXPECTED_GROUP_1, bot_groups_config.get_bot_group_config('bot1', None)) | 142 EXPECTED_GROUP_1, bot_groups_config.get_bot_group_config('bot1', None)) |
| 131 self.assertEquals( | 143 self.assertEquals( |
| 132 EXPECTED_GROUP_2, bot_groups_config.get_bot_group_config('botzz', 'mt')) | 144 EXPECTED_GROUP_2, bot_groups_config.get_bot_group_config('botzz', 'mt')) |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 'bot_group #0: machine_type #0: target_size must be positive' | 444 'bot_group #0: machine_type #0: target_size must be positive' |
| 433 ]) | 445 ]) |
| 434 | 446 |
| 435 | 447 |
| 436 if __name__ == '__main__': | 448 if __name__ == '__main__': |
| 437 if '-v' in sys.argv: | 449 if '-v' in sys.argv: |
| 438 unittest.TestCase.maxDiff = None | 450 unittest.TestCase.maxDiff = None |
| 439 logging.basicConfig( | 451 logging.basicConfig( |
| 440 level=logging.DEBUG if '-v' in sys.argv else logging.CRITICAL) | 452 level=logging.DEBUG if '-v' in sys.argv else logging.CRITICAL) |
| 441 unittest.main() | 453 unittest.main() |
| OLD | NEW |