Add exit_codes option to docker-cmd hook
Some command we need to run with 'exec' or 'run' have custom exit codes that are valid if the command completes sucessfully. This patch adds an 'exit_codes' option which takes an array of integers that are meant to represent the valid exit codes for this hook. If for example 0 or 1 is meant to be considered success you can specify: exit_codes: [0,1] Previously an non-zero exit code would always fail this hook. Change-Id: Ia6ca4b01982a0b33b26eca2a907d9d9f87c19922
This commit is contained in:
parent
1e87454039
commit
408c3f8642
|
@ -86,6 +86,7 @@ def main(argv=sys.argv):
|
|||
|
||||
for container in sorted(config):
|
||||
action = config[container].get('action', 'run')
|
||||
exit_codes = config[container].get('exit_codes', [0])
|
||||
|
||||
if action == 'run':
|
||||
cmd = [
|
||||
|
@ -137,8 +138,11 @@ def main(argv=sys.argv):
|
|||
else:
|
||||
log.debug('Completed %s' % cmd)
|
||||
|
||||
if subproc.returncode != 0:
|
||||
if subproc.returncode not in exit_codes:
|
||||
log.error("Error running %s. [%s]\n" % (cmd, subproc.returncode))
|
||||
deploy_status_code = subproc.returncode
|
||||
else:
|
||||
log.debug('Completed %s' % cmd)
|
||||
|
||||
json.dump(build_response(
|
||||
'\n'.join(stdout), '\n'.join(stderr), deploy_status_code), sys.stdout)
|
||||
|
|
|
@ -55,6 +55,18 @@ class HookDockerCmdTest(common.RunScriptTest):
|
|||
}
|
||||
}
|
||||
|
||||
data_exit_code = {
|
||||
"name": "abcdef001",
|
||||
"group": "docker-cmd",
|
||||
"config": {
|
||||
"web-ls": {
|
||||
"action": "exec",
|
||||
"command": ["web", "/bin/ls", "-l"],
|
||||
"exit_codes": [0, 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def setUp(self):
|
||||
super(HookDockerCmdTest, self).setUp()
|
||||
self.hook_path = self.relative_path(
|
||||
|
@ -139,6 +151,33 @@ class HookDockerCmdTest(common.RunScriptTest):
|
|||
'-l'
|
||||
], state_2['args'])
|
||||
|
||||
def test_hook_exit_codes(self):
|
||||
|
||||
self.env.update({
|
||||
'TEST_RESPONSE': json.dumps({
|
||||
'stdout': '',
|
||||
'stderr': 'Warning: custom exit code',
|
||||
'returncode': 1
|
||||
})
|
||||
})
|
||||
returncode, stdout, stderr = self.run_cmd(
|
||||
[self.hook_path], self.env, json.dumps(self.data_exit_code))
|
||||
|
||||
self.assertEqual({
|
||||
'deploy_stdout': '',
|
||||
'deploy_stderr': 'Warning: custom exit code',
|
||||
'deploy_status_code': 0
|
||||
}, json.loads(stdout))
|
||||
|
||||
state_0 = self.json_from_file(self.test_state_path)
|
||||
self.assertEqual([
|
||||
self.fake_tool_path,
|
||||
'exec',
|
||||
'web',
|
||||
'/bin/ls',
|
||||
'-l'
|
||||
], state_0['args'])
|
||||
|
||||
def test_hook_failed(self):
|
||||
|
||||
self.env.update({
|
||||
|
|
Loading…
Reference in New Issue