def test_lambda_upload_file(self):
        deploy_lambda = Deploy_Lambda(self.lambda_handler)
        package = deploy_lambda.package
        aws_lambda = package.aws_lambda

        deploy_lambda.add_function_source_code()

        assert run.__module__ == self.expected_module
        assert run.__name__ == 'run'
        assert '/osbot_aws/lambdas/dev/hello_world.py' in package.get_files()
        assert len(files_list(aws_lambda.folder_code)) == len(
            package.get_files())
        assert file_exists(
            path_combine(aws_lambda.folder_code,
                         'osbot_aws/lambdas/dev/hello_world.py'))

        assert aws_lambda.s3_bucket == f'{self.expected_account_id}-osbot-lambdas'
        assert aws_lambda.s3_key == f'lambdas/{self.expected_module}.zip'

        assert self.s3.file_exists(bucket=aws_lambda.s3_bucket,
                                   key=aws_lambda.s3_key) is True
        assert self.s3.file_delete(bucket=aws_lambda.s3_bucket,
                                   key=aws_lambda.s3_key) is True
        assert self.s3.file_exists(bucket=aws_lambda.s3_bucket,
                                   key=aws_lambda.s3_key) is False

        self.s3.folder_upload(folder=aws_lambda.folder_code,
                              s3_bucket=aws_lambda.s3_bucket,
                              s3_key=aws_lambda.s3_key)

        assert self.s3.file_exists(bucket=aws_lambda.s3_bucket,
                                   key=aws_lambda.s3_key) is True
 def setUp(self) -> None:
     self.handler = run  # link to the run method in exec_commands.py
     self.deploy_lambda = Deploy_Lambda(
         self.handler)  # use OSBot_AWS helper class for deploying lambdas
     self.aws_lambda = Lambda(self.deploy_lambda.lambda_name(
     ))  # use OSBot_Lambda helper class to invoke lambdas
     print()
Example #3
0
class Deploy:
    def __init__(self, handler):
        Env()
        self.deploy_lambda = Deploy_Lambda(handler)

    def now(self):
        return self.deploy_lambda.update()

    def invoke(self, params=None):
        return self.deploy_lambda.lambda_invoke(params)
    def test_lambda_create_using_deploy(self):
        deploy_lambda = Deploy_Lambda(self.lambda_handler)
        aws_lambda = deploy_lambda.package.aws_lambda

        aws_lambda.delete()

        assert aws_lambda.exists() is False

        assert deploy_lambda.deploy() is True
        assert aws_lambda.exists() is True
        assert aws_lambda.invoke() == 'From lambda code, hello None'
        assert aws_lambda.delete() is True
    def test_lambda_create_manually(self):
        deploy_lambda = Deploy_Lambda(self.lambda_handler)
        aws_lambda = deploy_lambda.package.aws_lambda
        function_arn = f'arn:aws:lambda:{self.expected_region}:{self.expected_account_id}:function:{self.function_name}'

        aws_lambda.delete()

        assert aws_lambda.exists() is False

        result = aws_lambda.create()

        data = result.get('data')

        assert aws_lambda.exists() is True

        assert result.get('status') == 'ok'
        assert result.get('name') == self.function_name
        assert result.get('status') == 'ok'
        assert data.get('CodeSize') > 80000
        assert data.get('FunctionArn') == function_arn
        assert data.get('Handler') == f'{self.expected_module}.run'
        assert data.get('LastUpdateStatus') == 'Successful'
        assert data.get('MemorySize') == 10240
        assert data.get('PackageType') == 'Zip'

        assert aws_lambda.invoke() == 'From lambda code, hello None'
        assert aws_lambda.delete()
        assert aws_lambda.invoke() == {
            'error':
            'An error occurred (ResourceNotFoundException) when calling the '
            'Invoke operation: Function not found: '
            f'{function_arn}'
        }
class test_hello_world(TestCase):
    def setUp(self) -> None:
        self.handler = run  # link to the run method in exec_commands.py
        self.deploy_lambda = Deploy_Lambda(
            self.handler)  # use OSBot_AWS helper class for deploying lambdas
        self.aws_lambda = Lambda(self.deploy_lambda.lambda_name(
        ))  # use OSBot_Lambda helper class to invoke lambdas
        print()

    def update_lambda(self):
        self.deploy_lambda.add_osbot_utils(
        )  # adding the osbot_utils package to the source code
        self.deploy_lambda.deploy()  # create or update lambda function

    def exec_in_lambda(self, command, params=None):
        #self.update_lambda()
        event = {'command': command, 'params': params}
        result = self.aws_lambda.invoke(event)
        return trim(result.get('error' )) + \
               trim(result.get('stderr')) + \
               trim(result.get('stdout'))

    def test_invoke_lambda(self):
        result = self.exec_in_lambda('ls', '/bin')
        print(result)

    def test_ip(self):
        assert self.exec_in_lambda(
            'ip') == "[Errno 2] No such file or directory: 'ip'"

    def test_ls(self):
        assert self.exec_in_lambda(
            'ls') == 'dotenv\nk8_kubernetes\nosbot_utils'

    def test_ls_root(self):
        assert self.exec_in_lambda('ls', '/') == (
            'bin\nboot\ndev\netc\nhome\nlib\nlib64\nmedia\nmnt\nopt\nproc\nroot\nrun\nsbin\nsrv\nsys\ntmp\nusr\nvar'
        )

    def test_pwd(self):
        assert self.exec_in_lambda('pwd') == '/var/task'

    def test_uname(self):
        assert self.exec_in_lambda('uname') == 'Linux'
