Skip to content

Commit b34a611

Browse files
committed
some work on tests
1 parent 4ea57a0 commit b34a611

File tree

3 files changed

+106
-48
lines changed

3 files changed

+106
-48
lines changed

tests/integrations/aws_lambda/lambda_functions/hello_world/index.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/integrations/aws_lambda/test_aws_lambda.py

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def test_environment():
5050

5151
def before_test():
5252
server.clear_envelopes()
53-
print("[TEST] Clearing envelopes before test")
5453

5554
yield {
5655
"stack": stack,
@@ -87,27 +86,81 @@ def lambda_client():
8786
)
8887

8988

90-
def test_basic(lambda_client, test_environment):
91-
response = lambda_client.invoke(
92-
FunctionName="BasicTestFunction", Payload=json.dumps({"name": "Ivana"})
93-
)
94-
result = json.loads(response["Payload"].read().decode())
95-
assert result == {"message": "Hello, Ivana!"}
89+
# def test_basic_ok(lambda_client, test_environment):
90+
# response = lambda_client.invoke(
91+
# FunctionName="BasicOk",
92+
# Payload=json.dumps({"name": "Ivana"}),
93+
# )
94+
# result = json.loads(response["Payload"].read().decode())
95+
# assert result == {"event": {"name": "Ivana"}}
9696

97-
message, transaction = test_environment["server"].envelopes
98-
assert message["message"] == "[SENTRY MESSAGE] Hello, Ivana!"
99-
assert transaction["type"] == "transaction"
97+
# envelopes = test_environment["server"].envelopes
98+
# assert len(envelopes) == 1
10099

100+
# transaction = envelopes[0]
101+
# assert transaction["type"] == "transaction"
101102

102-
def test_basic_2(lambda_client, test_environment):
103-
test_environment["server"].clear_envelopes()
104103

105-
response = lambda_client.invoke(
106-
FunctionName="BasicTestFunction", Payload=json.dumps({"name": "Neel"})
107-
)
108-
result = json.loads(response["Payload"].read().decode())
109-
assert result == {"message": "Hello, Neel!"}
104+
def test_xxx(lambda_client, test_environment):
105+
for x in range(20):
106+
test_environment["server"].clear_envelopes()
107+
print(f"*** BasicException {x} ***")
108+
response = lambda_client.invoke(
109+
FunctionName="BasicException",
110+
Payload=json.dumps({}),
111+
)
112+
print("- RESPONSE")
113+
print(response)
114+
print("- PAYLOAD")
115+
print(response["Payload"].read().decode())
116+
print(f'- ENVELOPES {len(test_environment["server"].envelopes)}')
117+
118+
119+
120+
assert False
121+
122+
123+
# def test_basic(lambda_client, test_environment):
124+
# response = lambda_client.invoke(
125+
# FunctionName="BasicException",
126+
# Payload=json.dumps({"name": "Neel"}),
127+
# )
128+
# print("RESPONSE")
129+
# print(response)
130+
# print("PAYLOAD")
131+
# print(response["Payload"].read().decode())
132+
# result = json.loads(response["Payload"].read().decode())
133+
# print("RESULT")
134+
# print(result)
135+
136+
# envelopes = test_environment["server"].envelopes
137+
# (error,) = envelopes
138+
139+
# assert error["level"] == "error"
140+
# (exception,) = error["exception"]["values"]
141+
# assert exception["type"] == "Exception"
142+
# assert exception["value"] == "Oh!"
143+
144+
# (frame1,) = exception["stacktrace"]["frames"]
145+
# assert frame1["filename"] == "test_lambda.py"
146+
# assert frame1["abs_path"] == "/var/task/test_lambda.py"
147+
# assert frame1["function"] == "test_handler"
148+
149+
# assert frame1["in_app"] is True
150+
151+
# assert exception["mechanism"]["type"] == "aws_lambda"
152+
# assert not exception["mechanism"]["handled"]
153+
154+
# assert error["extra"]["lambda"]["function_name"].startswith("test_")
155+
156+
# logs_url = error["extra"]["cloudwatch logs"]["url"]
157+
# assert logs_url.startswith("https://console.aws.amazon.com/cloudwatch/home?region")
158+
# assert not re.search("(=;|=$)", logs_url)
159+
# assert error["extra"]["cloudwatch logs"]["log_group"].startswith(
160+
# "/aws/lambda/test_"
161+
# )
162+
163+
# log_stream_re = "^[0-9]{4}/[0-9]{2}/[0-9]{2}/\\[[^\\]]+][a-f0-9]+$"
164+
# log_stream = error["extra"]["cloudwatch logs"]["log_stream"]
110165

