def delete_ecr_image(resources_info: Dict) -> None: """Delete the ECR repository created in _create_ecr_image function.""" ecr_cli = ECR(resources_info) repo_name = resources_info.get('lambda').get('name') if ecr_cli.get_repository_uri(repo_name): logger.info('Deleting ECR repo: %s' % repo_name) ecr_cli.delete_repository(repo_name)
def test_get_registry_url(self, boto_session): boto_session.return_value = self._init_mocks(['describe_registry']) ecr = ECR({}) ecr.client.client.describe_registry.return_value = { 'registryId': 'REG_ID' } self.assertEqual(ecr.get_registry_url(), "REG_ID.dkr.ecr.us-east-1.amazonaws.com")
def test_create_repository(self, boto_session): boto_session.return_value = self._init_mocks(['create_repository']) ecr = ECR({}) ecr.client.client.create_repository.return_value = { 'repository': { 'repositoryUri': 'URI' } } self.assertEqual(ecr.create_repository('repo_name'), 'URI') self.assertEqual(ecr.client.client.create_repository.call_args_list[0], call(repositoryName='repo_name'))
def test_get_authorization_token(self, boto_session): boto_session.return_value = self._init_mocks( ['get_authorization_token']) ecr = ECR({}) token = "QVdTOnRva2Vu" ecr.client.client.get_authorization_token.return_value = { 'authorizationData': [{ 'authorizationToken': token }] } self.assertEqual(ecr.get_authorization_token(), ["AWS", "token"])
def test_get_repository_uri(self, boto_session): boto_session.return_value = self._init_mocks(['describe_repositories']) ecr = ECR({}) ecr.client.client.describe_repositories.return_value = { 'repositories': [{ 'repositoryUri': 'URI' }] } self.assertEqual(ecr.get_repository_uri('repo_name'), 'URI') self.assertEqual( ecr.client.client.describe_repositories.call_args_list[0], call(repositoryNames=['repo_name']))
def create_ecr_image(resources_info: Dict, supervisor_version: str) -> str: """Creates an ECR image using the user provided image adding the supervisor tools.""" # If the user set an already prepared image return the image name image_name = ContainerImage._ecr_image_name_prepared( resources_info.get('lambda').get('container')) if image_name: return image_name tmp_folder = FileUtils.create_tmp_dir() # Create function config file FileUtils.write_yaml( FileUtils.join_paths(tmp_folder.name, "function_config.yaml"), create_function_config(resources_info)) init_script_path = resources_info.get('lambda').get('init_script') # Copy the init script defined by the user to the payload folder if init_script_path: FileUtils.copy_file( init_script_path, FileUtils.join_paths( tmp_folder.name, FileUtils.get_file_name(init_script_path))) # Get supervisor zip supervisor_zip_path = ContainerImage.get_supervisor_zip( resources_info, supervisor_version) # Unzip the supervisor file to the temp file FileUtils.unzip_folder(supervisor_zip_path, tmp_folder.name) # Create dockerfile to generate the new ECR image FileUtils.create_file_with_content( "%s/Dockerfile" % tmp_folder.name, ContainerImage._create_dockerfile_ecr_image( resources_info.get('lambda'))) # Create the ECR Repo and get the image uri ecr_cli = ECR(resources_info) repo_name = resources_info.get('lambda').get('name') ecr_image = ecr_cli.get_repository_uri(repo_name) if not ecr_image: logger.info('Creating ECR repository: %s' % repo_name) ecr_image = ecr_cli.create_repository(repo_name) # Build and push the image to the ECR repo platform = None arch = resources_info.get('lambda').get('architectures', ['x86_64'])[0] if arch == 'arm64': platform = 'linux/arm64' return ContainerImage._build_push_ecr_image( tmp_folder.name, ecr_image, platform, ecr_cli.get_authorization_token())
def test_delete_repository(self, boto_session): boto_session.return_value = self._init_mocks(['delete_repository']) ecr = ECR({}) ecr.delete_repository('repo_name') self.assertEqual(ecr.client.client.delete_repository.call_args_list[0], call(repositoryName='repo_name', force=True))
def test_init(self): ecr = ECR({}) self.assertEqual(type(ecr.client.client).__name__, "ECR")