How to use delete_layer_version method in localstack

Best Python code snippet using localstack_python

test_index.py

Source:test_index.py Github

copy

Full Screen

1# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.2# SPDX-License-Identifier: Apache-2.03from unittest import TestCase4from unittest.mock import patch, MagicMock5from botocore.exceptions import ClientError6with patch("boto3.client") as boto_client_mock:7 with patch("gamekitresourcemanagement.cfn_custom_resource.send_success_response") as mock_send_success_response:8 with patch('gamekitresourcemanagement.cfn_custom_resource.send_failure_response') as mock_send_failure_response:9 from functions.main.RemoveLambdaLayersOnDelete import index10LAYER_1 = 'LambdaLayer1'11LAYER_2 = 'LambdaLayer2'12UNMATCHED_LAYER = 'UnmatchedLambdaLayer'13LAYER_VERSIONS_1 = [1, 2, 3]14LAYER_VERSIONS_2 = [1]15NEXT_MARKER = 'NextMarker'16LAYER_PREFIX = 'LambdaLayer'17PHYSICAL_RESOURCE_ID = 'delete-lambda-layers-some-hash-here'18STACK_ID = 'test_stack_id'19LOGICAL_RESOURCE_ID = 'test_logical_resource_id'20REQUEST_ID = 'test_request_id'21RESPONSE_URL = 'https://website.tld/some_url'22class TestIndex(TestCase):23 def setUp(self):24 index.lambda_client = MagicMock()25 mock_send_success_response.reset_mock()26 mock_send_failure_response.reset_mock()27 def test_removes_all_matching_lambda_layers_no_pagination(self):28 # Arrange29 event = self.get_event()30 context = self.get_context()31 index.lambda_client.list_layers.return_value = self.get_list_layers_response(32 [LAYER_1, LAYER_2, UNMATCHED_LAYER])33 index.lambda_client.list_layer_versions.side_effect = [34 self.get_list_layer_versions_response(LAYER_VERSIONS_1),35 self.get_list_layer_versions_response(LAYER_VERSIONS_2)36 ]37 # Act38 index.lambda_handler(event, context)39 # Assert40 index.lambda_client.list_layers.assert_called_once()41 self.assertEqual(index.lambda_client.list_layer_versions.call_count, 2)42 self.assertEqual(index.lambda_client.delete_layer_version.call_count, 4)43 mock_send_success_response.assert_called_once()44 mock_send_failure_response.assert_not_called()45 def test_removes_all_matching_lambda_layers_pagination(self):46 # Arrange47 event = self.get_event()48 context = self.get_context()49 index.lambda_client.list_layers.side_effect = [50 self.get_list_layers_response([LAYER_1], NEXT_MARKER),51 self.get_list_layers_response([LAYER_2, UNMATCHED_LAYER])52 ]53 # Paginate layer versions request for the first layer54 index.lambda_client.list_layer_versions.side_effect = [55 self.get_list_layer_versions_response(LAYER_VERSIONS_1[0:2], NEXT_MARKER),56 self.get_list_layer_versions_response(LAYER_VERSIONS_1[2:]),57 self.get_list_layer_versions_response(LAYER_VERSIONS_2)58 ]59 # Act60 index.lambda_handler(event, context)61 # Assert62 self.assertEqual(index.lambda_client.list_layers.call_count, 2)63 self.assertEqual(index.lambda_client.list_layer_versions.call_count, 3)64 self.assertEqual(index.lambda_client.delete_layer_version.call_count, 4)65 mock_send_success_response.assert_called_once()66 mock_send_failure_response.assert_not_called()67 def test_deletes_nothing_if_no_layers_exist(self):68 # Arrange69 event = self.get_event()70 context = self.get_context()71 index.lambda_client.list_layers.return_value = self.get_list_layers_response([])72 # Act73 index.lambda_handler(event, context)74 # Assert75 index.lambda_client.list_layers.assert_called_once()76 index.lambda_client.list_layer_versions.assert_not_called()77 index.lambda_client.delete_layer_version.assert_not_called()78 mock_send_success_response.assert_called_once()79 mock_send_failure_response.assert_not_called()80 def test_sends_failure_on_list_layers_error(self):81 # Arrange82 event = self.get_event()83 context = self.get_context()84 index.lambda_client.list_layers.side_effect = ClientError({85 'Error': {86 'Code': 500,87 'Message': 'Some Lambda Error'88 }89 }, 'ListLayers')90 # Act91 with self.assertRaises(ClientError):92 index.lambda_handler(event, context)93 # Assert94 index.lambda_client.list_layers.assert_called_once()95 index.lambda_client.list_layer_versions.assert_not_called()96 index.lambda_client.delete_layer_version.assert_not_called()97 mock_send_success_response.assert_not_called()98 mock_send_failure_response.assert_called_once()99 def test_sends_failure_on_list_layer_versions_error(self):100 # Arrange101 event = self.get_event()102 context = self.get_context()103 index.lambda_client.list_layers.return_value = self.get_list_layers_response([LAYER_1])104 index.lambda_client.list_layer_versions.side_effect = ClientError({105 'Error': {106 'Code': 500,107 'Message': 'Some Lambda Error'108 }109 }, 'ListLayerVersions')110 # Act111 with self.assertRaises(ClientError):112 index.lambda_handler(event, context)113 # Assert114 index.lambda_client.list_layers.assert_called_once()115 index.lambda_client.list_layer_versions.assert_called_once()116 index.lambda_client.delete_layer_version.assert_not_called()117 mock_send_success_response.assert_not_called()118 mock_send_failure_response.assert_called_once()119 def test_sends_failure_on_delete_layer_version_error(self):120 # Arrange121 event = self.get_event()122 context = self.get_context()123 index.lambda_client.list_layers.return_value = self.get_list_layers_response([LAYER_1])124 index.lambda_client.list_layer_versions.return_value = self.get_list_layer_versions_response([LAYER_VERSIONS_1])125 index.lambda_client.delete_layer_version.side_effect = ClientError({126 'Error': {127 'Code': 500,128 'Message': 'Some Lambda Error'129 }130 }, 'DeleteLayerVersion')131 # Act132 with self.assertRaises(ClientError):133 index.lambda_handler(event, context)134 # Assert135 index.lambda_client.list_layers.assert_called_once()136 index.lambda_client.list_layer_versions.assert_called_once()137 index.lambda_client.delete_layer_version.assert_called_once()138 mock_send_success_response.assert_not_called()139 mock_send_failure_response.assert_called_once()140 def test_ignores_non_delete_events(self):141 # Arrange142 event = self.get_event()143 event['RequestType'] = 'Create'144 context = self.get_context()145 # Act146 index.lambda_handler(event, context)147 # Assert148 index.lambda_client.list_layers.assert_not_called()149 index.lambda_client.list_layer_versions.assert_not_called()150 index.lambda_client.delete_layer_versions.assert_not_called()151 mock_send_success_response.assert_called_once()152 mock_send_failure_response.assert_not_called()153 @staticmethod154 def get_list_layers_response(layers: [str], next_marker: str = None):155 return {156 'Layers': [{'LayerName': layer} for layer in layers],157 'NextMarker': next_marker158 }159 @staticmethod160 def get_list_layer_versions_response(layer_versions: [int], next_marker: str = None):161 return {162 'LayerVersions': [{'Version': layer_version} for layer_version in layer_versions],163 'NextMarker': next_marker164 }165 @staticmethod166 def get_event():167 return {168 'RequestType': 'Delete',169 'ResourceProperties': {170 'layer_prefix': LAYER_PREFIX171 },172 'PhysicalResourceId': PHYSICAL_RESOURCE_ID,173 'StackId': STACK_ID,174 'LogicalResourceId': LOGICAL_RESOURCE_ID,175 'RequestId': REQUEST_ID,176 'ResponseURL': RESPONSE_URL177 }178 @staticmethod179 def get_context():180 context = MagicMock()...

