isolate collect_all from main

This commit is contained in:
Clint Byrum 2013-07-02 12:31:07 -07:00
parent c56c63ddab
commit ec371c4421
2 changed files with 64 additions and 0 deletions

View File

@ -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")

View File

@ -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()