Skip to content

Commit

Permalink
ensure_image unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Franr committed Jun 22, 2024
1 parent e62e7dd commit a132707
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tests/test_containers/test_leverage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from unittest import mock

import pytest

from leverage._utils import ExitError
from leverage.container import LeverageContainer
from tests.test_containers import container_fixture_factory

Expand All @@ -26,3 +29,51 @@ def test_env_vars(muted_click_context):

container_args = container.client.api.create_container.call_args_list[0][1]
assert container_args["environment"] == {"foo": "bar", "testing": 123}


def test_ensure_image_already_available(leverage_container: LeverageContainer, fake_os_user, propagate_logs, caplog):
"""
Test that the local image is not re-built when is already available locally.
"""
# already available
with mock.patch.object(leverage_container.client.api, "images", return_value=True) as mocked_images:
leverage_container.ensure_image()

assert mocked_images.call_args_list[0][0][0] == "binbash/leverage-toolbox:test-5678-1234"
assert caplog.messages[0] == "Checking for local docker image, tag: test-5678-1234..."
assert "OK" in caplog.messages[1]


def test_ensure_image_failed(leverage_container: LeverageContainer, fake_os_user, propagate_logs, caplog):
"""
Test that we get a friendly error if re-building the image fails.
"""
build_response = [{"errorDetail": "Something went wrong"}]
# not available
with mock.patch.object(leverage_container.client.api, "images", return_value=False):
with mock.patch.object(leverage_container.client.api, "build", return_value=build_response) as mocked_build:
with pytest.raises(ExitError, match="Failed"):
leverage_container.ensure_image()

assert caplog.messages[1] == "Image not found, building it..."
assert caplog.messages[2] == "Failed building local image: Something went wrong"


def test_ensure_image(leverage_container: LeverageContainer, fake_os_user, propagate_logs, caplog):
"""
Test that the local image is not available locally, thus it has to be re-built.
"""
build_response = [{"stream": "Successfully built"}]
# not available
with mock.patch.object(leverage_container.client.api, "images", return_value=False):
with mock.patch.object(leverage_container.client.api, "build", return_value=build_response) as mocked_build:
leverage_container.ensure_image()

assert mocked_build.call_args_list[0][1]["buildargs"] == {
"GID": "5678",
"UID": "1234",
"UNAME": "leverage",
"IMAGE_TAG": "test",
}
assert caplog.messages[1] == "Image not found, building it..."
assert "OK" in caplog.messages[2]

0 comments on commit a132707

Please sign in to comment.