Пример #1
0
class test_run_command(TestCase):
    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 tearDown(self):
        if self.result:
            Dev.pprint(self.result)

    def test_invoke_lambda(self):
        payload = {
            'task_name': self.task_name,
            'command': self.command,
            'file_id': self.file_id
        }
        self.result = self.aws_lambda.invoke(payload)

    def test_ping(self):
        payload = {'task_name': 'ping'}
        self.result = self.aws_lambda.invoke(payload)
Пример #2
0
    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
Пример #3
0
class test_run_command(Test_Helper):
    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)
        #self.aws_lambda.create()  # use when wanting to update lambda function

    def update_lambda(self):
        self.aws_lambda.update_code()

    def test__invoke_directy(self):
        from oss_bot.lambdas.oss_bot import run
        self.result = run({'event': {'type': 'message', 'text': 'help'}},{})

    def test_invoke(self):
        self.update_lambda()
        self.result = self.aws_lambda.invoke({'event': {'type': 'message', 'text': 'help'}})

    def test_invoke_with_channel(self):
        self.update_lambda()
        self.result = self.aws_lambda.invoke({'event': {'type': 'message', 'text': 'help', "channel": "DJ8UA0RFT"}})

    def test_participant_view(self):
        self.update_lambda()
        text = 'participant view Dinis Cruz'
        self.result = self.aws_lambda.invoke({'event': {'type': 'message', 'text': text, "channel": "DJ8UA0RFT"}})
Пример #4
0
class test_jira_web(unittest.TestCase):
    def setUp(self):
        self.png_data = None
        self.result = None
        self._lambda = Lambda_Package('osbot_browser.lambdas.slack_web')

    def tearDown(self):
        if self.png_data:
            if type(self.png_data) is str:
                png_file = '/tmp/tmp-jira-screenshot.png'
                with open(png_file, "wb") as fh:
                    fh.write(base64.decodebytes(self.png_data.encode()))
                Dev.pprint("Png data with size {0} saved to {1}".format(
                    len(self.png_data), png_file))
            else:
                Dev.print(self.result)
        if self.result:
            Dev.print(self.result)

    #def test_invoke_directly(self):
    #    result = run({},{})
    #    assert result == '*Here are the `Browser_Commands` commands available:*'

    def test_invoke(self):
        self._lambda.update_code()
        target = '/messages/random/'

        payload = {
            'target': target,
            'channel': 'DDKUZTK6X',  # gsbot
            'team_id': 'T7F3AUXGV',  # GS-CST
            'width': 800,
            'height': 1000
        }
        self.result = self._lambda.invoke(payload)
        self.png_data = self.result  # self._lambda.invoke(payload)

    def test_invoke_oss(self):
        self._lambda.update_code()
        target = '/messages/oss-helpdesk'
        target = '/messages/oss-general'

        payload = {
            'target': target,
            'channel': 'DDKUZTK6X',  # gsbot
            #'team_id': 'T7F3AUXGV',  # GS-CST
            'team_id': 'TAULHPATC',  # OSS
            'width': 800,
            'height': 4000
        }
        #self.result = self._lambda.invoke(payload)
        self.png_data = self._lambda.invoke(payload)

    def test_update_lambda(self):
        self._lambda.update_code()
class test_run_command(TestCase):
    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 tearDown(self):
        if self.result:
            Dev.pprint(self.result)

        if self.png_data:
            png_file = '/tmp/lambda_png_file.png'
            with open(png_file, "wb") as fh:
                fh.write(base64.decodebytes(self.png_data.encode()))
            Dev.pprint("Png data with size {0} saved to {1}".format(
                len(self.png_data), png_file))

    def test_execute_python(self):
        code = '40+3'
        target = None
        keep_contents = None
        payload = {
            'short_id': self.short_id,
            'python_code': code,
            target: target,
            keep_contents: keep_contents
        }
        #self.png_data = self.aws_lambda.invoke(payload)
        self.result = self.aws_lambda.invoke(payload)
class test_screenshot_from_codebuild(TestCase):
    def setUp(self):
        self.code_build = CodeBuild_Jupyter()
        self.jp = Jupyter()
        self.png_data = None

    def tearDown(self):
        if self.png_data:
            png_file = '/tmp/lambda_png_file.png'
            with open(png_file, "wb") as fh:
                fh.write(base64.decodebytes(self.png_data.encode()))
            Dev.pprint("Png data with size {0} saved to {1}".format(
                len(self.png_data), png_file))

    def test_get_screenshot_using_local_chrome(self):
        url, token = self.code_build.get_server_details_from_logs()
        self.jp.set_url(url).set_token(token)
        self.jp.login()
        #self.jp.open('aaa')
        self.png_file = self.jp.screenshot()
        Dev.pprint(self.png_file)

    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)
