From 90a9235f4c8cecab346a7d0d5b21aa3bfc2f32fe Mon Sep 17 00:00:00 2001 From: Ian Yon Date: Mon, 4 Nov 2019 10:44:01 -0300 Subject: [PATCH] Allow lambda get function by arn and addded environment configuration --- moto/awslambda/models.py | 16 ++++++++------- moto/awslambda/responses.py | 2 +- tests/test_awslambda/test_lambda.py | 30 ++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/moto/awslambda/models.py b/moto/awslambda/models.py index 265472067..234950125 100644 --- a/moto/awslambda/models.py +++ b/moto/awslambda/models.py @@ -291,6 +291,8 @@ class LambdaFunction(BaseModel): self.timeout = value elif key == "VpcConfig": self.vpc_config = value + elif key == "Environment": + self.environment_vars = value["Variables"] return self.get_configuration() @@ -589,7 +591,7 @@ class LambdaStorage(object): def _get_alias(self, name, alias): return self._functions[name]['alias'].get(alias, None) - def get_function(self, name, qualifier=None): + def get_function_by_name(self, name, qualifier=None): if name not in self._functions: return None @@ -612,8 +614,8 @@ class LambdaStorage(object): def get_arn(self, arn): return self._arns.get(arn, None) - def get_function_by_name_or_arn(self, input): - return self.get_function(input) or self.get_arn(input) + def get_function_by_name_or_arn(self, input, qualifier=None): + return self.get_function_by_name(input, qualifier) or self.get_arn(input) def put_function(self, fn): """ @@ -671,7 +673,7 @@ class LambdaStorage(object): return True else: - fn = self.get_function(name, qualifier) + fn = self.get_function_by_name(name, qualifier) if fn: self._functions[name]['versions'].remove(fn) @@ -766,8 +768,8 @@ class LambdaBackend(BaseBackend): def publish_function(self, function_name): return self._lambdas.publish_function(function_name) - def get_function(self, function_name, qualifier=None): - return self._lambdas.get_function(function_name, qualifier) + def get_function(self, function_name_or_arn, qualifier=None): + return self._lambdas.get_function_by_name_or_arn(function_name_or_arn, qualifier) def list_versions_by_function(self, function_name): return self._lambdas.list_versions_by_function(function_name) @@ -877,7 +879,7 @@ class LambdaBackend(BaseBackend): ] } - func = self._lambdas.get_function(function_name, qualifier) + func = self._lambdas.get_function_by_name_or_arn(function_name, qualifier) func.invoke(json.dumps(event), {}, {}) def send_dynamodb_items(self, function_arn, items, source): diff --git a/moto/awslambda/responses.py b/moto/awslambda/responses.py index 5ad333abe..37016e718 100644 --- a/moto/awslambda/responses.py +++ b/moto/awslambda/responses.py @@ -276,7 +276,7 @@ class LambdaResponse(BaseResponse): return 404, {}, "{}" def _get_function(self, request, full_url, headers): - function_name = self.path.rsplit('/', 1)[-1] + function_name = unquote(self.path.rsplit('/', 1)[-1]) qualifier = self._get_param('Qualifier', None) fn = self.lambda_backend.get_function(function_name, qualifier) diff --git a/tests/test_awslambda/test_lambda.py b/tests/test_awslambda/test_lambda.py index 755661702..1ac1cb2a6 100644 --- a/tests/test_awslambda/test_lambda.py +++ b/tests/test_awslambda/test_lambda.py @@ -379,6 +379,7 @@ def test_get_function(): Timeout=3, MemorySize=128, Publish=True, + Environment={"Variables": {"test_variable": "test_value"}} ) result = conn.get_function(FunctionName='testFunction') @@ -403,6 +404,9 @@ def test_get_function(): result['Configuration']['Timeout'].should.equal(3) result['Configuration']['Version'].should.equal('$LATEST') result['Configuration'].should.contain('VpcConfig') + result['Configuration'].should.contain('Environment') + result['Configuration']['Environment'].should.contain('Variables') + result['Configuration']['Environment']["Variables"].should.equal({"test_variable": "test_value"}) # Test get function with result = conn.get_function(FunctionName='testFunction', Qualifier='$LATEST') @@ -414,6 +418,27 @@ def test_get_function(): with assert_raises(ClientError): conn.get_function(FunctionName='junk', Qualifier='$LATEST') +@mock_lambda +@mock_s3 +def test_get_function_by_arn(): + bucket_name = 'test-bucket' + s3_conn = boto3.client('s3', 'us-east-1') + s3_conn.create_bucket(Bucket=bucket_name) + + zip_content = get_test_zip_file2() + s3_conn.put_object(Bucket=bucket_name, Key='test.zip', Body=zip_content) + conn = boto3.client('lambda', 'us-east-1') + + fnc = conn.create_function(FunctionName='testFunction', + Runtime='python2.7', Role='test-iam-role', + Handler='lambda_function.lambda_handler', + Code={'S3Bucket': bucket_name, 'S3Key': 'test.zip'}, + Description='test lambda function', + Timeout=3, MemorySize=128, Publish=True) + + result = conn.get_function(FunctionName=fnc['FunctionArn']) + result['Configuration']['FunctionName'].should.equal('testFunction') + @mock_lambda @mock_s3 @@ -1346,6 +1371,7 @@ def test_update_configuration(): Timeout=3, MemorySize=128, Publish=True, + Environment={'Variables': {"test_old_environment": "test_old_value"}} ) assert fxn['Description'] == 'test lambda function' @@ -1359,7 +1385,8 @@ def test_update_configuration(): Description='updated test lambda function', Handler='lambda_function.new_lambda_handler', Runtime='python3.6', - Timeout=7 + Timeout=7, + Environment={'Variables': {"test_environment": "test_value"}} ) assert updated_config['ResponseMetadata']['HTTPStatusCode'] == 200 @@ -1368,6 +1395,7 @@ def test_update_configuration(): assert updated_config['MemorySize'] == 128 assert updated_config['Runtime'] == 'python3.6' assert updated_config['Timeout'] == 7 + assert updated_config['Environment']['Variables'] == {"test_environment": "test_value"} @mock_lambda