1+ import os
12import subprocess
23from pathlib import Path
34from typing import Callable
1011from 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" )
1423def 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" )
5885def 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" )
6289def 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
105110def 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
112120def 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