From 4c475abe30c36217da920477f3748e26f3395365 Mon Sep 17 00:00:00 2001 From: Ben Brown Date: Thu, 27 May 2021 14:44:46 +0100 Subject: [PATCH 1/7] test(functional): optionally keep containers running post-tests Additionally updates token creation to make use of `first_or_create()`, to avoid errors from the script caused by GitLab constraints preventing duplicate tokens with the same value. --- README.rst | 15 +++++++++++++++ tests/functional/conftest.py | 23 +++++++++++++++++++++++ tests/functional/fixtures/set_token.rb | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 36f018078..63df02ccf 100644 --- a/README.rst +++ b/README.rst @@ -186,6 +186,21 @@ To run these tests: # run the python API tests: tox -e py_func_v4 +When developing tests it can be a little frustrating to wait for GitLab to spin +up every run. To prevent the containers from being cleaned up afterwards, pass +`--keep-containers` to pytest, i.e.: + +.. code-block:: bash + + tox -e py_func_v4 -- --keep-containers + +If you then wish to test against a clean slate, you may perform a manual clean +up of the containers by running: + +.. code-block:: bash + + docker-compose -f tests/functional/fixtures/docker-compose.yml -p pytest-python-gitlab down -v + By default, the tests run against the latest version of the ``gitlab/gitlab-ce`` image. You can override both the image and tag by providing either the ``GITLAB_IMAGE`` or ``GITLAB_TAG`` environment variables. diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index 5d3b1b97d..2c38b9f9d 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -50,6 +50,14 @@ def pytest_report_collectionfinish(config, startdir, items): ] +def pytest_addoption(parser): + parser.addoption( + "--keep-containers", + action="store_true", + help="Keep containers running after testing", + ) + + @pytest.fixture(scope="session") def temp_dir(): return Path(tempfile.gettempdir()) @@ -65,6 +73,21 @@ def docker_compose_file(test_dir): return test_dir / "fixtures" / "docker-compose.yml" +@pytest.fixture(scope="session") +def docker_compose_project_name(): + """Set a consistent project name to enable optional reuse of containers.""" + return "pytest-python-gitlab" + + +@pytest.fixture(scope="session") +def docker_cleanup(request): + """Conditionally keep containers around by overriding the cleanup command.""" + if request.config.getoption("--keep-containers"): + # Print version and exit. + return "-v" + return "down -v" + + @pytest.fixture(scope="session") def check_is_alive(): """ diff --git a/tests/functional/fixtures/set_token.rb b/tests/functional/fixtures/set_token.rb index 735dcd55f..503588b9c 100644 --- a/tests/functional/fixtures/set_token.rb +++ b/tests/functional/fixtures/set_token.rb @@ -2,7 +2,7 @@ user = User.find_by_username('root') -token = user.personal_access_tokens.create(scopes: [:api, :sudo], name: 'default'); +token = user.personal_access_tokens.first_or_create(scopes: [:api, :sudo], name: 'default'); token.set_token('python-gitlab-token'); token.save! From 11ae11bfa5f9fcb903689805f8d35b4d62ab0c90 Mon Sep 17 00:00:00 2001 From: Ben Brown Date: Thu, 27 May 2021 16:25:34 +0100 Subject: [PATCH 2/7] test(cli): replace assignment expression This is a feature added in 3.8, removing it allows for the test to run with lower python versions. --- tests/functional/cli/test_cli_artifacts.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/functional/cli/test_cli_artifacts.py b/tests/functional/cli/test_cli_artifacts.py index 4cb69aaf9..aab05460b 100644 --- a/tests/functional/cli/test_cli_artifacts.py +++ b/tests/functional/cli/test_cli_artifacts.py @@ -1,12 +1,9 @@ import subprocess -import sys import textwrap import time from io import BytesIO from zipfile import is_zipfile -import pytest - content = textwrap.dedent( """\ test-artifact: @@ -23,11 +20,12 @@ } -@pytest.mark.skipif(sys.version_info < (3, 8), reason="I am the walrus") def test_cli_artifacts(capsysbinary, gitlab_config, gitlab_runner, project): project.files.create(data) - while not (jobs := project.jobs.list(scope="success")): + jobs = None + while not jobs: + jobs = project.jobs.list(scope="success") time.sleep(0.5) job = project.jobs.get(jobs[0].id) From 19a55d80762417311dcebde3f998f5ebc7e78264 Mon Sep 17 00:00:00 2001 From: Ben Brown Date: Tue, 1 Jun 2021 11:48:55 +0100 Subject: [PATCH 3/7] test(functional): explicitly remove deploy tokens on reset Deploy tokens would remain in the instance if the respective project or group was deleted without explicitly revoking the deploy tokens first. --- tests/functional/conftest.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index 2c38b9f9d..00c3e49dd 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -12,8 +12,12 @@ def reset_gitlab(gl): # previously tools/reset_gitlab.py for project in gl.projects.list(): + for deploy_token in project.deploytokens.list(): + deploy_token.delete() project.delete() for group in gl.groups.list(): + for deploy_token in group.deploytokens.list(): + deploy_token.delete() group.delete() for variable in gl.variables.list(): variable.delete() From 8e5b0de7d9b1631aac4e9ac03a286dfe80675040 Mon Sep 17 00:00:00 2001 From: Ben Brown Date: Tue, 1 Jun 2021 12:06:26 +0100 Subject: [PATCH 4/7] test(api): fix issues test Was incorrectly using the issue 'id' vs 'iid'. --- tests/functional/api/test_issues.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/functional/api/test_issues.py b/tests/functional/api/test_issues.py index f3a606bb7..64db46e51 100644 --- a/tests/functional/api/test_issues.py +++ b/tests/functional/api/test_issues.py @@ -4,11 +4,11 @@ def test_create_issue(project): issue = project.issues.create({"title": "my issue 1"}) issue2 = project.issues.create({"title": "my issue 2"}) - issue_ids = [issue.id for issue in project.issues.list()] - assert len(issue_ids) == 2 + issue_iids = [issue.iid for issue in project.issues.list()] + assert len(issue_iids) == 2 # Test 'iids' as a list - assert len(project.issues.list(iids=issue_ids)) == 2 + assert len(project.issues.list(iids=issue_iids)) == 2 issue2.state_event = "close" issue2.save() From 5226f095c39985d04c34e7703d60814e74be96f8 Mon Sep 17 00:00:00 2001 From: Ben Brown Date: Tue, 1 Jun 2021 14:43:36 +0100 Subject: [PATCH 5/7] docs: fix typo in http_delete docstring --- gitlab/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitlab/client.py b/gitlab/client.py index c0dc489df..d19acfb15 100644 --- a/gitlab/client.py +++ b/gitlab/client.py @@ -787,7 +787,7 @@ def http_put( ) from e def http_delete(self, path: str, **kwargs: Any) -> requests.Response: - """Make a PUT request to the Gitlab server. + """Make a DELETE request to the Gitlab server. Args: path (str): Path or full URL to query ('/projects' or From 8f814563beb601715930ed3b0f89c3871e6e2f33 Mon Sep 17 00:00:00 2001 From: Ben Brown Date: Tue, 1 Jun 2021 14:44:24 +0100 Subject: [PATCH 6/7] test(functional): force delete users on reset Timing issues between requesting group deletion and GitLab enacting that deletion resulted in errors while attempting to delete a user which was the sole owner of said group (see: test_groups). Pass the 'hard_delete' parameter to ensure user deletion. --- tests/functional/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index 00c3e49dd..23aa5830f 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -23,7 +23,7 @@ def reset_gitlab(gl): variable.delete() for user in gl.users.list(): if user.username != "root": - user.delete() + user.delete(hard_delete=True) def set_token(container, rootdir): From 4e690c256fc091ddf1649e48dbbf0b40cc5e6b95 Mon Sep 17 00:00:00 2001 From: Ben Brown Date: Tue, 1 Jun 2021 14:53:48 +0100 Subject: [PATCH 7/7] fix: ensure kwargs are passed appropriately for ObjectDeleteMixin --- gitlab/mixins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitlab/mixins.py b/gitlab/mixins.py index 852bc634c..3dae155c8 100644 --- a/gitlab/mixins.py +++ b/gitlab/mixins.py @@ -576,7 +576,7 @@ def delete(self, **kwargs: Any) -> None: """ if TYPE_CHECKING: assert isinstance(self.manager, DeleteMixin) - self.manager.delete(self.get_id()) + self.manager.delete(self.get_id(), **kwargs) class UserAgentDetailMixin(_RestObjectBase):