Skip to content

Commit 67508e8

Browse files
test: attempt to make functional test startup more reliable
The functional tests have been erratic. Current theory is that we are starting the tests before the GitLab container is fully up and running. * Add checking of the Health Check[1] endpoints. * Add a 20 second delay after we believe it is up and running. * Increase timeout from 300 to 400 seconds [1] https://docs.gitlab.com/ee/user/admin_area/monitoring/health_check.html
1 parent 8ba97aa commit 67508e8

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

tests/functional/conftest.py

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
11
import logging
2+
import pathlib
23
import tempfile
34
import time
45
import uuid
5-
from pathlib import Path
66
from subprocess import check_output
77

88
import pytest
9+
import requests
910

1011
import gitlab
1112
import gitlab.base
1213
from 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:
1720
return 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+
2046
def 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:
133164
setup_time = time.perf_counter() - start_time
134165
minutes, seconds = int(setup_time / 60), int(setup_time % 60)
135166
logging.info(
136167
f"Checking if GitLab container is up. "
137168
f"Have been checking for {minutes} minute(s), {seconds} seconds ..."
138169
)
139170
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
141189

142190
return _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+
):
171226
config_file = temp_dir / "python-gitlab.cfg"
172-
port = docker_services.port_for("gitlab", 80)
173227

174228
start_time = time.perf_counter()
175229
logging.info("Waiting for GitLab container to become ready.")
176230
docker_services.wait_until_responsive(
177231
timeout=300,
178232
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+
),
180238
)
181239
setup_time = time.perf_counter() - start_time
182240
minutes, seconds = int(setup_time / 60), int(setup_time % 60)
183241
logging.info(
184242
f"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

189247
config = f"""[global]
190248
default = local
191249
timeout = 60
192250
193251
[local]
194-
url = http://{docker_ip}:{port}
252+
url = {gitlab_url}
195253
private_token = {token}
196254
api_version = 4"""
197255

@@ -208,6 +266,7 @@ def gl(gitlab_config):
208266
logging.info("Instantiating python-gitlab gitlab.Gitlab instance")
209267
instance = gitlab.Gitlab.from_config("local", [gitlab_config])
210268

269+
logging.info("Reset GitLab")
211270
reset_gitlab(instance)
212271

213272
return instance

tests/functional/fixtures/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ services:
3232
grafana['enable'] = false
3333
letsencrypt['enable'] = false
3434
gitlab_rails['initial_license_file'] = '/python-gitlab-ci.gitlab-license'
35+
gitlab_rails['monitoring_whitelist'] = ['0.0.0.0/0']
3536
entrypoint:
3637
- /bin/sh
3738
- -c

0 commit comments

Comments
 (0)