def test_plugins_connection_file_transport_command(self, boto_client,
                                                    s_check_output):
     pc = PlayContext()
     new_stdin = StringIO()
     conn = connection_loader.get('community.aws.aws_ssm', pc, new_stdin)
     conn.get_option = MagicMock()
     conn.get_option.side_effect = ['1', '2', '3', '4', '5']
     conn._get_url = MagicMock()
     conn._get_url.side_effect = ['url1', 'url2']
     boto3 = MagicMock()
     boto3.client('s3').return_value = MagicMock()
     conn.get_option.return_value = 1
     ssm_action = 'get'
     get_command = MagicMock()
     put_command = MagicMock()
     conn.exec_command = MagicMock()
     conn.exec_command.return_value = (put_command, None, False)
     conn.download_fileobj = MagicMock()
     (returncode, stdout, stderr) = conn.exec_command(put_command,
                                                      in_data=None,
                                                      sudoable=False)
     returncode = 0
     (returncode, stdout, stderr) = conn.exec_command(get_command,
                                                      in_data=None,
                                                      sudoable=False)
 def test_plugins_connection_aws_ssm_fetch_file(self):
     pc = PlayContext()
     new_stdin = StringIO()
     conn = connection_loader.get('community.aws.aws_ssm', pc, new_stdin)
     conn._connect = MagicMock()
     conn._file_transport_command = MagicMock()
     conn._file_transport_command.return_value = (0, 'stdout', 'stderr')
     res, stdout, stderr = conn.fetch_file('/in/file', '/out/file')
 def test_plugins_connection_aws_ssm_get_url(self, boto):
     pc = PlayContext()
     new_stdin = StringIO()
     conn = connection_loader.get('community.aws.aws_ssm', pc, new_stdin)
     boto3 = MagicMock()
     boto3.client('s3').return_value = MagicMock()
     boto3.generate_presigned_url.return_value = MagicMock()
     return (boto3.generate_presigned_url.return_value)
 def test_plugins_connection_aws_ssm_flush_stderr(self, s_popen):
     pc = PlayContext()
     new_stdin = StringIO()
     conn = connection_loader.get('community.aws.aws_ssm', pc, new_stdin)
     conn.poll_stderr = MagicMock()
     conn.poll_stderr.register = MagicMock()
     conn.stderr = None
     s_popen.poll().return_value = 123
     return(conn.stderr)
 def test_plugins_connection_aws_ssm_post_process(self):
     pc = PlayContext()
     new_stdin = StringIO()
     conn = connection_loader.get('community.aws.aws_ssm', pc, new_stdin)
     conn.is_windows = MagicMock()
     conn.is_windows.return_value = True
     success = 3
     fail = 2
     conn.stdout = MagicMock()
     returncode = 0
     return(returncode, conn.stdout)
 def test_plugins_connection_aws_ssm_wrap_command(self):
     pc = PlayContext()
     new_stdin = StringIO()
     conn = connection_loader.get('community.aws.aws_ssm', pc, new_stdin)
     conn.is_windows = MagicMock()
     conn.is_windows.return_value = True
     return('windows1')
Exemple #7
0
 def test_current_config(self):
     api_config = {
         'Id': 'test-id',
         'LambdaFunctionArn': 'test-arn',
         'Events': [],
         'Filter': {
             'Key': {
                 'FilterRules': [{
                     'Name': 'Prefix',
                     'Value': ''
                 }, {
                     'Name': 'Suffix',
                     'Value': ''
                 }]
             }
         }
     }
     client = MagicMock()
     client.get_bucket_notification_configuration.return_value = {
         'LambdaFunctionConfigurations': [api_config]
     }
     bucket = AmazonBucket(self.module, client)
     current = bucket.current_config('test-id')
     assert current.raw == api_config
     assert client.get_bucket_notification_configuration.call_count == 1
def make_mock_no_connection_connection(config):
    """return a mock of ansible's boto3_conn ready to return a mock AWS API client"""
    lambda_client_double = MagicMock()
    lambda_client_double.get_function.configure_mock(return_value=False)
    lambda_client_double.update_function_configuration.configure_mock(
        return_value={'Version': 1})
    fake_boto3_conn = Mock(return_value=lambda_client_double)
    return (fake_boto3_conn, lambda_client_double)
Exemple #9
0
 def test_current_config_empty(self):
     client = MagicMock()
     client.get_bucket_notification_configuration.return_value = {
         'LambdaFunctionConfigurations': []
     }
     bucket = AmazonBucket(self.module, client)
     current = bucket.current_config('test-id')
     assert current is None
     assert client.get_bucket_notification_configuration.call_count == 1
