| 
									
										
										
										
											2021-08-04 00:21:15 +09:00
										 |  |  | import copy | 
					
						
							|  |  |  | import boto3 | 
					
						
							|  |  |  | import json | 
					
						
							|  |  |  | from moto import mock_cloudformation, mock_ecr | 
					
						
							| 
									
										
										
										
											2023-07-17 10:21:32 +00:00
										 |  |  | from string import Template | 
					
						
							| 
									
										
										
										
											2021-08-04 00:21:15 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-13 09:49:43 +00:00
										 |  |  | from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID | 
					
						
							| 
									
										
										
										
											2021-08-04 00:21:15 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | repo_template = Template( | 
					
						
							|  |  |  |     json.dumps( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "AWSTemplateFormatVersion": "2010-09-09", | 
					
						
							|  |  |  |             "Description": "ECR Repo Test", | 
					
						
							|  |  |  |             "Resources": { | 
					
						
							|  |  |  |                 "Repo": { | 
					
						
							|  |  |  |                     "Type": "AWS::ECR::Repository", | 
					
						
							| 
									
										
										
										
											2022-03-10 13:39:59 -01:00
										 |  |  |                     "Properties": {"RepositoryName": "${repo_name}"}, | 
					
						
							| 
									
										
										
										
											2021-08-04 00:21:15 +09:00
										 |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             "Outputs": { | 
					
						
							|  |  |  |                 "Arn": { | 
					
						
							|  |  |  |                     "Description": "Repo Arn", | 
					
						
							|  |  |  |                     "Value": {"Fn::GetAtt": ["Repo", "Arn"]}, | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ecr | 
					
						
							|  |  |  | @mock_cloudformation | 
					
						
							|  |  |  | def test_create_repository(): | 
					
						
							|  |  |  |     # given | 
					
						
							|  |  |  |     cfn_client = boto3.client("cloudformation", region_name="eu-central-1") | 
					
						
							|  |  |  |     name = "test-repo" | 
					
						
							|  |  |  |     stack_name = "test-stack" | 
					
						
							|  |  |  |     template = repo_template.substitute({"repo_name": name}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # when | 
					
						
							|  |  |  |     cfn_client.create_stack(StackName=stack_name, TemplateBody=template) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # then | 
					
						
							|  |  |  |     repo_arn = f"arn:aws:ecr:eu-central-1:{ACCOUNT_ID}:repository/{name}" | 
					
						
							|  |  |  |     stack = cfn_client.describe_stacks(StackName=stack_name)["Stacks"][0] | 
					
						
							| 
									
										
										
										
											2023-07-17 10:21:32 +00:00
										 |  |  |     assert stack["Outputs"][0]["OutputValue"] == repo_arn | 
					
						
							| 
									
										
										
										
											2021-08-04 00:21:15 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ecr_client = boto3.client("ecr", region_name="eu-central-1") | 
					
						
							|  |  |  |     response = ecr_client.describe_repositories(repositoryNames=[name]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 10:21:32 +00:00
										 |  |  |     assert response["repositories"][0]["repositoryArn"] == repo_arn | 
					
						
							| 
									
										
										
										
											2021-08-04 00:21:15 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ecr | 
					
						
							|  |  |  | @mock_cloudformation | 
					
						
							|  |  |  | def test_update_repository(): | 
					
						
							|  |  |  |     # given | 
					
						
							|  |  |  |     cfn_client = boto3.client("cloudformation", region_name="eu-central-1") | 
					
						
							|  |  |  |     name = "test-repo" | 
					
						
							|  |  |  |     stack_name = "test-stack" | 
					
						
							|  |  |  |     template = repo_template.substitute({"repo_name": name}) | 
					
						
							|  |  |  |     cfn_client.create_stack(StackName=stack_name, TemplateBody=template) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template_update = copy.deepcopy(json.loads(template)) | 
					
						
							|  |  |  |     template_update["Resources"]["Repo"]["Properties"][ | 
					
						
							|  |  |  |         "ImageTagMutability" | 
					
						
							|  |  |  |     ] = "IMMUTABLE" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # when | 
					
						
							|  |  |  |     cfn_client.update_stack( | 
					
						
							|  |  |  |         StackName=stack_name, TemplateBody=json.dumps(template_update) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # then | 
					
						
							|  |  |  |     ecr_client = boto3.client("ecr", region_name="eu-central-1") | 
					
						
							|  |  |  |     response = ecr_client.describe_repositories(repositoryNames=[name]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     repo = response["repositories"][0] | 
					
						
							| 
									
										
										
										
											2023-07-17 10:21:32 +00:00
										 |  |  |     assert ( | 
					
						
							|  |  |  |         repo["repositoryArn"] | 
					
						
							|  |  |  |         == f"arn:aws:ecr:eu-central-1:{ACCOUNT_ID}:repository/{name}" | 
					
						
							| 
									
										
										
										
											2021-08-04 00:21:15 +09:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-07-17 10:21:32 +00:00
										 |  |  |     assert repo["imageTagMutability"] == "IMMUTABLE" | 
					
						
							| 
									
										
										
										
											2021-08-04 00:21:15 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ecr | 
					
						
							|  |  |  | @mock_cloudformation | 
					
						
							|  |  |  | def test_delete_repository(): | 
					
						
							|  |  |  |     # given | 
					
						
							|  |  |  |     cfn_client = boto3.client("cloudformation", region_name="eu-central-1") | 
					
						
							|  |  |  |     name = "test-repo" | 
					
						
							|  |  |  |     stack_name = "test-stack" | 
					
						
							|  |  |  |     template = repo_template.substitute({"repo_name": name}) | 
					
						
							|  |  |  |     cfn_client.create_stack(StackName=stack_name, TemplateBody=template) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # when | 
					
						
							|  |  |  |     cfn_client.delete_stack(StackName=stack_name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # then | 
					
						
							|  |  |  |     ecr_client = boto3.client("ecr", region_name="eu-central-1") | 
					
						
							|  |  |  |     response = ecr_client.describe_repositories()["repositories"] | 
					
						
							| 
									
										
										
										
											2023-07-17 10:21:32 +00:00
										 |  |  |     assert len(response) == 0 |