Skip to content

Commit 009ba17

Browse files
Vladimir Kotalahornace
authored andcommitted
add check for commands section of the mirror configuration
1 parent 5036d90 commit 009ba17

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

tools/src/main/python/opengrok_tools/scm/repofactory.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919

2020
#
21-
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2222
#
2323
# Portions Copyright 2020 Robert Williams
2424

@@ -32,7 +32,10 @@
3232
from .svn import SubversionRepository
3333
from .teamware import TeamwareRepository
3434

35-
logger = logging.getLogger(__name__)
35+
36+
# Note: these have to correspond with get_repository().
37+
REPO_TYPES = ["mercurial", "hg", "teamware", "sccs", "cvs",
38+
"svn", "subversion", "git", "perforce", "repo"]
3639

3740

3841
def get_repository(path, repo_type, project,
@@ -49,6 +52,8 @@ def get_repository(path, repo_type, project,
4952
or None if given repository type cannot be found.
5053
"""
5154

55+
logger = logging.getLogger(__name__)
56+
5257
repo_lower = repo_type.lower()
5358

5459
logger.debug("Constructing repo object for path {}".format(path))

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from .command import Command
4343
from .restful import call_rest_api, do_api_call
4444

45-
from ..scm.repofactory import get_repository
45+
from ..scm.repofactory import get_repository, REPO_TYPES
4646
from ..scm.repository import RepositoryException
4747

4848

@@ -574,6 +574,43 @@ def check_project_configuration(multiple_project_config, hookdir=False,
574574
return True
575575

576576

577+
def check_commands(commands):
578+
"""
579+
Validate the commands section of the configuration that allows
580+
to override files used for SCM synchronization and incoming check.
581+
This should be simple dictionary of string values.
582+
:return: True if valid, False otherwise
583+
"""
584+
585+
logger = logging.getLogger(__name__)
586+
587+
if commands is None:
588+
return True
589+
590+
if type(commands) is not dict:
591+
logger.error("commands sections is not a dictionary")
592+
return False
593+
594+
for name, value in commands.items():
595+
if type(value) is not str:
596+
logger.error("value of {} is not string".format(name))
597+
return False
598+
599+
if name not in REPO_TYPES:
600+
logger.error("unknown repository type: {}".format(name))
601+
return False
602+
603+
if not os.path.exists(value):
604+
logger.error("path for {} does not exist: {}".format(name, value))
605+
return False
606+
607+
if not os.path.isfile(value):
608+
logger.error("path for {} is not a file: {}".format(name, value))
609+
return False
610+
611+
return True
612+
613+
577614
def check_configuration(config):
578615
"""
579616
Validate configuration
@@ -604,6 +641,9 @@ def check_configuration(config):
604641
if disabled_command:
605642
logger.debug("Disabled command: {}".format(disabled_command))
606643

644+
if not check_commands(config.get(COMMANDS_PROPERTY)):
645+
return False
646+
607647
if not check_project_configuration(config.get(PROJECTS_PROPERTY),
608648
config.get(HOOKDIR_PROPERTY),
609649
config.get(PROXY_PROPERTY)):

tools/src/test/python/test_mirror.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
PROJECTS_PROPERTY, DISABLED_CMD_PROPERTY, DISABLED_PROPERTY, \
4848
CMD_TIMEOUT_PROPERTY, HOOK_TIMEOUT_PROPERTY, DISABLED_REASON_PROPERTY, \
4949
INCOMING_PROPERTY, IGNORE_ERR_PROPERTY, HOOK_PRE_PROPERTY, \
50-
HOOKDIR_PROPERTY, HOOK_POST_PROPERTY
50+
HOOKDIR_PROPERTY, HOOK_POST_PROPERTY, COMMANDS_PROPERTY
5151
from opengrok_tools.utils.patterns import COMMAND_PROPERTY, PROJECT_SUBST
5252

5353

@@ -97,6 +97,41 @@ def test_invalid_project_config_hooknames():
9797
assert not check_project_configuration(config, hookdir=tmpdir)
9898

9999

100+
def test_invalid_configuration_commands_dict():
101+
"""
102+
The value of the commands property has to be a dictionary.
103+
"""
104+
config = {COMMANDS_PROPERTY: "foo"}
105+
assert not check_configuration(config)
106+
107+
108+
def test_invalid_configuration_commands_scm():
109+
"""
110+
The names in the commands section should be match recognized SCMs.
111+
"""
112+
config = {COMMANDS_PROPERTY: {"foo": "bar"}}
113+
assert not check_configuration(config)
114+
115+
116+
def test_invalid_configuration_commands_value():
117+
"""
118+
The values in the command section should be strings.
119+
"""
120+
config = {COMMANDS_PROPERTY: {"git": {"foo": "bar"}}}
121+
assert not check_configuration(config)
122+
123+
124+
def test_invalid_configuration_commands_exists():
125+
config = {COMMANDS_PROPERTY: {"git": "/usr/nonexistent/bin/git"}}
126+
assert not check_configuration(config)
127+
128+
129+
@posix_only
130+
def test_configuration_commands():
131+
config = {COMMANDS_PROPERTY: {"git": "/usr/bin/git"}}
132+
assert check_configuration(config)
133+
134+
100135
@posix_only
101136
def test_invalid_project_config_nonexec_hook():
102137
with tempfile.TemporaryDirectory() as tmpdir:

0 commit comments

Comments
 (0)