|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +import httpx |
| 4 | + |
| 5 | +from testcontainers.core.generic import SrvContainer |
| 6 | + |
| 7 | +RIE_PATH = "/2015-03-31/functions/function/invocations" |
| 8 | +# AWS OS-only base images contain an Amazon Linux distribution and the runtime interface emulator. |
| 9 | + |
| 10 | + |
| 11 | +class AWSLambdaContainer(SrvContainer): |
| 12 | + """ |
| 13 | + AWS Lambda container that is based on a custom image. |
| 14 | +
|
| 15 | + Example: |
| 16 | +
|
| 17 | + .. doctest:: |
| 18 | +
|
| 19 | + >>> from testcontainers.aws import AWSLambdaContainer |
| 20 | + >>> from testcontainers.core.waiting_utils import wait_for_logs |
| 21 | +
|
| 22 | + >>> with AWSLambdaContainer(path="./modules/aws/tests/lambda_sample", port=8080, tag="lambda_func:latest") as func: |
| 23 | + ... response = func.send_request(data={'payload': 'some data'}) |
| 24 | + ... assert response.status_code == 200 |
| 25 | + ... assert "Hello from AWS Lambda using Python" in response.json() |
| 26 | + ... delay = wait_for_logs(func, "START RequestId:") |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, path: str, port: int, tag: Optional[str] = None, image_cleanup: bool = True) -> None: |
| 30 | + """ |
| 31 | + :param path: Path to the AWS Lambda dockerfile. |
| 32 | + :param port: Port to expose the AWS Lambda function. |
| 33 | + :param tag: Tag for the image to be built (default: None). |
| 34 | + :param image_cleanup: Clean up the image after the container is stopped (default: True). |
| 35 | + """ |
| 36 | + super().__init__(path, port, tag, image_cleanup) |
| 37 | + |
| 38 | + def get_api_url(self) -> str: |
| 39 | + return self._create_connection_url() + RIE_PATH |
| 40 | + |
| 41 | + def send_request(self, data: dict) -> httpx.Response: |
| 42 | + """ |
| 43 | + Send a request to the AWS Lambda function. |
| 44 | +
|
| 45 | + :param data: Data to be sent to the AWS Lambda function. |
| 46 | + :return: Response from the AWS Lambda function. |
| 47 | + """ |
| 48 | + client = httpx.Client() |
| 49 | + return client.post(self.get_api_url(), json=data) |
| 50 | + |
| 51 | + def get_stdout(self) -> str: |
| 52 | + return self.get_logs()[0].decode("utf-8") |
0 commit comments