111-
message, transaction = test_environment["server"].envelopes
112-
assert message["message"] == "[SENTRY MESSAGE] Hello, Neel!"
113-
assert transaction["type"] == "transaction"
166+
# assert re.match(log_stream_re, log_stream)

tests/integrations/aws_lambda/utils.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ class DummyLambdaStack(Stack):
2323
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
2424
super().__init__(scope, construct_id, **kwargs)
2525

26+
print(f"CREATING STACK: {self}")
27+
2628
# Override the template synthesis
2729
self.template_options.template_format_version = "2010-09-09"
2830
self.template_options.transforms = ["AWS::Serverless-2016-10-31"]
2931

30-
# Create Sentry Lambda Layer
32+
# Create Sentry Lambda layer to the SAM stack
3133
filename = "sentry-sdk-lambda-layer.zip"
3234
build_packaged_zip(
3335
make_dist=True,
3436
out_zip_filename=filename,
3537
)
3638

37-
layer = CfnResource(
39+
self.sentry_layer = CfnResource(
3840
self,
3941
"SentryPythonServerlessSDK",
4042
type="AWS::Serverless::LayerVersion",
@@ -52,25 +54,35 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
5254
},
5355
)
5456

55-
# Add the function using SAM format
56-
CfnResource(
57-
self,
58-
"BasicTestFunction",
59-
type="AWS::Serverless::Function",
60-
properties={
61-
"CodeUri": "./tests/integrations/aws_lambda/lambda_functions/hello_world",
62-
"Handler": "sentry_sdk.integrations.init_serverless_sdk.sentry_lambda_handler",
63-
"Runtime": "python3.12",
64-
"Layers": [{"Ref": layer.logical_id}], # The layer adds the sentry-sdk
65-
"Environment": { # The environment variables are set up the Sentry SDK to instrument the lambda function
66-
"Variables": {
67-
"SENTRY_DSN": "http://[email protected]:9999/0",
68-
"SENTRY_INITIAL_HANDLER": "index.handler",
69-
"SENTRY_TRACES_SAMPLE_RATE": "1.0",
70-
}
57+
# Add all lambda functions from /tests/integrations/aws_lambda/lambda_functions/ to the SAM stack
58+
FUNCTIONS_DIR = "./tests/integrations/aws_lambda/lambda_functions/"
59+
lambda_dirs = [
60+
d for d in os.listdir(FUNCTIONS_DIR)
61+
if os.path.isdir(os.path.join(FUNCTIONS_DIR, d))
62+
]
63+
for lambda_dir in lambda_dirs:
64+
lambda_name = "".join(word.capitalize() for word in lambda_dir.replace("_", " ").replace("-", " ").split())
65+
66+
CfnResource(
67+
self,
68+
lambda_name,
69+
type="AWS::Serverless::Function",
70+
properties={
71+
"CodeUri": os.path.join(FUNCTIONS_DIR, lambda_dir),
72+
"Handler": "sentry_sdk.integrations.init_serverless_sdk.sentry_lambda_handler",
73+
"Runtime": "python3.12",
74+
"Layers": [{"Ref": self.sentry_layer.logical_id}], # Add layer containing the Sentry SDK to function.
75+
"Environment": {
76+
"Variables": {
77+
"SENTRY_DSN": "http://[email protected]:9999/0",
78+
"SENTRY_INITIAL_HANDLER": "index.handler",
79+
"SENTRY_TRACES_SAMPLE_RATE": "1.0",
80+
}
81+
},
7182
},
72-
},
73-
)
83+
)
84+
print(f"- created function: {lambda_name} / {os.path.join(FUNCTIONS_DIR, lambda_dir)}")
85+
7486

7587
@classmethod
7688
def wait_for_stack(cls, timeout=30, port=SAM_PORT):
@@ -142,5 +154,5 @@ def start(self):
142154
server_thread.start()
143155

144156
def clear_envelopes(self):
145-
print("[SENTRY SERVER] Clear envelopes")
157+
print("[SENTRY SERVER] Clearing envelopes")
146158
self.envelopes = []

0 commit comments

Comments
 (0)