Skip to content

Commit 79489c7

Browse files
committed
test(env): replace custom scripts with pytest and docker-compose
1 parent 0d89a6e commit 79489c7

15 files changed

+163
-287
lines changed

docker-requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-r requirements.txt
2+
-r test-requirements.txt
3+
pytest-console-scripts
4+
pytest-docker

tools/build_test_env.sh

Lines changed: 0 additions & 158 deletions
This file was deleted.

tools/functional/api/test_gitlab.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Temporary module to run legacy tests as a single pytest test case
3+
as they're all plain asserts at module level.
4+
"""
5+
6+
7+
def test_api_v4(gl):
8+
from tools.functional import python_test_v4

tools/functional/cli/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33

44
@pytest.fixture
5-
def gitlab_cli(script_runner, CONFIG):
5+
def gitlab_cli(script_runner, gitlab_config):
66
"""Wrapper fixture to help make test cases less verbose."""
77

88
def _gitlab_cli(subcommands):
99
"""
1010
Return a script_runner.run method that takes a default gitlab
1111
command, and subcommands passed as arguments inside test cases.
1212
"""
13-
command = ["gitlab", "--config-file", CONFIG]
13+
command = ["gitlab", "--config-file", gitlab_config]
1414

1515
for subcommand in subcommands:
1616
# ensure we get strings (e.g from IDs)

tools/functional/conftest.py

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import time
12
import tempfile
23
from pathlib import Path
34
from random import randint
5+
from subprocess import check_output
46

57
import pytest
68

79
import gitlab
810

911

1012
TEMP_DIR = tempfile.gettempdir()
13+
TEST_DIR = Path(__file__).resolve().parent
1114

1215

1316
def random_id():
@@ -20,15 +23,103 @@ def random_id():
2023
return randint(9, 9999)
2124

2225

26+
def reset_gitlab(gl):
27+
# previously tools/reset_gitlab.py
28+
for project in gl.projects.list():
29+
project.delete()
30+
for group in gl.groups.list():
31+
group.delete()
32+
for variable in gl.variables.list():
33+
variable.delete()
34+
for user in gl.users.list():
35+
if user.username != "root":
36+
user.delete()
37+
38+
39+
def set_token(container):
40+
set_token_rb = TEST_DIR / "fixtures" / "set_token.rb"
41+
42+
with open(set_token_rb, "r") as f:
43+
set_token_command = f.read().strip()
44+
45+
rails_command = [
46+
"docker",
47+
"exec",
48+
container,
49+
"gitlab-rails",
50+
"runner",
51+
set_token_command,
52+
]
53+
output = check_output(rails_command).decode().strip()
54+
55+
return output
56+
57+
2358
@pytest.fixture(scope="session")
24-
def CONFIG():
25-
return Path(TEMP_DIR) / "python-gitlab.cfg"
59+
def docker_compose_file():
60+
return TEST_DIR / "fixtures" / "docker-compose.yml"
2661

2762

2863
@pytest.fixture(scope="session")
29-
def gl(CONFIG):
64+
def check_is_alive(request):
65+
"""
66+
Return a healthcheck function fixture for the GitLab container spinup.
67+
"""
68+
start = time.time()
69+
70+
# Temporary manager to disable capsys in a session-scoped fixture
71+
# so people know it takes a while for GitLab to spin up
72+
# https://github.com/pytest-dev/pytest/issues/2704
73+
capmanager = request.config.pluginmanager.getplugin("capturemanager")
74+
75+
def _check(container):
76+
delay = int(time.time() - start)
77+
78+
with capmanager.global_and_fixture_disabled():
79+
print(f"Waiting for GitLab to reconfigure.. (~{delay}s)")
80+
81+
logs = ["docker", "logs", container]
82+
output = check_output(logs).decode()
83+
84+
return "gitlab Reconfigured!" in output
85+
86+
return _check
87+
88+
89+
@pytest.fixture(scope="session")
90+
def gitlab_config(check_is_alive, docker_ip, docker_services):
91+
config_file = Path(TEMP_DIR) / "python-gitlab.cfg"
92+
port = docker_services.port_for("gitlab", 80)
93+
94+
docker_services.wait_until_responsive(
95+
timeout=180, pause=5, check=lambda: check_is_alive("gitlab-test")
96+
)
97+
98+
token = set_token("gitlab-test")
99+
100+
config = f"""[global]
101+
default = local
102+
timeout = 60
103+
104+
[local]
105+
url = http://{docker_ip}:{port}
106+
private_token = {token}
107+
api_version = 4"""
108+
109+
with open(config_file, "w") as f:
110+
f.write(config)
111+
112+
return config_file
113+
114+
115+
@pytest.fixture(scope="session")
116+
def gl(gitlab_config):
30117
"""Helper instance to make fixtures and asserts directly via the API."""
31-
return gitlab.Gitlab.from_config("local", [CONFIG])
118+
119+
instance = gitlab.Gitlab.from_config("local", [gitlab_config])
120+
reset_gitlab(instance)
121+
122+
return instance
32123

33124

34125
@pytest.fixture(scope="module")
File renamed without changes.
File renamed without changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
version: '3'
2+
services:
3+
gitlab:
4+
image: 'gitlab/gitlab-ce:latest'
5+
container_name: 'gitlab-test'
6+
hostname: 'gitlab.test'
7+
privileged: true # Just in case https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/1350
8+
environment:
9+
GITLAB_OMNIBUS_CONFIG: |
10+
external_url 'http://gitlab.test'
11+
gitlab_rails['initial_root_password'] = '5iveL!fe'
12+
gitlab_rails['initial_shared_runners_registration_token'] = 'sTPNtWLEuSrHzoHP8oCU'
13+
registry['enable'] = false
14+
nginx['redirect_http_to_https'] = false
15+
nginx['listen_port'] = 80
16+
nginx['listen_https'] = false
17+
pages_external_url 'http://pages.gitlab.lxd'
18+
gitlab_pages['enable'] = true
19+
gitlab_pages['inplace_chroot'] = true
20+
prometheus['enable'] = false
21+
alertmanager['enable'] = false
22+
node_exporter['enable'] = false
23+
redis_exporter['enable'] = false
24+
postgres_exporter['enable'] = false
25+
pgbouncer_exporter['enable'] = false
26+
gitlab_exporter['enable'] = false
27+
grafana['enable'] = false
28+
letsencrypt['enable'] = false
29+
ports:
30+
- '8080:80'
31+
- '2222:22'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#programmatically-creating-a-personal-access-token
2+
3+
user = User.find_by_username('root')
4+
5+
token = user.personal_access_tokens.create(scopes: [:api, :sudo], name: 'default');
6+
token.set_token('python-gitlab-token');
7+
token.save!
8+
9+
puts token.token

0 commit comments

Comments
 (0)