Skip to content

Commit

Permalink
fix: gracefully exit on not finding a local image
Browse files Browse the repository at this point in the history
- For `build` with `--use-container` and `--build-image`
  • Loading branch information
sriram-mv committed Oct 5, 2023
1 parent 861a5ab commit c216a61
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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)

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

0 comments on commit c216a61

Please sign in to comment.