Skip to content
This repository was archived by the owner on Sep 10, 2023. It is now read-only.

Fix/add context to invoke lambda #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG PYTHON_IMAGE_TAG=3.7
ARG PYTHON_IMAGE_TAG=3.9
FROM python:$PYTHON_IMAGE_TAG

WORKDIR /app
Expand Down
417 changes: 207 additions & 210 deletions dev-requirements.txt

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
build:
context: .
args:
PYTHON_IMAGE_TAG: "${PYTEST_STEPFUNCTIONS_PYTHON_IMAGE_TAG:-3.7}"
PYTHON_IMAGE_TAG: "${PYTEST_STEPFUNCTIONS_PYTHON_IMAGE_TAG:-3.9}"
dockerfile: ./Dockerfile
environment:
AWS_DEFAULT_REGION: us-east-1
Expand All @@ -18,7 +18,7 @@ services:
./tests/pytest_stepfunctions_test"

sfn-endpoint:
image: amazon/aws-stepfunctions-local:1.7.1
image: amazon/aws-stepfunctions-local:latest
environment:
AWS_DEFAULT_REGION: us-east-1
AWS_ACCESS_KEY_ID: xxx
Expand Down
60 changes: 20 additions & 40 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,50 +1,30 @@
#
# This file is autogenerated by pip-compile
# To update, run:
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --generate-hashes
#
attrs==19.3.0 \
--hash=sha256:08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c \
--hash=sha256:f7b7ce16570fe9965acd6d30101a28f62fb4a7f9e926b3bbc9b61f8b04247e72 \
exceptiongroup==1.1.1 \
--hash=sha256:232c37c63e4f682982c8b6459f33a8981039e5fb8756b2074364e5055c498c9e \
--hash=sha256:d484c3090ba2889ae2928419117447a14daf3c1231d5e30d0aae34f354f01785
# via pytest
importlib-metadata==1.7.0 \
--hash=sha256:90bb658cdbbf6d1735b6341ce708fc7024a3e14e99ffdc5783edea9f9b077f83 \
--hash=sha256:dc15b2969b4ce36305c51eebe62d418ac7791e9a157911d58bfb1f9ccd8e2070 \
# via pluggy, pytest
more-itertools==8.4.0 \
--hash=sha256:68c70cc7167bdf5c7c9d8f6954a7837089c6a36bf565383919bb595efb8a17e5 \
--hash=sha256:b78134b2063dd214000685165d81c154522c3ee0a1c0d4d113c80361c234c5a2 \
iniconfig==2.0.0 \
--hash=sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3 \
--hash=sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374
# via pytest
packaging==20.4 \
--hash=sha256:4357f74f47b9c12db93624a82154e9b120fa8293699949152b22065d556079f8 \
--hash=sha256:998416ba6962ae7fbd6596850b80e17859a5753ba17c32284f67bfff33784181 \
packaging==23.1 \
--hash=sha256:994793af429502c4ea2ebf6bf664629d07c1a9fe974af92966e4b8d2df7edc61 \
--hash=sha256:a392980d2b6cffa644431898be54b0045151319d1e7ec34f0cfed48767dd334f
# via pytest
pluggy==0.13.1 \
--hash=sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0 \
--hash=sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d \
pluggy==1.0.0 \
--hash=sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159 \
--hash=sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3
# via pytest
py==1.10.0 \
--hash=sha256:21b81bda15b66ef5e1a777a21c4dcd9c20ad3efd0b3f817e7a809035269e1bd3 \
--hash=sha256:3b80836aa6d1feeaa108e046da6423ab8f6ceda6468545ae8d02d9d58d18818a \
# via pytest
pyparsing==2.4.7 \
--hash=sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1 \
--hash=sha256:ef9d7589ef3c200abe66653d3f1ab1033c3c419ae9b9bdb1240a85b024efc88b \
# via packaging
pytest==5.4.3 \
--hash=sha256:5c0db86b698e8f170ba4582a492248919255fcd4c79b1ee64ace34301fb589a1 \
--hash=sha256:7979331bfcba207414f5e1263b5a0f8f521d0f457318836a7355531ed1a4c7d8 \
pytest==7.3.1 \
--hash=sha256:3799fa815351fea3a5e96ac7e503a96fa51cc9942c3753cda7651b93c1cfa362 \
--hash=sha256:434afafd78b1d78ed0addf160ad2b77a30d35d4bdf8af234fe621919d9ed15e3
# via pytest-stepfunctions (setup.py)
six==1.15.0 \
--hash=sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259 \
--hash=sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced \
# via packaging
wcwidth==0.2.5 \
--hash=sha256:beb4802a9cebb9144e99086eff703a642a13d6a0052920003a230f3294bbe784 \
--hash=sha256:c4d647b99872929fdb7bdcaa4fbe7f01413ed3d98077df798530e5b04f116c83 \
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
zipp==3.1.0 \
--hash=sha256:aa36550ff0c0b7ef7fa639055d797116ee891440eac1a56f378e2d3179e0320b \
--hash=sha256:c599e4d75c98f6798c509911d08a22e6c021d074469042177c8c86fb92eefd96 \
# via importlib-metadata
23 changes: 18 additions & 5 deletions src/pytest_stepfunctions/services/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@
import re
import sys
import traceback
from typing import Match, Optional
from urllib.parse import urlparse, ParseResult
from dataclasses import dataclass
from http.server import BaseHTTPRequestHandler
from typing import Match, Optional
from urllib.parse import ParseResult, urlparse


