Example #1
0
 def test_message_send(self):
     with Temp_Queue() as temp_Queue:
         queue = temp_Queue.queue
         message_1 = Misc.random_string_and_numbers(prefix='Hello_')
         message_2 = Misc.random_string_and_numbers(prefix='World_')
         queue.add(message_1).add(message_2)
         messages = [queue.get_message(), queue.get_message()]
         assert message_1 in messages
         assert message_2 in messages
         assert queue.get_message() is None
    def test_participant_append_to_field(self):
        payload = {'action': 'participant_append_to_field',
                   'name': 'OSS Bot',
                   'field': 'test_field',
                   'value': Misc.random_string_and_numbers()}
        assert self.aws_lambda.invoke(payload) == {'status': 'ok'}

        payload = {'action': 'participant_append_to_field',
                   'name': 'OSS Bot',
                   'field': 'sessions',
                   'value': Misc.random_string_and_numbers()}

        assert self.aws_lambda.invoke(payload) == {'status': 'ok'}
    def _test_lambda_write_cloud_watch__with_asserts(self):
        group_name = '/unit-tests/test_log_group'
        stream_name = Misc.random_string_and_numbers(prefix='tmp_stream_')
        message = 'this is a message sent from an lambda function'
        lambda_name = 'osbot_aws.lambdas.dev.write_cloud_watch_log'
        log_group_arn = 'arn:aws:logs:eu-west-2:244560807427:log-group:{0}*'.format(
            group_name)
        policy_name = 'temp_policy_for_lambda_write_cloud_watch'
        role_name = 'temp_role_for_lambda_invocation'
        policy_actions = ['logs:PutLogEvents']

        logs = Logs(group_name=group_name, stream_name=stream_name)
        logs.group_create()
        logs.stream_create()

        iam_role = IAM(role_name=role_name)
        iam_policy = IAM_Policy(policy_name=policy_name)
        iam_policy.add_statement_allow(policy_actions, [log_group_arn])

        policy_arn = iam_policy.create(
            delete_before_create=True).get('policy_arn')

        assert iam_policy.exists() is True
        assert iam_role.role_exists() is True
        assert logs.group_exists() is True
        assert logs.stream_exists() is True
        assert set(iam_role.role_policies()) == {
            'AWSXrayWriteOnlyAccess', 'policy_temp_role_for_lambda_invocation'
        }

        iam_role.role_policy_attach(policy_arn)

        assert set(iam_role.role_policies()) == {
            'AWSXrayWriteOnlyAccess', 'policy_temp_role_for_lambda_invocation',
            'temp_policy_for_lambda_write_cloud_watch'
        }

        sleep(10)  # wait for AWS to propagate role update
        payload = {
            'group_name': group_name,
            'stream_name': stream_name,
            'message': message
        }
        lambda_obj = Lambda_Package(lambda_name)  #.update_with_root_folder()
        result = lambda_obj.invoke(payload)

        sleep(1)  # wait for Cloudwatch to update
        assert result.get('status') == 'ok'
        assert logs.messages() == [message]

        assert iam_policy.delete() is True
        assert logs.group_delete() is True
        assert logs.group_exists() is False
        assert set(iam_role.role_policies()) == {
            'AWSXrayWriteOnlyAccess', 'policy_temp_role_for_lambda_invocation'
        }
Example #4
0
 def __init__(self):
     self.name           = "temp_lambda_{0}".format(Misc.random_string_and_numbers())
     self.temp_lambda    = Lambda(self.name)
     self.tmp_folder     = Temp_Folder_Code(self.name)
     self.role_arn       = Temp_Aws_Roles().for_lambda_invocation()
     self.create_log     = None
     self.delete_on_exit = True
     self.lambda_name    = 'tmp_lambda_dev_test'
     self.s3_bucket      = Globals.lambda_s3_bucket
     self.s3_key         =  'unit_tests/lambdas/{0}.zip'.format(self.lambda_name)
Example #5
0
 def test_log_group_create_delete_exists_info(self):
     tmp_log_group = Misc.random_string_and_numbers(
         prefix='/unit-tests/test_log_group_')
     temp_logs = Logs(tmp_log_group, '')
     assert temp_logs.group_exists() is False
     assert temp_logs.group_create() is True
     assert temp_logs.group_exists() is True
     assert temp_logs.group_info().get('logGroupName') == tmp_log_group
     assert temp_logs.group_delete() == True
     assert temp_logs.group_exists() is False
