This commit is contained in:
Tobias Trabelsi 2022-05-21 23:59:10 +02:00
commit 53758f412e
Signed by: lerentis
GPG Key ID: FF0C2839718CAF2E
9 changed files with 223 additions and 0 deletions

20
.drone.yml Normal file
View File

@ -0,0 +1,20 @@
---
kind: pipeline
type: kubernetes
name: test
platform:
os: linux
arch: amd64
steps:
- name: test
image: python:3-alpine
commands:
- "pip install -r requirements.txt"
- "pytest --cov=app tests/"
when:
event:
- push
- pull_request

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
env.sh
__pycache__
.pytest_cache

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Gitlab Test
an excuse to start with pytest mock

0
app/__init__.py Normal file
View File

24
app/languages.py Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env python3
import gitlab
import os
def getProject(client, project_name):
return client.projects.get(project_name)
def getLanguage(project):
return list(project.languages().items())
def helmSet(lang):
ret = ''
for i in range(len(lang)):
ret += f"--set global.language[{i}]={lang[i][0]}"
return ret
if __name__ == '__main__':
with gitlab.Gitlab(url=os.environ['GITLAB_URL'], private_token=os.environ['GITLAB_TOKEN'], user_agent='my-package/1.0.0') as gl:
print(helmSet(getLanguage(getProject(gl, 'root/test'))), end=" ")

23
docker-compose.yml Normal file
View File

@ -0,0 +1,23 @@
version: '3.6'
services:
web:
image: 'gitlab/gitlab-ce:latest'
restart: always
hostname: 'gitlab.example.com'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://localhost'
# Add any other gitlab.rb configuration here, each on its own line
ports:
- '8080:80'
- '2222:22'
volumes:
- gitlab-config:/etc/gitlab
- gitlab-logs:/var/log/gitlab
- gitlab-data:/var/opt/gitlab
shm_size: '256m'
volumes:
gitlab-config:
gitlab-logs:
gitlab-data:

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
python-gitlab
pytest
pytest-mock
pytest-cov

0
tests/__init__.py Normal file
View File

146
tests/test_languages.py Normal file
View File

@ -0,0 +1,146 @@
from unittest.mock import MagicMock, patch
from app.languages import getProject, helmSet
import gitlab
from gitlab.v4.objects import ProjectManager as pm
def test_get_project():
project_object = {
'id': 34,
'description': 'A getting started template for go-micro (https://micro.mu/docs/go-micro.html)',
'name': 'test',
'name_with_namespace': 'Administrator / test',
'path': 'test',
'path_with_namespace': 'root/test',
'created_at': '2022-05-21T20:37:54.935Z',
'default_branch': 'master',
'tag_list': [],
'topics': [],
'ssh_url_to_repo': 'git@localhost:root/test.git',
'http_url_to_repo': 'http://localhost/root/test.git',
'web_url': 'http://localhost/root/test',
'readme_url': 'http://localhost/root/test/-/blob/master/README.md',
'avatar_url': None,
'forks_count': 0,
'star_count': 0,
'last_activity_at': '2022-05-21T20:37:54.935Z',
'namespace': {
'id': 1,
'name': 'Administrator',
'path': 'root',
'kind': 'user',
'full_path': 'root',
'parent_id': None,
'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon',
'web_url': 'http://localhost/root'},
'_links': {
'self': 'http://localhost/api/v4/projects/34',
'issues': 'http://localhost/api/v4/projects/34/issues',
'merge_requests': 'http://localhost/api/v4/projects/34/merge_requests',
'repo_branches': 'http://localhost/api/v4/projects/34/repository/branches',
'labels': 'http://localhost/api/v4/projects/34/labels',
'events': 'http://localhost/api/v4/projects/34/events',
'members': 'http://localhost/api/v4/projects/34/members'},
'packages_enabled': True,
'empty_repo': False,
'archived': False,
'visibility': 'public',
'owner': {
'id': 1,
'username': 'root',
'name': 'Administrator',
'state': 'active',
'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon',
'web_url': 'http://localhost/root'},
'resolve_outdated_diff_discussions': False,
'container_expiration_policy': {
'cadence': '1d',
'enabled': False,
'keep_n': 10,
'older_than': '90d',
'name_regex': '.*',
'name_regex_keep': None,
'next_run_at': '2022-05-22T20:37:55.006Z'},
'issues_enabled': True,
'merge_requests_enabled': True,
'wiki_enabled': True,
'jobs_enabled': True,
'snippets_enabled': True,
'container_registry_enabled': True,
'service_desk_enabled': False,
'service_desk_address': None,
'can_create_merge_request_in': True,
'issues_access_level': 'enabled',
'repository_access_level': 'enabled',
'merge_requests_access_level': 'enabled',
'forking_access_level': 'enabled',
'wiki_access_level': 'enabled',
'builds_access_level': 'enabled',
'snippets_access_level': 'enabled',
'pages_access_level': 'enabled',
'operations_access_level': 'enabled',
'analytics_access_level': 'enabled',
'container_registry_access_level': 'enabled',
'security_and_compliance_access_level': 'private',
'emails_disabled': None,
'shared_runners_enabled': True,
'lfs_enabled': True,
'creator_id': 1,
'import_url': None,
'import_type': 'gitlab_project',
'import_status': 'finished',
'import_error': None,
'open_issues_count': 0,
'runners_token': 'REDACTED',
'ci_default_git_depth': 20,
'ci_forward_deployment_enabled': True,
'ci_job_token_scope_enabled': False,
'ci_separated_caches': True,
'public_jobs': True,
'build_git_strategy': 'fetch',
'build_timeout': 3600,
'auto_cancel_pending_pipelines': 'enabled',
'build_coverage_regex': None,
'ci_config_path': None,
'shared_with_groups': [],
'only_allow_merge_if_pipeline_succeeds': False,
'allow_merge_on_skipped_pipeline': None,
'restrict_user_defined_variables': False,
'request_access_enabled': False,
'only_allow_merge_if_all_discussions_are_resolved': False,
'remove_source_branch_after_merge': True,
'printing_merge_request_link_enabled': True,
'merge_method': 'merge',
'squash_option': 'default_off',
'suggestion_commit_message': None,
'merge_commit_template': None,
'squash_commit_template': None,
'auto_devops_enabled': True,
'auto_devops_deploy_strategy': 'continuous',
'autoclose_referenced_issues': True,
'repository_storage': 'default',
'keep_latest_artifact': True,
'runner_token_expiration_interval': None,
'permissions': {
'project_access': {
'access_level': 50,
'notification_level': 3},
'group_access': None}}
with patch.object(pm, 'get', return_value=project_object) as mock_method:
gl = gitlab.Gitlab()
project = getProject(gl, 'root/test')
assert project['id'] == 34
def test_get_language():
getLanguage = MagicMock(return_value=[('Go', 100.0)])
expected = [('Go', 100.0)]
actual = getLanguage("project")
assert expected == actual
def test_helm_set():
excepted = '--set global.language[0]=Go'
actual = helmSet([('Go', 100.0)])
assert excepted == actual