Пример #7
0
class test_run_command(TestCase):
    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

    def tearDown(self):
        if self.result:
            Dev.pprint(self.result)

        if self.png_data:
            png_file = '/tmp/lambda_png_file.png'
            with open(png_file, "wb") as fh:
                fh.write(base64.decodebytes(self.png_data.encode()))
            Dev.pprint("Png data with size {0} saved to {1}".format(len(self.png_data), png_file))

    def test_upload_dependency(self):
        from osbot_aws.apis.Lambda import upload_dependency
        self.result = upload_dependency('jupyter')

    def test_nbconvert(self):
        payload     = { }
        from osbot_aws.Dependencies import load_dependency
        load_dependency('jupyter')
        #self.png_data = self.aws_lambda.invoke(payload)
        self.result =  self.aws_lambda.invoke(payload)
Пример #8
0
    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'
        }
Пример #9
0
    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)
Пример #10
0
    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
Пример #11
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
Пример #12
0
class test_run_command(Test_Helper):
    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 tearDown(self):
        super().tearDown()
        self.aws_lambda.delete()

    def test_invoke(self):
        assert self.aws_lambda.invoke({'name':'abc'}) == 'Hello abc (from lambda)'

    #workflow tests
    def test____check_current_lambdas(self):
        list = Lambdas().list()
        self.result = len(set(list))
Пример #13
0
class test_run_command(TestCase):
    def setUp(self):
        self.aws_lambda = Lambda_Package('osbot_jupyter.lambdas.browser')
        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 tearDown(self):
        if self.result:
            Dev.pprint(self.result)

        if self.png_data:
            png_file = '/tmp/lambda_png_file.png'
            with open(png_file, "wb") as fh:
                fh.write(base64.decodebytes(self.png_data.encode()))
            Dev.pprint("Png data with size {0} saved to {1}".format(len(self.png_data), png_file))

    def test_invoke_lambda(self):
        payload     = { 'url' : 'https://www.google.com/abc'}
        self.png_data = self.aws_lambda.invoke(payload)
Пример #14
0
class test_jira_web(unittest.TestCase):
    def setUp(self):
        self.png_data = None
        self.result = None
        self._lambda = Lambda_Package('osbot_browser.lambdas.jira_web')

    def tearDown(self):
        if self.png_data:
            png_file = '/tmp/tmp-jira-screenshot.png'
            with open(png_file, "wb") as fh:
                fh.write(base64.decodebytes(self.png_data.encode()))
            Dev.pprint("Png data with size {0} saved to {1}".format(
                len(self.png_data), png_file))
        if self.result:
            Dev.print(self.result)

    def test_invoke_directly(self):
        result = run({}, {})
        assert result == '*Here are the `Browser_Commands` commands available:*'

    def test_invoke(self):
        self._lambda.update_code()
        issue_id = 'SEC-10965'  # 'GSP-95'

        payload = {
            'issue_id': issue_id,
            'channel': 'DDKUZTK6X',  # gsbot
            'team_id': 'T7F3AUXGV',  # GS-CST
            'width': 2000,
            'height': 300
        }
        self.result = self._lambda.invoke(payload)
        #self.png_data = self._lambda.invoke(payload)

    def test_update_lambda(self):
        self._lambda.update_code()
