예제 #1
0
    def test_publish_update_version_increment(self):
        with self.app.test_request_context():
            self._create_function(self.FUNCTION_NAME)
            lambda_api.publish_version(self.FUNCTION_NAME)

            self._update_function_code(self.FUNCTION_NAME)
            result = json.loads(lambda_api.publish_version(self.FUNCTION_NAME).get_data())
            result.pop('RevisionId', None)  # we need to remove this, since this is random, so we cannot know its value

            expected_result = dict()
            expected_result['CodeSize'] = self.CODE_SIZE
            expected_result['CodeSha256'] = self.UPDATED_CODE_SHA_256
            expected_result['FunctionArn'] = str(lambda_api.func_arn(self.FUNCTION_NAME)) + ':2'
            expected_result['FunctionName'] = str(self.FUNCTION_NAME)
            expected_result['Handler'] = str(self.HANDLER)
            expected_result['Runtime'] = str(self.RUNTIME)
            expected_result['Timeout'] = self.TIMEOUT
            expected_result['Description'] = ''
            expected_result['MemorySize'] = self.MEMORY_SIZE
            expected_result['Role'] = self.ROLE
            expected_result['KMSKeyArn'] = None
            expected_result['VpcConfig'] = None
            expected_result['LastModified'] = isoformat_milliseconds(self.LAST_MODIFIED) + '+0000'
            expected_result['TracingConfig'] = self.TRACING_CONFIG
            expected_result['Version'] = '2'
            expected_result['State'] = 'Active'
            expected_result['LastUpdateStatus'] = 'Successful'
            expected_result['PackageType'] = None
            self.assertDictEqual(expected_result, result)
예제 #2
0
    def test_publish_update_version_increment(self):
        with self.app.test_request_context():
            self._create_function(self.FUNCTION_NAME)
            lambda_api.publish_version(self.FUNCTION_NAME)

            self._update_function_code(self.FUNCTION_NAME)
            result = json.loads(
                lambda_api.publish_version(self.FUNCTION_NAME).get_data())
            result.pop(
                "RevisionId", None
            )  # we need to remove this, since this is random, so we cannot know its value

            expected_result = dict()
            expected_result["CodeSize"] = self.CODE_SIZE
            expected_result["CodeSha256"] = self.UPDATED_CODE_SHA_256
            expected_result["FunctionArn"] = str(
                lambda_api.func_arn(self.FUNCTION_NAME)) + ":2"
            expected_result["FunctionName"] = str(self.FUNCTION_NAME)
            expected_result["Handler"] = str(self.HANDLER)
            expected_result["Runtime"] = str(self.RUNTIME)
            expected_result["Timeout"] = self.TIMEOUT
            expected_result["Description"] = ""
            expected_result["MemorySize"] = self.MEMORY_SIZE
            expected_result["Role"] = self.ROLE
            expected_result["KMSKeyArn"] = None
            expected_result["VpcConfig"] = None
            expected_result["LastModified"] = isoformat_milliseconds(
                self.LAST_MODIFIED) + "+0000"
            expected_result["TracingConfig"] = self.TRACING_CONFIG
            expected_result["Version"] = "2"
            expected_result["State"] = "Active"
            expected_result["LastUpdateStatus"] = "Successful"
            expected_result["PackageType"] = None
            expected_result["ImageConfig"] = {}
            self.assertDictEqual(expected_result, result)
예제 #3
0
def create_function():
    """ Create new function
        ---
        operationId: 'createFunction'
        parameters:
            - name: 'request'
              in: body
    """
    arn = 'n/a'
    try:
        data = json.loads(to_str(request.data))
        lambda_name = data['FunctionName']
        event_publisher.fire_event(
            event_publisher.EVENT_LAMBDA_CREATE_FUNC,
            payload={'n': event_publisher.get_hash(lambda_name)})
        arn = func_arn(lambda_name)
        if arn in arn_to_lambda:
            return error_response('Function already exist: %s' % lambda_name,
                                  409,
                                  error_type='ResourceConflictException')
        arn_to_lambda[arn] = func_details = LambdaFunction(arn)
        func_details.versions = {'$LATEST': {'RevisionId': str(uuid.uuid4())}}
        func_details.last_modified = isoformat_milliseconds(
            datetime.utcnow()) + '+0000'
        func_details.description = data.get('Description', '')
        func_details.handler = data['Handler']
        func_details.runtime = data['Runtime']
        func_details.envvars = data.get('Environment', {}).get('Variables', {})
        func_details.tags = data.get('Tags', {})
        func_details.timeout = data.get('Timeout', LAMBDA_DEFAULT_TIMEOUT)
        func_details.role = data['Role']
        func_details.memory_size = data.get('MemorySize')
        func_details.code = data['Code']
        result = set_function_code(func_details.code, lambda_name)
        if isinstance(result, Response):
            del arn_to_lambda[arn]
            return result
        # remove content from code attribute, if present
        func_details.code.pop('ZipFile', None)
        # prepare result
        result.update(format_func_details(func_details))
        if data.get('Publish', False):
            result['Version'] = publish_new_function_version(arn)['Version']
        return jsonify(result or {})
    except Exception as e:
        arn_to_lambda.pop(arn, None)
        if isinstance(e, ClientError):
            return e.get_response()
        return error_response('Unknown error: %s %s' %
                              (e, traceback.format_exc()))
예제 #4
0
    def test_list_function_versions(self):
        with self.app.test_request_context():
            self._create_function(self.FUNCTION_NAME)
            lambda_api.publish_version(self.FUNCTION_NAME)
            lambda_api.publish_version(self.FUNCTION_NAME)

            result = json.loads(
                lambda_api.list_versions(self.FUNCTION_NAME).get_data())
            for version in result["Versions"]:
                # we need to remove this, since this is random, so we cannot know its value
                version.pop("RevisionId", None)

            latest_version = dict()
            latest_version["CodeSize"] = self.CODE_SIZE
            latest_version["CodeSha256"] = self.CODE_SHA_256
            latest_version["FunctionArn"] = (
                str(lambda_api.func_arn(self.FUNCTION_NAME)) + ":$LATEST")
            latest_version["FunctionName"] = str(self.FUNCTION_NAME)
            latest_version["Handler"] = str(self.HANDLER)
            latest_version["Runtime"] = str(self.RUNTIME)
            latest_version["Timeout"] = self.TIMEOUT
            latest_version["Description"] = ""
            latest_version["MemorySize"] = self.MEMORY_SIZE
            latest_version["Role"] = self.ROLE
            latest_version["KMSKeyArn"] = None
            latest_version["VpcConfig"] = None
            latest_version["LastModified"] = isoformat_milliseconds(
                self.LAST_MODIFIED) + "+0000"
            latest_version["TracingConfig"] = self.TRACING_CONFIG
            latest_version["Version"] = "$LATEST"
            latest_version["State"] = "Active"
            latest_version["LastUpdateStatus"] = "Successful"
            latest_version["PackageType"] = None
            latest_version["ImageConfig"] = {}
            version1 = dict(latest_version)
            version1["FunctionArn"] = str(
                lambda_api.func_arn(self.FUNCTION_NAME)) + ":1"
            version1["Version"] = "1"
            expected_result = {
                "Versions":
                sorted([latest_version, version],
                       key=lambda k: str(k.get("Version")))
            }
            self.assertDictEqual(expected_result, result)
예제 #5
0
    def test_list_function_versions(self):
        with self.app.test_request_context():
            self._create_function(self.FUNCTION_NAME)
            lambda_api.publish_version(self.FUNCTION_NAME)
            lambda_api.publish_version(self.FUNCTION_NAME)

            result = json.loads(
                lambda_api.list_versions(self.FUNCTION_NAME).get_data())
            for version in result['Versions']:
                # we need to remove this, since this is random, so we cannot know its value
                version.pop('RevisionId', None)

            latest_version = dict()
            latest_version['CodeSize'] = self.CODE_SIZE
            latest_version['CodeSha256'] = self.CODE_SHA_256
            latest_version['FunctionArn'] = str(
                lambda_api.func_arn(self.FUNCTION_NAME)) + ':$LATEST'
            latest_version['FunctionName'] = str(self.FUNCTION_NAME)
            latest_version['Handler'] = str(self.HANDLER)
            latest_version['Runtime'] = str(self.RUNTIME)
            latest_version['Timeout'] = self.TIMEOUT
            latest_version['Description'] = ''
            latest_version['MemorySize'] = self.MEMORY_SIZE
            latest_version['Role'] = self.ROLE
            latest_version['KMSKeyArn'] = None
            latest_version['VpcConfig'] = None
            latest_version['LastModified'] = isoformat_milliseconds(
                self.LAST_MODIFIED) + '+0000'
            latest_version['TracingConfig'] = self.TRACING_CONFIG
            latest_version['Version'] = '$LATEST'
            latest_version['State'] = 'Active'
            latest_version['LastUpdateStatus'] = 'Successful'
            latest_version['PackageType'] = None
            latest_version['ImageConfig'] = {}
            version1 = dict(latest_version)
            version1['FunctionArn'] = str(
                lambda_api.func_arn(self.FUNCTION_NAME)) + ':1'
            version1['Version'] = '1'
            expected_result = {
                'Versions':
                sorted([latest_version, version],
                       key=lambda k: str(k.get('Version')))
            }
            self.assertDictEqual(expected_result, result)
예제 #6
0
    def put_function_event_invoke_config(self, data):
        if not isinstance(data, dict):
            return

        updated = False
        if 'DestinationConfig' in data:
            if 'OnFailure' in data['DestinationConfig']:
                dlq_arn = data['DestinationConfig']['OnFailure']['Destination']
                self.on_failed_invocation = dlq_arn
                updated = True

            if 'OnSuccess' in data['DestinationConfig']:
                sq_arn = data['DestinationConfig']['OnSuccess']['Destination']
                self.on_successful_invocation = sq_arn
                updated = True

        if 'MaximumRetryAttempts' in data:
            try:
                max_retry_attempts = int(data['MaximumRetryAttempts'])
            except Exception:
                max_retry_attempts = 3

            self.max_retry_attempts = max_retry_attempts
            updated = True

        if 'MaximumEventAgeInSeconds' in data:
            try:
                max_event_age = int(data['MaximumEventAgeInSeconds'])
            except Exception:
                max_event_age = 3600

            self.max_event_age = max_event_age
            updated = True

        if updated:
            self.last_modified = isoformat_milliseconds(
                datetime.utcnow()) + '+0000'

        return self
예제 #7
0
 def test_isoformat_milliseconds(self):
     env = common.isoformat_milliseconds(datetime(2010, 3, 20, 7, 24, 00,
                                                  0))
     self.assertEqual(env, '2010-03-20T07:24:00.000')
예제 #8
0
 def test_isoformat_milliseconds(self):
     env = common.isoformat_milliseconds(datetime(2010, 3, 20, 7, 24, 00, 0))
     assert env == "2010-03-20T07:24:00.000"