mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-17 06:29:12 -07:00
Refactor gitlab modules (#51141)
* gitlab_group: refactor module * gitlab_user: refactor module * gitlab_group, gitlab_user; pylint * gitlab_project: refactor module * gitlab_group, gitlab_project, gitlab_user: Enchance modules - Add generic loop to update object - Enchance return messages - PyLint * gitlab_runner: refactor module * gitlab_hooks: refactor module * gitlab_deploy_key: refactor module * gitlab_group: enchance module and documentation - Enchange function arguments - Add check_mode break - Rewrite module documentation * gitlab_hook: enchance module and documentation - Rewrite documentation - Enchance function parameters - Rename functions * gitlab_project: enchance module and documentation - Rewrite documentation - Enchance function parameters - Add try/except on project creation * gitlab_runner: enchance module and documentation - Rewrite documentation - Fix Copyright - Enchance function arguments - Add check_mode break - Add missing function: deletion * gitlab_user: enchance module and documentation - Rewrite documentation - Enchance function parameters - Add check_mode break - Add try/except on user creation * gitlab_deploy_key, gitlab_group, gitlab_hooks, gitlab_project, gitlab_runner, gitlab_user: Fix residual bugs - Fix Copyright - Fix result messages - Add missing check_mode break * gitlab_deploy_key, gitlab_group, gitlab_hooks, gitlab_project, gitlab_runner, gitlab_user: pylint * gitlab_runner: Add substitution function for 'cmp' in python3 * unit-test: remove deprecated gitlab module tests - gitlab_deploy_key - gitlab_hooks - gitlab_project Actually, they can't be reused because of the modification of the way that the module communicate with the Gitlab instance. It doesn't make direct call to the API, now it use a python library that do the job. So using a pytest mocker to test the module won't work. * gitlab_deploy_key, gitlab_group, gitlab_hooks, gitlab_project, gitlab_runner, gitlab_user: add copyright * gitlab_deploy_key, gitlab_group, gitlab_hooks, gitlab_project, gitlab_runner, gitlab_user: Support old parameters format * module_utils Gitlab: Edit copyright * gitlab_deploy_key, gitlab_group, gitlab_hooks, gitlab_project, gitlab_runner, gitlab_user: Unifying module inputs - Rename verify_ssl into validate_certs to match standards - Remove unused alias parameters - Unify parameters type and requirement - Reorder list order * gitlab_deploy_key, gitlab_group, gitlab_hooks, gitlab_project, gitlab_runner, gitlab_user: Unifying module outputs - Use standard output parameter "msg" instead of "return" - Use snail_case for return values instead of camelCase * validate-module: remove sanity ignore * BOTMETA: remove gitlab_* test - This tests need to be completely rewriten because of the refactoring of these modules - TodoList Community Wiki was updated * gitlab_user: Fix group identifier * gitlab_project: Fix when group was empty * gitlab_deploy_key: edit return msg * module_utils gitlab: fall back to user namespace is project not found * gitlab modules: Add units tests * unit test: gitlab module fake current user * gitlab_user: fix access_level verification * gitlab unit tests: use decoration instead of with statement * unit tests: gitlab module skip python 2.6 * unit tests: gitlab module skip library import if python 2.6 * gitlab unit tests: use builtin unittest class * gitlab unit tests: use custom test class * unit test: gitlab module lint * unit tests: move gitlab utils * unit test: gitlab fix imports * gitlab_module: edit requirement python-gitlab library require python >= 2.7 * gitlab_module: add myself as author * gitlab_modules: add python encoding tag * gitlab_modules: keep consistency between variable name "validate_certs" * gitlab_modules: enchance documentation * gitlab_runner: fix syntax error in documentation * gitlab_module: use basic_auth module_utils and add deprecation warning * gitlab_module: documentation corrections * gitlab_module: python lint * gitlab_module: deprecate options and aliases for ansible 2.10 * gitlab_group: don't use 'local_action' is documentation example * gitlab_module: correct return messages * gitlab_module: use module_util 'missing_required_lib' when python library is missing * gitlab_module: fix typo in function name. * gitlab_modules: unify return msg on check_mode * gitlab_modules: don't use deprecated options in examples
This commit is contained in:
parent
a682a0292d
commit
959939b866
17 changed files with 2874 additions and 1847 deletions
|
@ -1,82 +1,78 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2018 Pierre-Louis Bonicoli <pierre-louis@libregerbil.fr>
|
||||
|
||||
# Copyright: (c) 2019, Guillaume Martinez (lunik@tiwabbit.fr)
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
import json
|
||||
from __future__ import absolute_import
|
||||
|
||||
from units.compat.mock import MagicMock
|
||||
from ansible.modules.source_control import gitlab_project
|
||||
from ansible.modules.source_control.gitlab_project import GitLabProject
|
||||
|
||||
import pytest
|
||||
from .gitlab import (GitlabModuleTestCase,
|
||||
python_version_match_requirement,
|
||||
resp_get_group, resp_get_project_by_name, resp_create_project,
|
||||
resp_get_project, resp_delete_project, resp_get_user)
|
||||
|
||||
# Gitlab module requirements
|
||||
if python_version_match_requirement():
|
||||
from gitlab.v4.objects import Project
|
||||
|
||||
# Unit tests requirements
|
||||
from httmock import with_httmock # noqa
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patch_gitlab_project(mocker):
|
||||
mocker.patch.object(gitlab_project, 'HAS_GITLAB_PACKAGE', mocker.PropertyMock(return_value=True))
|
||||
class TestGitlabProject(GitlabModuleTestCase):
|
||||
@with_httmock(resp_get_user)
|
||||
def setUp(self):
|
||||
super(TestGitlabProject, self).setUp()
|
||||
|
||||
self.gitlab_instance.user = self.gitlab_instance.users.get(1)
|
||||
self.moduleUtil = GitLabProject(module=self.mock_module, gitlab_instance=self.gitlab_instance)
|
||||
|
||||
@pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module'])
|
||||
@pytest.mark.usefixtures('patch_ansible_module')
|
||||
def test_without_required_parameters(capfd):
|
||||
"""Failure must occurs when all parameters are missing"""
|
||||
@with_httmock(resp_get_group)
|
||||
@with_httmock(resp_get_project_by_name)
|
||||
def test_project_exist(self):
|
||||
group = self.gitlab_instance.groups.get(1)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
gitlab_project.main()
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
assert results['failed']
|
||||
assert 'missing required arguments' in results['msg']
|
||||
rvalue = self.moduleUtil.existsProject(group, "diaspora-client")
|
||||
|
||||
self.assertEqual(rvalue, True)
|
||||
|
||||
TEST_CASES = [
|
||||
[
|
||||
{
|
||||
'server_url': 'http://gitlab.test/gitlab',
|
||||
'validate_certs': True,
|
||||
'login_user': 'john',
|
||||
'login_token': 'TOKEN',
|
||||
'name': 'new_test_repo',
|
||||
'group': 'my_repo_group',
|
||||
'public': True,
|
||||
'visibility_level': 20,
|
||||
'issues_enabled': False,
|
||||
'wiki_enabled': True,
|
||||
'snippets_enabled': True,
|
||||
'import_url': 'http://gitlab.test/gitlab/gitrepothatdoesnotexist.git',
|
||||
'state': 'present'
|
||||
},
|
||||
{
|
||||
'msg': "Failed to create project 'new_test_repo'",
|
||||
'failed': True,
|
||||
}
|
||||
],
|
||||
]
|
||||
rvalue = self.moduleUtil.existsProject(group, "missing-project")
|
||||
|
||||
self.assertEqual(rvalue, False)
|
||||
|
||||
@pytest.mark.parametrize('patch_ansible_module, testcase', TEST_CASES, indirect=['patch_ansible_module'])
|
||||
@pytest.mark.usefixtures('patch_ansible_module')
|
||||
def test_fail_if_url_import_doesnt_exist(mocker, capfd, patch_gitlab_project, testcase):
|
||||
""" Test for #36495
|
||||
@with_httmock(resp_get_group)
|
||||
@with_httmock(resp_create_project)
|
||||
def test_create_project(self):
|
||||
group = self.gitlab_instance.groups.get(1)
|
||||
project = self.moduleUtil.createProject(group, {"name": "Diaspora Client", "path": "diaspora-client", "namespace_id": group.id})
|
||||
|
||||
Ensure errors are reported (meaning task report a failure),
|
||||
for example when url_import doesn't exist, an error must occur.
|
||||
"""
|
||||
self.assertEqual(type(project), Project)
|
||||
self.assertEqual(project.name, "Diaspora Client")
|
||||
|
||||
git = MagicMock()
|
||||
git.createprojectuser.return_value = False
|
||||
@with_httmock(resp_get_project)
|
||||
def test_update_project(self):
|
||||
project = self.gitlab_instance.projects.get(1)
|
||||
|
||||
gitlab = MagicMock()
|
||||
gitlab.Gitlab.return_value = git
|
||||
gitlab_project.gitlab = gitlab
|
||||
changed, newProject = self.moduleUtil.updateProject(project, {"name": "New Name"})
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
gitlab_project.main()
|
||||
self.assertEqual(changed, True)
|
||||
self.assertEqual(type(newProject), Project)
|
||||
self.assertEqual(newProject.name, "New Name")
|
||||
|
||||
# Check that 1. createprojectuser method has been called 2. with expected parameter
|
||||
assert git.createprojectuser.call_count == 1
|
||||
assert git.createprojectuser.call_args[1]['import_url'] == 'http://gitlab.test/gitlab/gitrepothatdoesnotexist.git'
|
||||
changed, newProject = self.moduleUtil.updateProject(project, {"name": "New Name"})
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
assert results.get('failed') == testcase.get('failed')
|
||||
assert results['msg'] == testcase['msg']
|
||||
self.assertEqual(changed, False)
|
||||
self.assertEqual(newProject.name, "New Name")
|
||||
|
||||
@with_httmock(resp_get_group)
|
||||
@with_httmock(resp_get_project_by_name)
|
||||
@with_httmock(resp_delete_project)
|
||||
def test_delete_project(self):
|
||||
group = self.gitlab_instance.groups.get(1)
|
||||
|
||||
self.moduleUtil.existsProject(group, "diaspora-client")
|
||||
|
||||
rvalue = self.moduleUtil.deleteProject()
|
||||
|
||||
self.assertEqual(rvalue, None)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue