Skip to content

Commit 692e944

Browse files
committed
Use HTTP mock server for tornado tests
Fixes open-telemetry#1681
1 parent 2edcc21 commit 692e944

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

instrumentation/opentelemetry-instrumentation-tornado/pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ instruments = [
3737
test = [
3838
"opentelemetry-instrumentation-tornado[instruments]",
3939
"opentelemetry-test-utils == 0.40b0.dev",
40+
"http-server-mock"
4041
]
4142

4243
[project.entry-points.opentelemetry_instrumentor]

instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py

+30-26
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from unittest.mock import Mock, patch
1717

18+
from http_server_mock import HttpServerMock
1819
from tornado.testing import AsyncHTTPTestCase
1920

2021
from opentelemetry import trace
@@ -494,32 +495,35 @@ def test_response_headers(self):
494495
self.memory_exporter.clear()
495496
set_global_response_propagator(orig)
496497

497-
# todo(srikanthccv): fix this test
498-
# this test is making request to real httpbin.org/status/200 which
499-
# is not a good idea as it can fail due to availability of the
500-
# service.
501-
# def test_credential_removal(self):
502-
# response = self.fetch(
503-
# "http://username:password@httpbin.org/status/200"
504-
# )
505-
# self.assertEqual(response.code, 200)
506-
507-
# spans = self.sorted_spans(self.memory_exporter.get_finished_spans())
508-
# self.assertEqual(len(spans), 1)
509-
# client = spans[0]
510-
511-
# self.assertEqual(client.name, "GET")
512-
# self.assertEqual(client.kind, SpanKind.CLIENT)
513-
# self.assertSpanHasAttributes(
514-
# client,
515-
# {
516-
# SpanAttributes.HTTP_URL: "http://httpbin.org/status/200",
517-
# SpanAttributes.HTTP_METHOD: "GET",
518-
# SpanAttributes.HTTP_STATUS_CODE: 200,
519-
# },
520-
# )
521-
522-
# self.memory_exporter.clear()
498+
def test_credential_removal(self):
499+
app = HttpServerMock("test_credential_removal")
500+
501+
@app.route("/status/200")
502+
def index():
503+
return "hello"
504+
505+
with app.run("localhost", 5000):
506+
response = self.fetch(
507+
"http://username:password@localhost:5000/status/200"
508+
)
509+
self.assertEqual(response.code, 200)
510+
511+
spans = self.sorted_spans(self.memory_exporter.get_finished_spans())
512+
self.assertEqual(len(spans), 1)
513+
client = spans[0]
514+
515+
self.assertEqual(client.name, "GET")
516+
self.assertEqual(client.kind, SpanKind.CLIENT)
517+
self.assertSpanHasAttributes(
518+
client,
519+
{
520+
SpanAttributes.HTTP_URL: "http://localhost:5000/status/200",
521+
SpanAttributes.HTTP_METHOD: "GET",
522+
SpanAttributes.HTTP_STATUS_CODE: 200,
523+
},
524+
)
525+
526+
self.memory_exporter.clear()
523527

524528

525529
class TestTornadoInstrumentationWithXHeaders(TornadoTest):

0 commit comments

Comments
 (0)