11import logging
2+ import pathlib
23import tempfile
34import time
45import uuid
5- from pathlib import Path
66from subprocess import check_output
77
88import pytest
9+ import requests
910
1011import gitlab
1112import gitlab .base
1213from tests .functional import helpers
1314
15+ SLEEP_TIME = 10
16+
1417
1518@pytest .fixture (scope = "session" )
16- def fixture_dir (test_dir ):
19+ def fixture_dir (test_dir )-> pathlib . Path :
1720return test_dir / "functional" / "fixtures"
1821
1922
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+
2046def reset_gitlab (gl :gitlab .Gitlab )-> None :
2147"""Delete resources (such as projects, groups, users) that shouldn't
2248 exist."""
@@ -99,8 +125,8 @@ def pytest_addoption(parser):
99125
100126
101127@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 ())
104130
105131
106132@pytest .fixture (scope = "session" )
@@ -129,15 +155,37 @@ def check_is_alive():
129155 Return a healthcheck function fixture for the GitLab container spinup.
130156 """
131157
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 :
133164setup_time = time .perf_counter ()- start_time
134165minutes ,seconds = int (setup_time / 60 ),int (setup_time % 60 )
135166logging .info (
136167f"Checking if GitLab container is up. "
137168f"Have been checking for{ minutes } minute(s),{ seconds } seconds ..."
138169 )
139170logs = ["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
141189
142190return _check
143191
@@ -167,31 +215,41 @@ def _wait(timeout=30, step=0.5):
167215
168216
169217@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+ ):
171226config_file = temp_dir / "python-gitlab.cfg"
172- port = docker_services .port_for ("gitlab" ,80 )
173227
174228start_time = time .perf_counter ()
175229logging .info ("Waiting for GitLab container to become ready." )
176230docker_services .wait_until_responsive (
177231timeout = 300 ,
178232pause = 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+ ),
180238 )
181239setup_time = time .perf_counter ()- start_time
182240minutes ,seconds = int (setup_time / 60 ),int (setup_time % 60 )
183241logging .info (
184242f"GitLab container is now ready after{ minutes } minute(s),{ seconds } seconds"
185243 )
186244
187- token = set_token ("gitlab-test" ,fixture_dir = fixture_dir )
245+ token = set_token (gitlab_container_name ,fixture_dir = fixture_dir )
188246
189247config = f"""[global]
190248default = local
191249timeout = 60
192250
193251[local]
194- url =http:// { docker_ip } : { port }
252+ url ={ gitlab_url }
195253private_token ={ token }
196254api_version = 4"""
197255
@@ -208,6 +266,7 @@ def gl(gitlab_config):
208266logging .info ("Instantiating python-gitlab gitlab.Gitlab instance" )
209267instance = gitlab .Gitlab .from_config ("local" , [gitlab_config ])
210268
269+ logging .info ("Reset GitLab" )
211270reset_gitlab (instance )
212271
213272return instance