Skip to content

Commit 14e3630

Browse files
authored
add --check_config to opengrok-sync (#3941)
1 parent 6333fd7 commit 14e3630

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def main():
143143
return 1
144144

145145
if args.check_config:
146-
logger.debug("Configuration check passed, exiting")
146+
logger.info("Configuration check passed, exiting")
147147
return 0
148148

149149
nomirror = os.environ.get(OPENGROK_NO_MIRROR_ENV)

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

+18-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"""
2828

2929
import argparse
30+
import logging
3031
import multiprocessing
3132
import os
3233
import sys
@@ -52,7 +53,7 @@
5253
print("Need Python 3, you are running {}".format(major_version))
5354
sys.exit(1)
5455

55-
__version__ = "1.4"
56+
__version__ = "1.5"
5657

5758

5859
def worker(base):
@@ -69,7 +70,7 @@ def worker(base):
6970

7071
def do_sync(loglevel, commands, cleanup, dirs_to_process, ignore_errors,
7172
uri, numworkers, driveon=False, print_output=False, logger=None,
72-
http_headers=None, timeout=None, api_timeout=None):
73+
http_headers=None, timeout=None, api_timeout=None, check_config=False):
7374
"""
7475
Process the list of directories in parallel.
7576
:param logger: logger to be used in this function
@@ -87,19 +88,26 @@ def do_sync(loglevel, commands, cleanup, dirs_to_process, ignore_errors,
8788
:param http_headers: optional dictionary of HTTP headers
8889
:param timeout: optional timeout in seconds for API call response
8990
:param api_timeout: optional timeout in seconds for async API call duration
91+
:param check_config: check configuration and return
9092
:return SUCCESS_EXITVAL on success, FAILURE_EXITVAL on error
9193
"""
9294

9395
cmds_base = []
94-
for dir in dirs_to_process:
95-
cmd_base = CommandSequenceBase(dir, commands, loglevel=loglevel,
96+
for directory in dirs_to_process:
97+
cmd_base = CommandSequenceBase(directory, commands, loglevel=loglevel,
9698
cleanup=cleanup,
9799
driveon=driveon, url=uri,
98100
http_headers=http_headers,
99101
api_timeout=timeout,
100102
async_api_timeout=api_timeout)
101103
cmds_base.append(cmd_base)
102104

105+
if check_config:
106+
if not logger:
107+
logger = logging.getLogger(__name__)
108+
logger.info("Configuration check passed")
109+
return SUCCESS_EXITVAL
110+
103111
# Map the commands into pool of workers, so they can be processed.
104112
retval = SUCCESS_EXITVAL
105113
with Pool(processes=numworkers) as pool:
@@ -160,6 +168,8 @@ def main():
160168
'for RESTful API calls')
161169
parser.add_argument('--async_api_timeout', type=int, default=300,
162170
help='Set timeout in seconds for asynchronous REST API calls')
171+
parser.add_argument('--check_config', action='store_true',
172+
help='check configuration and exit')
163173
add_http_headers(parser)
164174

165175
try:
@@ -280,7 +290,8 @@ def main():
280290
ignore_errors, uri, args.workers,
281291
driveon=args.driveon, http_headers=headers,
282292
timeout=args.api_timeout,
283-
api_timeout=args.async_api_timeout)
293+
api_timeout=args.async_api_timeout,
294+
check_config=args.check_config)
284295
except CommandConfigurationException as exc:
285296
logger.error("Invalid configuration: {}".format(exc))
286297
return FAILURE_EXITVAL
@@ -295,7 +306,8 @@ def main():
295306
ignore_errors, uri, args.workers,
296307
driveon=args.driveon, http_headers=headers,
297308
timeout=args.api_timeout,
298-
api_timeout=args.async_api_timeout)
309+
api_timeout=args.async_api_timeout,
310+
check_config=args.check_config)
299311
except CommandConfigurationException as exc:
300312
logger.error("Invalid configuration: {}".format(exc))
301313
return FAILURE_EXITVAL

tools/src/test/python/test_sync.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# CDDL HEADER START
5+
#
6+
# The contents of this file are subject to the terms of the
7+
# Common Development and Distribution License (the "License").
8+
# You may not use this file except in compliance with the License.
9+
#
10+
# See LICENSE.txt included in this distribution for the specific
11+
# language governing permissions and limitations under the License.
12+
#
13+
# When distributing Covered Code, include this CDDL HEADER in each
14+
# file and include the License file at LICENSE.txt.
15+
# If applicable, add the following below this CDDL HEADER, with the
16+
# fields enclosed by brackets "[]" replaced with your own identifying
17+
# information: Portions Copyright [yyyy] [name of copyright owner]
18+
#
19+
# CDDL HEADER END
20+
#
21+
22+
#
23+
# Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
24+
#
25+
26+
import logging
27+
28+
import pytest
29+
from multiprocessing import pool
30+
31+
from mockito import verify, ANY, patch
32+
33+
from opengrok_tools.sync import do_sync
34+
from opengrok_tools.utils.commandsequence import CommandConfigurationException
35+
from opengrok_tools.utils.exitvals import SUCCESS_EXITVAL
36+
37+
38+
@pytest.mark.parametrize(['check_config', 'expected_times'], [(True, 0), (False, 1)])
39+
def test_dosync_check_config_empty(check_config, expected_times):
40+
commands = []
41+
with patch(pool.Pool.map, lambda x, y, z: []):
42+
assert do_sync(logging.INFO, commands, None, ["foo", "bar"], [],
43+
"http://localhost:8080/source", 42, check_config=check_config) == SUCCESS_EXITVAL
44+
verify(pool.Pool, times=expected_times).map(ANY, ANY, ANY)
45+
46+
47+
def test_dosync_check_config_invalid():
48+
commands = ["foo"]
49+
with pytest.raises(CommandConfigurationException):
50+
do_sync(logging.INFO, commands, None, ["foo", "bar"], [],
51+
"http://localhost:8080/source", 42, check_config=True)

0 commit comments

Comments
 (0)