def _test_confirm_tmp_reuse(self): lambda_name = 'osbot_aws.lambdas.pocs.confirm_tmp_reuse' # lambda to execute lambda_obj = Lambda_Package(lambda_name).update_with_root_folder( ) # create lambda and upload code payload_1 = {'file_name': 'file_1.txt'} # first file to create result_1 = lambda_obj.invoke(payload_1) # invoke lambda function payload_2 = {'file_name': 'file_2.txt'} # 2nd file to create result_2 = lambda_obj.invoke( payload_2) # invoke lambda function again (no update) lambda_obj = Lambda_Package( lambda_name).update_with_root_folder() # update lambda code payload_3 = {'file_name': 'file_3.txt'} # 3rd file to create result_3 = lambda_obj.invoke( payload_3) # invoke lambda function (after update) assert len( result_1) is 1 # confirm we have 1 temp file in 1st execution assert len( result_2) is 2 # confirm we have 2 temp files in 2nd execution assert len( result_3) is 1 # confirm we have 1 temp files in 3rd execution assert '/tmp/file_1.txt' in result_1 # confirm 1st file was there on 1st execution assert '/tmp/file_1.txt' in result_2 # confirm 1st file was there on 2nd execution assert '/tmp/file_2.txt' in result_2 # confirm 2nd file was there on 2nd execution assert '/tmp/file_3.txt' in result_3 # confirm 3rd file was there on 3rd execution lambda_obj.delete() # delete lambda
def _test_sqs_to_lamdba_to_sqs(self): queue_1 = Queue('unit_tests_temp_queue__1').create() queue_2 = Queue('unit_tests_temp_queue__2').create() lambda_obj = Lambda_Package('osbot_aws.lambdas.pocs.send_event_data_to_queue') #.update_with_root_folder() payload = {'queue_url': queue_2.url(), 'message': 'test 123'} #queue_1.attributes_update({'VisibilityTimeout': '61'}) # needs to be bigger than lambda timeout queue_1_arn = queue_1.attributes().get('QueueArn') function_arn = lambda_obj._lambda.function_Arn() #Dev.pprint(lambda_obj.role_arn) # needed to add the extra priv to pull SQS messages #result = lambda_obj._lambda.boto_lambda().list_event_source_mappings(EventSourceArn=queue_1_arn) # result = lambda_obj._lambda.boto_lambda().create_event_source_mapping( # EventSourceArn=queue_1_arn, # FunctionName=function_arn, # Enabled=True, # BatchSize=1 # ) queue_1.push('some message 1') queue_1.push('some message 2') queue_1.push('some message 3') queue_1.push('some message 4') #lambda_obj.invoke(payload) sleep(2) Dev.pprint(queue_2.pull()) Dev.pprint(queue_2.pull()) Dev.pprint(queue_2.pull()) Dev.pprint(queue_2.pull()) queue_1.delete() queue_2.delete()
def lambda_package(self, lambda_name) -> Lambda_Package: lambda_package = Lambda_Package(lambda_name) lambda_package.tmp_s3_bucket = self.s3_bucket_lambdas # these four method calls need to be refactored lambda_package.tmp_s3_key = 'lambdas/{0}.zip'.format(lambda_name) lambda_package.aws_lambda.set_s3_bucket(lambda_package.tmp_s3_bucket) lambda_package.aws_lambda.set_s3_key(lambda_package.tmp_s3_key) return lambda_package
def test_get_screenshot_using_lambda(self): self.aws_lambda = Lambda_Package( 'osbot_jupyter.lambdas.jupyter_server') url, token = self.code_build.get_server_details_from_logs() #print(url,token) #return notebook = 'test-1' payload = {'url': url, 'token': token, 'notebook': notebook} self.png_data = self.aws_lambda.invoke(payload)
def setUp(self): self.short_id = '3ddc8' self.aws_lambda = Lambda_Package( 'osbot_jupyter.lambdas.execute_python') self.result = None self.png_data = None self.aws_lambda.add_module('osbot_browser') self.aws_lambda.update_code( ) # use when wanting to update lambda function
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' }
def setUp(self): self.task_name = 'update_sheet' self.command = 'load_sheet' self.file_id = '1_Bwz6z34wALFGb1ILUXG8CtF1-Km6F9_sGXnAu4gewY' self.aws_lambda = Lambda_Package( 'osbot_gsheet_sync.lambdas.run_command') self.result = None self.aws_lambda.update_code( ) # use when wanting to update lambda function
def test_lambda_write_to_queue(self): queue_name = 'unit_tests_temp_queue' # Queue(queue_name).create() lambda_name = 'osbot_aws.lambdas.dev.write_queue' message = 'this is a message sent from an lambda function...' queue = Queue(queue_name) queue_url = queue.url() payload = {'queue_url': queue_url, 'message': message} lambda_obj = Lambda_Package(lambda_name) assert lambda_obj.invoke(payload) == {'status': 'ok'} assert Queue(queue_name).pull() == message
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
def _test_s3_bucket_to_sqs(self): s3 = S3() queue = Queue('unit_tests_temp_queue') #.create() bucket_name = 'bucket-42-temp' region = 'eu-west-2' lambda_obj = Lambda_Package( 'osbot_aws.lambdas.pocs.send_event_data_to_queue' ).update_with_root_folder() lambda_arn = lambda_obj._lambda.function_Arn() bucket_arn = s3.bucket_arn(bucket_name) queue_arn = queue.arn() #lambda_obj.invoke({'a':43}) #s3.bucket_create(s3_bucket, region) def add_notification(source_arn): result = lambda_obj._lambda.client().permission_add( FunctionName=lambda_arn, StatementId='1', Action='lambda:InvokeFunction', Principal='s3.amazonaws.com', SourceArn=source_arn, SourceAccount=IAM().account_id()) Dev.pprint(result) #Dev.pprint(result == { 'Statement': '{"Sid":"1","Effect":"Allow","Principal":{"Service":"s3.amazonaws.com"},"Action":"lambda:InvokeFunction","Resource":"arn:aws:lambda:eu-west-2:244560807427:function:osbot_aws_lambdas_pocs_send_event_data_to_queue","Condition":{"StringEquals":{"AWS:SourceAccount":"244560807427"},"ArnLike":{"AWS:SourceArn":"arn:aws:sqs:eu-west-2:244560807427:unit_tests_temp_queue"}}}'}) #add_notification(bucket_arn) #return # resource = s3.s3_bucket_notification(bucket_name) # config = { 'LambdaFunctionConfigurations': [{ 'LambdaFunctionArn': lambda_arn , # 'Events' : ['s3:ObjectCreated:*']}]} # Dev.pprint(config) # Dev.pprint(resource.put(NotificationConfiguration=config)) #Dev.pprint(resource.lambda_function_configurations) #Dev.pprint(s3.s3().get_bucket_notification_configuration(Bucket=bucket_name)) #bucket_notification = s3.BucketNotification('bucket_name') #Dev.pprint(bucket_notification) #s3.boto_client_s3().bucket_notification() Dev.pprint(queue.pull()) Dev.pprint(queue.pull())
def _test_confirm_process_stay_alive(self): lambda_name = 'osbot_aws.lambdas.pocs.confirm_process_stay_alive' # lambda to execute lambda_obj = Lambda_Package(lambda_name).update_with_root_folder( ) # create lambda and upload code result_1 = lambda_obj.invoke({'port': 11111, 'count': 3}) result_2 = lambda_obj.invoke() sleep(1) result_3 = lambda_obj.invoke() sleep(10) result_4 = lambda_obj.invoke() print() print('\n***1st Execution***\n', result_1) print('\n***2nd Execution (no delay)***\n', result_2) print('\n***3rd Execution (1 sec delay) ***\n', result_3) print('\n***4th Execution (10 sec delay)***\n', result_4)
def setUp(self): super().setUp() self.lambda_graph = Lambda_Package('osbot_jira.lambdas.graph') self.result = None
def test_deploy_slack_callback(self): Lambda_Package('osbot.lambdas.slack_callback').update_code()
def test_deploy_osbot(self): #assert self.deploy.deploy().get('status') == 'ok' Lambda_Package('osbot.lambdas.osbot').update_code()
def setUp(self): self.lambda_name = "temp_lambda_{0}".format( Misc.random_string_and_numbers()) self.package = Lambda_Package(self.lambda_name)
def _reset_lambda(self): Lambda_Package(self.lambda_name).reset()
def test_lambda_update(self): Lambda_Package(self.lambda_name).update_code()
def get_package(self): package = Lambda_Package(self.module_name) package.aws_lambda.set_s3_bucket(self.osbot_setup.s3_bucket_lambdas) package.aws_lambda.set_role(self.role_arn) return package
def setUp(self): self.oss_setup = super().setUp() self.aws_lambda = Lambda_Package('oss_bot.lambdas.oss_bot') self.aws_lambda._lambda.set_s3_bucket(self.oss_setup.s3_bucket_lambdas) \ .set_role (self.oss_setup.role_lambdas)
def setUp(self): self.png_data = None self.result = None self._lambda = Lambda_Package('osbot_browser.lambdas.slack_web')
def __init__(self): self.osbot = Lambda_Package('osbot.lambdas.osbot') self.tmp_s3_bucket = 'gs-lambda-tests' self.tmp_s3_key = 'gsbot/gsbot.zip' self.setup()
def get_package(self, lambda_name): package = Lambda_Package(lambda_name) package._lambda.set_s3_bucket(self.oss_setup.s3_bucket_lambdas) \ .set_role(self.oss_setup.role_lambdas) return package
def __init__(self, lambda_name): self.package = Lambda_Package(lambda_name) self.tmp_s3_bucket = 'gs-lambda-tests' self.tmp_s3_key = 'gsbot/{0}.zip'.format(lambda_name) self.setup()
def setUp(self): self.oss_setup = super().setUp() self.aws_lambda = Lambda_Package('oss_bot.lambdas.dev.hello_world') self.aws_lambda._lambda.set_s3_bucket(self.oss_setup.s3_bucket_lambdas) \ .set_role (self.oss_setup.role_lambdas) self.aws_lambda.create() # use when wanting to update lambda function
def test_update_with_root_folder(self): name = 'osbot_aws.lambdas.dev.check_python_utils' assert 'checking python utils: /tmp' in Lambda_Package( name).update_with_root_folder().invoke()
def setUp(self): self.aws_lambda = Lambda_Package('osbot_jupyter.lambdas.nbconvert') #self.aws_lambda.add_module('osbot_browser') self.aws_lambda.update_code() # use when wanting to update lambda function self.result = None self.png_data = None