2020-03-09 10:11:07 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2019, Guillaume Martinez (lunik@tiwabbit.fr)
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-06-24 21:50:36 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
2023-03-25 08:23:20 +01:00
|
|
|
|
|
|
|
import gitlab
|
2020-06-24 21:50:36 +02:00
|
|
|
__metaclass__ = type
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2022-11-02 21:42:29 +01:00
|
|
|
from ansible_collections.community.general.plugins.modules.gitlab_runner import GitLabRunner
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _dummy(x):
|
|
|
|
"""Dummy function. Only used as a placeholder for toplevel definitions when the test is going
|
|
|
|
to be skipped anyway"""
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
pytestmark = []
|
|
|
|
try:
|
2022-02-16 22:54:46 +01:00
|
|
|
from .gitlab import (FakeAnsibleModule,
|
|
|
|
GitlabModuleTestCase,
|
2020-03-25 12:43:51 +01:00
|
|
|
python_version_match_requirement,
|
2023-03-25 08:23:20 +01:00
|
|
|
resp_find_runners_all, resp_find_runners_list,
|
|
|
|
resp_find_project_runners, resp_find_group_runners,
|
|
|
|
resp_get_runner,
|
|
|
|
resp_create_runner, resp_delete_runner,
|
|
|
|
resp_get_project_by_name, resp_get_group_by_name)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# GitLab module requirements
|
|
|
|
if python_version_match_requirement():
|
|
|
|
from gitlab.v4.objects import Runner
|
|
|
|
except ImportError:
|
|
|
|
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
|
|
|
|
# Need to set these to something so that we don't fail when parsing
|
|
|
|
GitlabModuleTestCase = object
|
|
|
|
resp_find_runners_list = _dummy
|
|
|
|
resp_get_runner = _dummy
|
|
|
|
resp_create_runner = _dummy
|
|
|
|
resp_delete_runner = _dummy
|
|
|
|
|
|
|
|
# Unit tests requirements
|
|
|
|
try:
|
|
|
|
from httmock import with_httmock # noqa
|
|
|
|
except ImportError:
|
|
|
|
pytestmark.append(pytest.mark.skip("Could not load httmock module required for testing"))
|
|
|
|
with_httmock = _dummy
|
|
|
|
|
|
|
|
|
|
|
|
class TestGitlabRunner(GitlabModuleTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super(TestGitlabRunner, self).setUp()
|
|
|
|
|
2022-02-16 22:54:46 +01:00
|
|
|
self.module_util_all = GitLabRunner(module=FakeAnsibleModule({"owned": False}), gitlab_instance=self.gitlab_instance)
|
|
|
|
self.module_util_owned = GitLabRunner(module=FakeAnsibleModule({"owned": True}), gitlab_instance=self.gitlab_instance)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-01-24 21:02:44 +01:00
|
|
|
@with_httmock(resp_find_runners_all)
|
2020-03-09 10:11:07 +01:00
|
|
|
@with_httmock(resp_get_runner)
|
2021-01-24 21:02:44 +01:00
|
|
|
def test_runner_exist_all(self):
|
2022-02-16 22:54:46 +01:00
|
|
|
rvalue = self.module_util_all.exists_runner("test-1-20150125")
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(rvalue, True)
|
|
|
|
|
2022-02-16 22:54:46 +01:00
|
|
|
rvalue = self.module_util_all.exists_runner("test-3-00000000")
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(rvalue, False)
|
|
|
|
|
2021-01-24 21:02:44 +01:00
|
|
|
@with_httmock(resp_find_runners_list)
|
|
|
|
@with_httmock(resp_get_runner)
|
|
|
|
def test_runner_exist_owned(self):
|
2022-02-16 22:54:46 +01:00
|
|
|
rvalue = self.module_util_owned.exists_runner("test-1-20201214")
|
2021-01-24 21:02:44 +01:00
|
|
|
|
|
|
|
self.assertEqual(rvalue, True)
|
|
|
|
|
2022-02-16 22:54:46 +01:00
|
|
|
rvalue = self.module_util_owned.exists_runner("test-3-00000000")
|
2021-01-24 21:02:44 +01:00
|
|
|
|
|
|
|
self.assertEqual(rvalue, False)
|
|
|
|
|
2023-03-25 08:23:20 +01:00
|
|
|
@with_httmock(resp_find_project_runners)
|
|
|
|
@with_httmock(resp_get_runner)
|
|
|
|
@with_httmock(resp_get_project_by_name)
|
|
|
|
def test_project_runner_exist(self):
|
|
|
|
gitlab_project = self.gitlab_instance.projects.get('foo-bar/diaspora-client')
|
|
|
|
module_util = GitLabRunner(module=FakeAnsibleModule(), gitlab_instance=self.gitlab_instance, project=gitlab_project)
|
|
|
|
|
|
|
|
rvalue = module_util.exists_runner("test-1-20220210")
|
|
|
|
|
|
|
|
self.assertEqual(rvalue, True)
|
|
|
|
|
|
|
|
rvalue = module_util.exists_runner("test-3-00000000")
|
|
|
|
|
|
|
|
self.assertEqual(rvalue, False)
|
|
|
|
|
|
|
|
@with_httmock(resp_find_group_runners)
|
|
|
|
@with_httmock(resp_get_group_by_name)
|
|
|
|
@with_httmock(resp_get_runner)
|
|
|
|
@pytest.mark.skipif(gitlab.__version__ < "2.3.0", reason="require python-gitlab >= 2.3.0")
|
|
|
|
def test_group_runner_exist(self):
|
|
|
|
gitlab_group = self.gitlab_instance.groups.get('foo-bar')
|
|
|
|
module_util = GitLabRunner(module=FakeAnsibleModule(), gitlab_instance=self.gitlab_instance, group=gitlab_group)
|
|
|
|
|
|
|
|
rvalue = module_util.exists_runner("test-3-20220210")
|
|
|
|
|
|
|
|
self.assertEqual(rvalue, True)
|
|
|
|
|
|
|
|
rvalue = module_util.exists_runner("test-3-00000000")
|
|
|
|
|
|
|
|
self.assertEqual(rvalue, False)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
@with_httmock(resp_create_runner)
|
|
|
|
def test_create_runner(self):
|
2022-02-16 22:54:46 +01:00
|
|
|
runner = self.module_util_all.create_runner({"token": "token", "description": "test-1-20150125"})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(type(runner), Runner)
|
|
|
|
self.assertEqual(runner.description, "test-1-20150125")
|
|
|
|
|
2021-01-24 21:02:44 +01:00
|
|
|
@with_httmock(resp_find_runners_all)
|
2020-03-09 10:11:07 +01:00
|
|
|
@with_httmock(resp_get_runner)
|
|
|
|
def test_update_runner(self):
|
2022-02-16 22:54:46 +01:00
|
|
|
runner = self.module_util_all.find_runner("test-1-20150125")
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-02-16 22:54:46 +01:00
|
|
|
changed, newRunner = self.module_util_all.update_runner(runner, {"description": "Runner description"})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(changed, True)
|
|
|
|
self.assertEqual(type(newRunner), Runner)
|
|
|
|
self.assertEqual(newRunner.description, "Runner description")
|
|
|
|
|
2022-02-16 22:54:46 +01:00
|
|
|
changed, newRunner = self.module_util_all.update_runner(runner, {"description": "Runner description"})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(changed, False)
|
|
|
|
self.assertEqual(newRunner.description, "Runner description")
|
|
|
|
|
2021-01-24 21:02:44 +01:00
|
|
|
@with_httmock(resp_find_runners_all)
|
2020-03-09 10:11:07 +01:00
|
|
|
@with_httmock(resp_get_runner)
|
|
|
|
@with_httmock(resp_delete_runner)
|
|
|
|
def test_delete_runner(self):
|
2022-02-16 22:54:46 +01:00
|
|
|
self.module_util_all.exists_runner("test-1-20150125")
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-02-16 22:54:46 +01:00
|
|
|
rvalue = self.module_util_all.delete_runner()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
self.assertEqual(rvalue, None)
|