gabrieldemarmiesse / python-on-whales

An awesome Python wrapper for an awesome Docker CLI!
MIT License
537 stars 100 forks source link

Fix test_save_load() testcase to handle the case an image has multiple tags #596

Closed LewisGaul closed 1 month ago

LewisGaul commented 3 months ago

If the user pulls the busybox:1 image under another tag/name then image save/load tests fail because ctr_client.image.pull("busybox:1", quiet=True).repo_tags returns all tags, while only a single tag is loaded.

There's also a subtle difference between ctr_client.image.remove(image_name, force=True) and image.remove(force=True) - the former runs docker image rm -f image_name, the latter runs docker image rm -f image_id removing all tags for that image ID. The tests should only remove the tag we're working with (ideally they'd use a test-specific tag that won't be in use on the system!).

Also note there's a questionable behaviour where getting an image save iterator doesn't wait for the save to complete, meaning a subsequent removal of the image can cause failures, although maybe this is implicit from the fact you get an iterator...

___________________________________________________ test_save_iterator_bytes_and_load[docker] ___________________________________________________

ctr_client = <python_on_whales.docker_client.DockerClient object at 0x7f7cc7fd3310>

    @pytest.mark.parametrize("ctr_client", ["docker", "podman"], indirect=True)
    def test_save_iterator_bytes_and_load(ctr_client: DockerClient):
        image = ctr_client.image.pull("busybox:1", quiet=True)
        image_tags = image.repo_tags
        iterator = ctr_client.image.save("busybox:1")
        my_tar_as_bytes = b"".join(iterator)
        image.remove(force=True)
>       assert ctr_client.image.load(my_tar_as_bytes) == image_tags
E       AssertionError: assert ['busybox:1'] == ['busybox:1', 'busybox:latest']
E         Right contains one more item: 'busybox:latest'
E         Full diff:
E         - ['busybox:1', 'busybox:latest']
E         + ['busybox:1']

tests/python_on_whales/components/test_image.py:70: AssertionError