Exemple #10
0
    def test_apply_config_add_event(self):
        api_config = {
            'Id': 'test-id',
            'LambdaFunctionArn': 'test-arn',
            'Events': ['s3:ObjectRemoved:*'],
            'Filter': {
                'Key': {
                    'FilterRules': [{
                        'Name': 'Prefix',
                        'Value': ''
                    }, {
                        'Name': 'Suffix',
                        'Value': ''
                    }]
                }
            }
        }
        client = MagicMock()
        client.get_bucket_notification_configuration.return_value = {
            'LambdaFunctionConfigurations': [api_config]
        }

        bucket = AmazonBucket(self.module, client)
        config = Config.from_params(
            **{
                'event_name': 'test-id',
                'lambda_function_arn': 'test-arn',
                'lambda_version': 1,
                'events': ['s3:ObjectRemoved:*', 's3:ObjectCreated:*'],
                'prefix': '',
                'suffix': ''
            })
        bucket.apply_config(config)
        assert client.get_bucket_notification_configuration.call_count == 1
        assert client.put_bucket_notification_configuration.call_count == 1
        client.put_bucket_notification_configuration.assert_called_with(
            Bucket='test-bucket',
            NotificationConfiguration={
                'LambdaFunctionConfigurations': [{
                    'Id':
                    'test-id',
                    'LambdaFunctionArn':
                    'test-arn:1',
                    'Events': ['s3:ObjectCreated:*', 's3:ObjectRemoved:*'],
                    'Filter': {
                        'Key': {
                            'FilterRules': [{
                                'Name': 'Prefix',
                                'Value': ''
                            }, {
                                'Name': 'Suffix',
                                'Value': ''
                            }]
                        }
                    }
                }]
            })
def test_manage_state_removes_unwanted_permissions():
    lambda_client_double = MagicMock()
    # Policy actually: present  Requested State: not present  Should: remove
    lambda_client_double.get_policy.return_value = fake_policy_return
    fake_module_params = copy.deepcopy(fake_module_params_absent)
    module_double.params = fake_module_params
    lambda_policy.manage_state(module_double, lambda_client_double)
    assert lambda_client_double.get_policy.call_count > 0
    lambda_client_double.add_permission.assert_not_called()
    assert lambda_client_double.remove_permission.call_count > 0
def test_manage_state_updates_nonmatching_permissions():
    lambda_client_double = MagicMock()
    # Policy actually: present   Requested State: present  Should: do nothing
    lambda_client_double.get_policy.return_value = fake_policy_return
    fake_module_params = copy.deepcopy(fake_module_params_different)
    module_double.params = fake_module_params
    lambda_policy.manage_state(module_double, lambda_client_double)
    assert lambda_client_double.get_policy.call_count > 0
    assert lambda_client_double.add_permission.call_count > 0
    assert lambda_client_double.remove_permission.call_count > 0
def test_manage_state_leaves_already_removed_permissions():
    lambda_client_double = MagicMock()
    # Policy actually: absent   Requested State: absent  Should: do nothing
    lambda_client_double.get_policy.side_effect = ClientError(
        error_response, operation_name)
    fake_module_params = copy.deepcopy(fake_module_params_absent)
    module_double.params = fake_module_params
    lambda_policy.manage_state(module_double, lambda_client_double)
    assert lambda_client_double.get_policy.call_count > 0
    lambda_client_double.add_permission.assert_not_called()
    lambda_client_double.remove_permission.assert_not_called()
Exemple #14
0
 def test_plugins_connection_aws_ssm_close(self, s_check_output):
     pc = PlayContext()
     new_stdin = StringIO()
     conn = connection_loader.get('community.aws.aws_ssm', pc, new_stdin)
     conn.instance_id = "i-12345"
     conn._session_id = True
     conn.get_option = MagicMock()
     conn.get_option.side_effect = ["/abc", "pqr"]
     conn._session = MagicMock()
     conn._session.terminate = MagicMock()
     conn._session.communicate = MagicMock()
     conn._terminate_session = MagicMock()
     conn._terminate_session.return_value = ''
     conn._session_id = MagicMock()
     conn._session_id.return_value = 'a'
     conn._client = MagicMock()
     conn.close()
Exemple #15
0
 def test_apply_invalid_config(self):
     client = MagicMock()
     client.get_bucket_notification_configuration.return_value = {
         'LambdaFunctionConfigurations': []
     }
     client.put_bucket_notification_configuration.side_effect = AnsibleFailJson(
         {}, '')
     bucket = AmazonBucket(self.module, client)
     config = Config.from_params(
         **{
             'event_name': 'test_event',
             'lambda_function_arn': 'lambda_arn',
             'lambda_version': 1,
             'events': ['s3:ObjectRemoved:*', 's3:ObjectCreated:*'],
             'prefix': '',
             'suffix': ''
         })
     with pytest.raises(AnsibleFailJson):
         bucket.apply_config(config)
Exemple #16
0
    def test_apply_config(self):
        client = MagicMock()
        client.get_bucket_notification_configuration.return_value = {
            'LambdaFunctionConfigurations': []
        }

        bucket = AmazonBucket(self.module, client)
        config = Config.from_params(
            **{
                'event_name': 'test_event',
                'lambda_function_arn': 'lambda_arn',
                'lambda_version': 1,
                'events': ['s3:ObjectRemoved:*', 's3:ObjectCreated:*'],
                'prefix': '',
                'suffix': ''
            })
        bucket.apply_config(config)
        assert client.get_bucket_notification_configuration.call_count == 1
        assert client.put_bucket_notification_configuration.call_count == 1
