From 0aa0b272a90b11951f900b290a8154408eace1de Mon Sep 17 00:00:00 2001 From: "John L. Villalovos" Date: Tue, 28 Dec 2021 16:02:28 -0800 Subject: [PATCH] chore: ensure reset_gitlab() succeeds Ensure reset_gitlab() succeeds by waiting to make sure everything has been deleted as expected. If the timeout is exceeded fail the test. Not using `wait_for_sidekiq` as it didn't work. During testing I didn't see any sidekiq processes as being busy even though not everything was deleted. --- tests/functional/conftest.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index 625cff986..7c4e58480 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -7,6 +7,10 @@ import pytest import gitlab +import gitlab.base + +SLEEP_INTERVAL = 0.1 +TIMEOUT = 60 # seconds before timeout will occur @pytest.fixture(scope="session") @@ -30,6 +34,32 @@ def reset_gitlab(gl): if user.username != "root": user.delete(hard_delete=True) + 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 + ) -> None: + """Wait for the list() length to be no greater than expected maximum or fail + test if timeout is exceeded""" + for _ in range(max_iterations): + if len(rest_manager.list()) <= max_length: + break + time.sleep(SLEEP_INTERVAL) + assert len(rest_manager.list()) <= max_length, ( + f"Did not delete required items for {description}. " + f"Elapsed_time: {time.perf_counter() - start_time}" + ) + + 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") + wait_for_maximum_list_length( + rest_manager=gl.users, description="users", max_length=1 + ) + def set_token(container, fixture_dir): set_token_rb = fixture_dir / "set_token.rb"