|
| 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 | +# Portions Copyright (c) 2020, Krystof Tulinger <[email protected]> |
| 25 | +# |
| 26 | + |
| 27 | +import multiprocessing |
| 28 | +import os |
| 29 | +import sys |
| 30 | +import tempfile |
| 31 | + |
| 32 | +import pytest |
| 33 | +from git import Repo |
| 34 | + |
| 35 | +import opengrok_tools.mirror |
| 36 | +from opengrok_tools.scm import get_repository |
| 37 | +from opengrok_tools.utils.exitvals import CONTINUE_EXITVAL |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix") |
| 41 | +def test_incoming_retval(monkeypatch): |
| 42 | + """ |
| 43 | + Test that the special CONTINUE_EXITVAL value bubbles all the way up to |
| 44 | + the mirror.py return value. |
| 45 | + """ |
| 46 | + |
| 47 | + # The default has changed for python 3.8 (see https://github.com/oracle/opengrok/issues/3296). |
| 48 | + # Because of the mocking we need to use the "fork" type to propagate all mocks to the |
| 49 | + # processes spawned by mirror command |
| 50 | + multiprocessing.set_start_method('fork') |
| 51 | + |
| 52 | + class MockResponse: |
| 53 | + |
| 54 | + # mock json() method always returns a specific testing dictionary |
| 55 | + @staticmethod |
| 56 | + def json(): |
| 57 | + return "true" |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + def raise_for_status(): |
| 61 | + pass |
| 62 | + |
| 63 | + with tempfile.TemporaryDirectory() as source_root: |
| 64 | + repo_name = "parent_repo" |
| 65 | + repo_path = os.path.join(source_root, repo_name) |
| 66 | + cloned_repo_name = "cloned_repo" |
| 67 | + cloned_repo_path = os.path.join(source_root, cloned_repo_name) |
| 68 | + project_name = "foo" # does not matter for this test |
| 69 | + |
| 70 | + os.mkdir(repo_path) |
| 71 | + |
| 72 | + def mock_get_repos(*args, **kwargs): |
| 73 | + return [get_repository(cloned_repo_path, |
| 74 | + "git", project_name)] |
| 75 | + |
| 76 | + def mock_get(*args, **kwargs): |
| 77 | + return MockResponse() |
| 78 | + |
| 79 | + # Clone a Git repository so that it can pull. |
| 80 | + repo = Repo.init(repo_path) |
| 81 | + with repo.config_writer() as git_config: |
| 82 | + git_config. set_value( 'user', 'email', '[email protected]') |
| 83 | + git_config.set_value('user', 'name', 'John Doe') |
| 84 | + |
| 85 | + new_file_path = os.path.join(repo_path, 'foo') |
| 86 | + with open(new_file_path, 'w'): |
| 87 | + pass |
| 88 | + assert os.path.isfile(new_file_path) |
| 89 | + index = repo.index |
| 90 | + index.add([new_file_path]) |
| 91 | + index.commit("add file") |
| 92 | + repo.clone(cloned_repo_path) |
| 93 | + |
| 94 | + with monkeypatch.context() as m: |
| 95 | + m.setattr(sys, 'argv', ['prog', "-I", project_name]) |
| 96 | + |
| 97 | + # With mocking done via pytest it is necessary to patch |
| 98 | + # the call-site rather than the absolute object path. |
| 99 | + m.setattr("opengrok_tools.mirror.get_config_value", lambda x, y, z: source_root) |
| 100 | + m.setattr("opengrok_tools.utils.mirror.get_repos_for_project", mock_get_repos) |
| 101 | + m.setattr("opengrok_tools.utils.mirror.do_api_call", mock_get) |
| 102 | + |
| 103 | + assert opengrok_tools.mirror.main() == CONTINUE_EXITVAL |
0 commit comments