Ejemplo n.º 1
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
Ejemplo n.º 2
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))
Ejemplo n.º 3
0
class Deploy:
    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 setup(self):
        (self.osbot._lambda.set_s3_bucket(self.tmp_s3_bucket).set_s3_key(
            self.tmp_s3_key).set_xrays_on())

    def deploy(self, delete_before=False):
        if delete_before:
            self.osbot.delete()
        code_folder = Files.path_combine(__file__, '..')
        self.osbot.add_folder(code_folder)
        self.osbot.add_root_folder()
        self.osbot.add_pbx_gs_python_utils()
        return self.osbot.update()
Ejemplo n.º 4
0
class Deploy:
    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.package._lambda.set_s3_bucket(self.tmp_s3_bucket).set_s3_key(
            self.tmp_s3_key).set_xrays_on())

    def deploy(self, delete_before=False):
        if delete_before:
            self.package.delete()
        code_folder = Files.path_combine(__file__, '..')
        self.package.add_folder(code_folder)
        self.package.add_root_folder()
        self.package.add_pbx_gs_python_utils()
        #Dev.pprint(self.package.get_files())
        return self.package.update()
Ejemplo n.º 5
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()