From 6d32511dd052abeac62744246ae6fd55869169a4 Mon Sep 17 00:00:00 2001 From: Gregory Haynes Date: Fri, 1 Aug 2014 09:51:15 -0700 Subject: [PATCH] Exit with error status on failed command When we detect a failed command we log ERROR but we do not return an error status. This makes it difficult for programs which may run os-collect-config to detect whether a run was sucessful. This only applies to runs which are performed with --one-time argument as this is a straightforward case. Change-Id: I168862e8c75c15d1ea405a417908d1284feb7b32 --- os_collect_config/collect.py | 5 ++++- os_collect_config/tests/test_collect.py | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/os_collect_config/collect.py b/os_collect_config/collect.py index ca44cfb..084b41c 100644 --- a/os_collect_config/collect.py +++ b/os_collect_config/collect.py @@ -224,6 +224,7 @@ def __main__(args=sys.argv, collector_kwargs_map=None): if CONF.force: CONF.set_override('one_time', True) + exitval = 0 config_files = CONF.config_file config_hash = getfilehash(config_files) while True: @@ -239,6 +240,7 @@ def __main__(args=sys.argv, collector_kwargs_map=None): try: call_command(content, CONF.command) except subprocess.CalledProcessError as e: + exitval = e.returncode logger.error('Command failed, will not cache new data. %s' % e) if not CONF.one_time: @@ -270,7 +272,8 @@ def __main__(args=sys.argv, collector_kwargs_map=None): else: print(json.dumps(content, indent=1)) break + return exitval if __name__ == '__main__': - __main__() + sys.exit(__main__()) diff --git a/os_collect_config/tests/test_collect.py b/os_collect_config/tests/test_collect.py index 5cbabfb..09dbc43 100644 --- a/os_collect_config/tests/test_collect.py +++ b/os_collect_config/tests/test_collect.py @@ -65,8 +65,8 @@ class TestCollect(testtools.TestCase): 'heatclient': test_heat.FakeHeatClient(self) } } - collect.__main__(args=fake_args, - collector_kwargs_map=collector_kwargs_map) + return collect.__main__(args=fake_args, + collector_kwargs_map=collector_kwargs_map) def _fake_popen_call_main(self, occ_args): calls = [] @@ -75,7 +75,7 @@ class TestCollect(testtools.TestCase): calls.append(proc_args) return dict(returncode=0) self.useFixture(fixtures.FakePopen(capture_popen)) - self._call_main(occ_args) + self.assertEqual(0, self._call_main(occ_args)) return calls def test_main(self): @@ -196,7 +196,7 @@ class TestCollect(testtools.TestCase): calls.append(proc_args) return dict(returncode=1) self.useFixture(fixtures.FakePopen(capture_popen)) - self._call_main(occ_args) + self.assertEqual(1, self._call_main(occ_args)) for test_dir in (cache_dir, backup_cache_dir): cache_contents = os.listdir(test_dir.path) last_files = [n for n in cache_contents if n.endswith('last')]