Skip to content

Commit 4c475ab

Browse files
committed
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.
1 parent ac92205 commit 4c475ab

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
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.

tests/functional/conftest.py

+23
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ def pytest_report_collectionfinish(config, startdir, items):
5050
]
5151

5252

53+
def pytest_addoption(parser):
54+
parser.addoption(
55+
"--keep-containers",
56+
action="store_true",
57+
help="Keep containers running after testing",
58+
)
59+
60+
5361
@pytest.fixture(scope="session")
5462
def temp_dir():
5563
return Path(tempfile.gettempdir())
@@ -65,6 +73,21 @@ def docker_compose_file(test_dir):
6573
return test_dir / "fixtures" / "docker-compose.yml"
6674

6775

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

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)