def test_ignore_errors_sync(monkeypatch, per_project):
    """
    Test that ignore errors overrides failed repository sync().
    """

    mock_repo = mock(spec=GitRepository)
    when(mock_repo).sync().thenReturn(FAILURE_EXITVAL)

    def mock_get_repos(*args, **kwargs):
        return [mock_repo]

    project_name = "foo"
    if per_project:
        config = {
            PROJECTS_PROPERTY: {
                project_name: {IGNORE_ERR_PROPERTY: True}
            }
        }
    else:
        config = {
            IGNORE_ERR_PROPERTY: True,
        }

    with monkeypatch.context() as m:
        mock_get_repos.called = False
        m.setattr("opengrok_tools.utils.mirror.get_repos_for_project",
                  mock_get_repos)

        src_root = "srcroot"
        assert mirror_project(config, project_name, False, False,
                              None, src_root) == SUCCESS_EXITVAL
def test_disabled_command_api():
    """
    Test that mirror_project() calls call_rest_api() if API
    call is specified in the configuration for disabled project.
    """
    def mock_call_rest_api(command, b, http_headers=None, timeout=None, api_timeout=None):
        return mock(spec=requests.Response)

    with patch(opengrok_tools.utils.mirror.call_rest_api,
               mock_call_rest_api):
        project_name = "foo"
        config = {
            DISABLED_CMD_PROPERTY: {
                CALL_PROPERTY: {
                    "uri": "http://localhost:8080/source/api/v1/foo",
                    "method": "POST", "data": "data"}
            },
            PROJECTS_PROPERTY: {
                project_name: {DISABLED_PROPERTY: True}
            }
        }

        assert mirror_project(config, project_name, False, False,
                              None, None) == CONTINUE_EXITVAL
        verify(opengrok_tools.utils.mirror). \
            call_rest_api(ANY,
                          {PROJECT_SUBST: project_name},
                          http_headers=None, timeout=None, api_timeout=None)
Exemple #3
0
def test_disabled_command_api():
    """
    Test that mirror_project() calls call_rest_api() if API
    call is specified in the configuration for disabled project.
    """
    with patch(opengrok_tools.utils.mirror.call_rest_api,
               lambda a, b, c: mock(spec=requests.Response)):
        project_name = "foo"
        config = {
            DISABLED_CMD_PROPERTY: {
                COMMAND_PROPERTY:
                ["http://localhost:8080/source/api/v1/foo", "POST", "data"]
            },
            PROJECTS_PROPERTY: {
                project_name: {
                    DISABLED_PROPERTY: True
                }
            }
        }

        assert mirror_project(config, project_name, False, None,
                              None) == CONTINUE_EXITVAL
        verify(opengrok_tools.utils.mirror). \
            call_rest_api(config.get(DISABLED_CMD_PROPERTY),
                          PROJECT_SUBST, project_name)
Exemple #4
0
    def test_mirror_project(config):
        retval = mirror_project(config, project_name, False,
                                "http://localhost:8080/source", "srcroot")
        assert retval == FAILURE_EXITVAL

        # TODO: is there better way to ensure that get_repos_for_project()
        #       was actually called ?
        assert mock_get_repos.called
Exemple #5
0
def test_disabled_command_api_text_append(monkeypatch):
    """
    Test that message text is appended if DISABLED_PROPERTY is a string.
    """

    text_to_append = "foo bar"

    def mock_call_rest_api(command, b, c):
        disabled_command = config.get(DISABLED_CMD_PROPERTY)
        assert disabled_command
        command_args = disabled_command.get(COMMAND_PROPERTY)
        assert command_args
        data = command_args[2]
        assert data
        text = data.get("text")
        assert text
        assert text.find(text_to_append)

        return mock(spec=requests.Response)

    with monkeypatch.context() as m:
        m.setattr("opengrok_tools.utils.mirror.call_rest_api",
                  mock_call_rest_api)

        project_name = "foo"
        data = {
            'messageLevel': 'info',
            'duration': 'PT5M',
            'tags': ['%PROJECT%'],
            'text': 'disabled project'
        }
        config = {
            DISABLED_CMD_PROPERTY: {
                COMMAND_PROPERTY:
                ["http://localhost:8080/source/api/v1/foo", "POST", data]
            },
            PROJECTS_PROPERTY: {
                project_name: {
                    DISABLED_REASON_PROPERTY: text_to_append,
                    DISABLED_PROPERTY: True
                }
            }
        }

        mirror_project(config, project_name, False, None, None)
