Skip to content

Commit 87507d0

Browse files
authored
rework do_sync() tests (#4431)
1 parent a8851ab commit 87507d0

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

tools/src/main/python/opengrok_tools/sync.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ def do_sync(loglevel, commands, cleanup, dirs_to_process, ignore_errors,
9393
:return SUCCESS_EXITVAL on success, FAILURE_EXITVAL on error
9494
"""
9595

96+
# Nothing to do.
97+
if len(commands) == 0:
98+
return SUCCESS_EXITVAL
99+
96100
cmds_base = []
97101
for directory in dirs_to_process:
98102
cmd_base = CommandSequenceBase(directory, commands, loglevel=loglevel,
@@ -104,6 +108,11 @@ def do_sync(loglevel, commands, cleanup, dirs_to_process, ignore_errors,
104108
cmds_base.append(cmd_base)
105109

106110
if check_config:
111+
#
112+
# The configuration check was done unconditionally in CommandSequenceBase initialization above.
113+
# If there was a problem with the configuration, the CommandConfigurationException was raised,
114+
# so if the code flow arrived to this very block, this means that the check had been successful.
115+
#
107116
if not logger:
108117
logger = logging.getLogger(__name__)
109118
logger.info("Configuration check passed")

tools/src/test/python/test_sync.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#
2121

2222
#
23-
# Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
23+
# Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
2424
#
2525

2626
import logging
@@ -35,16 +35,38 @@
3535
from opengrok_tools.utils.exitvals import SUCCESS_EXITVAL
3636

3737

38-
@pytest.mark.parametrize(['check_config', 'expected_times'], [(True, 0), (False, 1)])
39-
def test_dosync_check_config_empty(check_config, expected_times):
38+
@pytest.mark.parametrize('check_config', [True, False])
39+
def test_dosync_empty_commands(check_config):
40+
"""
41+
If the commands structure is empty, the do_sync() code should never make it to the Pool.map() call,
42+
regardless of the check_config parameter value.
43+
"""
4044
commands = []
4145
with patch(pool.Pool.map, lambda x, y, z: []):
4246
assert do_sync(logging.INFO, commands, None, ["foo", "bar"], [],
43-
"http://localhost:8080/source", 42, check_config=check_config) == SUCCESS_EXITVAL
47+
"http://localhost:8080/source", 1, check_config=check_config) == SUCCESS_EXITVAL
48+
verify(pool.Pool, times=0).map(ANY, ANY, ANY)
49+
50+
51+
@pytest.mark.parametrize(['check_config', 'expected_times'], [(True, 0), (False, 1)])
52+
def test_dosync_check_config(check_config, expected_times):
53+
"""
54+
If the check_config parameter is True, the do_sync() code should never make it to the Pool.map() call.
55+
"""
56+
# The port used in the call within the commands structure is not expected to be reachable
57+
# since there is no call made because the map() function is patched below.
58+
commands = [{"call": {"uri": "http://localhost:11"}}]
59+
with patch(pool.Pool.map, lambda x, y, z: []):
60+
assert do_sync(logging.INFO, commands, None, ["foo", "bar"], [],
61+
"http://localhost:8080/source", 1, check_config=check_config) == SUCCESS_EXITVAL
4462
verify(pool.Pool, times=expected_times).map(ANY, ANY, ANY)
4563

4664

4765
def test_dosync_check_config_invalid():
66+
"""
67+
The commands list should contain a dictionary and the config check should recognize this
68+
and raise CommandConfigurationException.
69+
"""
4870
commands = ["foo"]
4971
with pytest.raises(CommandConfigurationException):
5072
do_sync(logging.INFO, commands, None, ["foo", "bar"], [],

0 commit comments

Comments
 (0)