Skip to content

Commit 1b73d8b

Browse files
committed
tests(asgi+starlette): adapt tests to OTel usage.
Signed-off-by: Paulo Vital <[email protected]>
1 parent e2c6efa commit 1b73d8b

12 files changed

+511
-279
lines changed

.coveragerc

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ exclude_lines =
33
pragma: no cover
44
if TYPE_CHECKING:
55
except ImportError:
6+
except Exception:
7+
except Exception as exc:

tests/apps/starlette_app/__init__.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@
22
# (c) Copyright Instana Inc. 2020
33

44
import uvicorn
5-
from ...helpers import testenv
6-
from instana.log import logger
75

6+
from tests.helpers import testenv
7+
8+
testenv["starlette_host"] = "127.0.0.1"
89
testenv["starlette_port"] = 10817
9-
testenv["starlette_server"] = ("http://127.0.0.1:" + str(testenv["starlette_port"]))
10+
testenv["starlette_server"] = "http://" + testenv["starlette_host"] + ":" + str(testenv["starlette_port"])
11+
12+
1013

1114
def launch_starlette():
1215
from .app import starlette_server
1316
from instana.singletons import agent
1417

1518
# Hack together a manual custom headers list; We'll use this in tests
16-
agent.options.extra_http_headers = [u'X-Capture-This', u'X-Capture-That']
19+
agent.options.extra_http_headers = [
20+
"X-Capture-This",
21+
"X-Capture-That",
22+
]
1723

18-
uvicorn.run(starlette_server, host='127.0.0.1', port=testenv['starlette_port'], log_level="critical")
24+
uvicorn.run(
25+
starlette_server,
26+
host=testenv["starlette_host"],
27+
port=testenv["starlette_port"],
28+
log_level="critical",
29+
)

tests/apps/starlette_app/app.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
# (c) Copyright IBM Corp. 2021
22
# (c) Copyright Instana Inc. 2020
33

4+
import os
5+
46
from starlette.applications import Starlette
57
from starlette.responses import PlainTextResponse
6-
from starlette.routing import Route, Mount, WebSocketRoute
8+
from starlette.routing import Mount, Route, WebSocketRoute
79
from starlette.staticfiles import StaticFiles
810

9-
import os
1011
dir_path = os.path.dirname(os.path.realpath(__file__))
1112

13+
1214
def homepage(request):
13-
return PlainTextResponse('Hello, world!')
15+
return PlainTextResponse("Hello, world!")
16+
1417

1518
def user(request):
16-
user_id = request.path_params['user_id']
17-
return PlainTextResponse('Hello, user id %s!' % user_id)
19+
user_id = request.path_params["user_id"]
20+
return PlainTextResponse("Hello, user id %s!" % user_id)
21+
1822

1923
async def websocket_endpoint(websocket):
2024
await websocket.accept()
21-
await websocket.send_text('Hello, websocket!')
25+
await websocket.send_text("Hello, websocket!")
2226
await websocket.close()
2327

28+
2429
def startup():
25-
print('Ready to go')
30+
print("Ready to go")
2631

2732

2833
routes = [
29-
Route('/', homepage),
30-
Route('/users/{user_id}', user),
31-
WebSocketRoute('/ws', websocket_endpoint),
32-
Mount('/static', StaticFiles(directory=dir_path + "/static")),
34+
Route("/", homepage),
35+
Route("/users/{user_id}", user),
36+
WebSocketRoute("/ws", websocket_endpoint),
37+
Mount("/static", StaticFiles(directory=dir_path + "/static")),
3338
]
3439

35-
starlette_server = Starlette(debug=True, routes=routes, on_startup=[startup])
40+
starlette_server = Starlette(debug=True, routes=routes, on_startup=[startup])

tests/apps/starlette_app/app2.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# (c) Copyright IBM Corp. 2024
2+
3+
import os
4+
5+
from starlette.applications import Starlette
6+
from starlette.middleware import Middleware
7+
from starlette.middleware.trustedhost import TrustedHostMiddleware
8+
from starlette.responses import PlainTextResponse
9+
from starlette.routing import Route
10+
11+
dir_path = os.path.dirname(os.path.realpath(__file__))
12+
13+
14+
def homepage(request):
15+
return PlainTextResponse("Hello, world!")
16+
17+
18+
def five_hundred(request):
19+
return PlainTextResponse("Something went wrong!", status_code=500)
20+
21+
22+
def startup():
23+
print("Ready to go")
24+
25+
26+
routes = [
27+
Route("/", homepage),
28+
Route("/five", five_hundred),
29+
]
30+
31+
starlette_server = Starlette(
32+
debug=True,
33+
routes=routes,
34+
on_startup=[startup],
35+
middleware=[
36+
Middleware(
37+
TrustedHostMiddleware,
38+
allowed_hosts=["*"],
39+
),
40+
],
41+
)

tests/conftest.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
# codes are finalised.
4040
collect_ignore_glob.append("*clients/boto*")
4141
collect_ignore_glob.append("*clients/test_cassandra*")
42-
collect_ignore_glob.append("*clients/test_counchbase*")
42+
collect_ignore_glob.append("*clients/test_couchbase*")
4343
collect_ignore_glob.append("*clients/test_google*")
4444
collect_ignore_glob.append("*clients/test_mysql*")
4545
collect_ignore_glob.append("*clients/test_pika*")
@@ -57,20 +57,19 @@
5757
collect_ignore_glob.append("*frameworks/test_grpcio*")
5858
collect_ignore_glob.append("*frameworks/test_pyramid*")
5959
collect_ignore_glob.append("*frameworks/test_sanic*")
60-
collect_ignore_glob.append("*frameworks/test_starlette*")
6160
collect_ignore_glob.append("*frameworks/test_tornado*")
6261

63-
# Cassandra and gevent tests are run in dedicated jobs on CircleCI and will
64-
# be run explicitly. (So always exclude them here)
65-
if not os.environ.get("CASSANDRA_TEST"):
66-
collect_ignore_glob.append("*test_cassandra*")
62+
# # Cassandra and gevent tests are run in dedicated jobs on CircleCI and will
63+
# # be run explicitly. (So always exclude them here)
64+
# if not os.environ.get("CASSANDRA_TEST"):
65+
# collect_ignore_glob.append("*test_cassandra*")
6766

68-
if not os.environ.get("COUCHBASE_TEST"):
69-
collect_ignore_glob.append("*test_couchbase*")
67+
# if not os.environ.get("COUCHBASE_TEST"):
68+
# collect_ignore_glob.append("*test_couchbase*")
7069

71-
if not os.environ.get("GEVENT_STARLETTE_TEST"):
72-
collect_ignore_glob.append("*test_gevent*")
73-
collect_ignore_glob.append("*test_starlette*")
70+
# if not os.environ.get("GEVENT_STARLETTE_TEST"):
71+
# collect_ignore_glob.append("*test_gevent*")
72+
# collect_ignore_glob.append("*test_starlette*")
7473

7574
# Python 3.10 support is incomplete yet
7675
# TODO: Remove this once we start supporting Tornado >= 6.0

0 commit comments

Comments
 (0)