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 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_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 get(self): config.refetch_config()
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 get(self): if config.is_remote_configured(): config.refetch_config()