diff options
author | John L. Villalovos <john@sodarock.com> | 2021-12-31 13:14:47 -0800 |
---|---|---|
committer | John L. Villalovos <john@sodarock.com> | 2021-12-31 13:14:47 -0800 |
commit | 67dd7b495c83911b9718aa07cb21626400992e80 (patch) | |
tree | 1e2db21a15b9359d7be613b5b9869c483300d0ca | |
parent | 3264aa65b18f623f972be38672f085c288b82839 (diff) | |
download | gitlab-jlvillal/reset_gitlab.tar.gz |
chore: make reset_gitlab() betterjlvillal/reset_gitlab
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.
-rw-r--r-- | tests/functional/conftest.py | 52 |
1 files changed, 48 insertions, 4 deletions
diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index 06f12c6..a3455af 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -19,8 +19,18 @@ def fixture_dir(test_dir): return test_dir / "functional" / "fixtures" -def reset_gitlab(gl): - # previously tools/reset_gitlab.py +def reset_gitlab(gl: gitlab.Gitlab) -> None: + # Mark our resources for deletion. It takes time for them to actually be deleted + # though. + _reset_gitlab_delete_resources(gl=gl) + + # Wait for all resources to be deleted + _reset_gitlab_wait_deletion_finish(gl=gl) + + +def _reset_gitlab_delete_resources(gl: gitlab.Gitlab) -> None: + """Mark for deletion, resources (such as projects, groups, users) that shouldn't + exist. Once marked they will still take time to be deleted.""" for project in gl.projects.list(): logging.info(f"Mark for deletion project: {project.name!r}") for deploy_token in project.deploytokens.list(): @@ -47,13 +57,24 @@ def reset_gitlab(gl): logging.info(f"Mark for deletion user: {user.username!r}") user.delete(hard_delete=True) + +def _reset_gitlab_wait_deletion_finish(gl: gitlab.Gitlab) -> None: + """Wait for all of our resources to be deleted. + + If anything exists then mark it again for deletion in case initial call to delete + didn't work, which has been seen :(""" + max_iterations = int(TIMEOUT / SLEEP_INTERVAL) # Ensure everything has been reset start_time = time.perf_counter() def wait_for_maximum_list_length( - rest_manager: gitlab.base.RESTManager, description: str, max_length: int = 0 + rest_manager: gitlab.base.RESTManager, + description: str, + max_length: int = 0, + should_delete_func=lambda x: True, + delete_kwargs={}, ) -> None: """Wait for the list() length to be no greater than expected maximum or fail test if timeout is exceeded""" @@ -66,6 +87,19 @@ def reset_gitlab(gl): f"Iteration: {count} Waiting for {description!r} items to be deleted: " f"{[x.name for x in items]}" ) + for item in items: + if should_delete_func(item): + logging.info( + f"Marking {description!r} item again for deletion: " + f"{item.name!r}" + ) + try: + item.delete(**delete_kwargs) + except gitlab.exceptions.GitlabDeleteError as exc: + logging.info( + f"{description!r} item already marked for deletion: " + f"{item.name!r} {exc}" + ) time.sleep(SLEEP_INTERVAL) items = rest_manager.list() @@ -83,8 +117,18 @@ def reset_gitlab(gl): wait_for_maximum_list_length(rest_manager=gl.projects, description="projects") wait_for_maximum_list_length(rest_manager=gl.groups, description="groups") wait_for_maximum_list_length(rest_manager=gl.variables, description="variables") + + def should_delete_user(user): + if user.username == "root": + return False + return True + wait_for_maximum_list_length( - rest_manager=gl.users, description="users", max_length=1 + rest_manager=gl.users, + description="users", + max_length=1, + should_delete_func=should_delete_user, + delete_kwargs={"hard_delete": True}, ) |