1+ import time
12import tempfile
23from pathlib import Path
34from random import randint
5+ from subprocess import check_output
46
57import pytest
68
79import gitlab
810
911
1012TEMP_DIR = tempfile .gettempdir ()
13+ TEST_DIR = Path (__file__ ).resolve ().parent
1114
1215
1316def 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" )
0 commit comments