Skip to content

Commit 67dd7b4

Browse files
chore: make reset_gitlab() better
Saw issues in the CI where reset_gitlab() would fail. It would fail to delete the group that is created when GitLab starts up. Extending the timeout didn't fix the issue. Changed the code so now it will check if the item is deleted and if it isn't it will call the delete() method again to ensure that GitLab knows it should be deleted. Since making this change I have not been able to reproduce the failure in reset_gitlab(). Also added some logging functionality that can be seen if logging is turned on in pytest.
1 parent 3264aa6 commit 67dd7b4

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

tests/functional/conftest.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,18 @@ def fixture_dir(test_dir):
1919
return test_dir / "functional" / "fixtures"
2020

2121

22-
def reset_gitlab(gl):
23-
# previously tools/reset_gitlab.py
22+
def reset_gitlab(gl: gitlab.Gitlab) -> None:
23+
# Mark our resources for deletion. It takes time for them to actually be deleted
24+
# though.
25+
_reset_gitlab_delete_resources(gl=gl)
26+
27+
# Wait for all resources to be deleted
28+
_reset_gitlab_wait_deletion_finish(gl=gl)
29+
30+
31+
def _reset_gitlab_delete_resources(gl: gitlab.Gitlab) -> None:
32+
"""Mark for deletion, resources (such as projects, groups, users) that shouldn't
33+
exist. Once marked they will still take time to be deleted."""
2434
for project in gl.projects.list():
2535
logging.info(f"Mark for deletion project: {project.name!r}")
2636
for deploy_token in project.deploytokens.list():
@@ -47,13 +57,24 @@ def reset_gitlab(gl):
4757
logging.info(f"Mark for deletion user: {user.username!r}")
4858
user.delete(hard_delete=True)
4959

60+
61+
def _reset_gitlab_wait_deletion_finish(gl: gitlab.Gitlab) -> None:
62+
"""Wait for all of our resources to be deleted.
63+
64+
If anything exists then mark it again for deletion in case initial call to delete
65+
didn't work, which has been seen :("""
66+
5067
max_iterations = int(TIMEOUT / SLEEP_INTERVAL)
5168

5269
# Ensure everything has been reset
5370
start_time = time.perf_counter()
5471

5572
def wait_for_maximum_list_length(
56-
rest_manager: gitlab.base.RESTManager, description: str, max_length: int = 0
73+
rest_manager: gitlab.base.RESTManager,
74+
description: str,
75+
max_length: int = 0,
76+
should_delete_func=lambda x: True,
77+
delete_kwargs={},
5778
) -> None:
5879
"""Wait for the list() length to be no greater than expected maximum or fail
5980
test if timeout is exceeded"""
@@ -66,6 +87,19 @@ def wait_for_maximum_list_length(
6687
f"Iteration: {count} Waiting for {description!r} items to be deleted: "
6788
f"{[x.name for x in items]}"
6889
)
90+
for item in items:
91+
if should_delete_func(item):
92+
logging.info(
93+
f"Marking {description!r} item again for deletion: "
94+
f"{item.name!r}"
95+
)
96+
try:
97+
item.delete(**delete_kwargs)
98+
except gitlab.exceptions.GitlabDeleteError as exc:
99+
logging.info(
100+
f"{description!r} item already marked for deletion: "
101+
f"{item.name!r} {exc}"
102+
)
69103
time.sleep(SLEEP_INTERVAL)
70104

71105
items = rest_manager.list()
@@ -83,8 +117,18 @@ def wait_for_maximum_list_length(
83117
wait_for_maximum_list_length(rest_manager=gl.projects, description="projects")
84118
wait_for_maximum_list_length(rest_manager=gl.groups, description="groups")
85119
wait_for_maximum_list_length(rest_manager=gl.variables, description="variables")
120+
121+
def should_delete_user(user):
122+
if user.username == "root":
123+
return False
124+
return True
125+
86126
wait_for_maximum_list_length(
87-
rest_manager=gl.users, description="users", max_length=1
127+
rest_manager=gl.users,
128+
description="users",
129+
max_length=1,
130+
should_delete_func=should_delete_user,
131+
delete_kwargs={"hard_delete": True},
88132
)
89133

90134

0 commit comments

Comments
 (0)