Exemple #6
0
def test_disabled_command_run():
    """
    Make sure that mirror_project() results in calling run_command().
    """
    spy2(opengrok_tools.utils.mirror.run_command)
    project_name = "foo"
    config = {DISABLED_CMD_PROPERTY:
              {COMMAND_PROPERTY: ["cat"]},
              PROJECTS_PROPERTY: {project_name: {DISABLED_PROPERTY: True}}}

    assert mirror_project(config, project_name, False,
                          None, None) == CONTINUE_EXITVAL
    verify(opengrok_tools.utils.mirror).run_command(ANY, project_name)
def test_disabled_command_api_text_append(monkeypatch):
    """
    Test that message text is appended if DISABLED_PROPERTY is a string.
    """

    text_to_append = "foo bar"

    def mock_call_rest_api(call, b, http_headers=None, timeout=None, api_timeout=None):
        call_data = call.data
        assert data
        text = call_data.get("text")
        assert text
        assert text.find(text_to_append)

        return mock(spec=requests.Response)

    with monkeypatch.context() as m:
        m.setattr("opengrok_tools.utils.mirror.call_rest_api",
                  mock_call_rest_api)

        project_name = "foo"
        data = {'messageLevel': 'info', 'duration': 'PT5M',
                'tags': ['%PROJECT%'],
                'text': 'disabled project'}
        config = {
            DISABLED_CMD_PROPERTY: {
                CALL_PROPERTY: {
                    "uri": "http://localhost:8080/source/api/v1/foo",
                    "method": "POST", "data": data}
            },
            PROJECTS_PROPERTY: {
                project_name: {
                    DISABLED_REASON_PROPERTY: text_to_append,
                    DISABLED_PROPERTY: True
                }
            }
        }

        mirror_project(config, project_name, False, False, None, None)
Exemple #8
0
def test_ignore_errors_hooks(monkeypatch, hook_type, per_project):
    """
    Test that ignore errors property overrides failed hook.
    """
    def mock_get_repos(*args, **kwargs):
        return [mock(spec=GitRepository)]

    spy2(opengrok_tools.utils.mirror.process_hook)
    project_name = "foo"
    hook_dir = "/befelemepeseveze"
    hook_name = "nonexistent"
    if per_project:
        config = {
            HOOKDIR_PROPERTY: hook_dir,
            PROJECTS_PROPERTY: {
                project_name: {
                    IGNORE_ERR_PROPERTY: True,
                    HOOKS_PROPERTY: {
                        hook_type: hook_name
                    }
                }
            }
        }
    else:
        config = {
            IGNORE_ERR_PROPERTY: True,
            HOOKDIR_PROPERTY: hook_dir,
            PROJECTS_PROPERTY: {
                project_name: {
                    HOOKS_PROPERTY: {
                        hook_type: hook_name
                    }
                }
            }
        }

    with monkeypatch.context() as m:
        mock_get_repos.called = False
        m.setattr("opengrok_tools.utils.mirror.get_repos_for_project",
                  mock_get_repos)

        src_root = "srcroot"
        assert mirror_project(config, project_name, False, None,
                              src_root) == SUCCESS_EXITVAL
        verify(opengrok_tools.utils.mirror).\
            process_hook(hook_type, os.path.join(hook_dir, hook_name),
                         src_root, project_name, None, None)
        # Necessary to disable the process_hook spy otherwise mockito will
        # complain about recursive invocation.
        unstub()