def test_ensure_image_in_ecr_with_image_not_in_ecr_built_with_custom_dockerfile(self, mock_create_ecr_client, mock_subprocess): mock_ecr_client = MagicMock() mock_create_ecr_client.return_value = mock_ecr_client mock_ecr_client.batch_get_image.side_effect = [ {'images': []}, {'images': [{'imageManifest': 'manifest-01'}]}, ] mock_ecr_client.get_authorization_token.return_value = { 'authorizationData': [ {'authorizationToken': 'dXNlcjp0b2tlbgo=', 'proxyEndpoint': 'http://proxy'} ] } mock_subprocess.check_output.side_effect = _mock_git_calls ecr = ECR("aws-region", "target-repo", "acc-id", dockerfile='CustomDockerFile') ecr.ensure_image_in_ecr() mock_subprocess.check_call.assert_has_calls([ call(['docker', 'tag', 'target-repo:v1-CustomDockerFile', 'acc-id.dkr.ecr.aws-region.amazonaws.com/target-repo:v1-CustomDockerFile']), ]) self.assertEqual('acc-id.dkr.ecr.aws-region.amazonaws.com/target-repo:v1-CustomDockerFile', ecr.image_uri) mock_ecr_client.put_image.assert_called_with( imageManifest='manifest-01', imageTag='v1-CustomDockerFile', repositoryName='target-repo', )
def test_ensure_image_in_ecr_for_explicit_version(self, mock_create_ecr_client, mock_subprocess): mock_ecr_client = MagicMock() mock_create_ecr_client.return_value = mock_ecr_client mock_ecr_client.batch_get_image.return_value = {'images': 'image-01'} def mock_check_output(cmd): if " ".join(cmd) == "git rev-list -n 1 HEAD": mock = MagicMock() mock.strip.return_value.decode.return_value = Exception('mocked error') return mock if " ".join(cmd) == 'git show -s --format="%ct" HEAD': mock = MagicMock() mock.strip.return_value.decode.return_value = '"1602236172"' return mock mock = MagicMock() mock.decode.return_value = None return mock mock_subprocess.check_output.side_effect = mock_check_output ecr = ECR("aws-region", "target-repo", "acc-id", version="v1") ecr.ensure_image_in_ecr() mock_ecr_client.batch_get_image.assert_called_with( imageIds=[{'imageTag': 'v1'}], repositoryName='target-repo', )
def test_ensure_image_in_ecr_for_derived_version_with_image_in_ecr(self, mock_create_ecr_client, mock_subprocess, mock_log_intent): mock_ecr_client = MagicMock() mock_create_ecr_client.return_value = mock_ecr_client mock_ecr_client.batch_get_image.return_value = {'images': [{'imageManifest': 'manifest-01'}]} mock_subprocess.check_output.side_effect = _mock_git_calls ecr = ECR("aws-region", "target-repo", "acc-id") ecr.ensure_image_in_ecr() mock_log_intent.assert_has_calls([call('Image found in ECR')]) mock_ecr_client.put_image.assert_has_calls([ call(imageManifest='manifest-01', imageTag='v1', repositoryName='target-repo'), call(imageManifest='manifest-01', imageTag='v1-1602236172', repositoryName='target-repo'), ])