def test_cache_ignores_json_inequality(self): content1 = u'{"a": "value-a", "b": "value-b"}' content2 = u'{"b": "value-b", "a": "value-a"}' value1 = json.loads(content1) value2 = json.loads(content2) self.assertEqual(value1, value2) (changed, path) = cache.store('content', value1) self.assertTrue(changed) cache.commit('content') (changed, path) = cache.store('content', value1) self.assertFalse(changed) (changed, path) = cache.store('content', value2) self.assertFalse(changed)
def collect_all(collectors, store=False, requests_impl_map=None): any_changed = False if store: paths_or_content = [] else: paths_or_content = {} for collector in collectors: if requests_impl_map and collector.name in requests_impl_map: requests_impl = requests_impl_map[collector.name] else: requests_impl = common.requests try: content = collector.Collector( requests_impl=requests_impl).collect() except exc.SourceNotAvailable: logger.warn('Source [%s] Unavailable.' % collector.name) continue if store: (changed, path) = cache.store(collector.name, content) any_changed |= changed paths_or_content.append(path) else: paths_or_content[collector.name] = content return (any_changed, paths_or_content)
def collect_all(collectors, store=False, requests_impl_map=None): any_changed = False if store: paths_or_content = [] else: paths_or_content = {} for collector in collectors: module = COLLECTORS[collector] if requests_impl_map and collector in requests_impl_map: requests_impl = requests_impl_map[collector] else: requests_impl = common.requests try: content = module.Collector(requests_impl=requests_impl).collect() except exc.SourceNotAvailable: logger.warn("Source [%s] Unavailable." % collector) continue if store: for output_key, output_content in content: (changed, path) = cache.store(output_key, output_content) any_changed |= changed paths_or_content.append(path) else: paths_or_content.update(content) if any_changed: cache.store_meta_list("os_config_files", collectors) if os.path.exists(CONF.backup_cachedir): shutil.rmtree(CONF.backup_cachedir) if os.path.exists(CONF.cachedir): shutil.copytree(CONF.cachedir, CONF.backup_cachedir) return (any_changed, paths_or_content)
def apply_config(self, ctxt, dct): changed, filename = cache.store('messaging', dct) env = dict(os.environ) env['OS_CONFIG_FILES'] = filename logger.info('Will run "%s" with OS_CONFIG_FILES=%s' % (CONF.command, env["OS_CONFIG_FILES"])) if not CONF.print_only: logger.info('Executing...') subprocess.check_call(CONF.command, env=env, shell=True)
def test_cache(self): # Never seen, so changed is expected. (changed, path) = cache.store('foo', {'a': 1}) self.assertTrue(changed) self.assertTrue(os.path.exists(self.cache_dir)) self.assertTrue(os.path.exists(path)) orig_path = '%s.orig' % path self.assertTrue(os.path.exists(orig_path)) last_path = '%s.last' % path self.assertFalse(os.path.exists(last_path)) # .orig exists now but not .last so this will shortcut to changed (changed, path) = cache.store('foo', {'a': 2}) self.assertTrue(changed) orig_path = '%s.orig' % path with open(path) as now: with open(orig_path) as then: self.assertNotEqual(now.read(), then.read()) # Saves the current copy as .last cache.commit('foo') last_path = '%s.last' % path self.assertTrue(os.path.exists(last_path)) # We committed this already, so we should have no changes (changed, path) = cache.store('foo', {'a': 2}) self.assertFalse(changed) cache.commit('foo') # Fully exercising the line-by-line matching now that a .last exists (changed, path) = cache.store('foo', {'a': 3}) self.assertTrue(changed) self.assertTrue(os.path.exists(path)) # And the meta list list_path = cache.store_meta_list('foo_list', ['foo']) self.assertTrue(os.path.exists(list_path)) with open(list_path) as list_file: list_list = json.loads(list_file.read()) self.assertThat(list_list, matchers.IsInstance(list)) self.assertIn(path, list_list)
def test_cache(self): cache_root = self.useFixture(fixtures.TempDir()) cache_dir = os.path.join(cache_root.path, 'cache') collect.setup_conf() cfg.CONF(['--cachedir', cache_dir]) # Never seen, so changed is expected. (changed, path) = cache.store('foo', {'a': 1}) self.assertTrue(changed) self.assertTrue(os.path.exists(cache_dir)) self.assertTrue(os.path.exists(path)) orig_path = '%s.orig' % path self.assertTrue(os.path.exists(orig_path)) last_path = '%s.last' % path self.assertFalse(os.path.exists(last_path)) # .orig exists now but not .last so this will shortcut to changed (changed, path) = cache.store('foo', {'a': 2}) self.assertTrue(changed) orig_path = '%s.orig' % path with open(path) as now: with open(orig_path) as then: self.assertNotEquals(now.read(), then.read()) # Saves the current copy as .last cache.commit('foo') last_path = '%s.last' % path self.assertTrue(os.path.exists(last_path)) # We committed this already, so we should have no changes (changed, path) = cache.store('foo', {'a': 2}) self.assertFalse(changed) cache.commit('foo') # Fully exercising the line-by-line matching now that a .last exists (changed, path) = cache.store('foo', {'a': 3}) self.assertTrue(changed) self.assertTrue(os.path.exists(path))
def collect_all(collectors, store=False, collector_kwargs_map=None): changed_keys = set() all_keys = list() if store: paths_or_content = [] else: paths_or_content = {} for collector in collectors: module = COLLECTORS[collector] if collector_kwargs_map and collector in collector_kwargs_map: collector_kwargs = collector_kwargs_map[collector] else: collector_kwargs = {} try: content = module.Collector(**collector_kwargs).collect() except exc.SourceNotAvailable: logger.warn('Source [%s] Unavailable.' % collector) continue except exc.SourceNotConfigured: logger.debug('Source [%s] Not configured.' % collector) continue if store: for output_key, output_content in content: all_keys.append(output_key) (changed, path) = cache.store(output_key, output_content) if changed: changed_keys.add(output_key) paths_or_content.append(path) else: paths_or_content.update(content) if changed_keys: cache.store_meta_list('os_config_files', all_keys) if os.path.exists(CONF.backup_cachedir): shutil.rmtree(CONF.backup_cachedir) if os.path.exists(CONF.cachedir): shutil.copytree(CONF.cachedir, CONF.backup_cachedir) return (changed_keys, paths_or_content)