Exemple #17
0
 def test_delete_config(self):
     api_config = {
         'Id': 'test-id',
         'LambdaFunctionArn': 'test-arn',
         'Events': [],
         'Filter': {
             'Key': {
                 'FilterRules': [{
                     'Name': 'Prefix',
                     'Value': ''
                 }, {
                     'Name': 'Suffix',
                     'Value': ''
                 }]
             }
         }
     }
     client = MagicMock()
     client.get_bucket_notification_configuration.return_value = {
         'LambdaFunctionConfigurations': [api_config]
     }
     bucket = AmazonBucket(client, 'test-bucket')
     config = Config.from_params(
         **{
             'event_name': 'test-id',
             'lambda_function_arn': 'lambda_arn',
             'lambda_version': 1,
             'events': [],
             'prefix': '',
             'suffix': ''
         })
     bucket.delete_config(config)
     assert client.get_bucket_notification_configuration.call_count == 1
     assert client.put_bucket_notification_configuration.call_count == 1
     client.put_bucket_notification_configuration.assert_called_with(
         Bucket='test-bucket',
         NotificationConfiguration={'LambdaFunctionConfigurations': []})
    "region": "us-west-1",
    "function_name": "this_is_a_test_function",
    "state": "present",
    "statement_id": "test-allow-lambda",
    "principal": 123456,
    "action": "lambda:*"
}


def test_module_is_created_sensibly():
    set_module_args(base_module_args)
    module = setup_module_object()
    assert module.params['function_name'] == 'this_is_a_test_function'


module_double = MagicMock()
module_double.fail_json_aws.side_effect = Exception(
    "unexpected call to fail_json_aws")
module_double.check_mode = False

fake_module_params_present = {
    "state": "present",
    "statement_id": "test-allow-lambda",
    "principal": "apigateway.amazonaws.com",
    "action": "lambda:InvokeFunction",
    "source_arn":
    u'arn:aws:execute-api:us-east-1:123456789:efghijklmn/authorizers/*',
    "version": 0,
    "alias": None
}
fake_module_params_different = copy.deepcopy(fake_module_params_present)
Exemple #19
0
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible_collections.community.aws.tests.unit.compat.mock import MagicMock
from ansible.utils.path import unfrackpath

mock_unfrackpath_noop = MagicMock(spec_set=unfrackpath,
                                  side_effect=lambda x, *args, **kwargs: x)
Exemple #20
0
 def test_plugins_connection_aws_ssm_exec_command(self, r_choice):
     pc = PlayContext()
     new_stdin = StringIO()
     conn = connection_loader.get('community.aws.aws_ssm', pc, new_stdin)
     r_choice.side_effect = ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b']
     conn.MARK_LENGTH = 5
     conn._session = MagicMock()
     conn._session.stdin.write = MagicMock()
     conn._wrap_command = MagicMock()
     conn._wrap_command.return_value = 'cmd1'
     conn._flush_stderr = MagicMock()
     conn._windows = MagicMock()
     conn._windows.return_value = True
     sudoable = True
     conn._session.poll = MagicMock()
     conn._session.poll.return_value = None
     remaining = 0
     conn._timeout = MagicMock()
     conn._poll_stdout = MagicMock()
     conn._poll_stdout.poll = MagicMock()
     conn._poll_stdout.poll.return_value = True
     conn._session.stdout = MagicMock()
     conn._session.stdout.readline = MagicMock()
     begin = True
     mark_end = 'a'
     line = ['a', 'b']
     conn._post_process = MagicMock()
     conn._post_process.return_value = 'test'
     conn._session.stdout.readline.side_effect = iter(['aaaaa\n', 'Hi\n', '0\n', 'bbbbb\n'])
     conn.get_option = MagicMock()
     conn.get_option.return_value = 1
     cmd = MagicMock()
     returncode = 'a'
     stdout = 'b'
     return (returncode, stdout, conn._flush_stderr)
Exemple #21
0
 def test_plugins_connection_aws_ssm_start_session(self, boto_client, s_poll, s_popen, mock_ospe):
     pc = PlayContext()
     new_stdin = StringIO()
     conn = connection_loader.get('community.aws.aws_ssm', pc, new_stdin)
     conn.get_option = MagicMock()
     conn.get_option.side_effect = ['i1234', 'executable', 'abcd', 'i1234']
     conn.host = 'abc'
     mock_ospe.return_value = True
     boto3 = MagicMock()
     boto3.client('ssm').return_value = MagicMock()
     conn.start_session = MagicMock()
     conn._session_id = MagicMock()
     conn._session_id.return_value = 's1'
     s_popen.return_value.stdin.write = MagicMock()
     s_poll.return_value = MagicMock()
     s_poll.return_value.register = MagicMock()
     s_popen.return_value.poll = MagicMock()
     s_popen.return_value.poll.return_value = None
     conn._stdin_readline = MagicMock()
     conn._stdin_readline.return_value = 'abc123'
     conn.SESSION_START = 'abc'
     conn.start_session()