Skip to content

Commit

Permalink
tests(asgi+starlette): adapt tests to OTel usage.
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Vital <[email protected]>
  • Loading branch information
pvital committed Sep 30, 2024
1 parent e2c6efa commit 1b73d8b
Show file tree
Hide file tree
Showing 12 changed files with 511 additions and 279 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ exclude_lines =
pragma: no cover
if TYPE_CHECKING:
except ImportError:
except Exception:
except Exception as exc:
21 changes: 16 additions & 5 deletions tests/apps/starlette_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@
# (c) Copyright Instana Inc. 2020

import uvicorn
from ...helpers import testenv
from instana.log import logger

from tests.helpers import testenv

testenv["starlette_host"] = "127.0.0.1"
testenv["starlette_port"] = 10817
testenv["starlette_server"] = ("http://127.0.0.1:" + str(testenv["starlette_port"]))
testenv["starlette_server"] = "http://" + testenv["starlette_host"] + ":" + str(testenv["starlette_port"])



def launch_starlette():
from .app import starlette_server
from instana.singletons import agent

# Hack together a manual custom headers list; We'll use this in tests
agent.options.extra_http_headers = [u'X-Capture-This', u'X-Capture-That']
agent.options.extra_http_headers = [
"X-Capture-This",
"X-Capture-That",
]

uvicorn.run(starlette_server, host='127.0.0.1', port=testenv['starlette_port'], log_level="critical")
uvicorn.run(
starlette_server,
host=testenv["starlette_host"],
port=testenv["starlette_port"],
log_level="critical",
)
29 changes: 17 additions & 12 deletions tests/apps/starlette_app/app.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. 2020

import os

from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.routing import Route, Mount, WebSocketRoute
from starlette.routing import Mount, Route, WebSocketRoute
from starlette.staticfiles import StaticFiles

import os
dir_path = os.path.dirname(os.path.realpath(__file__))


def homepage(request):
return PlainTextResponse('Hello, world!')
return PlainTextResponse("Hello, world!")


def user(request):
user_id = request.path_params['user_id']
return PlainTextResponse('Hello, user id %s!' % user_id)
user_id = request.path_params["user_id"]
return PlainTextResponse("Hello, user id %s!" % user_id)


async def websocket_endpoint(websocket):
await websocket.accept()
await websocket.send_text('Hello, websocket!')
await websocket.send_text("Hello, websocket!")
await websocket.close()


def startup():
print('Ready to go')
print("Ready to go")


routes = [
Route('/', homepage),
Route('/users/{user_id}', user),
WebSocketRoute('/ws', websocket_endpoint),
Mount('/static', StaticFiles(directory=dir_path + "/static")),
Route("/", homepage),
Route("/users/{user_id}", user),
WebSocketRoute("/ws", websocket_endpoint),
Mount("/static", StaticFiles(directory=dir_path + "/static")),
]

starlette_server = Starlette(debug=True, routes=routes, on_startup=[startup])
starlette_server = Starlette(debug=True, routes=routes, on_startup=[startup])
41 changes: 41 additions & 0 deletions tests/apps/starlette_app/app2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# (c) Copyright IBM Corp. 2024

import os

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.trustedhost import TrustedHostMiddleware
from starlette.responses import PlainTextResponse
from starlette.routing import Route

dir_path = os.path.dirname(os.path.realpath(__file__))


def homepage(request):
return PlainTextResponse("Hello, world!")


def five_hundred(request):
return PlainTextResponse("Something went wrong!", status_code=500)


def startup():
print("Ready to go")


routes = [
Route("/", homepage),
Route("/five", five_hundred),
]

starlette_server = Starlette(
debug=True,
routes=routes,
on_startup=[startup],
middleware=[
Middleware(
TrustedHostMiddleware,
allowed_hosts=["*"],
),
],
)
21 changes: 10 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# codes are finalised.
collect_ignore_glob.append("*clients/boto*")
collect_ignore_glob.append("*clients/test_cassandra*")
collect_ignore_glob.append("*clients/test_counchbase*")
collect_ignore_glob.append("*clients/test_couchbase*")
collect_ignore_glob.append("*clients/test_google*")
collect_ignore_glob.append("*clients/test_mysql*")
collect_ignore_glob.append("*clients/test_pika*")
Expand All @@ -57,20 +57,19 @@
collect_ignore_glob.append("*frameworks/test_grpcio*")
collect_ignore_glob.append("*frameworks/test_pyramid*")
collect_ignore_glob.append("*frameworks/test_sanic*")
collect_ignore_glob.append("*frameworks/test_starlette*")
collect_ignore_glob.append("*frameworks/test_tornado*")

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

if not os.environ.get("COUCHBASE_TEST"):
collect_ignore_glob.append("*test_couchbase*")
# if not os.environ.get("COUCHBASE_TEST"):
# collect_ignore_glob.append("*test_couchbase*")

if not os.environ.get("GEVENT_STARLETTE_TEST"):
collect_ignore_glob.append("*test_gevent*")
collect_ignore_glob.append("*test_starlette*")
# if not os.environ.get("GEVENT_STARLETTE_TEST"):
# collect_ignore_glob.append("*test_gevent*")
# collect_ignore_glob.append("*test_starlette*")

# Python 3.10 support is incomplete yet
# TODO: Remove this once we start supporting Tornado >= 6.0
Expand Down
Loading

0 comments on commit 1b73d8b

Please sign in to comment.