Skip to content

Commit

Permalink
Take a Swing at Integration Tests
Browse files Browse the repository at this point in the history
Also, genericize unit tests a little.

See: aws#6909
  • Loading branch information
chrisoverzero committed Apr 11, 2024
1 parent a83de54 commit 4c3f4b8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 9 deletions.
2 changes: 1 addition & 1 deletion samcli/lib/package/ecr_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def upload(self, image, resource_name):
self.login()
self.login_session_active = True

# Sometimes the `resource_name`` is used as the `image` parameter to `tag_translation`.
# Sometimes the `resource_name` is used as the `image` parameter to `tag_translation`.
# This is because these two cases (directly from an archive or by ID) are effectively
# anonymous, so the best identifier available in scope is the resource name.
try:
Expand Down
43 changes: 43 additions & 0 deletions tests/integration/buildcmd/test_build_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,49 @@ def test_with_invalid_dockerfile_definition(self):
self.assertIn("COPY requires at least two arguments", command_result.stderr.decode())


@skipIf(SKIP_DOCKER_TESTS, SKIP_DOCKER_MESSAGE)
class TestLoadingImagesFromArchive(BuildIntegBase):
template = "template_loadable_image.yaml"

FUNCTION_LOGICAL_ID = "ImageFunction"

def test_load_success(self):
overrides = {"ImageUri": "./load_image_archive/archive.tar.gz"}
cmdlist = self.get_command_list(parameter_overrides=overrides)
command_result = run_command(cmdlist, cwd=self.working_dir)

self.assertEqual(command_result.process.returncode, 0)
self._verify_image_build_artifact(
self.built_template,
self.FUNCTION_LOGICAL_ID,
"ImageUri",
"sha256:46bd05c4a04f3d121198e054da02daed22d0f561764acb0f0594066d5972619b",
)

def test_load_not_an_archive_passthrough(self):
imageuri = "repository:tag"

overrides = {"ImageUri": imageuri}
cmdlist = self.get_command_list(parameter_overrides=overrides)
command_result = run_command(cmdlist, cwd=self.working_dir)

self.assertEqual(command_result.process.returncode, 0)
self._verify_image_build_artifact(
self.built_template,
self.FUNCTION_LOGICAL_ID,
"ImageUri",
imageuri,
)

def test_bad_image_archive(self):
overrides = {"ImageUri": "./load_image_archive/error.tar.gz"}
cmdlist = self.get_command_list(parameter_overrides=overrides)
command_result = run_command(cmdlist, cwd=self.working_dir)

self.assertEqual(command_result.process.returncode, 1)
self.assertIn("unexpected EOF", command_result.stderr.decode())


@skipIf(
# Hits public ECR pull limitation, move it to canary tests
(not RUN_BY_CANARY and not CI_OVERRIDE),
Expand Down
Binary file not shown.
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/integration/testdata/buildcmd/template_loadable_image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Parameters:
ImageUri:
Type: String

Resources:

ImageFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
ImageUri: !Ref ImageUri
Timeout: 600
10 changes: 6 additions & 4 deletions tests/unit/lib/build_module/test_app_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import docker
import json

from uuid import uuid4

from unittest import TestCase
from unittest.mock import Mock, MagicMock, call, mock_open, patch, ANY
from pathlib import Path, WindowsPath
Expand Down Expand Up @@ -1748,7 +1750,7 @@ def setUp(self):

@patch("builtins.open", new_callable=mock_open)
def test_loads_image_archive(self, mock_open):
id = "sha256:1a2b3c4d5e6f"
id = f"sha256:{uuid4().hex}"

self.docker_client_mock.images.load.return_value = [Mock(id=id)]

Expand All @@ -1758,8 +1760,8 @@ def test_loads_image_archive(self, mock_open):
@patch("builtins.open", new_callable=mock_open)
def test_archive_must_represent_a_single_image(self, mock_open):
self.docker_client_mock.images.load.return_value = [
Mock(id="sha256:1a2b3c4d5e6f"),
Mock(id="sha256:1f2e3d4c5b6a"),
Mock(id=f"sha256:{uuid4().hex}"),
Mock(id=f"sha256:{uuid4().hex}"),
]

with self.assertRaises(DockerBuildFailed) as ex:
Expand Down Expand Up @@ -2602,7 +2604,7 @@ def test_must_build_in_container_with_custom_default_build_image(self, osutils_m
@patch.object(Path, "is_file", return_value=True)
@patch("builtins.open", new_callable=mock_open)
def test_loads_if_path_exists(self, mock_open, mock_is_file, architecture):
id = "sha256:1a2b3c4d5e6f"
id = f"sha256:{uuid4().hex}"
function_name = "function_name"
imageuri = str(Path("./path/to/archive.tar.gz"))

Expand Down
11 changes: 7 additions & 4 deletions tests/unit/lib/package/test_ecr_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from unittest.mock import MagicMock, Mock, call, mock_open, patch

from pathlib import Path
from uuid import uuid4

from botocore.exceptions import ClientError
from docker.errors import APIError, BuildError
from parameterized import parameterized
Expand All @@ -17,6 +19,7 @@
DeleteArtifactFailedError,
)
from samcli.lib.package.ecr_uploader import ECRUploader
from samcli.lib.package.image_utils import SHA_CHECKSUM_TRUNCATION_LENGTH
from samcli.lib.utils.stream_writer import StreamWriter


Expand Down Expand Up @@ -201,7 +204,7 @@ def test_upload_failure_while_streaming(self):
@patch("builtins.open", new_callable=mock_open)
def test_upload_from_image_archive(self, mock_open, mock_is_file):
resource_name = "HelloWorldFunction"
digest = "1a2b3c4d5e6f"
digest = uuid4().hex
id = f"sha256:{digest}"
image = "./path/to/archive.tar.gz"

Expand Down Expand Up @@ -232,12 +235,12 @@ def test_upload_from_image_archive(self, mock_open, mock_is_file):
)
ecr_uploader.login = MagicMock()
tag = ecr_uploader.upload(image, resource_name=resource_name)
self.assertEqual(f"{self.ecr_repo}:{resource_name}-{digest}-{self.tag}", tag)
self.assertEqual(f"{self.ecr_repo}:{resource_name}-{digest[:SHA_CHECKSUM_TRUNCATION_LENGTH]}-{self.tag}", tag)

@patch.object(Path, "is_file", return_value=False)
def test_upload_from_digest(self, mock_is_file):
resource_name = "HelloWorldFunction"
digest = "1a2b3c4d5e6f"
digest = uuid4().hex
id = f"sha256:{digest}"
image = id

Expand Down Expand Up @@ -268,7 +271,7 @@ def test_upload_from_digest(self, mock_is_file):
)
ecr_uploader.login = MagicMock()
tag = ecr_uploader.upload(image, resource_name=resource_name)
self.assertEqual(f"{self.ecr_repo}:{resource_name}-{digest}-{self.tag}", tag)
self.assertEqual(f"{self.ecr_repo}:{resource_name}-{digest[:SHA_CHECKSUM_TRUNCATION_LENGTH]}-{self.tag}", tag)

@patch.object(Path, "is_file", return_value=True)
@patch("builtins.open", new_callable=mock_open)
Expand Down

0 comments on commit 4c3f4b8

Please sign in to comment.