Skip to content

Commit c829b26

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 4b798fc commit c829b26

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

tests/functional/conftest.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
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 = 20
16+
1417

1518
@pytest.fixture(scope="session")
1619
def fixture_dir(test_dir):
@@ -129,15 +132,30 @@ def check_is_alive():
129132
Return a healthcheck function fixture for the GitLab container spinup.
130133
"""
131134

132-
def _check(container: str, start_time: float) -> bool:
135+
def _check(
136+
*, container: str, start_time: float, docker_ip: str, docker_port: int
137+
) -> bool:
133138
setup_time = time.perf_counter() - start_time
134139
minutes, seconds = int(setup_time / 60), int(setup_time % 60)
135140
logging.info(
136141
f"Checking if GitLab container is up. "
137142
f"Have been checking for {minutes} minute(s), {seconds} seconds ..."
138143
)
139144
logs = ["docker", "logs", container]
140-
return "gitlab Reconfigured!" in check_output(logs).decode()
145+
if "gitlab Reconfigured!" not in check_output(logs).decode():
146+
return False
147+
logging.debug("GitLab has finished reconfiguring.")
148+
gitlab_url = f"http://{docker_ip}:{docker_port}"
149+
for check in ("health", "readiness", "liveness"):
150+
url = f"{gitlab_url}/-/{check}"
151+
logging.debug(f"Checking {check!r} endpoint at: {url}")
152+
result = requests.get(url)
153+
if result.status_code != 200:
154+
logging.debug(f"{check!r} check did not return 200: {result}")
155+
return False
156+
logging.debug(f"Sleeping for {SLEEP_TIME}")
157+
time.sleep(SLEEP_TIME)
158+
return True
141159

142160
return _check
143161

@@ -174,9 +192,14 @@ def gitlab_config(check_is_alive, docker_ip, docker_services, temp_dir, fixture_
174192
start_time = time.perf_counter()
175193
logging.info("Waiting for GitLab container to become ready.")
176194
docker_services.wait_until_responsive(
177-
timeout=300,
195+
timeout=400,
178196
pause=10,
179-
check=lambda: check_is_alive("gitlab-test", start_time=start_time),
197+
check=lambda: check_is_alive(
198+
container="gitlab-test",
199+
start_time=start_time,
200+
docker_ip=docker_ip,
201+
docker_port=port,
202+
),
180203
)
181204
setup_time = time.perf_counter() - start_time
182205
minutes, seconds = int(setup_time / 60), int(setup_time % 60)
@@ -208,6 +231,7 @@ def gl(gitlab_config):
208231
logging.info("Instantiating python-gitlab gitlab.Gitlab instance")
209232
instance = gitlab.Gitlab.from_config("local", [gitlab_config])
210233

234+
logging.info("Reset GitLab")
211235
reset_gitlab(instance)
212236

213237
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)