| 1 |
# Copyright (c) 2019, Guillaume Martinez ([email protected]) |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import gitlab |
| 8 |
import pytest |
| 9 |
|
| 10 |
from ansible_collections.community.general.plugins.modules.gitlab_runner import GitLabRunner |
| 11 |
|
| 12 |
|
| 13 |
def _dummy(x): |
| 14 |
"""Dummy function. Only used as a placeholder for toplevel definitions when the test is going |
| 15 |
to be skipped anyway""" |
| 16 |
return x |
| 17 |
|
| 18 |
|
| 19 |
pytestmark = [] |
| 20 |
try: |
| 21 |
# GitLab module requirements |
| 22 |
from gitlab.v4.objects import Runner |
| 23 |
|
| 24 |
from .gitlab import ( |
| 25 |
FakeAnsibleModule, |
| 26 |
GitlabModuleTestCase, |
| 27 |
resp_create_runner, |
| 28 |
resp_delete_runner, |
| 29 |
resp_find_group_runners, |
| 30 |
resp_find_project_runners, |
| 31 |
resp_find_runners_all, |
| 32 |
resp_find_runners_list, |
| 33 |
resp_get_group_by_name, |
| 34 |
resp_get_project_by_name, |
| 35 |
resp_get_runner, |
| 36 |
) |
| 37 |
except ImportError: |
| 38 |
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing")) |
| 39 |
# Need to set these to something so that we don't fail when parsing |
| 40 |
GitlabModuleTestCase = object # type: ignore |
| 41 |
resp_find_runners_list = _dummy |
| 42 |
resp_get_runner = _dummy |
| 43 |
resp_create_runner = _dummy |
| 44 |
resp_delete_runner = _dummy |
| 45 |
|
| 46 |
# Unit tests requirements |
| 47 |
try: |
| 48 |
from httmock import with_httmock # noqa |
| 49 |
except ImportError: |
| 50 |
pytestmark.append(pytest.mark.skip("Could not load httmock module required for testing")) |
| 51 |
with_httmock = _dummy |
| 52 |
|
| 53 |
|
| 54 |
class TestGitlabRunner(GitlabModuleTestCase): |
| 55 |
def setUp(self): |
| 56 |
super().setUp() |
| 57 |
|
| 58 |
self.module_util_all = GitLabRunner( |
| 59 |
module=FakeAnsibleModule({"owned": False}), gitlab_instance=self.gitlab_instance |
| 60 |
) |
| 61 |
self.module_util_owned = GitLabRunner( |
| 62 |
module=FakeAnsibleModule({"owned": True}), gitlab_instance=self.gitlab_instance |
| 63 |
) |
| 64 |
|
| 65 |
@with_httmock(resp_find_runners_all) |
| 66 |
@with_httmock(resp_get_runner) |
| 67 |
def test_runner_exist_all(self): |
| 68 |
rvalue = self.module_util_all.exists_runner("test-1-20150125") |
| 69 |
|
| 70 |
self.assertEqual(rvalue, True) |
| 71 |
|
| 72 |
rvalue = self.module_util_all.exists_runner("test-3-00000000") |
| 73 |
|
| 74 |
self.assertEqual(rvalue, False) |
| 75 |
|
| 76 |
@with_httmock(resp_find_runners_list) |
| 77 |
@with_httmock(resp_get_runner) |
| 78 |
def test_runner_exist_owned(self): |
| 79 |
rvalue = self.module_util_owned.exists_runner("test-1-20201214") |
| 80 |
|
| 81 |
self.assertEqual(rvalue, True) |
| 82 |
|
| 83 |
rvalue = self.module_util_owned.exists_runner("test-3-00000000") |
| 84 |
|
| 85 |
self.assertEqual(rvalue, False) |
| 86 |
|
| 87 |
@with_httmock(resp_find_project_runners) |
| 88 |
@with_httmock(resp_get_runner) |
| 89 |
@with_httmock(resp_get_project_by_name) |
| 90 |
def test_project_runner_exist(self): |
| 91 |
gitlab_project = self.gitlab_instance.projects.get("foo-bar/diaspora-client") |
| 92 |
module_util = GitLabRunner( |
| 93 |
module=FakeAnsibleModule(), gitlab_instance=self.gitlab_instance, project=gitlab_project |
| 94 |
) |
| 95 |
|
| 96 |
rvalue = module_util.exists_runner("test-1-20220210") |
| 97 |
|
| 98 |
self.assertEqual(rvalue, True) |
| 99 |
|
| 100 |
rvalue = module_util.exists_runner("test-3-00000000") |
| 101 |
|
| 102 |
self.assertEqual(rvalue, False) |
| 103 |
|
| 104 |
@with_httmock(resp_find_group_runners) |
| 105 |
@with_httmock(resp_get_group_by_name) |
| 106 |
@with_httmock(resp_get_runner) |
| 107 |
@pytest.mark.skipif(gitlab.__version__ < "2.3.0", reason="require python-gitlab >= 2.3.0") |
| 108 |
def test_group_runner_exist(self): |
| 109 |
gitlab_group = self.gitlab_instance.groups.get("foo-bar") |
| 110 |
module_util = GitLabRunner(module=FakeAnsibleModule(), gitlab_instance=self.gitlab_instance, group=gitlab_group) |
| 111 |
|
| 112 |
rvalue = module_util.exists_runner("test-3-20220210") |
| 113 |
|
| 114 |
self.assertEqual(rvalue, True) |
| 115 |
|
| 116 |
rvalue = module_util.exists_runner("test-3-00000000") |
| 117 |
|
| 118 |
self.assertEqual(rvalue, False) |
| 119 |
|
| 120 |
@with_httmock(resp_create_runner) |
| 121 |
def test_create_runner(self): |
| 122 |
runner = self.module_util_all.create_runner({"token": "token", "description": "test-1-20150125"}) |
| 123 |
|
| 124 |
self.assertEqual(type(runner), Runner) |
| 125 |
self.assertEqual(runner.description, "test-1-20150125") |
| 126 |
|
| 127 |
@with_httmock(resp_find_runners_all) |
| 128 |
@with_httmock(resp_get_runner) |
| 129 |
def test_update_runner(self): |
| 130 |
runner = self.module_util_all.find_runner("test-1-20150125") |
| 131 |
|
| 132 |
changed, newRunner = self.module_util_all.update_runner(runner, {"description": "Runner description"}) |
| 133 |
|
| 134 |
self.assertEqual(changed, True) |
| 135 |
self.assertEqual(type(newRunner), Runner) |
| 136 |
self.assertEqual(newRunner.description, "Runner description") |
| 137 |
|
| 138 |
changed, newRunner = self.module_util_all.update_runner(runner, {"description": "Runner description"}) |
| 139 |
|
| 140 |
self.assertEqual(changed, False) |
| 141 |
self.assertEqual(newRunner.description, "Runner description") |
| 142 |
|
| 143 |
@with_httmock(resp_find_runners_all) |
| 144 |
@with_httmock(resp_get_runner) |
| 145 |
@with_httmock(resp_delete_runner) |
| 146 |
def test_delete_runner(self): |
| 147 |
self.module_util_all.exists_runner("test-1-20150125") |
| 148 |
|
| 149 |
rvalue = self.module_util_all.delete_runner() |
| 150 |
|
| 151 |
self.assertEqual(rvalue, None) |