|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +import subprocess |
| 6 | +import boto3 |
| 7 | +import uuid |
| 8 | +import base64 |
| 9 | + |
| 10 | + |
| 11 | +def get_boto_client(): |
| 12 | + return boto3.client( |
| 13 | + "lambda", |
| 14 | + aws_access_key_id=os.environ["SENTRY_PYTHON_TEST_AWS_ACCESS_KEY_ID"], |
| 15 | + aws_secret_access_key=os.environ["SENTRY_PYTHON_TEST_AWS_SECRET_ACCESS_KEY"], |
| 16 | + region_name="us-east-1", |
| 17 | + ) |
| 18 | + |
| 19 | + |
| 20 | +def run_lambda_function( |
| 21 | + client, |
| 22 | + runtime, |
| 23 | + code, |
| 24 | + payload, |
| 25 | + add_finalizer, |
| 26 | + syntax_check=True, |
| 27 | + timeout=30, |
| 28 | + subprocess_kwargs=(), |
| 29 | +): |
| 30 | + subprocess_kwargs = dict(subprocess_kwargs) |
| 31 | + |
| 32 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 33 | + test_lambda_py = os.path.join(tmpdir, "test_lambda.py") |
| 34 | + with open(test_lambda_py, "w") as f: |
| 35 | + f.write(code) |
| 36 | + |
| 37 | + if syntax_check: |
| 38 | + # Check file for valid syntax first, and that the integration does not |
| 39 | + # crash when not running in Lambda (but rather a local deployment tool |
| 40 | + # such as chalice's) |
| 41 | + subprocess.check_call([sys.executable, test_lambda_py]) |
| 42 | + |
| 43 | + setup_cfg = os.path.join(tmpdir, "setup.cfg") |
| 44 | + with open(setup_cfg, "w") as f: |
| 45 | + f.write("[install]\nprefix=") |
| 46 | + |
| 47 | + subprocess.check_call( |
| 48 | + [sys.executable, "setup.py", "sdist", "-d", os.path.join(tmpdir, "..")], |
| 49 | + **subprocess_kwargs |
| 50 | + ) |
| 51 | + |
| 52 | + # https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html |
| 53 | + subprocess.check_call( |
| 54 | + "pip install ../*.tar.gz -t .", cwd=tmpdir, shell=True, **subprocess_kwargs |
| 55 | + ) |
| 56 | + shutil.make_archive(os.path.join(tmpdir, "ball"), "zip", tmpdir) |
| 57 | + |
| 58 | + fn_name = "test_function_{}".format(uuid.uuid4()) |
| 59 | + |
| 60 | + with open(os.path.join(tmpdir, "ball.zip"), "rb") as zip: |
| 61 | + client.create_function( |
| 62 | + FunctionName=fn_name, |
| 63 | + Runtime=runtime, |
| 64 | + Timeout=timeout, |
| 65 | + Role=os.environ["SENTRY_PYTHON_TEST_AWS_IAM_ROLE"], |
| 66 | + Handler="test_lambda.test_handler", |
| 67 | + Code={"ZipFile": zip.read()}, |
| 68 | + Description="Created as part of testsuite for getsentry/sentry-python", |
| 69 | + ) |
| 70 | + |
| 71 | + @add_finalizer |
| 72 | + def delete_function(): |
| 73 | + client.delete_function(FunctionName=fn_name) |
| 74 | + |
| 75 | + response = client.invoke( |
| 76 | + FunctionName=fn_name, |
| 77 | + InvocationType="RequestResponse", |
| 78 | + LogType="Tail", |
| 79 | + Payload=payload, |
| 80 | + ) |
| 81 | + |
| 82 | + assert 200 <= response["StatusCode"] < 300, response |
| 83 | + return response |
| 84 | + |
| 85 | + |
| 86 | +_REPL_CODE = """ |
| 87 | +import os |
| 88 | +
|
| 89 | +def test_handler(event, context): |
| 90 | + line = {line!r} |
| 91 | + if line.startswith(">>> "): |
| 92 | + exec(line[4:]) |
| 93 | + elif line.startswith("$ "): |
| 94 | + os.system(line[2:]) |
| 95 | + else: |
| 96 | + print("Start a line with $ or >>>") |
| 97 | +
|
| 98 | + return b"" |
| 99 | +""" |
| 100 | + |
| 101 | +try: |
| 102 | + import click |
| 103 | +except ImportError: |
| 104 | + pass |
| 105 | +else: |
| 106 | + |
| 107 | + @click.command() |
| 108 | + @click.option( |
| 109 | + "--runtime", required=True, help="name of the runtime to use, eg python3.8" |
| 110 | + ) |
| 111 | + @click.option("--verbose", is_flag=True, default=False) |
| 112 | + def repl(runtime, verbose): |
| 113 | + """ |
| 114 | + Launch a "REPL" against AWS Lambda to inspect their runtime. |
| 115 | + """ |
| 116 | + |
| 117 | + cleanup = [] |
| 118 | + client = get_boto_client() |
| 119 | + |
| 120 | + print("Start a line with `$ ` to run shell commands, or `>>> ` to run Python") |
| 121 | + |
| 122 | + while True: |
| 123 | + line = input() |
| 124 | + |
| 125 | + response = run_lambda_function( |
| 126 | + client, |
| 127 | + runtime, |
| 128 | + _REPL_CODE.format(line=line), |
| 129 | + b"", |
| 130 | + cleanup.append, |
| 131 | + subprocess_kwargs={ |
| 132 | + "stdout": subprocess.DEVNULL, |
| 133 | + "stderr": subprocess.DEVNULL, |
| 134 | + } |
| 135 | + if not verbose |
| 136 | + else {}, |
| 137 | + ) |
| 138 | + |
| 139 | + for line in base64.b64decode(response["LogResult"]).splitlines(): |
| 140 | + print(line.decode("utf8")) |
| 141 | + |
| 142 | + for f in cleanup: |
| 143 | + f() |
| 144 | + |
| 145 | + cleanup = [] |
| 146 | + |
| 147 | + if __name__ == "__main__": |
| 148 | + repl() |
0 commit comments