예제 #1
0
    def setup_class(cls):
        """
        This method does the setup and script execution once for all the tests
        """
        # setup test
        # unpack the example_skills in a temp dir
        cls.output_dir = tempfile.mkdtemp()
        cls.cmd = ['unpack_example_skills', '--output-dir', cls.output_dir]
        # this will raise an exception if the return code is not 0
        # TODO: log process output
        # this will raise an exception if the return code is not 0
        std_out = subprocess.check_output(cls.cmd)
        print(std_out)
        cls.created_examples_dir = os.path.join(cls.output_dir,
                                                'example_skills')

        # prepare running of the update script - most importantly delete
        # any existing test functions in the cloud
        cls.alexa_event_template = lambda_utils.get_eventtemplate_fn()
        cls.alexa_test_data = os.path.join(cls.created_examples_dir,
                                           'alexa_skill_first', 'tests',
                                           'data', 'lambda_test_data.json')
        cls.user = '******'
        cls.region = 'eu-west-1'
        cls.execution_role = 'basic_lambda_execute'
        cls.script = 'update_lambda_function'
        cls.skill_dir = os.path.join(cls.created_examples_dir,
                                     'alexa_skill_first')
        cls.function_name = 'firstskill_test'
        cls.session = boto3.Session(profile_name=cls.user,
                                    region_name=cls.region)
        cls.client = cls.session.client('lambda')
        # check if lambda function exists and if yes, delete it
        existing_funs_list = lambda_utils.list_lambda_functions(cls.client)
        if cls.function_name in existing_funs_list:
            response = cls.client.delete_function(
                FunctionName=cls.function_name)
            print(response)
        cls.cmd = [
            cls.script, '--function-name', cls.function_name, '--dir',
            cls.skill_dir, '--test-data', cls.alexa_test_data
        ]
        existing_funs_list = lambda_utils.list_lambda_functions(cls.client)
        if cls.function_name in existing_funs_list:
            raise Exception("Lambda function already exists - "
                            "perhaps it couldn't be deleted correctly?!")
        time.sleep(1)
        # create the function so that we can update it
        create_cmd = [
            'create_lambda_function', '--function-name', cls.function_name,
            '--dir', cls.skill_dir, '--execution-role', cls.execution_role,
            '--test-data', cls.alexa_test_data
        ]
        subprocess.call(create_cmd)
 def teardown_class(cls):
     """This method is run once for each class _after_ all tests are run"""
     # delete the created function
     session = boto3.Session(profile_name=cls.user, region_name=cls.region)
     client = session.client('lambda')
     # create the function if it doesn't exist
     existing_funs_list = lambda_utils.list_lambda_functions(client)
     if cls.function_name in existing_funs_list:
         response = client.delete_function(FunctionName=cls.function_name)
         print(response)
 def teardown_class(cls):
     """This method is run once for each class _after_ all tests are run"""
     # delete the created function
     if os.path.exists(cls.output_dir):
         shutil.rmtree(cls.output_dir)
     # delete the created function
     existing_funs_list = lambda_utils.list_lambda_functions(cls.client)
     if cls.function_name in existing_funs_list:
         response = cls.client.delete_function(
             FunctionName=cls.function_name)
         print(response)
 def setup_class(cls):
     """This method does the setup and script execution once for all the tests"""
     # setup test:
     cls.user = '******'
     cls.region = 'eu-west-1'
     account_id = lambda_utils.get_account_id(profile_name=cls.user,
                                              region=cls.region)
     cls.execution_role = 'arn:aws:iam::' + account_id + ':role/basic_lambda_execute'
     cls.dir = os.path.join(package_root_dir, 'example_skills',
                            'alexa_skill_first')
     cls.event_template = lambda_utils.get_eventtemplate_fn()
     cls.alexa_event_data = os.path.join(package_root_dir, 'example_skills',
                                         'alexa_skill_first', 'tests',
                                         'data', 'lambda_test_data.json')
     # not generating random names on purpose,
     # if things go wrong, we don't pollute the account
     cls.function_name = 'firstskill_test'
     cls.session = boto3.Session(profile_name=cls.user,
                                 region_name=cls.region)
     cls.client = cls.session.client('lambda')
     # create the function if it doesn't exist
     existing_funs_list = lambda_utils.list_lambda_functions(cls.client)
     if cls.function_name in existing_funs_list:
         response = cls.client.delete_function(
             FunctionName=cls.function_name)
         print(response)
         time.sleep(1)
     fun_arn = lambda_utils.create_lambda(cls.client, cls.function_name,
                                          cls.dir, cls.execution_role)
     if fun_arn:
         print("Function succesfully created!")
         print("AWS Lambda function ARN: " + str(fun_arn))
     else:
         raise Exception(
             "Failed to create Lambda function when setting up test!")
     cls.event_list = lambda_utils.generate_testevents(
         cls.alexa_event_data, cls.event_template)
     cls.ref_reply_list = [
         ["hmm not sure how to deal with your request"],
         ["I say whatever I please"], [None],
         ['robogals was founded in 2008 in melbourne, australia'],
         ["Lord of the rings", "Batman, the dark knight", "Indiana Jones"]
     ]
    def setup_class(cls):
        """This method does the setup and script execution once for all the tests"""
        # setup test
        # load reference reply
        cls.ref_reply = [
            '############################################',
            'Testing function now!',
            '############################################',
            'Sending Alexa Intent: FakeIntent and slots:{}',
            'Lambda function replied: hmm not sure how to deal with your request',
            'Sending Alexa Intent: saySomethingIntent and slots:{}',
            'Lambda function replied: I say whatever I please'
        ]
        # unpack the example_skills in a temp dir
        cls.output_dir = tempfile.mkdtemp()
        cls.cmd = ['unpack_example_skills', '--output-dir', cls.output_dir]
        # this will raise an exception if the return code is not 0
        std_out = subprocess.check_output(cls.cmd)
        print(std_out)
        cls.created_examples_dir = os.path.join(cls.output_dir,
                                                'example_skills')

        # prepare running of the update script - most importantly,
        # delete any existing test functions in the cloud
        cls.alexa_event_template = lambda_utils.get_eventtemplate_fn()
        cls.alexa_test_data = os.path.join(cls.created_examples_dir,
                                           'alexa_skill_first', 'tests',
                                           'data', 'lambda_test_data.json')
        cls.user = '******'
        cls.region = 'eu-west-1'
        cls.execution_role = 'basic_lambda_execute'
        cls.script = 'test_lambda_function'
        cls.skill_dir = os.path.join(cls.created_examples_dir,
                                     'alexa_skill_first')
        cls.function_name = 'firstskill_test'
        cls.session = boto3.Session(profile_name=cls.user,
                                    region_name=cls.region)
        cls.client = cls.session.client('lambda')
        # check if lambda function exists and if yes, delete it
        existing_funs_list = lambda_utils.list_lambda_functions(cls.client)
        if cls.function_name in existing_funs_list:
            response = cls.client.delete_function(
                FunctionName=cls.function_name)
            print(response)
        create_cmd = [
            'create_lambda_function', '--function-name', cls.function_name,
            '--dir', cls.skill_dir, '--execution-role', cls.execution_role
        ]
        cls.cmd = [
            cls.script, '--function-name', cls.function_name, '--dir',
            cls.skill_dir, '--execution-role', cls.execution_role,
            '--test-data', cls.alexa_test_data
        ]
        time.sleep(1)
        existing_funs_list = lambda_utils.list_lambda_functions(cls.client)
        if cls.function_name in existing_funs_list:
            raise Exception("Lambda function already exists - "
                            "perhaps it couldn't be deleted correctly?!")
        else:
            # this will raise an exception if the return code is not 0
            std_out = subprocess.check_output(create_cmd)
            print(std_out)
            existing_funs_list = lambda_utils.list_lambda_functions(cls.client)
            if cls.function_name not in existing_funs_list:
                raise Exception("Lambda function couldn't be created - "
                                "test setup failed")