Skip to content

Commit 71e56bb

Browse files
author
Vladimir Kotal
authored
use /bin/sh instead of /bin/false to make sure exit value is 1 (#2426)
1 parent c55cba5 commit 71e56bb

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ def run_command(self, command):
8888

8989
def call_rest_api(self, command):
9090
"""
91-
Make RESTful API call.
91+
Make RESTful API call. Occurrence of PROJECT_SUBST in the URI will be
92+
replaced by project name.
9293
"""
9394
command = command.get("command")
9495
uri = command[0].replace(self.PROJECT_SUBST, self.name)
@@ -97,7 +98,7 @@ def call_rest_api(self, command):
9798

9899
headers = None
99100
json_data = None
100-
if len(data) > 0:
101+
if data:
101102
headers = {'Content-Type': 'application/json'}
102103
json_data = json.dumps(data).replace(self.PROJECT_SUBST, self.name)
103104
self.logger.debug("JSON data: {}".format(json_data))
@@ -109,7 +110,7 @@ def call_rest_api(self, command):
109110
elif verb == 'DELETE':
110111
delete(self.logger, uri, data)
111112
else:
112-
self.logger.error('Unknown verb in command {}'.
113+
self.logger.error('Unknown HTTP verb in command {}'.
113114
format(command))
114115

115116
def run(self):
@@ -119,6 +120,10 @@ def run(self):
119120
If the command has return code 2, the sequence will be terminated
120121
however it will not be treated as error.
121122
123+
If a command contains PROJECT_SUBST pattern, it will be replaced
124+
by project name, otherwise project name will be appended to the
125+
argument list of the command.
126+
122127
Any command entry that is a URI, will be used to submit RESTful API
123128
request. Return codes for these requests are not checked.
124129
"""

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
sys.path.insert(0, os.path.abspath(
3333
os.path.join(os.path.dirname(__file__), '..', '..',
34-
'main', 'python', 'opengrok_tools')))
34+
'main', 'python')))
3535

3636
from opengrok_tools.utils.command import Command
3737
import tempfile

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

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
sys.path.insert(0, os.path.abspath(
3232
os.path.join(os.path.dirname(__file__), '..', '..',
33-
'main', 'python', 'opengrok_tools')))
33+
'main', 'python')))
3434

3535
from opengrok_tools.utils.commands import Commands, CommandsBase
3636

@@ -41,50 +41,38 @@ def test_str(self):
4141
[{"command": ['foo']}, {"command": ["bar"]}]))
4242
self.assertEqual("opengrok-master", str(cmds))
4343

44-
@unittest.skipUnless(os.path.exists('/bin/true') and os.path.exists('/bin/false'), "requires Unix")
44+
@unittest.skipUnless(os.path.exists('/bin/sh') and os.path.exists('/bin/echo'), "requires Unix")
4545
def test_run_retcodes(self):
4646
cmds = Commands(CommandsBase("opengrok-master",
4747
[{"command": ["/bin/echo"]},
48-
{"command": ["/bin/true"]},
49-
{"command": ["/bin/false"]}]))
48+
{"command": ["/bin/sh", "-c", "echo " + Commands.PROJECT_SUBST + "; exit 0"]},
49+
{"command": ["/bin/sh", "-c", "echo " + Commands.PROJECT_SUBST + "; exit 1"]}]))
5050
cmds.run()
51-
# print(p.retcodes)
5251
self.assertEqual({'/bin/echo opengrok-master': 0,
53-
'/bin/true opengrok-master': 0,
54-
'/bin/false opengrok-master': 1}, cmds.retcodes)
52+
'/bin/sh -c echo opengrok-master; exit 0': 0,
53+
'/bin/sh -c echo opengrok-master; exit 1': 1}, cmds.retcodes)
5554

56-
@unittest.skipUnless(os.path.exists('/usr/bin/true') and os.path.exists('/usr/bin/false'), "requires Unix")
57-
def test_run_retcodes_usr(self):
58-
cmds = Commands(CommandsBase("opengrok-master",
59-
[{"command": ["/bin/echo"]},
60-
{"command": ["/usr/bin/true"]},
61-
{"command": ["/usr/bin/false"]}]))
62-
cmds.run()
63-
# print(p.retcodes)
64-
self.assertEqual({'/bin/echo opengrok-master': 0,
65-
'/usr/bin/true opengrok-master': 0,
66-
'/usr/bin/false opengrok-master': 1}, cmds.retcodes)
67-
68-
@unittest.skipUnless(os.path.exists('/bin/true') and os.path.exists('/bin/false'), "requires Unix")
55+
@unittest.skipUnless(os.path.exists('/bin/sh') and os.path.exists('/bin/echo'), "requires Unix")
6956
def test_terminate_after_non_zero_code(self):
7057
cmds = Commands(CommandsBase("opengrok-master",
71-
[{"command": ["/bin/false"]},
72-
{"command": ["/bin/true"]}]))
58+
[{"command": ["/bin/sh", "-c", "echo " + Commands.PROJECT_SUBST + "; exit 255"]},
59+
{"command": ["/bin/echo"]}]))
7360
cmds.run()
74-
self.assertEqual({'/bin/false opengrok-master': 1}, cmds.retcodes)
61+
self.assertEqual({'/bin/sh -c echo opengrok-master; exit 255': 255}, cmds.retcodes)
7562

76-
@unittest.skipUnless(os.path.exists('/usr/bin/true') and os.path.exists('/usr/bin/false'), "requires Unix")
77-
def test_terminate_after_non_zero_code_usr(self):
63+
@unittest.skipUnless(os.path.exists('/bin/sh') and os.path.exists('/bin/echo'), "requires Unix")
64+
def test_exit_2_handling(self):
7865
cmds = Commands(CommandsBase("opengrok-master",
79-
[{"command": ["/usr/bin/false"]},
80-
{"command": ["/usr/bin/true"]}]))
66+
[{"command": ["/bin/sh", "-c", "echo " + Commands.PROJECT_SUBST + "; exit 2"]},
67+
{"command": ["/bin/echo"]}]))
8168
cmds.run()
82-
self.assertEqual({'/usr/bin/false opengrok-master': 1}, cmds.retcodes)
69+
self.assertEqual({'/bin/sh -c echo opengrok-master; exit 2': 2}, cmds.retcodes)
70+
self.assertFalse(cmds.failed)
8371

84-
@unittest.skipUnless(os.name.startswith("posix"), "requires Unix")
72+
@unittest.skipUnless(os.path.exists('/bin/echo'), "requires Unix")
8573
def test_project_subst(self):
8674
cmds = Commands(CommandsBase("test-subst",
87-
[{"command": ["/bin/echo", '%PROJECT%']}]))
75+
[{"command": ["/bin/echo", Commands.PROJECT_SUBST]}]))
8876
cmds.run()
8977
self.assertEqual(['test-subst\n'],
9078
cmds.outputs['/bin/echo test-subst'])

0 commit comments

Comments
 (0)