def test_fetch_configs_ok(self): fetches = { 'imports.cfg': ('imports_cfg_rev', 'tarball{url:"a" systems:"b"}'), 'ip_whitelist.cfg': ('ip_whitelist_cfg_rev', config_pb2.IPWhitelistConfig()), 'oauth.cfg': ('oauth_cfg_rev', config_pb2.OAuthConfig(primary_client_id='a')), } @ndb.tasklet def get_self_config_mock(path, *_args, **_kwargs): self.assertIn(path, fetches) raise ndb.Return(fetches.pop(path)) self.mock(config_component, 'get_self_config_async', get_self_config_mock) self.mock(config, '_get_configs_url', lambda: 'http://url') result = config._fetch_configs(fetches.keys()) self.assertFalse(fetches) self.assertEqual( { 'imports.cfg': (config.Revision('imports_cfg_rev', 'http://url'), 'tarball{url:"a" systems:"b"}'), 'ip_whitelist.cfg': (config.Revision('ip_whitelist_cfg_rev', 'http://url'), config_pb2.IPWhitelistConfig()), 'oauth.cfg': (config.Revision('oauth_cfg_rev', 'http://url'), config_pb2.OAuthConfig(primary_client_id='a')), }, result)
def test_update_service_config(self): # Missing. self.assertIsNone(config._get_service_config('abc.cfg')) self.assertIsNone(config._get_service_config_rev('abc.cfg')) # Updated. rev = config.Revision('rev', 'url') self.assertTrue(config._update_service_config('abc.cfg', rev, 'body')) self.assertEqual('body', config._get_service_config('abc.cfg')) self.assertEqual(rev, config._get_service_config_rev('abc.cfg')) # Same body, returns False, though updates rev. rev2 = config.Revision('rev2', 'url') self.assertFalse(config._update_service_config('abc.cfg', rev2, 'body')) self.assertEqual(rev2, config._get_service_config_rev('abc.cfg'))
def test_update_imports_config(self): new_rev = config.Revision('rev', 'url') body = 'tarball{url:"a" systems:"b"}' self.assertTrue(config._update_imports_config(None, new_rev, body)) self.assertEqual( new_rev, config._get_imports_config_revision_async().get_result())
def test_settings_updates(self): # Fetch only settings.cfg in this test case. self.mock(config, 'is_remote_configured', lambda: True) self.mock(config, '_CONFIG_SCHEMAS', { 'settings.cfg': config._CONFIG_SCHEMAS['settings.cfg'], }) # Default settings. self.assertEqual(config_pb2.SettingsCfg(), config.get_settings()) # Mock new settings value in luci-config. settings_cfg_text = 'enable_ts_monitoring: true' self.mock( config, '_fetch_configs', lambda _: { 'settings.cfg': (config.Revision('rev', 'url'), settings_cfg_text), }) # Fetch them. config.refetch_config() # Verify they are used now. utils.clear_cache(config.get_settings) self.assertEqual(config_pb2.SettingsCfg(enable_ts_monitoring=True), config.get_settings()) # "Delete" them from luci-config. self.mock( config, '_fetch_configs', lambda _: { 'settings.cfg': (config.Revision('0' * 40, 'url'), ''), }) # Fetch them. config.refetch_config() # Verify defaults are restored. utils.clear_cache(config.get_settings) self.assertEqual(config_pb2.SettingsCfg(), config.get_settings())
def test_get_delegation_config(self): # Missing -> returns empty proto. proto = config.get_delegation_config() self.assertFalse(proto.rules) # Add some. body = """rules { user_id: "service:abc" target_service: "*" max_validity_duration: 3600 }""" config._update_service_config('delegation.cfg', config.Revision('rev', 'url'), body) utils.clear_cache(config.get_delegation_config) proto = config.get_delegation_config() self.assertEqual(1, len(proto.rules))
def run(conf): return config._update_oauth_config( config.Revision('oauth_cfg_rev', 'http://url'), conf)
def test_refetch_config(self): # Mock of "current known revision" state. revs = { 'a.cfg': config.Revision('old_a_rev', 'urla'), 'b.cfg': config.Revision('old_b_rev', 'urlb'), 'c.cfg': config.Revision('old_c_rev', 'urlc'), } bumps = [] def bump_rev(pkg, rev, conf): revs[pkg] = rev bumps.append((pkg, rev, conf, ndb.in_transaction())) return True self.mock(config, 'is_remote_configured', lambda: True) self.mock( config, '_CONFIG_SCHEMAS', { # Will be updated outside of auth db transaction. 'a.cfg': { 'proto_class': None, 'revision_getter': lambda: revs['a.cfg'], 'validator': lambda body: self.assertEqual(body, 'new a body'), 'updater': lambda rev, conf: bump_rev('a.cfg', rev, conf), 'use_authdb_transaction': False, }, # Will not be changed. 'b.cfg': { 'proto_class': None, 'revision_getter': lambda: revs['b.cfg'], 'validator': lambda _body: True, 'updater': lambda rev, conf: bump_rev('b.cfg', rev, conf), 'use_authdb_transaction': False, }, # Will be updated instance auth db transaction. 'c.cfg': { 'proto_class': None, 'revision_getter': lambda: revs['c.cfg'], 'validator': lambda body: self.assertEqual(body, 'new c body'), 'updater': lambda rev, conf: bump_rev('c.cfg', rev, conf), 'use_authdb_transaction': True, }, }) self.mock( config, '_fetch_configs', lambda _: { 'a.cfg': (config.Revision('new_a_rev', 'urla'), 'new a body'), 'b.cfg': (config.Revision('old_b_rev', 'urlb'), 'old b body'), 'c.cfg': (config.Revision('new_c_rev', 'urlc'), 'new c body'), }) # Initial update. config.refetch_config() self.assertEqual([ ('a.cfg', config.Revision('new_a_rev', 'urla'), 'new a body', False), ('c.cfg', config.Revision('new_c_rev', 'urlc'), 'new c body', True), ], bumps) del bumps[:] # Refetch, nothing new. config.refetch_config() self.assertFalse(bumps)
def run(conf): return config._update_ip_whitelist_config( config.Revision('ip_whitelist_cfg_rev', 'http://url'), conf)
def run(oauth, sec): return config._update_authdb_configs({ 'oauth.cfg': (config.Revision('cfg_rev', 'http://url'), oauth), 'security.cfg': (config.Revision('cfg_rev', 'http://url'), sec), })
def run(conf): return config._update_authdb_configs({ 'security.cfg': (config.Revision('cfg_rev', 'http://url'), conf), })
def test_refetch_config(self): initial_revs = { 'a.cfg': config.Revision('old_a_rev', 'urla'), 'b.cfg': config.Revision('old_b_rev', 'urlb'), 'c.cfg': config.Revision('old_c_rev', 'urlc'), } revs = initial_revs.copy() bumps = [] def bump_rev(pkg, rev, conf): revs[pkg] = rev bumps.append((pkg, rev, conf, ndb.in_transaction())) return True @ndb.tasklet def get_rev_async(pkg): raise ndb.Return(revs[pkg]) self.mock(config, 'is_remote_configured', lambda: True) self.mock( config, '_CONFIG_SCHEMAS', { # Will be updated outside of auth db transaction. 'a.cfg': { 'proto_class': None, 'revision_getter': lambda: get_rev_async('a.cfg'), 'validator': lambda body: self.assertEqual(body, 'new a body'), 'updater': lambda root, rev, conf: bump_rev('a.cfg', rev, conf), 'use_authdb_transaction': False, }, # Will not be changed. 'b.cfg': { 'proto_class': None, 'revision_getter': lambda: get_rev_async('b.cfg'), 'validator': lambda _body: True, 'updater': lambda root, rev, conf: bump_rev('b.cfg', rev, conf), 'use_authdb_transaction': False, }, # Will be updated inside auth db transaction. 'c.cfg': { 'proto_class': None, 'revision_getter': lambda: get_rev_async('c.cfg'), 'validator': lambda body: self.assertEqual(body, 'new c body'), 'updater': lambda root, rev, conf: bump_rev('c.cfg', rev, conf), 'use_authdb_transaction': True, }, }) # _fetch_configs is called by config.refetch_config(). configs_to_fetch = { 'a.cfg': (config.Revision('new_a_rev', 'urla'), 'new a body'), 'b.cfg': (config.Revision('old_b_rev', 'urlb'), 'old b body'), 'c.cfg': (config.Revision('new_c_rev', 'urlc'), 'new c body'), } self.mock(config, '_fetch_configs', lambda _: configs_to_fetch) # Old revisions initially. self.assertEqual(initial_revs, config.get_revisions()) # Initial update. config.refetch_config() self.assertEqual([ ('a.cfg', config.Revision('new_a_rev', 'urla'), 'new a body', False), ('c.cfg', config.Revision('new_c_rev', 'urlc'), 'new c body', True), ], bumps) del bumps[:] # Updated revisions now. self.assertEqual({k: v[0] for k, v in configs_to_fetch.items()}, config.get_revisions()) # Refetch, nothing new. config.refetch_config() self.assertFalse(bumps)
def run(conf): return config._update_authdb_configs({ 'ip_whitelist.cfg': (config.Revision('ip_whitelist_cfg_rev', 'http://url'), conf), })