From 312592d3dbfa49033d9e4d9fbea63b6c9239f27e Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Fri, 28 Jun 2013 09:16:10 -0700 Subject: [PATCH] If no command is specified, print collected data as json --- os_collect_config/collect.py | 3 +++ os_collect_config/tests/test_collect.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/os_collect_config/collect.py b/os_collect_config/collect.py index 0ea66f5..493a5b8 100644 --- a/os_collect_config/collect.py +++ b/os_collect_config/collect.py @@ -101,6 +101,9 @@ def __main__(): logger.info("Executing %s" % CONF.command) subprocess.call(CONF.command, env=env, shell=True) commit_cache('ec2') + else: + content = {'ec2': ec2_content} + print json.dumps(content, indent=1) if __name__ == '__main__': diff --git a/os_collect_config/tests/test_collect.py b/os_collect_config/tests/test_collect.py index ded8625..42ee0ba 100644 --- a/os_collect_config/tests/test_collect.py +++ b/os_collect_config/tests/test_collect.py @@ -18,6 +18,7 @@ import json import os from oslo.config import cfg import testtools +from testtools import matchers from os_collect_config import collect from os_collect_config.tests import test_ec2 @@ -70,6 +71,22 @@ class TestCollect(testtools.TestCase): self.assertTrue(self.called_fake_call) + def test_main_no_command(self): + fake_args = [ + 'os-collect-config', + '--config-file', + '/dev/null', + ] + self.useFixture( + fixtures.MonkeyPatch('sys.argv', fake_args)) + output = self.useFixture(fixtures.ByteStream('stdout')) + self.useFixture( + fixtures.MonkeyPatch('sys.stdout', output.stream)) + collect.__main__() + out_struct = json.loads(output.stream.getvalue()) + self.assertThat(out_struct, matchers.IsInstance(dict)) + self.assertIn('ec2', out_struct) + class TestConf(testtools.TestCase): def test_setup_conf(self):