def test_parse_response_plain_text(self, mock_parse_payload, mock_parse_base64_response_values, mock_parse_log_ids): mock_parse_payload.return_value = 'test payload' mock_parse_base64_response_values.return_value = 'test base 64' mock_parse_log_ids.return_value = { 'StatusCode': '42', 'Payload': 'test payload', 'LogGroupName': 'test log group', 'LogStreamName': 'test log stream', 'ResponseMetadata': { 'RequestId': '99' }, 'Extra': 'test_verbose' } AwsClient().parse_response('test_response', 'test_function', False, False, False) output = TestAwsClient.capturedOutput.getvalue() self.assertEquals(output, 'SCAR: Request Id: 99\ntest payload\n\n')
def test_create_trigger_from_bucket(self, mock_s3_client): AwsClient().create_trigger_from_bucket('test_bucket', 'arn:test2') self.assertEqual(mock_s3_client.call_count, 1) self.assertTrue(call().put_bucket_notification_configuration( Bucket='test_bucket', NotificationConfiguration={ 'LambdaFunctionConfigurations': [{ 'LambdaFunctionArn': 'arn:test2', 'Events': ['s3:ObjectCreated:*'], 'Filter': { 'Key': { 'FilterRules': [{ 'Value': 'input/', 'Name': 'prefix' }] } } }] }) in mock_s3_client.mock_calls)
def test_delete_cloudwatch_group_warning(self, mock_log_client): mock_log_client.side_effect = ClientError( { 'Error': { 'Code': 'ResourceNotFoundException', 'Message': 'test_message' } }, 'test2') result = Result() AwsClient().delete_cloudwatch_group('f1', result) self.assertEqual( result.plain_text, "Warning: Cannot delete log group '/aws/lambda/f1'. Group not found.\n" ) self.assertEqual( result.json, { 'Warning': "Cannot delete log group '/aws/lambda/f1'. Group not found." }) self.assertEqual( result.verbose, { 'Warning': "Cannot delete log group '/aws/lambda/f1'. Group not found." })
def test_parse_response_async_verbose(self, mock_parse_payload, mock_result): mock_parse_payload.return_value = { 'StatusCode': '42', 'Payload': 'test payload', 'ResponseMetadata': { 'RequestId': '99' }, 'Extra': 'test_verbose' } AwsClient().parse_response('test_response', 'test_function', True, False, True) self.assertEqual(mock_result.call_count, 1) self.assertTrue(call().append_to_verbose( 'LambdaOutput', { 'StatusCode': '42', 'Payload': 'test payload', 'ResponseMetadata': { 'RequestId': '99' }, 'Extra': 'test_verbose' }) in mock_result.mock_calls) self.assertTrue(call().print_results(json=False, verbose=True) in mock_result.mock_calls)
def test_update_function_memory(self, mock_lambda_client): AwsClient().update_function_memory('test', 256) self.assertEqual(mock_lambda_client.call_count, 1) self.assertTrue(call().update_function_configuration( FunctionName='test', MemorySize=256) in mock_lambda_client.mock_calls)
def test_update_function_timeout_error(self, mock_lambda_client): mock_lambda_client.side_effect = ClientError({'Error' : {'Code' : '42', 'Message' : 'test_message'}}, 'test2') AwsClient().update_function_timeout('test', 125) output = TestAwsClient.capturedOutput.getvalue() self.assertTrue('Error updating lambda function timeout:' in output) self.assertTrue('An error occurred (42) when calling the test2 operation: test_message' in output)
def test_check_function_name_exists(self, mock_find_function_name): mock_find_function_name.return_value = False AwsClient().check_function_name_exists('test', True)
def test_update_function_timeout(self, mock_lambda_client): AwsClient().update_function_timeout('test', 125) self.assertEqual(mock_lambda_client.call_count, 1) self.assertTrue(call().update_function_configuration( FunctionName='test', Timeout=125) in mock_lambda_client.mock_calls)
def test_create_trigger_from_bucket_error(self, mock_s3_client): mock_s3_client.side_effect = ClientError({'Error' : {'Code' : '42', 'Message' : 'test_message'}}, 'test2') AwsClient().create_trigger_from_bucket('test_bucket', 'arn:test2') output = TestAwsClient.capturedOutput.getvalue() self.assertTrue('Error configuring S3 bucket:' in output) self.assertTrue('An error occurred (42) when calling the test2 operation: test_message' in output)
def test_check_function_name_exists_json(self, mock_find_function_name): mock_find_function_name.return_value = True with self.assertRaises(SystemExit): AwsClient().check_function_name_exists('test', True)
def test_get_function_environment_variables(self, mock_lambda_client): mock_lambda_client.return_value.get_function.return_value = {'Configuration' : {'Environment' : 'test'}} result = AwsClient().get_function_environment_variables('test') self.assertEqual(mock_lambda_client.call_count, 1) self.assertTrue(call().get_function(FunctionName='test') in mock_lambda_client.mock_calls) self.assertEqual(result, 'test')
def test_check_time(self): self.assertEqual(1, AwsClient().check_time(1)) self.assertEqual(300, AwsClient().check_time(300)) self.assertEqual(147, AwsClient().check_time(147))
def test_delete_cloudwatch_group_error(self, mock_log_client): mock_log_client.side_effect = ClientError({'Error' : {'Code' : '42', 'Message' : 'test_message'}}, 'test2') AwsClient().delete_cloudwatch_group('f1', Result()) output = TestAwsClient.capturedOutput.getvalue() self.assertTrue("Error deleting the cloudwatch log:" in output) self.assertTrue('An error occurred (42) when calling the test2 operation: test_message' in output)
def test_invoke_function_read_timeout_error(self, mock_lambda_client): mock_lambda_client.side_effect = ReadTimeout() with self.assertRaises(SystemExit): AwsClient().invoke_function('f1', 'test_inv', 'test_log', 'test_payload') output = TestAwsClient.capturedOutput.getvalue() self.assertTrue("Timeout reading connection pool:" in output)
def test_get_functions_arn_list_error(self, mock_resource_groups_client): mock_resource_groups_client.side_effect = ClientError({'Error' : {'Code' : '42', 'Message' : 'test_message'}}, 'test2') AwsClient().get_functions_arn_list() output = TestAwsClient.capturedOutput.getvalue() self.assertTrue("Error getting function arn by tag:" in output) self.assertTrue('An error occurred (42) when calling the test2 operation: test_message' in output)
def test_check_and_create_s3_bucket(self, mock_s3_client, mock_s3_bucket_folders): mock_s3_client.return_value.list_buckets.return_value = {'Buckets' : [{'Name' : 'test1'}, {'Name' : 'test_bucket'}]} AwsClient().check_and_create_s3_bucket('test_bucket') self.assertEqual(mock_s3_bucket_folders.call_count, 2) self.assertTrue(call('test_bucket', 'input/') in mock_s3_bucket_folders.mock_calls) self.assertTrue(call('test_bucket', 'output/') in mock_s3_bucket_folders.mock_calls)
def test_add_lambda_permissions_error(self, mock_lambda_client): mock_lambda_client.side_effect = ClientError({'Error' : {'Code' : '42', 'Message' : 'test_message'}}, 'test2') AwsClient().add_lambda_permissions('test_bucket') output = TestAwsClient.capturedOutput.getvalue() self.assertTrue('Error setting lambda permissions:' in output) self.assertTrue('An error occurred (42) when calling the test2 operation: test_message' in output)
def test_check_memory(self): self.assertEqual(128, AwsClient().check_memory(128)) self.assertEqual(1536, AwsClient().check_memory(1536)) self.assertEqual(256, AwsClient().check_memory(237))
def test_get_s3_file_list_empty_list(self, mock_s3_client): mock_s3_client.return_value.list_objects_v2.return_value = {'Contents' : []} file_list = AwsClient().get_s3_file_list('test_bucket') self.assertEqual(mock_s3_client.call_count, 1) self.assertTrue(call().list_objects_v2(Bucket='test_bucket', Prefix='input/') in mock_s3_client.mock_calls) self.assertEqual(file_list, [])
def test_create_s3_bucket(self, mock_s3_client): AwsClient().create_s3_bucket('test_bucket') self.assertEqual(mock_s3_client.call_count, 1) self.assertTrue(call().create_bucket( ACL='private', Bucket='test_bucket') in mock_s3_client.mock_calls)
def test_get_s3(self, mock_client, mock_config): mock_config.return_value = 'config' AwsClient().get_s3() self.assertEqual(mock_client.call_count, 1) self.assertEqual(mock_client.call_args, call('s3', config='config', region_name='us-east-1'))
def test_get_user_name_or_id_error(self, mock_client): mock_client.side_effect = ClientError({'Error' : {'Code' : '42', 'Message' : 'user/testu bla'}}, 'test2') user = AwsClient().get_user_name_or_id() self.assertEqual(user, 'testu')
def test_update_function_env_variables_error(self, mock_lambda_client): mock_lambda_client.side_effect = ClientError({'Error' : {'Code' : '42', 'Message' : 'test_message'}}, 'test2') AwsClient().update_function_env_variables('test1', 'test2') output = TestAwsClient.capturedOutput.getvalue() self.assertTrue('Error updating the environment variables of the lambda function:' in output) self.assertTrue('An error occurred (42) when calling the test2 operation: test_message' in output)
def test_get_boto3_client(self, mock_client, mock_config): mock_config.return_value = 'config' AwsClient().get_boto3_client('test', 'test-region') self.assertEqual(mock_client.call_count, 1) self.assertEqual(mock_client.call_args, call('test', config='config', region_name='test-region'))
def test_get_resource_groups_tagging_api(self, mock_client, mock_config): mock_config.return_value = 'config' AwsClient().get_resource_groups_tagging_api() self.assertEqual(mock_client.call_count, 1) self.assertEqual(mock_client.call_args, call('resourcegroupstaggingapi', config='config', region_name='us-east-1'))