-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
525c3cc
commit 57fa0ab
Showing
6 changed files
with
383 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
132 changes: 132 additions & 0 deletions
132
python_modules/libraries/dagster-aws/dagster_aws_tests/pipes_tests/fake_lambda.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import base64 | ||
import io | ||
import json | ||
import os | ||
import subprocess | ||
import sys | ||
import tempfile | ||
import traceback | ||
from typing import Any, Dict | ||
|
||
from dagster_pipes import PipesSourceParamsLoader, open_dagster_pipes | ||
|
||
|
||
class LambdaFunctions: | ||
@staticmethod | ||
def trunc_logs(event, context): | ||
sys.stdout.write("O" * 1024 * 3) | ||
sys.stderr.write("E" * 1024 * 3) | ||
|
||
@staticmethod | ||
def small_logs(event, context): | ||
print("S" * event["size"]) | ||
|
||
@staticmethod | ||
def pipes_basic(event, _lambda_context): | ||
class LambdaEventLoader(PipesSourceParamsLoader): | ||
def __init__(self, event): | ||
self._event = event | ||
|
||
@property | ||
def source(self): | ||
return self._event | ||
|
||
with open_dagster_pipes(params_loader=LambdaEventLoader(event)) as dagster_context: | ||
dagster_context.report_asset_materialization(metadata={"meta": "data"}) | ||
|
||
@staticmethod | ||
def error(event, _lambda_context): | ||
raise Exception("boom") | ||
|
||
|
||
class FakeLambdaContext: | ||
pass | ||
|
||
|
||
LOG_TAIL_LIMIT = 4096 | ||
|
||
|
||
class FakeLambdaClient: | ||
def invoke(self, **kwargs): | ||
# emulate lambda constraints with a subprocess invocation | ||
# * json serialized "Payload" result | ||
# * 4k log output as base64 "LogResult" | ||
|
||
with tempfile.TemporaryDirectory() as tempdir: | ||
in_path = os.path.join(tempdir, "in.json") | ||
out_path = os.path.join(tempdir, "out.json") | ||
log_path = os.path.join(tempdir, "logs") | ||
|
||
with open(in_path, "w") as f: | ||
f.write(kwargs["Payload"]) | ||
|
||
with open(log_path, "w") as log_file: | ||
result = subprocess.run( | ||
[ | ||
sys.executable, | ||
os.path.join(os.path.dirname(__file__), "fake_lambda.py"), | ||
kwargs["FunctionName"], | ||
in_path, | ||
out_path, | ||
], | ||
check=False, | ||
env={}, # env vars part of lambda fn definition, can't vary at runtime | ||
stdout=log_file, | ||
stderr=log_file, | ||
) | ||
|
||
response: Dict[str, Any] = {} | ||
|
||
if result.returncode == 42: | ||
response["FunctionError"] = "Unhandled" | ||
|
||
elif result.returncode != 0: | ||
with open(log_path, "r") as f: | ||
print(f.read()) | ||
result.check_returncode() | ||
|
||
with open(out_path, "rb") as f: | ||
payload = io.BytesIO(f.read()) | ||
|
||
response["Payload"] = payload | ||
|
||
if kwargs.get("LogType") == "Tail": | ||
logs_len = os.path.getsize(log_path) | ||
with open(log_path, "rb") as log_file: | ||
if logs_len > LOG_TAIL_LIMIT: | ||
log_file.seek(-LOG_TAIL_LIMIT, os.SEEK_END) | ||
|
||
outro = log_file.read() | ||
|
||
log_result = base64.encodebytes(outro) | ||
|
||
response["LogResult"] = log_result | ||
|
||
return response | ||
|
||
|
||
if __name__ == "__main__": | ||
assert len(sys.argv) == 4, "python fake_lambda.py <fn_name> <in_path> <out_path>" | ||
_, fn_name, in_path, out_path = sys.argv | ||
|
||
event = json.load(open(in_path)) | ||
fn = getattr(LambdaFunctions, fn_name) | ||
|
||
val = None | ||
return_code = 0 | ||
try: | ||
val = fn(event, FakeLambdaContext()) | ||
except Exception as e: | ||
tb = traceback.TracebackException.from_exception(e) | ||
val = { | ||
"errorMessage": str(tb), | ||
"errorType": tb.exc_type.__name__, | ||
"stackTrace": tb.stack.format(), | ||
"requestId": "fake-request-id", | ||
} | ||
return_code = 42 | ||
|
||
with open(out_path, "w") as f: | ||
json.dump(val, f) | ||
|
||
sys.exit(return_code) |
Oops, something went wrong.