|
1 | 1 | import base64
|
2 | 2 | import json
|
3 | 3 | import os
|
| 4 | +from io import BytesIO |
4 | 5 |
|
5 | 6 | import pytest
|
6 | 7 |
|
7 |
| -from localstack.aws.api.lambda_ import InvocationType, State |
| 8 | +from localstack.aws.api.lambda_ import InvocationType, Runtime, State |
8 | 9 | from localstack.testing.aws.lambda_utils import is_new_provider, is_old_provider
|
9 | 10 | from localstack.testing.snapshots.transformer import SortingTransformer
|
10 | 11 | from localstack.utils.common import short_uid
|
| 12 | +from localstack.utils.files import load_file |
11 | 13 | from localstack.utils.http import safe_requests
|
12 | 14 | from localstack.utils.strings import to_bytes, to_str
|
13 | 15 | from localstack.utils.sync import retry, wait_until
|
14 |
| -from localstack.utils.testutil import get_lambda_log_events |
| 16 | +from localstack.utils.testutil import create_lambda_archive, get_lambda_log_events |
15 | 17 |
|
16 | 18 | pytestmark = pytest.mark.skip_snapshot_verify(
|
17 | 19 | condition=is_old_provider,
|
@@ -1024,3 +1026,35 @@ def wait_for_logs():
|
1024 | 1026 | # return len(events) >= 6 # note: each invoke comes with at least 3 events even without printing
|
1025 | 1027 |
|
1026 | 1028 | wait_until(wait_for_logs)
|
| 1029 | + |
| 1030 | + |
| 1031 | +@pytest.mark.aws_validated |
| 1032 | +def test_python_lambda_code_deployed_via_s3(deploy_cfn_template, aws_client, s3_bucket): |
| 1033 | + bucket_key = "handler.zip" |
| 1034 | + zip_file = create_lambda_archive( |
| 1035 | + load_file( |
| 1036 | + os.path.join(os.path.dirname(__file__), "../../awslambda/functions/lambda_echo.py") |
| 1037 | + ), |
| 1038 | + get_content=True, |
| 1039 | + runtime=Runtime.python3_10, |
| 1040 | + ) |
| 1041 | + aws_client.s3.upload_fileobj(BytesIO(zip_file), s3_bucket, bucket_key) |
| 1042 | + |
| 1043 | + deployment = deploy_cfn_template( |
| 1044 | + template_path=os.path.join( |
| 1045 | + os.path.dirname(__file__), "../../templates/cfn_lambda_s3_code.yaml" |
| 1046 | + ), |
| 1047 | + parameters={ |
| 1048 | + "LambdaCodeBucket": s3_bucket, |
| 1049 | + "LambdaRuntime": "python3.10", |
| 1050 | + "LambdaHandler": "handler.handler", |
| 1051 | + }, |
| 1052 | + ) |
| 1053 | + |
| 1054 | + function_name = deployment.outputs["LambdaName"] |
| 1055 | + invocation_result = aws_client.awslambda.invoke( |
| 1056 | + FunctionName=function_name, Payload=json.dumps({"hello": "world"}) |
| 1057 | + ) |
| 1058 | + payload = json.loads(to_str(invocation_result["Payload"].read())) |
| 1059 | + assert payload == {"hello": "world"} |
| 1060 | + assert invocation_result["StatusCode"] == 200 |
0 commit comments