Best Python code snippet using localstack_python
test_delete.py
Source:test_delete.py
1import mock2import unittest3import botocore4from slam import cli5from .test_deploy import config, describe_stacks_response6class DeleteTests(unittest.TestCase):7 @mock.patch('slam.cli.boto3.client')8 @mock.patch('slam.cli._load_config', return_value=config)9 def test_delete(self, _load_config, client):10 mock_s3 = mock.MagicMock()11 mock_logs = mock.MagicMock()12 mock_cfn = mock.MagicMock()13 mock_cfn.describe_stacks.return_value = describe_stacks_response14 client.side_effect = [mock_s3, mock_cfn, mock_logs]15 cli.main(['delete'])16 mock_cfn.describe_stacks.assert_called_once_with(StackName='foo')17 mock_cfn.delete_stack.assert_called_once_with(StackName='foo')18 mock_cfn.get_waiter.assert_called_once_with('stack_delete_complete')19 mock_cfn.get_waiter().wait.assert_called_once_with(StackName='foo')20 mock_logs.delete_log_group.assert_any_call(21 logGroupName='/aws/lambda/foo')22 mock_logs.delete_log_group.assert_any_call(23 logGroupName='API-Gateway-Execution-Logs_123abc/dev')24 mock_logs.delete_log_group.assert_any_call(25 logGroupName='API-Gateway-Execution-Logs_123abc/staging')26 mock_logs.delete_log_group.assert_any_call(27 logGroupName='API-Gateway-Execution-Logs_123abc/prod')28 mock_s3.delete_object.assert_called_once_with(Bucket='bucket',29 Key='lambda-old.zip')30 mock_s3.delete_bucket(Bucket='bucket')31 @mock.patch('slam.cli.boto3.client')32 @mock.patch('slam.cli._load_config', return_value=config)33 def test_delete_no_logs(self, _load_config, client):34 mock_s3 = mock.MagicMock()35 mock_logs = mock.MagicMock()36 mock_cfn = mock.MagicMock()37 mock_cfn.describe_stacks.return_value = describe_stacks_response38 client.side_effect = [mock_s3, mock_cfn, mock_logs]39 cli.main(['delete', '--no-logs'])40 mock_cfn.describe_stacks.assert_called_once_with(StackName='foo')41 mock_cfn.delete_stack.assert_called_once_with(StackName='foo')42 mock_cfn.get_waiter.assert_called_once_with('stack_delete_complete')43 mock_cfn.get_waiter().wait.assert_called_once_with(StackName='foo')44 mock_logs.delete_log_group.assert_not_called()45 mock_s3.delete_object.assert_called_once_with(Bucket='bucket',46 Key='lambda-old.zip')47 mock_s3.delete_bucket(Bucket='bucket')48 @mock.patch('slam.cli.boto3.client')49 @mock.patch('slam.cli._load_config', return_value=config)50 def test_delete_no_api_gateway(self, _load_config, client):51 mock_s3 = mock.MagicMock()52 mock_logs = mock.MagicMock()53 mock_cfn = mock.MagicMock()54 mock_cfn.describe_stacks.return_value = {'Stacks': [{55 'Parameters': [56 {'ParameterKey': 'LambdaS3Bucket', 'ParameterValue': 'bucket'},57 {'ParameterKey': 'LambdaS3Key',58 'ParameterValue': 'lambda-old.zip'},59 ],60 'Outputs': [61 {'OutputKey': 'FunctionArn', 'OutputValue': 'arn:lambda:foo'},62 ]63 }]}64 client.side_effect = [mock_s3, mock_cfn, mock_logs]65 cli.main(['delete'])66 mock_cfn.describe_stacks.assert_called_once_with(StackName='foo')67 mock_cfn.delete_stack.assert_called_once_with(StackName='foo')68 mock_cfn.get_waiter.assert_called_once_with('stack_delete_complete')69 mock_cfn.get_waiter().wait.assert_called_once_with(StackName='foo')70 mock_logs.delete_log_group.assert_called_once_with(71 logGroupName='/aws/lambda/foo')72 mock_s3.delete_object.assert_called_once_with(Bucket='bucket',73 Key='lambda-old.zip')74 mock_s3.delete_bucket(Bucket='bucket')75 @mock.patch('slam.cli.boto3.client')76 @mock.patch('slam.cli._load_config', return_value=config)77 def test_delete_not_deployed(self, _load_config, client):78 mock_s3 = mock.MagicMock()79 mock_logs = mock.MagicMock()80 mock_cfn = mock.MagicMock()81 mock_cfn.describe_stacks.side_effect = \82 botocore.exceptions.ClientError({'Error': {}}, 'operation')83 client.side_effect = [mock_s3, mock_cfn, mock_logs]84 self.assertRaises(RuntimeError, cli.main, ['delete'])85 @mock.patch('slam.cli.boto3.client')86 @mock.patch('slam.cli._load_config', return_value=config)87 def test_delete_fail_log_group(self, _load_config, client):88 mock_s3 = mock.MagicMock()89 mock_logs = mock.MagicMock()90 mock_cfn = mock.MagicMock()91 mock_cfn.describe_stacks.return_value = describe_stacks_response92 mock_logs.delete_log_group.side_effect = \93 botocore.exceptions.ClientError({'Error': {}}, 'operation')94 client.side_effect = [mock_s3, mock_cfn, mock_logs]95 cli.main(['delete']) # should still work in spite of error96 @mock.patch('slam.cli.boto3.client')97 @mock.patch('slam.cli._load_config', return_value=config)98 def test_delete_fail_s3_file(self, _load_config, client):99 mock_s3 = mock.MagicMock()100 mock_logs = mock.MagicMock()101 mock_cfn = mock.MagicMock()102 mock_cfn.describe_stacks.return_value = describe_stacks_response103 mock_s3.delete_object.side_effect = \104 botocore.exceptions.ClientError({'Error': {}}, 'operation')105 client.side_effect = [mock_s3, mock_cfn, mock_logs]106 cli.main(['delete']) # should still work in spite of error107 @mock.patch('slam.cli.boto3.client')108 @mock.patch('slam.cli._load_config', return_value=config)109 def test_delete_fail_s3_bucket(self, _load_config, client):110 mock_s3 = mock.MagicMock()111 mock_logs = mock.MagicMock()112 mock_cfn = mock.MagicMock()113 mock_cfn.describe_stacks.return_value = describe_stacks_response114 mock_s3.delete_bucket.side_effect = \115 botocore.exceptions.ClientError({'Error': {}}, 'operation')116 client.side_effect = [mock_s3, mock_cfn, mock_logs]...
delete_lambda_log_groups.py
Source:delete_lambda_log_groups.py
1import boto32boto3.setup_default_session(profile_name='ocp-at')3client = boto3.client('logs')4def delete_log_group(logGroupNamePrefix: str):5 paginator = client.get_paginator('describe_log_groups')6 for page in paginator.paginate(logGroupNamePrefix=logGroupNamePrefix):7 for group in page['logGroups']:8 client.delete_log_group(logGroupName=group['logGroupName'])9def test_delete_lambda_loggroups():10 delete_log_group('describe_log_groups')11def test_delete_api_gateway_execution_logs():...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!