Skip to content

Commit d981956

Browse files
authored
Merge pull request #1478 from benjamb/benbrown/keep-containers
Optionally keep containers after running integration tests
2 parents 55ae61a + 4e690c2 commit d981956

File tree

7 files changed

+52
-12
lines changed

7 files changed

+52
-12
lines changed

README.rst

+15
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,21 @@ To run these tests:
186186
# run the python API tests:
187187
tox -e py_func_v4
188188
189+
When developing tests it can be a little frustrating to wait for GitLab to spin
190+
up every run. To prevent the containers from being cleaned up afterwards, pass
191+
`--keep-containers` to pytest, i.e.:
192+
193+
.. code-block:: bash
194+
195+
tox -e py_func_v4 -- --keep-containers
196+
197+
If you then wish to test against a clean slate, you may perform a manual clean
198+
up of the containers by running:
199+
200+
.. code-block:: bash
201+
202+
docker-compose -f tests/functional/fixtures/docker-compose.yml -p pytest-python-gitlab down -v
203+
189204
By default, the tests run against the latest version of the ``gitlab/gitlab-ce``
190205
image. You can override both the image and tag by providing either the
191206
``GITLAB_IMAGE`` or ``GITLAB_TAG`` environment variables.

gitlab/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ def http_put(
787787
) from e
788788

789789
def http_delete(self, path: str, **kwargs: Any) -> requests.Response:
790-
"""Make a PUT request to the Gitlab server.
790+
"""Make a DELETE request to the Gitlab server.
791791
792792
Args:
793793
path (str): Path or full URL to query ('/projects' or

gitlab/mixins.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ def delete(self, **kwargs: Any) -> None:
576576
"""
577577
if TYPE_CHECKING:
578578
assert isinstance(self.manager, DeleteMixin)
579-
self.manager.delete(self.get_id())
579+
self.manager.delete(self.get_id(), **kwargs)
580580

581581

582582
class UserAgentDetailMixin(_RestObjectBase):

tests/functional/api/test_issues.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
def test_create_issue(project):
55
issue = project.issues.create({"title": "my issue 1"})
66
issue2 = project.issues.create({"title": "my issue 2"})
7-
issue_ids = [issue.id for issue in project.issues.list()]
8-
assert len(issue_ids) == 2
7+
issue_iids = [issue.iid for issue in project.issues.list()]
8+
assert len(issue_iids) == 2
99

1010
# Test 'iids' as a list
11-
assert len(project.issues.list(iids=issue_ids)) == 2
11+
assert len(project.issues.list(iids=issue_iids)) == 2
1212

1313
issue2.state_event = "close"
1414
issue2.save()

tests/functional/cli/test_cli_artifacts.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import subprocess
2-
import sys
32
import textwrap
43
import time
54
from io import BytesIO
65
from zipfile import is_zipfile
76

8-
import pytest
9-
107
content = textwrap.dedent(
118
"""\
129
test-artifact:
@@ -23,11 +20,12 @@
2320
}
2421

2522

26-
@pytest.mark.skipif(sys.version_info < (3, 8), reason="I am the walrus")
2723
def test_cli_artifacts(capsysbinary, gitlab_config, gitlab_runner, project):
2824
project.files.create(data)
2925

30-
while not (jobs := project.jobs.list(scope="success")):
26+
jobs = None
27+
while not jobs:
28+
jobs = project.jobs.list(scope="success")
3129
time.sleep(0.5)
3230

3331
job = project.jobs.get(jobs[0].id)

tests/functional/conftest.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212
def reset_gitlab(gl):
1313
# previously tools/reset_gitlab.py
1414
for project in gl.projects.list():
15+
for deploy_token in project.deploytokens.list():
16+
deploy_token.delete()
1517
project.delete()
1618
for group in gl.groups.list():
19+
for deploy_token in group.deploytokens.list():
20+
deploy_token.delete()
1721
group.delete()
1822
for variable in gl.variables.list():
1923
variable.delete()
2024
for user in gl.users.list():
2125
if user.username != "root":
22-
user.delete()
26+
user.delete(hard_delete=True)
2327

2428

2529
def set_token(container, rootdir):
@@ -50,6 +54,14 @@ def pytest_report_collectionfinish(config, startdir, items):
5054
]
5155

5256

57+
def pytest_addoption(parser):
58+
parser.addoption(
59+
"--keep-containers",
60+
action="store_true",
61+
help="Keep containers running after testing",
62+
)
63+
64+
5365
@pytest.fixture(scope="session")
5466
def temp_dir():
5567
return Path(tempfile.gettempdir())
@@ -65,6 +77,21 @@ def docker_compose_file(test_dir):
6577
return test_dir / "fixtures" / "docker-compose.yml"
6678

6779

80+
@pytest.fixture(scope="session")
81+
def docker_compose_project_name():
82+
"""Set a consistent project name to enable optional reuse of containers."""
83+
return "pytest-python-gitlab"
84+
85+
86+
@pytest.fixture(scope="session")
87+
def docker_cleanup(request):
88+
"""Conditionally keep containers around by overriding the cleanup command."""
89+
if request.config.getoption("--keep-containers"):
90+
# Print version and exit.
91+
return "-v"
92+
return "down -v"
93+
94+
6895
@pytest.fixture(scope="session")
6996
def check_is_alive():
7097
"""

tests/functional/fixtures/set_token.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
user = User.find_by_username('root')
44

5-
token = user.personal_access_tokens.create(scopes: [:api, :sudo], name: 'default');
5+
token = user.personal_access_tokens.first_or_create(scopes: [:api, :sudo], name: 'default');
66
token.set_token('python-gitlab-token');
77
token.save!
88

0 commit comments

Comments
 (0)