gabrieldemarmiesse / python-on-whales

An awesome Python wrapper for an awesome Docker CLI!
MIT License
541 stars 101 forks source link

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

Closed LewisGaul closed 8 months ago

LewisGaul commented 8 months ago

From https://github.com/gabrieldemarmiesse/python-on-whales/pull/520#issuecomment-1880067542:

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 =========================================