Example #7
0
class test_run_command(Test_Helper):
    def setUp(self):
        super().setUp()
        self.handler = run
        self.aws_lambda = Deploy_Lambda(self.handler)
        self.lambda_shell = Lambda_Shell()

    def test_invoke_directly(self):
        assert run({}) == 'in lambda shell'
        auth_key = self.lambda_shell.get_lambda_shell_auth()
        pprint(auth_key)
        self.result = run(
            {'lambda_shell': {
                'method_name': 'ping',
                'auth_key': auth_key
            }})

    def test_update_lambda_function(self):
        self.aws_lambda.add_osbot_aws().deploy()
        assert self.aws_lambda.invoke({}) == 'in lambda shell'

    def test_invoke_shell_cmd(self):
        self.aws_lambda.add_osbot_aws().deploy()
        auth_key = self.lambda_shell.get_lambda_shell_auth()
        method_name = 'ping'
        method_kwargs = {}
        payload = {
            'lambda_shell': {
                'method_name': method_name,
                'method_kwargs': method_kwargs,
                'auth_key': auth_key
            }
        }
        self.result = self.aws_lambda.invoke(payload)
class test_hello_world(TestCase):
    def setUp(self) -> None:
        self.handler = run
        self.deploy = Deploy_Lambda(self.handler)
        self.lambda_name = self.deploy.lambda_name()

    def test_invoke_directly(self):
        assert self.handler.__module__ == self.lambda_name
        assert self.handler({}) == 'From lambda code, hello None'
        assert self.handler({'name':
                             'world'}) == 'From lambda code, hello world'

    def test_deploy_lambda(self):
        assert self.deploy.deploy() is True

    def test_invoke_lambda__using_k8_lambda_class(self):
        assert self.deploy.invoke() == 'From lambda code, hello None'

    def test_invoke_lambda__using_lambda_class(self):
        event = {'name': 'world'}
        assert Lambda(
            self.lambda_name).invoke(event) == 'From lambda code, hello world'
Example #9
0
    def test_update(self):
        deploy = Deploy_Lambda(self.lambda_function)
        result = deploy.update()

        assert result['status'] == 'ok'
        assert result['name'] == self.deploy.lambda_name().replace('.', "_")

        assert result['data'][
            'FunctionArn'] == f'arn:aws:lambda:{self.aws_config.aws_session_region_name()}:{self.aws_config.aws_session_account_id()}:function:osbot_test_deploy_lambda_osbot_test_deploy_lambda'
        assert result['data'][
            'FunctionName'] == 'osbot_test_deploy_lambda_osbot_test_deploy_lambda'
        assert result['data'][
            'Handler'] == 'osbot_test_deploy_lambda.osbot_test_deploy_lambda.run'
        assert result['data']['MemorySize'] == 10240
        assert result['data']['PackageType'] == 'Zip'

        assert deploy.invoke() == 'hello None'
        assert deploy.invoke({"name": "world"}) == "hello world"

        print(deploy.delete())
Example #10
0
class test_run_command(Test_Helper):
    def setUp(self):
        super().setUp()
        self.handler     = run
        self.aws_lambda  = Deploy_Lambda(self.handler)

    def test_invoke_directly(self):
        #self.result = run({'method_name': 'pwd'},{})
        self.result = run({'method_name': 'python_exec','method_kwargs': {'code': 'result=40+2' }})

    def test_update_lambda_function(self):
        self.aws_lambda.add_osbot_aws().deploy()
        self.result = self.aws_lambda.invoke({'method_name': 'ping'})

    # test the invocation
    def test_invoke(self):
        #self.test_update_lambda_function()
        pprint(self.aws_lambda.invoke({'method_name': 'ping'          }).get('return_value'))
        pprint(self.aws_lambda.invoke({'method_name': 'pwd'           }).get('return_value'))
        pprint(self.aws_lambda.invoke({'method_name': 'disk_space'    }).get('return_value'))
        pprint(self.aws_lambda.invoke({'method_name'  : 'python_exec',
                                       'method_kwargs': {'code':"result=40+2"}}))
Example #11
0
 def __init__(self, handler):
     self.deploy_lambda = Deploy_Lambda(handler)
     self.deploy_lambda.package.aws_lambda.runtime = 'python3.7'
