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

fix: gracefully exit on not finding a local image #6039

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion samcli/lib/build/app_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
from samcli.lib.utils import osutils
from samcli.lib.utils.packagetype import IMAGE, ZIP
from samcli.lib.utils.stream_writer import StreamWriter
from samcli.local.docker.exceptions import ContainerNotStartableException
from samcli.local.docker.lambda_build_container import LambdaBuildContainer
from samcli.local.docker.utils import is_docker_reachable, get_docker_platform
from samcli.local.docker.manager import ContainerManager
from samcli.local.docker.manager import ContainerManager, DockerImagePullFailedException
from samcli.commands._utils.experimental import get_enabled_experimental_flags
from samcli.lib.build.exceptions import (
DockerConnectionError,
Expand Down Expand Up @@ -960,6 +961,10 @@ def _build_function_on_container(
# "/." is a Docker thing that instructions the copy command to download contents of the folder only
result_dir_in_container = response["result"]["artifacts_dir"] + "/."
container.copy(result_dir_in_container, artifacts_dir)

except DockerImagePullFailedException as ex:
raise BuildInsideContainerError(ex)
mndeveci marked this conversation as resolved.
Show resolved Hide resolved

finally:
self._container_manager.stop(container)

Expand Down
21 changes: 21 additions & 0 deletions tests/unit/lib/build_module/test_app_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from samcli.lib.utils.architecture import X86_64, ARM64
from samcli.lib.utils.packagetype import IMAGE, ZIP
from samcli.lib.utils.stream_writer import StreamWriter
from samcli.local.docker.manager import DockerImagePullFailedException
from tests.unit.lib.build_module.test_build_graph import generate_function


Expand Down Expand Up @@ -2724,6 +2725,26 @@ def test_must_raise_on_unsupported_container(self, LambdaBuildContainerMock):
self.assertEqual(str(ctx.exception), msg)
self.container_manager.stop.assert_called_with(container_mock)

@patch("samcli.lib.build.app_builder.LambdaBuildContainer")
def test_must_raise_on_image_not_found(self, LambdaBuildContainerMock):
config = Mock()

container_mock = LambdaBuildContainerMock.return_value = Mock()
container_mock.image = "image name"

self.container_manager.run.side_effect = DockerImagePullFailedException(
f"Could not find {container_mock.image} image locally and failed to pull it from docker."
)

with self.assertRaises(BuildInsideContainerError) as ctx:
self.builder._build_function_on_container(
config, "source_dir", "artifacts_dir", "scratch_dir", "manifest_path", "runtime", X86_64, {}
)

msg = f"Could not find {container_mock.image} image locally and failed to pull it from docker."

self.assertEqual(str(ctx.exception), msg)

def test_must_raise_on_docker_not_running(self):
config = Mock()

Expand Down
Loading