Skip to content

Commit 1f94c19

Browse files
use docker compose profiling
1 parent 6018f09 commit 1f94c19

5 files changed

Lines changed: 63 additions & 39 deletions

File tree

poetry.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ behave = "^1.3.3"
6262
python-dotenv = "^1.2.1"
6363
openapi-spec-validator = "^0.7.2"
6464
pip-licenses = "^5.5.0"
65-
65+
cachetools = "^7.0.1"
6666

6767
[tool.poetry-plugin-lambda-build]
6868
docker-image = "public.ecr.aws/sam/build-python3.13:1.139-x86_64" # See https://gallery.ecr.aws/search?searchTerm=%22python%22&architecture=x86-64&popularRegistries=amazon&verified=verified&operatingSystems=Linux

tests/docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
services:
22
moto-server:
3+
#used for s3, dynamodb, kinesis, secret manager
34
image: motoserver/moto:latest
45
container_name: moto-server
56
ports:
@@ -8,6 +9,8 @@ services:
89
- test-network
910

1011
lambda-api:
12+
#used for lambda simulation
13+
profiles: ["lambda-test"]
1114
image: public.ecr.aws/lambda/python:3.13
1215
container_name: lambda-api
1316
ports:
@@ -39,6 +42,8 @@ services:
3942
- test-network
4043

4144
api-gateway-mock:
45+
#used for api gateway simulation
46+
profiles: ["lambda-test"]
4247
image: openresty/openresty:alpine
4348
ports:
4449
- "9123:9123"

tests/integration/conftest.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
import json
33
import logging
44
import os
5-
import subprocess
65
from collections.abc import Callable, Generator
7-
from pathlib import Path
8-
from typing import List
96
from typing import TYPE_CHECKING, Any
107

118
import httpx

tests/integration/lambda/conftest.py

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import subprocess
23
from pathlib import Path
34
from typing import Callable
@@ -10,10 +11,17 @@
1011
from tests.integration.conftest import is_responsive
1112

1213

14+
def get_project_root() -> Path:
15+
"""Finds the project root by looking for the 'dist' directory or a git folder."""
16+
current = Path(__file__).resolve()
17+
for parent in current.parents:
18+
if (parent / "dist").exists() or (parent / ".git").exists():
19+
return parent
20+
return Path(__file__).resolve().parents[3]
21+
1322
@pytest.fixture(scope="session")
1423
def lambda_zip() -> Path:
15-
# Determine project root (directory containing this conftest.py)
16-
project_root = Path(__file__).resolve().parents[3]
24+
project_root = get_project_root()
1725

1826
build_result = subprocess.run(
1927
["make", "build"],
@@ -37,29 +45,51 @@ def lambda_zip() -> Path:
3745
return zip_path
3846

3947
@pytest.fixture(scope="session")
40-
def lambda_runtime_url(request: pytest.FixtureRequest, lambda_zip: Path) -> URL:
48+
def lambda_runtime_url(request, lambda_zip):
49+
"""
50+
kick-starts the lambda simulation
51+
"""
4152
docker_services = request.getfixturevalue("docker_services")
4253
docker_ip = request.getfixturevalue("docker_ip")
43-
44-
wait_for_zip_in_container(docker_services, "lambda-api", "/tmp/lambda.zip")
45-
force_lambda_reload(docker_services)
54+
project_root = get_project_root()
55+
compose_file = project_root / "tests/docker-compose.yml"
56+
57+
env = os.environ.copy()
58+
env["COMPOSE_PROFILES"] = "lambda-test"
59+
60+
subprocess.run(
61+
[
62+
"docker", "compose",
63+
"-f", str(compose_file),
64+
"up", "-d", "--build", "--force-recreate",
65+
"lambda-api", "api-gateway-mock",
66+
],
67+
env=env,
68+
check=True,
69+
capture_output=True,
70+
text=True,
71+
)
4672

4773
port = docker_services.port_for("lambda-api", 8080)
4874
base_url = URL(f"http://{docker_ip}:{port}")
4975

5076
docker_services.wait_until_responsive(
51-
timeout=30.0,
52-
pause=1.0,
53-
check=lambda: is_responsive(base_url)
77+
timeout=30.0, pause=0.5, check=lambda: is_responsive(base_url)
5478
)
79+
5580
return base_url
5681

82+
83+
5784
@pytest.fixture(scope="session")
5885
def lambda_client(boto3_session: Session, lambda_runtime_url: URL) -> BaseClient:
5986
return boto3_session.client("lambda", endpoint_url=str(lambda_runtime_url))
6087

6188
@pytest.fixture(scope="session")
6289
def api_gateway_endpoint(request: pytest.FixtureRequest, lambda_runtime_url) -> URL:
90+
"""
91+
kick-starts the api-gateway lambda simulation
92+
"""
6393
docker_services = request.getfixturevalue("docker_services")
6494
docker_ip = request.getfixturevalue("docker_ip")
6595

@@ -75,41 +105,21 @@ def api_gateway_endpoint(request: pytest.FixtureRequest, lambda_runtime_url) ->
75105
)
76106
return base_url
77107

78-
def wait_for_zip_in_container(docker_services, service: str, path: str) -> None:
79-
def _check() -> bool:
80-
try:
81-
docker_services._docker_compose.execute(
82-
f"exec {service} test -f {path}"
83-
)
84-
return True
85-
except Exception:
86-
return False
87-
88-
docker_services.wait_until_responsive(
89-
timeout=30,
90-
pause=1,
91-
check=_check,
92-
)
93-
94-
95-
def force_lambda_reload(docker_services):
96-
# Stop and remove lambda-api
97-
docker_services._docker_compose.execute(f"stop lambda-api")
98-
docker_services._docker_compose.execute(f"rm -f lambda-api")
99-
100-
# Start lambda-api fresh so it re-reads the mounted ZIP
101-
docker_services._docker_compose.execute(f"up --build -d lambda-api")
102-
103108

104109
@pytest.fixture
105110
def lambda_logs(docker_services) -> Callable[[], list[str]]:
111+
"""Returns a callable that fetches the latest lambda-api logs,
112+
allowing tests to inspect runtime output on demand."""
113+
106114
def _get_messages() -> list[str]:
107115
return get_lambda_logs(docker_services)
108116

109117
return _get_messages
110118

111119

112120
def get_lambda_logs(docker_services) -> list[str]:
121+
"""returns logs from lambda-api container"""
122+
113123
result: bytes = docker_services._docker_compose.execute("logs --no-color lambda-api")
114124
raw_lines = result.decode("utf-8").splitlines()
115125
return [line.partition("|")[-1].strip() for line in raw_lines]

0 commit comments

Comments
 (0)