Full Screen

Full Screen

test_basic_layer_version.py

Source:test_basic_layer_version.py Github

copy

Full Screen

...27 license = outputs["License"]28 layer_name = outputs["LayerName"]29 description = outputs["Description"]30 layer_version_result = self.client_provider.lambda_client.get_layer_version_by_arn(Arn=layer_arn)31 self.client_provider.lambda_client.delete_layer_version(32 LayerName=layer_name, VersionNumber=layer_version_result["Version"]33 )34 self.assertEqual(layer_version_result["LicenseInfo"], license)35 self.assertEqual(layer_version_result["Description"], description)36 @skipIf(current_region_does_not_support([ARM]), "ARM is not supported in this testing region")37 def test_basic_layer_with_architecture(self):38 """39 Creates a basic lambda layer version specifying compatible architecture40 """41 self.create_and_verify_stack("single/basic_layer_with_compatible_architecture")42 outputs = self.get_stack_outputs()43 layer_arn = outputs["MyLayerArn"]44 layer_name = outputs["LayerName"]45 layer_version_result = self.client_provider.lambda_client.get_layer_version_by_arn(Arn=layer_arn)46 self.client_provider.lambda_client.delete_layer_version(47 LayerName=layer_name, VersionNumber=layer_version_result["Version"]48 )...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful