Beispiel #1
0
    def test_aliases(self):
        lambda_api = self.aws_lambda  # make it easier to read below
        alias_name = 'temp_name'
        alias_description = 'temp description'
        function_version = '$LATEST'
        with Temp_Lambda() as temp_lambda:
            assert lambda_api.aliases(function_name=temp_lambda.arn()) == []
            assert lambda_api.alias_create(
                function_name=temp_lambda.lambda_name,
                function_version=function_version,
                name=alias_name,
                description=alias_description).get('Name') == alias_name
            data = lambda_api.alias(function_name=temp_lambda.lambda_name,
                                    name=alias_name)
            del data['RevisionId']
            assert data == {
                'AliasArn': f'{temp_lambda.arn()}:{alias_name}',
                'Description': alias_description,
                'FunctionVersion': function_version,
                'Name': alias_name
            }

            assert lambda_api.aliases(function_name=temp_lambda.arn(),
                                      index_by='Name').get(alias_name).get(
                                          'Description') == alias_description
            assert lambda_api.alias_delete(temp_lambda.lambda_name,
                                           alias_name) == {}
            assert lambda_api.aliases(function_name=temp_lambda.arn()) == []
Beispiel #2
0
 def test_configuration(self):
     with Temp_Lambda() as temp_lambda:
         aws_lambda = temp_lambda.aws_lambda
         value = 10
         assert aws_lambda.configuration() == aws_lambda.info().get('Configuration')
         assert aws_lambda.configuration_update(Timeout=value).get('Timeout') == value
         assert aws_lambda.configuration().get('Timeout') == value
Beispiel #3
0
    def _test_run_lambdas_in_multiple_accounts(self):
        from osbot_aws.Globals import Globals
        from osbot_aws.apis.test_helpers.Temp_Lambda import Temp_Lambda
        Globals.aws_session_profile_name = 'gs-detect-aws'
        Globals.lambda_s3_bucket         = 'gs-detect-lambda'

        with Temp_Lambda() as _:
            _.invoke_raw().get('status') == 'ok'

        Globals.aws_session_profile_name = 'default'
Beispiel #4
0
    def _test_run_lambdas_in_multiple_accounts(self):

        from osbot_aws.apis.test_helpers.Temp_Lambda import Temp_Lambda
        AWS_Config().set_aws_session_profile_name('gs-detect-aws')
        AWS_Config().set_lambda_s3_bucket('gs-detect-lambda')

        with Temp_Lambda() as _:
            _.invoke_raw().get('status') == 'ok'

        AWS_Config().set_aws_session_profile_name('default')
Beispiel #5
0
 def test_policy__permissions(self):
     with Temp_Lambda() as temp_lambda:
         temp_lambda.aws_lambda.permission_add(temp_lambda.arn(), 'an_id',
                                               'lambda:action',
                                               'lambda.amazonaws.com')
         if len(temp_lambda.aws_lambda.permissions()) == 0:
             pytest.skip(
                 "in test_policy__permissions, len(temp_lambda.aws_lambda.permissions()) was 0 (doesn't happen when executing it directly"
             )  # todo: fix this race condition that happens when running all tests at the same time
         assert temp_lambda.aws_lambda.permissions()[0].get(
             'Action') == 'lambda:action'
Beispiel #6
0
 def test_invoke_raw(self):
     with Temp_Lambda() as temp_lambda:
         result   = temp_lambda.aws_lambda.invoke_raw({'name' : temp_lambda.lambda_name})
         data     = result.get('data')
         name     = result.get('name')
         status   = result.get('status')
         response = result.get('response')
         assert status == 'ok'
         assert name   == temp_lambda.lambda_name
         assert data   == 'hello {0}'.format(temp_lambda.lambda_name)
         assert response.get('StatusCode') == 200
Beispiel #7
0
    def test_log_group(self):
        with Temp_Lambda(delete_on_exit=True) as aws_lambda:
            result = aws_lambda.invoke_return_logs()
            request_id = result.get("request_id")
            filter_pattern = f'"{request_id}"'
            log_group = aws_lambda.aws_lambda.log_group()
            messages = log_group.group_messages_wait_for_pattern(
                filter_pattern=filter_pattern)
            message = list_contains(messages, 'Duration:').pop()

        assert len(messages) == 3
        assert message.find(request_id) > -1
Beispiel #8
0
    def test_update(self):
        with Temp_Lambda() as temp_lambda:
            tmp_text = Misc.random_string_and_numbers(prefix='updated code ')
            temp_lambda.tmp_folder.create_temp_file('def run(event, context): return "{0}"'.format(tmp_text))

            result = temp_lambda.aws_lambda.update()
            status = result.get('status')
            data   = result.get('data')

            assert 'TracingConfig' in data #set(data) == { 'CodeSha256', 'CodeSize', 'Description', 'FunctionArn', 'FunctionName', 'Handler', 'LastModified', 'MemorySize', 'ResponseMetadata', 'RevisionId', 'Role', 'Runtime', 'Timeout', 'TracingConfig', 'Version'}
            assert status    == 'ok'
            assert temp_lambda.aws_lambda.invoke() == tmp_text
Beispiel #9
0
    def test_event_source_create(self):
        with Temp_Lambda() as temp_lambda:                                      # create a temp lambda function that will deleted on exit
            with Temp_Queue() as temp_queue:                                    # create a temp sqs queue that will be deleted on exit
                event_source_arn = temp_queue .queue.arn()
                lambda_name      = temp_lambda.lambda_name
                aws_lambda       = temp_lambda.aws_lambda
                queue            = temp_queue .queue

                aws_lambda.configuration_update(Timeout=10)                     # needs to be less than 30 (which is the default value sent in the queue)
                queue.policy_add_sqs_permissions_to_lambda_role(lambda_name)    # add permissions needed to role
                sleep(2)                                                        # todo: need a better solution to find out when the permission is available
                aws_lambda.event_source_create(event_source_arn, lambda_name)   # todo: add asserts
                queue.policy_remove_sqs_permissions_to_lambda_role(lambda_name) # remove permissions todo: create custom role
    def test_lambdas_cloud_watch_logs(self):
        with Temp_Lambda() as _:
            assert _.invoke() == 'hello None'
            log_name = '/aws/lambda/{0}'.format(_.name)
            logs = Logs(group_name=log_name)
            assert logs.group_exists() is True
            logs.stream_name = logs.streams()[0].get('logStreamName')

            sleep(
                1.0
            )  # wait for logs to be updated (0.5 has failed in CodeBuild)
            messages = logs.messages()
            assert len(messages) > 0
            assert 'START RequestId: ' in messages.pop()
Beispiel #11
0
    def test_policy_add_sqs_permissions_to_lambda_role(self):
        policy_name = self.iam_utils.arn_aws_policy_service_sqs_lambda.split(
            '/').pop(-1)
        with Temp_Lambda() as temp_lambda:
            lambda_name = temp_lambda.lambda_name
            iam_role_name = self.iam_utils.policy_add_sqs_permissions_to_lambda_role(
                lambda_name)
            iam_role = IAM_Role(role_name=iam_role_name)
            pprint(iam_role.info())
            assert policy_name in iam_role.policies_statements()
            assert iam_role.exists() is True

            self.iam_utils.policy_remove_sqs_permissions_to_lambda_role(
                lambda_name)

            assert policy_name not in iam_role.policies_statements()
Beispiel #12
0
    def test_invoke_raw_with_logs(self):
        payload = {'name': 'test'}
        with Temp_Lambda() as aws_lambda:
            result = aws_lambda.invoke_return_logs(payload)
            execution_logs = result.get('execution_logs')
            request_id = result.get('request_id')
            assert result['status'] == 'ok'
            assert result['name'] == aws_lambda.lambda_name
            assert result['return_value'] == f'hello {payload["name"]}'
            assert f'START RequestId: {request_id}' in execution_logs
            assert f'END RequestId: {request_id}' in execution_logs
            assert 'OSBot inside an Lambda :)' in execution_logs
            assert 'Version: $LATEST' in execution_logs
            assert 'Duration:' in execution_logs

            assert aws_lambda.aws_lambda.invoke_dry_run().get(
                'StatusCode'
            ) == 204  # todo move to dedicated function (once we have one test function created for all tests)
 def test_simple_execution(self):
     with Temp_Lambda() as _:
         assert 'temp_lambda_' in _.aws_lambda.name
         assert _.invoke({'name': 'world'}) == 'hello world'
Beispiel #14
0
 def test_simple_execution(self):
     with Temp_Lambda() as _:
         assert 'temp_lambda_' in _.temp_lambda.name
         assert _.invoke() == 'hello None'
Beispiel #15
0
 def test_create_temp_lambda(self):
     with Temp_Lambda() as temp_lambda:
         temp_lambda.delete_on_exit = False
         self.result = temp_lambda.lambda_name  # temp_lambda_LVY1LL
Beispiel #16
0
 def test_invoke_return_logs(self):
     with Temp_Lambda() as temp_lambda:
         self.result = temp_lambda.aws_lambda.invoke_return_logs()
Beispiel #17
0
 def test_policy__permissions(self):
     with Temp_Lambda() as temp_lambda:
         temp_lambda.aws_lambda.permission_add(temp_lambda.arn(), 'an_id', 'lambda:action', 'lambda.amazonaws.com')
         assert temp_lambda.aws_lambda.permissions()[0].get('Action') == 'lambda:action'
Beispiel #18
0
 def test_invoke_async(self):
     with Temp_Lambda() as temp_lambda:
         result = temp_lambda.aws_lambda.invoke_async({'name': temp_lambda.lambda_name})
         assert result.get('StatusCode') == 202
Beispiel #19
0
 def test_invoke(self):
     with Temp_Lambda() as temp_lambda:
         result = temp_lambda.aws_lambda.invoke({'name': temp_lambda.lambda_name})
         assert result == 'hello {0}'.format(temp_lambda.lambda_name)
 def test_check_lambda_role_has_access_to_auth_key(self):
     with Temp_Lambda() as temp_lambda:
         pprint(temp_lambda.info())
         pprint(temp_lambda.invoke())