Skip to content

Commit 87b6150

Browse files
chore: add logging to tests/functional/conftest.py
I have found trying to debug issues in the functional tests can be difficult. Especially when trying to figure out failures in the CI running on Github. Add logging to `tests/functional/conftest.py` to have a better understanding of what is happening during a test run which is useful when trying to troubleshoot issues in the CI.
1 parent ca58008 commit 87b6150

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,10 @@ disable = [
9090

9191
[tool.pytest.ini_options]
9292
xfail_strict = true
93+
94+
log_cli = true
95+
# NOTE: If set 'log_cli_level' to 'DEBUG' will show a log of all of the HTTP requests
96+
# made in functional tests.
97+
log_cli_level = "INFO"
98+
log_cli_format = "%(asctime)s.%(msecs)03d [%(levelname)8s] (%(filename)s:%(funcName)s:L%(lineno)s) %(message)s"
99+
log_cli_date_format = "%Y-%m-%d %H:%M:%S"

tests/functional/conftest.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import tempfile
23
import time
34
import uuid
@@ -21,17 +22,29 @@ def fixture_dir(test_dir):
2122
def reset_gitlab(gl):
2223
# previously tools/reset_gitlab.py
2324
for project in gl.projects.list():
25+
logging.info(f"Mark for deletion project: {project.name!r}")
2426
for deploy_token in project.deploytokens.list():
27+
logging.info(
28+
f"Mark for deletion token: {deploy_token.name!r} in "
29+
f"project: {project.name!r}"
30+
)
2531
deploy_token.delete()
2632
project.delete()
2733
for group in gl.groups.list():
34+
logging.info(f"Mark for deletion group: {group.name!r}")
2835
for deploy_token in group.deploytokens.list():
36+
logging.info(
37+
f"Mark for deletion token: {deploy_token.name!r} in "
38+
f"group: {group.name!r}"
39+
)
2940
deploy_token.delete()
3041
group.delete()
3142
for variable in gl.variables.list():
43+
logging.info(f"Mark for deletion variable: {variable.name!r}")
3244
variable.delete()
3345
for user in gl.users.list():
3446
if user.username != "root":
47+
logging.info(f"Mark for deletion user: {user.username!r}")
3548
user.delete(hard_delete=True)
3649

3750
max_iterations = int(TIMEOUT / SLEEP_INTERVAL)
@@ -44,13 +57,27 @@ def wait_for_maximum_list_length(
4457
) -> None:
4558
"""Wait for the list() length to be no greater than expected maximum or fail
4659
test if timeout is exceeded"""
47-
for _ in range(max_iterations):
48-
if len(rest_manager.list()) <= max_length:
60+
logging.info(f"Checking {description!r} has no more than {max_length} items")
61+
for count in range(max_iterations):
62+
items = rest_manager.list()
63+
if len(items) <= max_length:
4964
break
65+
logging.info(
66+
f"Iteration: {count} Waiting for {description!r} items to be deleted: "
67+
f"{[x.name for x in items]}"
68+
)
5069
time.sleep(SLEEP_INTERVAL)
51-
assert len(rest_manager.list()) <= max_length, (
52-
f"Did not delete required items for {description}. "
53-
f"Elapsed_time: {time.perf_counter() - start_time}"
70+
71+
items = rest_manager.list()
72+
elapsed_time = time.perf_counter() - start_time
73+
if len(items) > max_length:
74+
logging.error(
75+
f"Too many {description!r} items still remaining and timeout "
76+
f"({elapsed_time}) exceeded: {[x.name for x in items]}"
77+
)
78+
assert len(items) <= max_length, (
79+
f"Did not delete required items for {description!r}. "
80+
f"Elapsed_time: {elapsed_time}"
5481
)
5582

5683
wait_for_maximum_list_length(rest_manager=gl.projects, description="projects")
@@ -62,6 +89,7 @@ def wait_for_maximum_list_length(
6289

6390

6491
def set_token(container, fixture_dir):
92+
logging.info("Creating API token.")
6593
set_token_rb = fixture_dir / "set_token.rb"
6694

6795
with open(set_token_rb, "r") as f:
@@ -76,6 +104,7 @@ def set_token(container, fixture_dir):
76104
set_token_command,
77105
]
78106
output = check_output(rails_command).decode().strip()
107+
logging.info("Finished creating API token.")
79108

80109
return output
81110

@@ -85,7 +114,7 @@ def pytest_report_collectionfinish(config, startdir, items):
85114
"",
86115
"Starting GitLab container.",
87116
"Waiting for GitLab to reconfigure.",
88-
"This may take a few minutes.",
117+
"This will take a few minutes.",
89118
]
90119

91120

@@ -129,6 +158,7 @@ def check_is_alive():
129158
"""
130159

131160
def _check(container):
161+
logging.info("Checking if GitLab container is up...")
132162
logs = ["docker", "logs", container]
133163
return "gitlab Reconfigured!" in check_output(logs).decode()
134164

@@ -144,7 +174,7 @@ def wait_for_sidekiq(gl):
144174
"""
145175

146176
def _wait(timeout=30, step=0.5):
147-
for _ in range(timeout):
177+
for count in range(timeout):
148178
time.sleep(step)
149179
busy = False
150180
processes = gl.sidekiq.process_metrics()["processes"]
@@ -153,6 +183,7 @@ def _wait(timeout=30, step=0.5):
153183
busy = True
154184
if not busy:
155185
return True
186+
logging.info(f"sidekiq busy {count} of {timeout}")
156187
return False
157188

158189
return _wait
@@ -163,9 +194,11 @@ def gitlab_config(check_is_alive, docker_ip, docker_services, temp_dir, fixture_
163194
config_file = temp_dir / "python-gitlab.cfg"
164195
port = docker_services.port_for("gitlab", 80)
165196

197+
logging.info("Waiting for GitLab container to become ready.")
166198
docker_services.wait_until_responsive(
167199
timeout=200, pause=5, check=lambda: check_is_alive("gitlab-test")
168200
)
201+
logging.info("GitLab container is now ready.")
169202

170203
token = set_token("gitlab-test", fixture_dir=fixture_dir)
171204

@@ -188,7 +221,9 @@ def gitlab_config(check_is_alive, docker_ip, docker_services, temp_dir, fixture_
188221
def gl(gitlab_config):
189222
"""Helper instance to make fixtures and asserts directly via the API."""
190223

224+
logging.info("Create python-gitlab gitlab.Gitlab object")
191225
instance = gitlab.Gitlab.from_config("local", [gitlab_config])
226+
192227
reset_gitlab(instance)
193228

194229
return instance

tox.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ skipsdist = True
44
envlist = py310,py39,py38,py37,pep8,black,twine-check,mypy,isort
55

66
[testenv]
7-
passenv = GITLAB_IMAGE GITLAB_TAG
7+
# NOTE: PY_COLORS, NO_COLOR, and FORCE_COLOR are used by pytest, so pass them through if
8+
# set. https://docs.pytest.org/en/6.2.x/reference.html
9+
passenv = GITLAB_IMAGE GITLAB_TAG PY_COLORS NO_COLOR FORCE_COLOR
810
setenv = VIRTUAL_ENV={envdir}
911
whitelist_externals = true
1012
usedevelop = True

0 commit comments

Comments
 (0)