44import requests
55import subprocess
66import time
7+ import threading
78import yaml
89from aws_cdk import (
910 App ,
@@ -26,6 +27,25 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
2627 self .template_options .template_format_version = "2010-09-09"
2728 self .template_options .transforms = ["AWS::Serverless-2016-10-31" ]
2829
30+ # Create Sentry Lambda Layer
31+ layer = CfnResource (
32+ self ,
33+ "SentryPythonServerlessSDK" ,
34+ type = "AWS::Serverless::LayerVersion" ,
35+ properties = {
36+ "ContentUri" : "./tests/integrations/aws_lambda/lambda_layer" ,
37+ "CompatibleRuntimes" : [
38+ "python3.7" ,
39+ "python3.8" ,
40+ "python3.9" ,
41+ "python3.10" ,
42+ "python3.11" ,
43+ "python3.12" ,
44+ "python3.13" ,
45+ ],
46+ },
47+ )
48+
2949 # Add the function using SAM format
3050 CfnResource (
3151 self ,
@@ -35,6 +55,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
3555 "CodeUri" : "./tests/integrations/aws_lambda/lambda_functions/hello_world" ,
3656 "Handler" : "index.handler" ,
3757 "Runtime" : "python3.12" ,
58+ "Layers" : [{"Ref" : layer .logical_id }],
3859 },
3960 )
4061
@@ -59,11 +80,55 @@ def wait_for_sam(timeout=30, port=SAM_PORT):
5980 continue
6081
6182
83+ class SentryTestServer :
84+ def __init__ (self , port = 9999 ):
85+ self .envelopes = []
86+ self .port = port
87+
88+ from fastapi import FastAPI , Request
89+
90+ self .app = FastAPI ()
91+
92+ @self .app .get ("/" )
93+ async def root ():
94+ return {
95+ "message" : "Sentry Test Server. Use DSN http://123@localhost:9999/0 in your SDK."
96+ }
97+
98+ @self .app .post ("/api/0/envelope/" )
99+ async def envelope (request : Request ):
100+ self .envelopes .append (await request .json ())
101+ return {"status" : "ok" }
102+
103+ def run_server (self ):
104+ import uvicorn
105+
106+ uvicorn .run (self .app , host = "0.0.0.0" , port = self .port )
107+
108+ def start (self ):
109+ server_thread = threading .Thread (target = self .run_server , daemon = True )
110+ server_thread .start ()
111+
112+ def clear_envelopes (self ):
113+ self .envelopes = []
114+
115+
116+ @pytest .fixture (scope = "session" )
117+ def sentry_test_server ():
118+ server = SentryTestServer ()
119+ server .start ()
120+
121+ time .sleep (1 ) # Give it a moment to start up
122+
123+ yield server
124+
125+
62126@pytest .fixture (scope = "session" )
63127def sam_stack ():
64128 """
65129 Create and deploy the SAM stack once for all tests
66130 """
131+ # Create the SAM stack
67132 app = App ()
68133 stack = DummyLambdaStack (app , "DummyLambdaStack" , env = {"region" : SAM_REGION })
69134
@@ -92,6 +157,7 @@ def sam_stack():
92157 # Wait for SAM to be ready
93158 wait_for_sam ()
94159 yield stack
160+
95161 finally :
96162 process .terminate ()
97163 process .wait (timeout = 5 ) # Give it time to shut down gracefully
@@ -115,13 +181,25 @@ def lambda_client():
115181 )
116182
117183
118- def test_basic (lambda_client , sam_stack ):
119- region = boto3 .Session ().region_name
120- print ("Region: " , region )
184+ def test_basic (lambda_client , sam_stack , sentry_test_server ):
185+ sentry_test_server .clear_envelopes ()
186+
187+ response = lambda_client .invoke (
188+ FunctionName = "BasicTestFunction" , Payload = json .dumps ({"name" : "Anton" })
189+ )
190+ result = json .loads (response ["Payload" ].read ().decode ())
191+
192+ print (sentry_test_server .envelopes )
193+ assert result == {"message" : "Hello, Anton!" }
194+
195+
196+ def test_basic_2 (lambda_client , sam_stack , sentry_test_server ):
197+ sentry_test_server .clear_envelopes ()
121198
122199 response = lambda_client .invoke (
123200 FunctionName = "BasicTestFunction" , Payload = json .dumps ({"name" : "Anton" })
124201 )
125202 result = json .loads (response ["Payload" ].read ().decode ())
126203
204+ print (sentry_test_server .envelopes )
127205 assert result == {"message" : "Hello, Anton!" }
0 commit comments