Пример #15
0
class test_Lambda_Package(TestCase):
    def setUp(self):
        self.lambda_name = "temp_lambda_{0}".format(
            Misc.random_string_and_numbers())
        self.package = Lambda_Package(self.lambda_name)

    def tearDown(self):
        if self.package.aws_lambda.exists():
            self.package.delete()
            assert self.package.aws_lambda.exists() is False

    def test__init__(self):
        assert type(self.package.aws_lambda).__name__ == 'Lambda'

    def test_add_file(self):
        lambda_file = Files.path_combine(
            __file__, '../../../../osbot_aws/lambdas/dev/hello_world.py')
        assert Files.exists(lambda_file)
        self.package.add_file(lambda_file)
        assert self.package.get_files() == ['/hello_world.py']
        self.package.aws_lambda.handler = 'hello_world.run'
        self.package.update()
        assert self.package.invoke() == 'From lambda code, hello None'

    def test_add_folder(self):
        self.package.add_folder(self.package.get_root_folder())
        self.package.aws_lambda.handler = 'osbot_aws.lambdas.dev.check_aws_api.run'
        self.package.update()
        assert self.package.invoke(
        ) == "checking aws api: <class 'osbot_aws.Globals.Globals'>"

    def test_add_pbx_gs_python_utils(self):
        self.package.add_root_folder()
        self.package.add_pbx_gs_python_utils()
        assert len(self.package.get_files()) > 10
        self.package.aws_lambda.handler = 'osbot_aws.lambdas.dev.check_python_utils.run'
        self.package.update()
        assert 'checking python utils: /tmp' in self.package.invoke()

    def test_get_root_folder(self):
        assert self.package.get_root_folder().endswith('osbot_aws') is True

    def test_use_lambda_file(self):
        assert self.package.use_lambda_file('lambdas/dev/hello_world.py').get(
            'status') == 'ok'

        files = self.package.get_files()
        assert len(files) == 1
        assert Files.file_name(files.pop()) == '{0}.py'.format(
            self.package.lambda_name)

        assert self.package.update().get('status') == 'ok'
        assert self.package.invoke({'name': 'world'
                                    }) == 'From lambda code, hello world'

    def test_use_lambda_file__bad_file(self):
        result = self.package.use_lambda_file('lambdas/dev/aaaaaaa')
        assert result.get('status') == 'error'
        assert 'could not find lambda file `lambdas/dev/aaaaaaa`' in result.get(
            'data')

    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()
class test_lambda_gsbot_graph(Test_Helper):
    def setUp(self):
        super().setUp()
        self.lambda_graph = Lambda_Package('osbot_jira.lambdas.graph')
        self.result       = None

    def tearDown(self):
        if self.result is not None:
            Dev.pprint(self.result)


    def test_invoke_directly(self):
        response = run({},{})
        Dev.pprint(response)

    def test_invoke_directly_raw_data(self):
        graph_name = 'graph_J2O'
        payload = {'params': ['raw_data', graph_name, 'details'], 'data': {}}
        # data = Lambda('lambdas.gsbot.gsbot_graph').invoke(params)
        self.result = run(payload,{})
        #self.results = Lambda('osbot_jira.lambdas.graph').invoke(params)

    def test_update_lambda(self):
        Deploy().deploy_lambda__jira('osbot_jira.lambdas.graph')

    def invoke(self, command):
        payload = {
            "params": command.split(' '),
            #"data" : {}
            "data": {"channel": "DRE51D4EM" }
        }
        return self.lambda_graph.invoke(payload)

    def test_invoke(self):
        assert self.invoke('')[0] == '*Here are the `graph` commands available:*'


    def test_invoke_raw_data(self):
        graph_name = 'graph_J2O'
        payload = {'params': ['raw_data', graph_name, 'details'], 'data': {}}
        self.result = Lambda('osbot_jira.lambdas.graph').invoke(payload)

    def test_invoke___last(self):

        assert "| #  |    who    | nodes | edges | " in self.invoke('last 20')[0].get('text')

    def test_invoke___show_last(self):
        self.invoke('show_last 1')

    def test_invoke___save(self):
        self.invoke('save 1 test-save')

    def test_nodes_add_node(self):
        self.invoke('nodes add_node graph_T28 RISK-443')

    def test_nodes_add_edge(self):
        self.invoke('nodes add_edge graph_WLA GSP-1 connects_to GSP-95')

    def test_story__id_stakeholders(self):
        self.invoke('story sec-47-up stakeholders')

    def test_story__story_id___stakeholder___stakeholder_id(self):
        self.invoke('story sec-47-up stakeholder GSP-1 2')


    def test_story__story_id___stakeholder___stakeholder_id__babel_admins(self):
        self.invoke('story babel-admin-vuln stakeholder GSP-4 2')


    def test_expand___abc(self):
        Dev.print(self.invoke('expand abc'))

    def test_raw_data(self):
        result = self.invoke('raw_data graph_J2O details')
        Dev.print(result)

    def test_raw_data__details(self):
        result = self.invoke('raw_data graph_MKF')
        Dev.print(result)

    def test_raw_data__issue_id(self):
        result = self.invoke('raw_data GSSP-111 details')
        Dev.print(result)