@dataclass
class LambdaContext:
function_name: str = "test-function"
memory_limit_in_mb: int = 128
invoked_function_arn: str = (
f"arn:aws:lambda:us-east-1:123456789012:function{function_name}"
)
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could probably be changed to aws_request_id: str = str(uuid4()) to dynamically generate a request id.



class AWSLambdaRequestHandler(BaseHTTPRequestHandler):
Expand All @@ -14,7 +25,8 @@ def do_POST(self) -> None:
parsed_path: ParseResult = urlparse(self.path)

match: Optional[Match[str]] = re.match(
"/2015-03-31/functions/(?P<function_name>[a-zA-Z0-9_.]+)/invocations", parsed_path.path
"/2015-03-31/functions/(?P<function_name>[a-zA-Z0-9_.]+)/invocations",
parsed_path.path,
)
if match:
return self._handle_invoke(function_name=match.group("function_name"))
Expand All @@ -37,7 +49,6 @@ def _retrieve_payload(self) -> bytes:
if "Content-Length" in self.headers:
content_length: int = int(self.headers["Content-Length"])
return self.rfile.read(content_length) if content_length > 0 else b"{}"

assert self.headers["Transfer-Encoding"] == "chunked"

content: bytes = b""
Expand All @@ -51,8 +62,10 @@ def _retrieve_payload(self) -> bytes:
def _handle_invoke(self, function_name: str) -> None:
module_name, method_name = function_name.rsplit(".", maxsplit=1)

context = LambdaContext(function_name)

event = json.loads(self._retrieve_payload().decode())
module = importlib.import_module(module_name)
output = getattr(module, method_name)(event)
output = getattr(module, method_name)(event, context)

self.wfile.write(b"HTTP/1.1 200 OK\n\n" + json.dumps(output).encode())
4 changes: 3 additions & 1 deletion tests/pytest_stepfunctions_test/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def run(self, definition: str, input_string: str) -> Dict[str, Any]:
sfn_client = boto3.client("stepfunctions", endpoint_url=self.endpoint_url)

state_machine_arn = sfn_client.create_state_machine(
name=str(uuid4()), definition=definition, roleArn="arn:aws:iam::012345678901:role/DummyRole",
name=str(uuid4()),
definition=definition,
roleArn="arn:aws:iam::012345678901:role/DummyRole",
)["stateMachineArn"]
execution_arn: str = sfn_client.start_execution(
stateMachineArn=state_machine_arn, name=str(uuid4()), input=input_string
Expand Down