Skip to content

Commit 114d3a0

Browse files
author
Vladimir Kotal
authored
make cleanup a list in CommandSequence (#2878)
fixes #2830
1 parent a2306f0 commit 114d3a0

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

opengrok-tools/src/main/python/opengrok_tools/utils/commandsequence.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ def __init__(self, name, commands, loglevel=logging.INFO, cleanup=None,
4848
self.failed = False
4949
self.retcodes = {}
5050
self.outputs = {}
51+
if cleanup and not isinstance(cleanup, list):
52+
raise Exception("cleanup is not a list of commands")
53+
5154
self.cleanup = cleanup
5255
self.loglevel = loglevel
5356
self.driveon = driveon
@@ -173,13 +176,17 @@ def run(self):
173176

174177
def run_cleanup(self):
175178
"""
176-
Call cleanup in case the sequence failed or termination was requested.
179+
Call cleanup sequence in case the command sequence failed
180+
or termination was requested.
177181
"""
178-
if self.cleanup:
179-
if is_web_uri(self.cleanup.get("command")[0]):
180-
self.call_rest_api(self.cleanup)
182+
if self.cleanup is None:
183+
return
184+
185+
for cleanup_cmd in self.cleanup:
186+
if is_web_uri(cleanup_cmd.get("command")[0]):
187+
self.call_rest_api(cleanup_cmd)
181188
else:
182-
command_args = self.cleanup.get("command")
189+
command_args = cleanup_cmd.get("command")
183190
self.logger.debug("Running cleanup command '{}'".
184191
format(command_args))
185192
cmd = Command(command_args,

opengrok-tools/src/test/python/test_command_sequence.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import os
2727

2828
import pytest
29+
import tempfile
2930

3031
from opengrok_tools.utils.commandsequence import CommandSequence, \
3132
CommandSequenceBase
@@ -116,3 +117,37 @@ def test_project_subst():
116117
cmds.run()
117118

118119
assert cmds.outputs['/bin/echo test-subst'] == ['test-subst\n']
120+
121+
122+
def test_cleanup_exception():
123+
"""
124+
If cleanup is not a list, Exception should be thrown when initializing
125+
the CommandSequence object.
126+
"""
127+
cleanup = {"cleanup": ["foo", CommandSequence.PROJECT_SUBST]}
128+
with pytest.raises(Exception):
129+
CommandSequence(CommandSequenceBase("test-cleanup-list", None,
130+
cleanup=cleanup))
131+
132+
133+
@pytest.mark.skipif(not os.path.exists('/usr/bin/touch') or
134+
not os.path.exists('/bin/cat'),
135+
reason="requires Unix")
136+
def test_cleanup():
137+
with tempfile.TemporaryDirectory() as tmpdir:
138+
file_foo = os.path.join(tmpdir, "foo")
139+
file_bar = os.path.join(tmpdir, "bar")
140+
cleanup_list = [{"command": ["/usr/bin/touch", file_foo]},
141+
{"command": ["/bin/cat", "/totallynonexistent"]},
142+
{"command": ["/usr/bin/touch", file_bar]}]
143+
# Running 'cat' on non-existing entry causes it to return 1.
144+
cmd = ["/bin/cat", "/foobar"]
145+
cmd_list = [{"command": cmd}]
146+
commands = CommandSequence(CommandSequenceBase("test-cleanup-list",
147+
cmd_list,
148+
cleanup=cleanup_list))
149+
assert commands is not None
150+
commands.run()
151+
assert list(commands.retcodes.values()) == [1]
152+
assert os.path.isfile(file_foo)
153+
assert os.path.isfile(file_bar)

0 commit comments

Comments
 (0)