Skip to content

Commit 9b4c01b

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 916b1db commit 9b4c01b

File tree

1 file changed

+17
-48
lines changed

1 file changed

+17
-48
lines changed

tests/functional/conftest.py

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,37 @@ def fixture_dir(test_dir):
1717
return test_dir / "functional" / "fixtures"
1818

1919

20-
def reset_gitlab(gl):
21-
# previously tools/reset_gitlab.py
20+
def reset_gitlab(gl: gitlab.Gitlab) -> None:
21+
"""Delete resources (such as projects, groups, users) that shouldn't
22+
exist."""
2223
for project in gl.projects.list():
23-
logging.info(f"Marking for deletion project: {project.path_with_namespace!r}")
2424
for deploy_token in project.deploytokens.list():
2525
logging.info(
26-
f"Marking for deletion token: {deploy_token.username!r} in "
26+
f"Deleting deploy token: {deploy_token.username!r} in "
2727
f"project: {project.path_with_namespace!r}"
2828
)
29-
deploy_token.delete()
30-
project.delete()
29+
helpers.safe_delete(deploy_token)
30+
logging.info(f"Deleting project: {project.path_with_namespace!r}")
31+
helpers.safe_delete(project)
3132
for group in gl.groups.list():
32-
logging.info(f"Marking for deletion group: {group.full_path!r}")
3333
for deploy_token in group.deploytokens.list():
3434
logging.info(
35-
f"Marking for deletion token: {deploy_token.username!r} in "
35+
f"Deleting deploy token: {deploy_token.username!r} in "
3636
f"group: {group.path_with_namespace!r}"
3737
)
38-
deploy_token.delete()
39-
group.delete()
38+
helpers.safe_delete(deploy_token)
39+
logging.info(f"Deleting group: {group.full_path!r}")
40+
helpers.safe_delete(group)
4041
for topic in gl.topics.list():
41-
topic.delete()
42+
logging.info(f"Deleting topic: {topic.name!r}")
43+
helpers.safe_delete(topic)
4244
for variable in gl.variables.list():
43-
logging.info(f"Marking for deletion variable: {variable.key!r}")
44-
variable.delete()
45+
logging.info(f"Deleting variable: {variable.key!r}")
46+
helpers.safe_delete(variable)
4547
for user in gl.users.list():
4648
if user.username != "root":
47-
logging.info(f"Marking for deletion user: {user.username!r}")
48-
user.delete(hard_delete=True)
49-
50-
# Ensure everything has been reset
51-
start_time = time.perf_counter()
52-
53-
def wait_for_list_size(
54-
rest_manager: gitlab.base.RESTManager, description: str, max_length: int = 0
55-
) -> None:
56-
"""Wait for the list() length to be no greater than expected maximum or fail
57-
test if timeout is exceeded"""
58-
logging.info(f"Checking {description!r} has no more than {max_length} items")
59-
for count in range(helpers.MAX_ITERATIONS):
60-
items = rest_manager.list()
61-
if len(items) <= max_length:
62-
break
63-
logging.info(
64-
f"Iteration: {count} Waiting for {description!r} items to be deleted: "
65-
f"{[x.name for x in items]}"
66-
)
67-
time.sleep(helpers.SLEEP_INTERVAL)
68-
69-
elapsed_time = time.perf_counter() - start_time
70-
error_message = (
71-
f"More than {max_length} {description!r} items still remaining and timeout "
72-
f"({elapsed_time}) exceeded: {[x.name for x in items]}"
73-
)
74-
if len(items) > max_length:
75-
logging.error(error_message)
76-
assert len(items) <= max_length, error_message
77-
78-
wait_for_list_size(rest_manager=gl.projects, description="projects")
79-
wait_for_list_size(rest_manager=gl.groups, description="groups")
80-
wait_for_list_size(rest_manager=gl.variables, description="variables")
81-
wait_for_list_size(rest_manager=gl.users, description="users", max_length=1)
49+
logging.info(f"Deleting user: {user.username!r}")
50+
helpers.safe_delete(user, hard_delete=True)
8251

8352

8453
def set_token(container, fixture_dir):

0 commit comments

Comments
 (0)