1
1
import logging
2
+ import pathlib
2
3
import tempfile
3
4
import time
4
5
import uuid
5
- from pathlib import Path
6
6
from subprocess import check_output
7
7
8
8
import pytest
9
+ import requests
9
10
10
11
import gitlab
11
12
import gitlab .base
12
13
from tests .functional import helpers
13
14
15
+ SLEEP_TIME = 10
16
+
14
17
15
18
@pytest .fixture (scope = "session" )
16
- def fixture_dir (test_dir ):
19
+ def fixture_dir (test_dir ) -> pathlib . Path :
17
20
return test_dir / "functional" / "fixtures"
18
21
19
22
23
+ @pytest .fixture (scope = "session" )
24
+ def gitlab_service_name () -> str :
25
+ """The "service" name is the one defined in the `docker-compose.yml` file"""
26
+ return "gitlab"
27
+
28
+
29
+ @pytest .fixture (scope = "session" )
30
+ def gitlab_container_name () -> str :
31
+ """The "container" name is the one defined in the `docker-compose.yml` file
32
+ for the "gitlab" service"""
33
+ return "gitlab-test"
34
+
35
+
36
+ @pytest .fixture (scope = "session" )
37
+ def gitlab_docker_port (docker_services , gitlab_service_name : str ) -> int :
38
+ return docker_services .port_for (service = gitlab_service_name , container_port = 80 )
39
+
40
+
41
+ @pytest .fixture (scope = "session" )
42
+ def gitlab_url (docker_ip : str , gitlab_docker_port : int ) -> str :
43
+ return f"http://{ docker_ip } :{ gitlab_docker_port } "
44
+
45
+
20
46
def reset_gitlab (gl : gitlab .Gitlab ) -> None :
21
47
"""Delete resources (such as projects, groups, users) that shouldn't
22
48
exist."""
@@ -99,8 +125,8 @@ def pytest_addoption(parser):
99
125
100
126
101
127
@pytest .fixture (scope = "session" )
102
- def temp_dir ():
103
- return Path (tempfile .gettempdir ())
128
+ def temp_dir () -> pathlib . Path :
129
+ return pathlib . Path (tempfile .gettempdir ())
104
130
105
131
106
132
@pytest .fixture (scope = "session" )
@@ -129,15 +155,37 @@ def check_is_alive():
129
155
Return a healthcheck function fixture for the GitLab container spinup.
130
156
"""
131
157
132
- def _check (container : str , start_time : float ) -> bool :
158
+ def _check (
159
+ * ,
160
+ container : str ,
161
+ start_time : float ,
162
+ gitlab_url : str ,
163
+ ) -> bool :
133
164
setup_time = time .perf_counter () - start_time
134
165
minutes , seconds = int (setup_time / 60 ), int (setup_time % 60 )
135
166
logging .info (
136
167
f"Checking if GitLab container is up. "
137
168
f"Have been checking for { minutes } minute(s), { seconds } seconds ..."
138
169
)
139
170
logs = ["docker" , "logs" , container ]
140
- return "gitlab Reconfigured!" in check_output (logs ).decode ()
171
+ if "gitlab Reconfigured!" not in check_output (logs ).decode ():
172
+ return False
173
+ logging .debug ("GitLab has finished reconfiguring." )
174
+ for check in ("health" , "readiness" , "liveness" ):
175
+ url = f"{ gitlab_url } /-/{ check } "
176
+ logging .debug (f"Checking { check !r} endpoint at: { url } " )
177
+ try :
178
+ result = requests .get (url , timeout = 1.0 )
179
+ except requests .exceptions .Timeout :
180
+ logging .info (f"{ check !r} check timed out" )
181
+ return False
182
+ if result .status_code != 200 :
183
+ logging .info (f"{ check !r} check did not return 200: { result !r} " )
184
+ return False
185
+ logging .debug (f"{ check !r} check passed: { result !r} " )
186
+ logging .debug (f"Sleeping for { SLEEP_TIME } " )
187
+ time .sleep (SLEEP_TIME )
188
+ return True
141
189
142
190
return _check
143
191
@@ -167,31 +215,41 @@ def _wait(timeout=30, step=0.5):
167
215
168
216
169
217
@pytest .fixture (scope = "session" )
170
- def gitlab_config (check_is_alive , docker_ip , docker_services , temp_dir , fixture_dir ):
218
+ def gitlab_config (
219
+ check_is_alive ,
220
+ gitlab_container_name : str ,
221
+ gitlab_url : str ,
222
+ docker_services ,
223
+ temp_dir : pathlib .Path ,
224
+ fixture_dir : pathlib .Path ,
225
+ ):
171
226
config_file = temp_dir / "python-gitlab.cfg"
172
- port = docker_services .port_for ("gitlab" , 80 )
173
227
174
228
start_time = time .perf_counter ()
175
229
logging .info ("Waiting for GitLab container to become ready." )
176
230
docker_services .wait_until_responsive (
177
231
timeout = 300 ,
178
232
pause = 10 ,
179
- check = lambda : check_is_alive ("gitlab-test" , start_time = start_time ),
233
+ check = lambda : check_is_alive (
234
+ container = gitlab_container_name ,
235
+ start_time = start_time ,
236
+ gitlab_url = gitlab_url ,
237
+ ),
180
238
)
181
239
setup_time = time .perf_counter () - start_time
182
240
minutes , seconds = int (setup_time / 60 ), int (setup_time % 60 )
183
241
logging .info (
184
242
f"GitLab container is now ready after { minutes } minute(s), { seconds } seconds"
185
243
)
186
244
187
- token = set_token ("gitlab-test" , fixture_dir = fixture_dir )
245
+ token = set_token (gitlab_container_name , fixture_dir = fixture_dir )
188
246
189
247
config = f"""[global]
190
248
default = local
191
249
timeout = 60
192
250
193
251
[local]
194
- url = http:// { docker_ip } : { port }
252
+ url = { gitlab_url }
195
253
private_token = { token }
196
254
api_version = 4"""
197
255
@@ -208,6 +266,7 @@ def gl(gitlab_config):
208
266
logging .info ("Instantiating python-gitlab gitlab.Gitlab instance" )
209
267
instance = gitlab .Gitlab .from_config ("local" , [gitlab_config ])
210
268
269
+ logging .info ("Reset GitLab" )
211
270
reset_gitlab (instance )
212
271
213
272
return instance
0 commit comments