Example #6
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
Example #7
0
    def test_lambda_write_cloud_watch(self):
        group_name      = '/aws/lambda/unit-tests/test_log_group'
        stream_name     = Misc.random_string_and_numbers(prefix='tmp_stream_')
        message         = 'this is a message sent from an lambda function'
        lambda_name     = 'osbot_aws.lambdas.dev.write_cloud_watch_log'
        logs            = Logs(group_name=group_name, stream_name=stream_name).create()
        lambda_obj      = Lambda_Package(lambda_name)

        payload = {'group_name': group_name, 'stream_name': stream_name, 'message': message}

        assert lambda_obj.invoke(payload).get('status') == 'ok'

        #sleep(0.8)
        # assert logs.messages() == ['this is a message sent from an lambda function']
        assert logs.group_delete() is True
Example #8
0
    def test_run_container_on_temp_cluster(self):
        subnet_id = ' subnet-49391932'
        security_group_id = 'sg-e6ea548e'
        cluster_name = Misc.random_string_and_numbers(6, 'temp_cluster_')
        task_name = 'temp_task_on_{0}'.format(cluster_name)
        task_role = 'task_role_{0}'.format(task_name)
        execution_role = 'execution_role_{0}'.format(task_name)
        docker_image_name = 'gs-docker-codebuild'
        log_group_name = "awslogs-{0}".format(task_name)
        log_group_region = "eu-west-2"
        log_group_stream_prefix = "awslogs-example"

        # Create Cluster
        self.fargate.cluster_create(cluster_name)

        # Create Roles
        self.fargate.policy_create_for_task_role(task_role)
        self.fargate.policy_create_for_execution_role(execution_role)

        #Create Task
        task_arn = self.fargate.task_create(
            task_name, task_role, execution_role).get('taskDefinitionArn')

        # Run Task
        task_run = self.fargate.task_run(cluster_name, task_arn, subnet_id,
                                         security_group_id)
        task_run_arn = task_run.get('taskArn')
        task_details = self.fargate.task_wait_for_completion(
            cluster_name, task_run_arn)

        task_id = 'asd'
        # Get logs

        #group_name  = 'awslogs-temp_task_on_temp_cluster_X29B3K'
        #stream_name = 'awslogs-example/gs-docker-codebuild/f8ccf213-b642-466c-8458-86af9933eca9'
        stream_name = "{0}/{1}{2}".format(log_group_stream_prefix,
                                          docker_image_name, task_id)

        messages = self.cloud_watch.logs_get_messages(log_group_name,
                                                      stream_name)
        # Print details
        Dev.pprint(messages)
 def setUp(self):
     self.lambda_name = "temp_lambda_{0}".format(
         Misc.random_string_and_numbers())
     self.package = Lambda_Package(self.lambda_name)
 def test_markdown(self):
     markdown = Misc.random_string_and_numbers(
         prefix='# Created from Lambda ')
     payload = {"params": ['markdown', markdown, " \n normal text"]}
     png_data = self.lambda_browser.invoke(payload)
     self._save_png_file(png_data)
Example #11
0
 def setUp(self):
     self.render_page = Render_Page(headless=False)
     self.random_value = Misc.random_string_and_numbers(6, "dynamic text ")
     self.html = "<html><script>document.write('{0}')</script></html>".format(
         self.random_value)
     self.tmp_img = '/tmp/lambda_png_file.png'
Example #12
0
 def random_text(prefix=None, length=12):
     if prefix is None: prefix = 'text_'
     if Temp_Misc.last_letter(prefix) != '_':
         prefix += '_'
     return Misc.random_string_and_numbers(length=length, prefix=prefix)
Example #13
0
 def test_put_secret(self):
     value = Misc.random_string_and_numbers()
     self.param.put_secret(value)
     assert value == self.param.value_secret()
     assert self.param.value() != self.param.value_secret()
Example #14
0
 def test_put(self):
     value = Misc.random_string_and_numbers()
     assert self.param.get().get('Type') == 'String'
     self.param.put(value)
     assert self.param.value() == value
Example #15
0
 def __init__(self):
     self.name_prefix = 'unit_tests_temp_queue_'
     self.queue_name = Misc.random_string_and_numbers(
         prefix=self.name_prefix)
     self.queue = None