Skip to content

Commit cf1700c

Browse files
author
Vladimir Kotal
authored
fix timeout inheritance in mirror.py (#3025)
fixes #3022
1 parent 73a3b0f commit cf1700c

File tree

4 files changed

+120
-6
lines changed

4 files changed

+120
-6
lines changed

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

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

2020
#
21-
# Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
2222
#
2323

2424
import logging
@@ -105,6 +105,7 @@ def terminate(self, p):
105105
"""
106106
Make sure the process goes away.
107107
"""
108+
self.logger.info("Terminating PID {}".format(p.pid))
108109
p.terminate()
109110

110111
# The following code tries more methods to terminate

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

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

2121
#
22-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
22+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2323
#
2424

2525
import re
@@ -99,7 +99,7 @@ def get_repos_for_project(project_name, ignored_repos, uri, source_root,
9999
kwargs[COMMANDS_PROPERTY],
100100
kwargs[PROXY_PROPERTY],
101101
None,
102-
kwargs['command_timeout'])
102+
kwargs[CMD_TIMEOUT_PROPERTY])
103103
except (RepositoryException, OSError) as e:
104104
logger.error("Cannot get repository for {}: {}".
105105
format(repo_path, e))
@@ -339,6 +339,11 @@ def mirror_project(config, project_name, check_changes, uri,
339339
project_name,
340340
config.get(HOOKDIR_PROPERTY))
341341

342+
if not command_timeout:
343+
command_timeout = config.get(CMD_TIMEOUT_PROPERTY)
344+
if not hook_timeout:
345+
hook_timeout = config.get(HOOK_TIMEOUT_PROPERTY)
346+
342347
proxy = None
343348
if use_proxy:
344349
proxy = config.get(PROXY_PROPERTY)

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

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

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

2626

@@ -37,10 +37,11 @@
3737
from opengrok_tools.utils.mirror import check_project_configuration, \
3838
check_configuration, mirror_project, run_command, \
3939
HOOKS_PROPERTY, PROXY_PROPERTY, IGNORED_REPOS_PROPERTY, \
40-
PROJECTS_PROPERTY, DISABLED_CMD_PROPERTY, DISABLED_PROPERTY
40+
PROJECTS_PROPERTY, DISABLED_CMD_PROPERTY, DISABLED_PROPERTY, \
41+
CMD_TIMEOUT_PROPERTY, HOOK_TIMEOUT_PROPERTY
4142
import opengrok_tools.mirror
4243
from opengrok_tools.utils.exitvals import (
43-
CONTINUE_EXITVAL,
44+
CONTINUE_EXITVAL, FAILURE_EXITVAL
4445
)
4546
from opengrok_tools.utils.patterns import COMMAND_PROPERTY, PROJECT_SUBST
4647
from opengrok_tools.utils.command import Command
@@ -219,3 +220,63 @@ def test_disabled_command_run_args():
219220
project_name = "foo"
220221
run_command(cmd, project_name)
221222
verify(cmd).execute()
223+
224+
225+
def test_mirror_project_timeout(monkeypatch):
226+
"""
227+
Test mirror_project() timeout inheritance/override from global
228+
configuration to get_repos_for_project(). The test merely verifies
229+
that the timeout values are passed between the expected functions,
230+
not whether it actually affects the execution.
231+
"""
232+
cmd_timeout = 3
233+
hook_timeout = 4
234+
235+
def mock_get_repos(*args, **kwargs):
236+
mock_get_repos.called = True
237+
238+
assert kwargs[CMD_TIMEOUT_PROPERTY] == cmd_timeout
239+
240+
# Technically this function should return list of Repository objects
241+
# however for this test this is not necessary.
242+
return ['foo']
243+
244+
def mock_process_hook(hook_ident, hook, source_root, project_name_arg,
245+
proxy, hook_timeout_arg):
246+
247+
assert hook_timeout_arg == hook_timeout
248+
249+
# We want to terminate mirror_project() once this function runs.
250+
# This way mirror_project() will return FAILURE_EXITVAL
251+
return False
252+
253+
def test_mirror_project(config):
254+
retval = mirror_project(config, project_name, False,
255+
"http://localhost:8080/source", "srcroot")
256+
assert retval == FAILURE_EXITVAL
257+
258+
# TODO: is there better way to ensure that get_repos_for_project()
259+
# was actually called ?
260+
assert mock_get_repos.called
261+
262+
with monkeypatch.context() as m:
263+
mock_get_repos.called = False
264+
m.setattr("opengrok_tools.utils.mirror.get_repos_for_project",
265+
mock_get_repos)
266+
m.setattr("opengrok_tools.utils.mirror.process_hook",
267+
mock_process_hook)
268+
269+
project_name = "foo"
270+
# override testing
271+
global_config_1 = {PROJECTS_PROPERTY:
272+
{project_name:
273+
{CMD_TIMEOUT_PROPERTY: cmd_timeout,
274+
HOOK_TIMEOUT_PROPERTY: hook_timeout}},
275+
CMD_TIMEOUT_PROPERTY: cmd_timeout * 2,
276+
HOOK_TIMEOUT_PROPERTY: hook_timeout * 2}
277+
# inheritance testing
278+
global_config_2 = {CMD_TIMEOUT_PROPERTY: cmd_timeout,
279+
HOOK_TIMEOUT_PROPERTY: hook_timeout}
280+
281+
test_mirror_project(global_config_1)
282+
test_mirror_project(global_config_2)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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) 2020, Oracle and/or its affiliates. All rights reserved.
24+
#
25+
26+
import os
27+
import tempfile
28+
from git import Repo
29+
30+
from opengrok_tools.scm import GitRepository
31+
from opengrok_tools.scm.repofactory import get_repository
32+
33+
34+
def test_repofactory_timeout():
35+
with tempfile.TemporaryDirectory() as source_root:
36+
repo_name = "foo"
37+
repo_path = os.path.join(source_root, repo_name)
38+
Repo.init(repo_path)
39+
timeout = 3
40+
41+
project_name = "foo" # does not matter for this test
42+
repo = get_repository(repo_path,
43+
"git", project_name,
44+
None, None, None, timeout)
45+
assert repo is not None
46+
assert isinstance(repo, GitRepository)
47+
assert repo.timeout == timeout

0 commit comments

Comments
 (0)