Skip to content

Commit 517a330

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 3d000d3 commit 517a330

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

tests/functional/conftest.py

Lines changed: 50 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"Marking for deletion project: {project.path_with_namespace!r}")
2636
for deploy_token in project.deploytokens.list():
@@ -49,13 +59,24 @@ def reset_gitlab(gl):
4959
logging.info(f"Marking for deletion user: {user.username!r}")
5060
user.delete(hard_delete=True)
5161

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

5471
# Ensure everything has been reset
5572
start_time = time.perf_counter()
5673

5774
def wait_for_list_size(
58-
rest_manager: gitlab.base.RESTManager, description: str, max_length: int = 0
75+
rest_manager: gitlab.base.RESTManager,
76+
description: str,
77+
max_length: int = 0,
78+
should_delete_func=lambda x: True,
79+
delete_kwargs={},
5980
) -> None:
6081
"""Wait for the list() length to be no greater than expected maximum or fail
6182
test if timeout is exceeded"""
@@ -68,6 +89,19 @@ def wait_for_list_size(
6889
f"Iteration: {count} Waiting for {description!r} items to be deleted: "
6990
f"{[x.name for x in items]}"
7091
)
92+
for item in items:
93+
if should_delete_func(item):
94+
logging.info(
95+
f"Marking {description!r} item again for deletion: "
96+
f"{item.name!r}"
97+
)
98+
try:
99+
item.delete(**delete_kwargs)
100+
except gitlab.exceptions.GitlabDeleteError as exc:
101+
logging.info(
102+
f"{description!r} item already marked for deletion: "
103+
f"{item.name!r} {exc}"
104+
)
71105
time.sleep(SLEEP_INTERVAL)
72106

73107
elapsed_time = time.perf_counter() - start_time
@@ -82,7 +116,19 @@ def wait_for_list_size(
82116
wait_for_list_size(rest_manager=gl.projects, description="projects")
83117
wait_for_list_size(rest_manager=gl.groups, description="groups")
84118
wait_for_list_size(rest_manager=gl.variables, description="variables")
85-
wait_for_list_size(rest_manager=gl.users, description="users", max_length=1)
119+
120+
def should_delete_user(user):
121+
if user.username == "root":
122+
return False
123+
return True
124+
125+
wait_for_list_size(
126+
rest_manager=gl.users,
127+
description="users",
128+
max_length=1,
129+
should_delete_func=should_delete_user,
130+
delete_kwargs={"hard_delete": True},
131+
)
86132

87133

88134
def set_token(container, fixture_dir):

0 commit comments

Comments
 (0)