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
This commit is contained in:
Gregory Haynes 2014-08-01 09:51:15 -07:00
parent 7e913145a3
commit 6d32511dd0
2 changed files with 8 additions and 5 deletions

View File

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

View File

@ -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')]