Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ctr_client fixture, add --ctr-exe CLI arg #522

Closed

Conversation

LewisGaul
Copy link
Collaborator

@LewisGaul LewisGaul commented Jan 7, 2024

From #520 (comment):

I would say the more correct way to use pytest is to use fixtures for any resources (where the DockerClient instance is the main resource in this case), i.e. this should be done whether used for parameterisation or not. This allows performing checks, controlling initialisation and controlling cleanup - in our case we want it to be parameterised and controlled via a CLI arg.

This PR illustrates the improvement this offers - it makes it more explicit when testcases are using the DockerClient resource, which can be suitably set up in the fixture.

Extensive information about pytest's fixtures and how they can/should be used is available at https://docs.pytest.org/en/latest/explanation/fixtures.html.

I've currently only updated a few test modules to use the fixture, but happy to do the same for the rest of the tests. This should be considered a nice precursor to parameterising on other runtimes, but for now this would at least allow manually running with podman.

Default, using docker (no change):

(venv) python-on-whales/(ctr_client_fixture)$python -m pytest tests/ -k test_image_repr -vv --log-cli-level info
=============================================== test session starts ================================================
platform linux -- Python 3.9.5, pytest-7.1.2, pluggy-1.0.0 -- /mnt/c/Users/legaul/repos/python-on-whales/venv/bin/python
cachedir: .pytest_cache
rootdir: /mnt/c/Users/legaul/repos/python-on-whales
plugins: mock-3.8.2
collected 459 items / 458 deselected / 1 selected

tests/python_on_whales/components/test_image.py::test_image_repr
-------------------------------------------------- live log setup --------------------------------------------------
INFO     conftest:conftest.py:19 Running tests with container exe: docker
PASSED                                                                                                       [100%]

======================================== 1 passed, 458 deselected in 9.67s =========================================

Using podman:

(venv) python-on-whales/(ctr_client_fixture)$python -m pytest tests/ -k test_image_repr -vv --log-cli-level info --ctr-exe podman
=============================================== test session starts ================================================
platform linux -- Python 3.9.5, pytest-7.1.2, pluggy-1.0.0 -- /mnt/c/Users/legaul/repos/python-on-whales/venv/bin/python
cachedir: .pytest_cache
rootdir: /mnt/c/Users/legaul/repos/python-on-whales
plugins: mock-3.8.2
collected 459 items / 458 deselected / 1 selected

tests/python_on_whales/components/test_image.py::test_image_repr
-------------------------------------------------- live log setup --------------------------------------------------
INFO     conftest:conftest.py:19 Running tests with container exe: podman
PASSED                                                                                                       [100%]

======================================== 1 passed, 458 deselected in 10.62s ========================================

Using a container exe that's not available:

(venv) python-on-whales/(ctr_client_fixture)$python -m pytest tests/ -k test_image_repr -vv --log-cli-level info --ctr-exe nerdctl --tb short
=============================================== test session starts ================================================
platform linux -- Python 3.9.5, pytest-7.1.2, pluggy-1.0.0 -- /mnt/c/Users/legaul/repos/python-on-whales/venv/bin/python
cachedir: .pytest_cache
rootdir: /mnt/c/Users/legaul/repos/python-on-whales
plugins: mock-3.8.2
collected 459 items / 458 deselected / 1 selected

tests/python_on_whales/components/test_image.py::test_image_repr
-------------------------------------------------- live log setup --------------------------------------------------
INFO     conftest:conftest.py:19 Running tests with container exe: nerdctl
ERROR                                                                                                        [100%]

====================================================== ERRORS ======================================================
________________________________________ ERROR at setup of test_image_repr _________________________________________
tests/conftest.py:24: in ctr_client
    subprocess.run(
/usr/lib/python3.9/subprocess.py:505: in run
    with Popen(*popenargs, **kwargs) as process:
/usr/lib/python3.9/subprocess.py:951: in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
/usr/lib/python3.9/subprocess.py:1821: in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
E   FileNotFoundError: [Errno 2] No such file or directory: 'nerdctl'

The above exception was the direct cause of the following exception:
tests/conftest.py:43: in ctr_client
    raise RuntimeError(
E   RuntimeError: Unable to run with container exe 'nerdctl'.
E   'nerdctl version' command failed: executable not found
------------------------------------------------ Captured log setup ------------------------------------------------
INFO     conftest:conftest.py:19 Running tests with container exe: nerdctl
============================================= short test summary info ==============================================
ERROR tests/python_on_whales/components/test_image.py::test_image_repr - RuntimeError: Unable to run with contain...
========================================= 458 deselected, 1 error in 3.18s =========================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant