|
| 1 | +import pytest |
| 2 | +import urllib3 |
| 3 | +import epsagon.wrappers.python_function |
| 4 | +from epsagon.trace import trace_factory |
| 5 | +import epsagon.runners.python_function |
| 6 | +import epsagon.constants |
| 7 | + |
| 8 | + |
| 9 | +TEST_DOMAIN = 'jsonplaceholder.typicode.com' |
| 10 | +TEST_PATH = '/todos/1' |
| 11 | + |
| 12 | +def setup_function(func): |
| 13 | + trace_factory.use_single_trace = True |
| 14 | + epsagon.constants.COLD_START = True |
| 15 | + |
| 16 | +def test_no_data_capture_with_urlopen(trace_transport): |
| 17 | + def use_urlopen(): |
| 18 | + headers = { |
| 19 | + 'Content-Type': 'application/json' |
| 20 | + } |
| 21 | + |
| 22 | + http = urllib3.PoolManager() |
| 23 | + conn = http.connection_from_url(TEST_DOMAIN) |
| 24 | + urllib_response = conn.urlopen( |
| 25 | + method='GET', |
| 26 | + url=TEST_PATH, |
| 27 | + body='', |
| 28 | + headers=headers, |
| 29 | + preload_content=False, |
| 30 | + decode_content=False, |
| 31 | + ) |
| 32 | + return urllib_response |
| 33 | + |
| 34 | + response = use_urlopen() |
| 35 | + data = response.read() |
| 36 | + |
| 37 | + @epsagon.wrappers.python_function.python_wrapper |
| 38 | + def wrapped_function(): |
| 39 | + return use_urlopen() |
| 40 | + |
| 41 | + wrapped_response = wrapped_function() |
| 42 | + wrapped_data = wrapped_response.read() |
| 43 | + # We expect in this case that the data will be available in the buffer |
| 44 | + assert(len(wrapped_data) > 0) |
| 45 | + assert(len(wrapped_response.data) == 0) |
| 46 | + urllib3_event = trace_transport.last_trace.events[-1] |
| 47 | + # Payload should not be collected in the case of raw-stream usage. |
| 48 | + assert(urllib3_event.resource['metadata']['response_body'] is None) |
| 49 | + # Compare un-instrumented vs instrumented data |
| 50 | + assert (len(data) > 0) |
| 51 | + assert (len(response.data) == 0) |
| 52 | + assert(wrapped_data == data) |
| 53 | + assert(wrapped_response.data == response.data) |
| 54 | + |
| 55 | +@pytest.mark.parametrize("preload_content", [True, False]) |
| 56 | +def test_data_capture_with_pool_manager(preload_content, trace_transport): |
| 57 | + def use_poolmanager(): |
| 58 | + headers = { |
| 59 | + 'Content-Type': 'application/json' |
| 60 | + } |
| 61 | + |
| 62 | + http = urllib3.PoolManager() |
| 63 | + return http.request( |
| 64 | + 'GET', TEST_DOMAIN + TEST_PATH, |
| 65 | + headers=headers, body='', |
| 66 | + preload_content=preload_content |
| 67 | + ) |
| 68 | + |
| 69 | + response = use_poolmanager() |
| 70 | + data = response.read() |
| 71 | + |
| 72 | + @epsagon.wrappers.python_function.python_wrapper |
| 73 | + def wrapped_function(): |
| 74 | + return use_poolmanager() |
| 75 | + |
| 76 | + wrapped_response = wrapped_function() |
| 77 | + wrapped_data = wrapped_response.read() |
| 78 | + if not preload_content: |
| 79 | + # In this case data will not be available in the buffer |
| 80 | + assert (len(wrapped_data) > 0) |
| 81 | + # But, data will not be stored in the `data` object |
| 82 | + assert (len(wrapped_response.data) == 0) |
| 83 | + # Compare un-instrumented vs instrumented data |
| 84 | + assert (len(data) > 0) |
| 85 | + assert (len(response.data) == 0) |
| 86 | + else: |
| 87 | + # In this case data will not be available in the buffer |
| 88 | + assert (len(wrapped_data) == 0) |
| 89 | + # But, data will be stored in the `data` object |
| 90 | + assert (len(wrapped_response.data) > 0) |
| 91 | + # Compare un-instrumented vs instrumented data |
| 92 | + assert (len(data) == 0) |
| 93 | + assert (len(response.data) > 0) |
| 94 | + assert(wrapped_data == data) |
| 95 | + assert(wrapped_response.data == response.data) |
0 commit comments