Skip to content

Commit 2576948

Browse files
committed
Make it work in CI and locally
1 parent ce9e90f commit 2576948

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

tests/integrations/aws_lambda/test_aws_lambda.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import boto3
22
import docker
33
import json
4+
import os
5+
import platform
46
import pytest
57
import socket
68
import subprocess
@@ -15,29 +17,45 @@
1517
from .utils import LocalLambdaStack, SentryServerForTesting, SAM_PORT
1618

1719

20+
DOCKER_NETWORK_NAME = "lambda-test-network"
1821
SAM_TEMPLATE_FILE = "sam.template.yaml"
1922

2023

24+
def _get_host_ip():
25+
if os.environ.get("GITHUB_ACTIONS"):
26+
# Running in GitHub Actions
27+
hostname = socket.gethostname()
28+
host = socket.gethostbyname(hostname)
29+
else:
30+
# Running locally
31+
if platform.system() in ["Darwin", "Windows"]:
32+
# Windows or MacOS
33+
host = "host.docker.internal"
34+
else:
35+
# Linux
36+
hostname = socket.gethostname()
37+
host = socket.gethostbyname(hostname)
38+
39+
return host
40+
41+
2142
@pytest.fixture(scope="session", autouse=True)
2243
def test_environment():
2344
print("[test_environment fixture] Setting up AWS Lambda test infrastructure")
2445

2546
# Create a Docker network
2647
docker_client = docker.from_env()
27-
network = docker_client.networks.create("lambda-test-network", driver="bridge")
48+
docker_client.networks.prune()
49+
docker_client.networks.create(DOCKER_NETWORK_NAME, driver="bridge")
2850

2951
# Start Sentry server
3052
server = SentryServerForTesting()
3153
server.start()
3254
time.sleep(1) # Give it a moment to start up
3355

34-
# Get the host IP address
35-
hostname = socket.gethostname()
36-
host_ip = socket.gethostbyname(hostname)
37-
3856
# Create local AWS SAM stack
3957
app = App()
40-
stack = LocalLambdaStack(app, "LocalLambdaStack", host=host_ip)
58+
stack = LocalLambdaStack(app, "LocalLambdaStack", host=_get_host_ip())
4159

4260
# Write SAM template to file
4361
template = app.synth().get_stack_by_name("LocalLambdaStack").template
@@ -61,7 +79,7 @@ def test_environment():
6179
"--warm-containers",
6280
"EAGER",
6381
"--docker-network",
64-
"lambda-test-network",
82+
DOCKER_NETWORK_NAME,
6583
],
6684
stdout=debug_log,
6785
stderr=debug_log,

tests/integrations/aws_lambda/utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ class LocalLambdaStack(Stack):
3131

3232
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
3333
host = kwargs.pop("host", "host-not-specified")
34+
dsn = f"http://123@{host}:9999/0" # noqa: E231
3435

3536
super().__init__(scope, construct_id, **kwargs)
36-
print("[LocalLambdaStack] Creating local SAM Lambda Stack: %s" % self)
37+
print(
38+
"[LocalLambdaStack] Creating local SAM Lambda Stack (Sentry DSN: %s)" % dsn
39+
)
3740

3841
# Override the template synthesis
3942
self.template_options.template_format_version = "2010-09-09"
@@ -85,7 +88,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
8588
], # Add layer containing the Sentry SDK to function.
8689
"Environment": {
8790
"Variables": {
88-
"SENTRY_DSN": f"http://123@{host}:9999/0",
91+
"SENTRY_DSN": dsn,
8992
"SENTRY_INITIAL_HANDLER": "index.handler",
9093
"SENTRY_TRACES_SAMPLE_RATE": "1.0",
9194
}
@@ -109,7 +112,7 @@ def wait_for_stack(cls, timeout=30, port=SAM_PORT):
109112
while True:
110113
if time.time() - start_time > timeout:
111114
raise TimeoutError(
112-
"SAM failed to start within %s seconds. (Maybe Docker is not running?)"
115+
"AWS SAM failed to start within %s seconds. (Maybe Docker is not running?)"
113116
% timeout
114117
)
115118

@@ -173,7 +176,9 @@ def run_server(self):
173176
uvicorn.run(self.app, host=self.host, port=self.port, log_level=self.log_level)
174177

175178
def start(self):
176-
print("[SentryServerForTesting] Starting server on %s:%s" % (self.host, self.port))
179+
print(
180+
"[SentryServerForTesting] Starting server on %s:%s" % (self.host, self.port)
181+
)
177182
server_thread = threading.Thread(target=self.run_server, daemon=True)
178183
server_thread.start()
179184

0 commit comments

Comments
 (0)