Example #12
0
 def setUp(self):
     super().setUp()
     self.handler = run
     self.aws_lambda = Deploy_Lambda(self.handler)
     self.lambda_shell = Lambda_Shell()
 def setUp(self) -> None:
     STS().check_current_session_credentials()
     self.handler = run
     self.deploy = Deploy_Lambda(self.handler)
     self.lambda_name = self.deploy.lambda_name()
Example #14
0
 def __init__(self, handler):
     Env()
     self.deploy_lambda = Deploy_Lambda(handler)
Example #15
0
 def setUp(self):
     super().setUp()
     self.handler     = run
     self.aws_lambda  = Deploy_Lambda(self.handler)
Example #16
0
 def setUp(self) -> None:
     self.deploy = Deploy_Lambda(self.lambda_function)
     self.aws_config = AWS_Config()
     # Test_Helper().check_aws_token()
     pass
Example #17
0
class test_Deploy_Lambda(TestCase):
    lambda_name = None
    lambda_code = None
    code_folder = None

    @staticmethod
    def setup_test_environment__Deploy_Lambda(
            cls):  # todo: refactor into separate class
        STS().check_current_session_credentials()
        cls.lambda_name = "osbot_test_deploy_lambda"
        cls.lambda_code = Temp_Folder_With_Lambda_File(cls.lambda_name)
        cls.code_folder = cls.lambda_code.folder

        lambda_file = cls.lambda_code.tmp_file
        module_folder = path_combine(cls.code_folder,
                                     "osbot_test_deploy_lambda")
        lambda_in_module = path_combine(module_folder, file_name(lambda_file))
        folder_create(module_folder)
        file_copy(lambda_file,
                  lambda_in_module)  # todo add a file_move to OSBot_Utils
        file_delete(lambda_file)
        file_create(path_combine(module_folder, '__init__.py'), "")

        sys.path.append(cls.code_folder)

        cls.lambda_module = importlib.import_module(
            "osbot_test_deploy_lambda.osbot_test_deploy_lambda")
        cls.lambda_function = cls.lambda_module.run

    @staticmethod
    def teardown_test_environment__Deploy_Lambda(cls):
        sys.path.remove(cls.code_folder)
        pass

    @classmethod
    def setUpClass(cls) -> None:
        cls.setup_test_environment__Deploy_Lambda(cls)

    @classmethod
    def tearDownClass(cls):
        cls.teardown_test_environment__Deploy_Lambda(cls)

    def setUp(self) -> None:
        self.deploy = Deploy_Lambda(self.lambda_function)
        self.aws_config = AWS_Config()
        # Test_Helper().check_aws_token()
        pass

    def tearDown(self):
        self.deploy.delete()

    def test_check_aws_role(self):
        assert Temp_Aws_Roles().for_lambda_invocation_exists()

    def test_get_package(self):
        package = self.deploy.get_package()
        assert package.lambda_name == 'osbot_test_deploy_lambda.osbot_test_deploy_lambda'
        assert package.s3_bucket == self.aws_config.lambda_s3_bucket()
        assert package.s3_key == f'{self.aws_config.lambda_s3_folder_lambdas()}/{package.lambda_name}.zip'
        assert package.role_arn == f"arn:aws:iam::{self.aws_config.aws_session_account_id()}:role/temp_role_for_lambda_invocation"
        assert folder_exists(package.tmp_folder)

    def test_update(self):
        deploy = Deploy_Lambda(self.lambda_function)
        result = deploy.update()

        assert result['status'] == 'ok'
        assert result['name'] == self.deploy.lambda_name().replace('.', "_")

        assert result['data'][
            'FunctionArn'] == f'arn:aws:lambda:{self.aws_config.aws_session_region_name()}:{self.aws_config.aws_session_account_id()}:function:osbot_test_deploy_lambda_osbot_test_deploy_lambda'
        assert result['data'][
            'FunctionName'] == 'osbot_test_deploy_lambda_osbot_test_deploy_lambda'
        assert result['data'][
            'Handler'] == 'osbot_test_deploy_lambda.osbot_test_deploy_lambda.run'
        assert result['data']['MemorySize'] == 10240
        assert result['data']['PackageType'] == 'Zip'

        assert deploy.invoke() == 'hello None'
        assert deploy.invoke({"name": "world"}) == "hello world"

        print(deploy.delete())

    def test_invoke(self):
        self.deploy.update()

        assert self.deploy.invoke({"name": "world"}) == "hello world"

        #invoke directly
        aws_lambda = Lambda(name=self.deploy.lambda_name())
        assert aws_lambda.invoke() == 'hello None'
        assert aws_lambda.invoke({'name': 'world'}) == 'hello world'
 def setUp(self) -> None:
     self.handler = run
     self.deploy = Deploy_Lambda(self.handler)
     self.lambda_name = self.deploy.lambda_name()