diff --git a/os_collect_config/collect.py b/os_collect_config/collect.py index b89f664..426f39b 100644 --- a/os_collect_config/collect.py +++ b/os_collect_config/collect.py @@ -54,6 +54,31 @@ def setup_conf(): CONF.register_cli_opts(opts) +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: + print [store, any_changed, collector] + if requests_impl_map and collector.name in requests_impl_map: + requests_impl = requests_impl_map[collector.name] + else: + requests_impl = common.requests + content = collector.Collector(requests_impl=requests_impl).collect() + + 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 __main__(requests_impl_map=None): setup_conf() CONF(prog="os-collect-config") diff --git a/os_collect_config/tests/test_collect.py b/os_collect_config/tests/test_collect.py index 801b711..432ec28 100644 --- a/os_collect_config/tests/test_collect.py +++ b/os_collect_config/tests/test_collect.py @@ -105,6 +105,45 @@ class TestCollect(testtools.TestCase): self.assertIn('cfn', out_struct) +class TestCollectAll(testtools.TestCase): + def setUp(self): + super(TestCollectAll, self).setUp() + self.useFixture(fixtures.FakeLogger()) + collect.setup_conf() + self.cache_dir = self.useFixture(fixtures.TempDir()) + cfg.CONF.cachedir = self.cache_dir.path + cfg.CONF.cfn.metadata_url = 'http://127.0.0.1:8000/' + cfg.CONF.cfn.stack_name = 'foo' + cfg.CONF.cfn.path = ['foo.Metadata'] + self.addCleanup(cfg.CONF.reset) + + def _call_collect_all(self, store): + requests_impl_map = {'ec2': test_ec2.FakeRequests, + 'cfn': test_cfn.FakeRequests(self)} + return collect.collect_all( + collect.COLLECTORS, + store=store, + requests_impl_map=requests_impl_map) + + def test_collect_all_store(self): + (any_changed, paths) = self._call_collect_all(store=True) + self.assertTrue(any_changed) + self.assertThat(paths, matchers.IsInstance(list)) + for collector in collect.COLLECTORS: + self.assertIn(os.path.join(self.cache_dir.path, '%s.json' % + collector.name), + paths) + self.assertTrue(any_changed) + + def test_collect_all_nostore(self): + (any_changed, content) = self._call_collect_all(store=False) + self.assertFalse(any_changed) + self.assertThat(content, matchers.IsInstance(dict)) + for collector in collect.COLLECTORS: + self.assertIn(collector.name, content) + self.assertThat(content[collector.name], matchers.IsInstance(dict)) + + class TestConf(testtools.TestCase): def test_setup_conf(self): collect.setup_conf()