Skip to content

Commit 5fd5f69

Browse files
brettlangdonaniszoubiramar
authored andcommitted
ci: reduce flakiness from httplib https tests (#15318)
## Description We are trying to hit a real third party service, which is now down because of cloudflare outage. We don't actually care that the request was successful, at least from a "200" vs "404" vs "500", we just want to be sure that using the HTTPS request mechanism does properly create a span. ## Testing <!-- Describe your testing strategy or note what tests are included --> ## Risks <!-- Note any risks associated with this change, or "None" if no risks --> ## Additional Notes <!-- Any other information that would be helpful for reviewers -->
1 parent 691ddd2 commit 5fd5f69

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

tests/contrib/httplib/test_httplib.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import socket
44
import sys
55
from urllib import parse
6+
import urllib.error
67
from urllib.request import Request
78
from urllib.request import build_opener
89
from urllib.request import urlopen
@@ -204,11 +205,9 @@ def test_httplib_request_get_request_https(self):
204205
headers={"Accept": "text/plain", "User-Agent": "ddtrace-test"},
205206
)
206207
resp = conn.getresponse()
207-
self.assertEqual(resp.status, 200)
208-
self.assertEqual(
209-
self.to_str(resp.read()),
210-
"My dog used to chase people on a bike a lot. It got so bad I had to take his bike away.",
211-
)
208+
# DEV: We don't care if the result was successful or not, just that we succesfully
209+
# traced an HTTPS request. Relying on third party services is not ideal and can be flaky
210+
status = resp.status
212211

213212
spans = self.pop_spans()
214213
self.assertEqual(len(spans), 1)
@@ -217,12 +216,12 @@ def test_httplib_request_get_request_https(self):
217216
self.assertEqual(span.span_type, "http")
218217
self.assertEqual(span.service, "tests.contrib.httplib")
219218
self.assertEqual(span.name, self.SPAN_NAME)
220-
self.assertEqual(span.error, 0)
219+
self.assertEqual(span.error, status not in (200, 404))
221220
assert span.get_tag("http.method") == "GET"
222221
assert span.get_tag("component") == "httplib"
223222
assert span.get_tag("span.kind") == "client"
224223
assert span.get_tag("out.host") == "icanhazdadjoke.com"
225-
assert_span_http_status_code(span, 200)
224+
assert_span_http_status_code(span, status)
226225
assert span.get_tag("http.url") == "https://icanhazdadjoke.com/j/R7UfaahVfFd"
227226

228227
def test_httplib_request_post_request(self):
@@ -447,14 +446,14 @@ def test_urllib_request_https(self):
447446
url,
448447
headers={"Accept": "text/plain", "User-Agent": "ddtrace-test"},
449448
)
449+
# DEV: We don't care if the result was successful or not, just that we succesfully
450+
# traced an HTTPS request. Relying on third party services is not ideal and can be flaky
450451
with override_global_tracer(self.tracer):
451-
resp = urlopen(req)
452-
453-
self.assertEqual(resp.getcode(), 200)
454-
self.assertEqual(
455-
self.to_str(resp.read()),
456-
"My dog used to chase people on a bike a lot. It got so bad I had to take his bike away.",
457-
)
452+
try:
453+
resp = urlopen(req)
454+
status = resp.getcode()
455+
except urllib.error.HTTPError as e:
456+
status = e.code
458457

459458
spans = self.pop_spans()
460459
self.assertEqual(len(spans), 1)
@@ -463,12 +462,12 @@ def test_urllib_request_https(self):
463462
self.assertEqual(span.span_type, "http")
464463
self.assertEqual(span.service, "tests.contrib.httplib")
465464
self.assertEqual(span.name, self.SPAN_NAME)
466-
self.assertEqual(span.error, 0)
465+
self.assertEqual(span.error, status not in (200, 404))
467466
self.assertEqual(span.get_tag("http.method"), "GET")
468467
self.assertEqual(span.get_tag("component"), "httplib")
469468
self.assertEqual(span.get_tag("span.kind"), "client")
470469
self.assertEqual(span.get_tag("out.host"), "icanhazdadjoke.com")
471-
assert_span_http_status_code(span, 200)
470+
assert_span_http_status_code(span, status)
472471
self.assertEqual(span.get_tag("http.url"), url)
473472

474473
def test_urllib_request_object(self):

0 commit comments

Comments
 (0)