|
| 1 | +from __future__ import absolute_import |
| 2 | +from nose.tools import assert_equals |
| 3 | +import opentracing |
| 4 | +import instana.instrumentation.urllib3 |
| 5 | +import urllib3 |
| 6 | + |
| 7 | + |
| 8 | +class TestUrllib3: |
| 9 | + def setUp(self): |
| 10 | + """ Clear all spans before a test run """ |
| 11 | + self.http = urllib3.PoolManager() |
| 12 | + self.recorder = opentracing.global_tracer.recorder |
| 13 | + self.recorder.clear_spans() |
| 14 | + |
| 15 | + def tearDown(self): |
| 16 | + """ Do nothing for now """ |
| 17 | + return None |
| 18 | + |
| 19 | + def test_vanilla_requests(self): |
| 20 | + r = self.http.request('GET', 'http://127.0.0.1:5000/') |
| 21 | + assert_equals(r.status, 200) |
| 22 | + |
| 23 | + def test_get_request(self): |
| 24 | + span = opentracing.global_tracer.start_span("test") |
| 25 | + r = self.http.request('GET', 'http://127.0.0.1:5000/') |
| 26 | + span.finish() |
| 27 | + |
| 28 | + spans = self.recorder.queued_spans() |
| 29 | + first_span = spans[1] |
| 30 | + second_span = spans[0] |
| 31 | + |
| 32 | + assert(r) |
| 33 | + assert_equals(200, r.status) |
| 34 | + assert_equals(2, len(spans)) |
| 35 | + assert_equals("test", first_span.data.sdk.name) |
| 36 | + assert_equals("urllib3", second_span.n) |
| 37 | + assert_equals(200, second_span.data.http.status) |
| 38 | + assert_equals("http://127.0.0.1:5000/", second_span.data.http.url) |
| 39 | + assert_equals("GET", second_span.data.http.method) |
| 40 | + |
| 41 | + assert_equals(None, second_span.error) |
| 42 | + assert_equals(None, second_span.ec) |
| 43 | + |
| 44 | + def test_put_request(self): |
| 45 | + span = opentracing.global_tracer.start_span("test") |
| 46 | + r = self.http.request('PUT', 'http://127.0.0.1:5000/notfound') |
| 47 | + span.finish() |
| 48 | + |
| 49 | + spans = self.recorder.queued_spans() |
| 50 | + first_span = spans[1] |
| 51 | + second_span = spans[0] |
| 52 | + |
| 53 | + assert(r) |
| 54 | + assert_equals(404, r.status) |
| 55 | + assert_equals(2, len(spans)) |
| 56 | + assert_equals("test", first_span.data.sdk.name) |
| 57 | + assert_equals("urllib3", second_span.n) |
| 58 | + assert_equals(404, second_span.data.http.status) |
| 59 | + assert_equals("http://127.0.0.1:5000/notfound", second_span.data.http.url) |
| 60 | + assert_equals("PUT", second_span.data.http.method) |
| 61 | + assert_equals(None, second_span.error) |
| 62 | + assert_equals(None, second_span.ec) |
0 commit comments