Skip to content

Commit b0a597c

Browse files
committed
Turn requests.X mocks into requests.Session.X
1 parent b9e4d73 commit b0a597c

File tree

23 files changed

+82
-82
lines changed

23 files changed

+82
-82
lines changed

amazon_msk/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def mock_requests_get(url, *args, **kwargs):
3232

3333
@pytest.fixture
3434
def mock_data():
35-
with mock.patch('requests.get', side_effect=mock_requests_get, autospec=True):
35+
with mock.patch('requests.Session.get', side_effect=mock_requests_get, autospec=True):
3636
yield
3737

3838

arangodb/tests/test_arangodb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def mock_requests_get(url, *args, **kwargs):
5656
fixture = url.rsplit('/', 1)[-1]
5757
return MockResponse(file_path=os.path.join(os.path.dirname(__file__), 'fixtures', tag_condition, fixture))
5858

59-
with mock.patch('requests.get', side_effect=mock_requests_get, autospec=True):
59+
with mock.patch('requests.Session.get', side_effect=mock_requests_get, autospec=True):
6060
dd_run_check(check)
6161

6262
aggregator.assert_service_check(

cert_manager/tests/test_cert_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@pytest.fixture()
1616
def error_metrics():
1717
with mock.patch(
18-
'requests.get',
18+
'requests.Session.get',
1919
return_value=mock.MagicMock(status_code=502, headers={'Content-Type': "text/plain"}),
2020
):
2121
yield
@@ -34,7 +34,7 @@ def test_check(aggregator, dd_run_check):
3434
def mock_requests_get(url, *args, **kwargs):
3535
return MockResponse(file_path=os.path.join(os.path.dirname(__file__), 'fixtures', 'cert_manager.txt'))
3636

37-
with mock.patch('requests.get', side_effect=mock_requests_get, autospec=True):
37+
with mock.patch('requests.Session.get', side_effect=mock_requests_get, autospec=True):
3838
dd_run_check(check)
3939

4040
expected_metrics = dict(CERT_METRICS)

citrix_hypervisor/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ def mock_requests_get(url, *args, **kwargs):
5050

5151
@pytest.fixture
5252
def mock_responses():
53-
with mock.patch('requests.get', side_effect=mock_requests_get):
53+
with mock.patch('requests.Session.get', side_effect=mock_requests_get):
5454
yield

datadog_checks_base/tests/base/utils/http/test_authtoken.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ def auth(*args, **kwargs):
612612
return MockResponse(status_code=404)
613613

614614
http = RequestsWrapper(instance, init_config)
615-
with mock.patch('requests.post', side_effect=login), mock.patch('requests.Session.get', side_effect=auth):
615+
with mock.patch('requests.Session.post', side_effect=login), mock.patch('requests.Session.get', side_effect=auth):
616616
http.get('https://leader.mesos/service/some-service')
617617

618618

envoy/tests/legacy/test_unit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def test_metadata_with_exception(
189189
check.check_id = 'test:123'
190190
check.log = mock.MagicMock()
191191

192-
with mock.patch('requests.get', side_effect=exception):
192+
with mock.patch('requests.Session.get', side_effect=exception):
193193
check._collect_metadata()
194194
datadog_agent.assert_metadata_count(0)
195195
check.log.warning.assert_called_with(*log_call_parameters)

haproxy/tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def haproxy_mock():
210210
filepath = os.path.join(HERE, 'fixtures', 'mock_data')
211211
with open(filepath, 'rb') as f:
212212
data = f.read()
213-
p = mock.patch('requests.get', return_value=mock.Mock(content=data))
213+
p = mock.patch('requests.Session.get', return_value=mock.Mock(content=data))
214214
yield p.start()
215215
p.stop()
216216

@@ -228,7 +228,7 @@ def haproxy_mock_evil():
228228
filepath = os.path.join(HERE, 'fixtures', 'mock_data_evil')
229229
with open(filepath, 'rb') as f:
230230
data = f.read()
231-
p = mock.patch('requests.get', return_value=mock.Mock(content=data))
231+
p = mock.patch('requests.Session.get', return_value=mock.Mock(content=data))
232232
yield p.start()
233233
p.stop()
234234

@@ -238,7 +238,7 @@ def haproxy_mock_enterprise_version_info():
238238
filepath = os.path.join(HERE, 'fixtures', 'enterprise_version_info.html')
239239
with open(filepath, 'rb') as f:
240240
data = f.read()
241-
with mock.patch('requests.get', return_value=mock.Mock(content=data)) as p:
241+
with mock.patch('requests.Session.get', return_value=mock.Mock(content=data)) as p:
242242
yield p
243243

244244

haproxy/tests/legacy/test_unit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def test_version_failure(aggregator, check, datadog_agent):
433433
filepath = os.path.join(os.path.dirname(common.HERE), 'fixtures', 'mock_data')
434434
with open(filepath, 'rb') as f:
435435
data = f.read()
436-
with mock.patch('requests.get') as m:
436+
with mock.patch('requests.Session.get') as m:
437437
m.side_effect = [RuntimeError("Ooops"), mock.Mock(content=data)]
438438
haproxy_check.check(config)
439439

kube_controller_manager/tests/test_kube_controller_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def mock_metrics():
3737
with open(f_name, 'r') as f:
3838
text_data = f.read()
3939
with mock.patch(
40-
'requests.get',
40+
'requests.Session.get',
4141
return_value=mock.MagicMock(
4242
status_code=200, iter_lines=lambda **kwargs: text_data.split("\n"), headers={'Content-Type': "text/plain"}
4343
),
@@ -146,13 +146,13 @@ def test_service_check_ok(monkeypatch):
146146
]
147147

148148
# successful health check
149-
with mock.patch("requests.get", return_value=mock.MagicMock(status_code=200)):
149+
with mock.patch('requests.Session.get', return_value=mock.MagicMock(status_code=200)):
150150
check._perform_service_check(instance)
151151

152152
# failed health check
153153
raise_error = mock.Mock()
154154
raise_error.side_effect = requests.HTTPError('health check failed')
155-
with mock.patch("requests.get", return_value=mock.MagicMock(raise_for_status=raise_error)):
155+
with mock.patch('requests.Session.get', return_value=mock.MagicMock(raise_for_status=raise_error)):
156156
check._perform_service_check(instance)
157157

158158
check.service_check.assert_has_calls(calls)

kube_controller_manager/tests/test_sli_metrics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def mock_metrics():
2222
with open(f_name, 'r') as f:
2323
text_data = f.read()
2424
with mock.patch(
25-
'requests.get',
25+
'requests.Session.get',
2626
return_value=mock.MagicMock(
2727
status_code=200, iter_lines=lambda **kwargs: text_data.split("\n"), headers={'Content-Type': "text/plain"}
2828
),
@@ -205,23 +205,23 @@ def mock_request():
205205

206206

207207
def test_detect_sli_endpoint(mock_metrics, instance):
208-
with mock.patch('requests.get') as mock_request:
208+
with mock.patch('requests.Session.get') as mock_request:
209209
mock_request.return_value.status_code = 200
210210
c = KubeControllerManagerCheck(CHECK_NAME, {}, [instance])
211211
c.check(instance)
212212
assert instance["slis_available"] is True
213213

214214

215215
def test_detect_sli_endpoint_404(mock_metrics, instance):
216-
with mock.patch('requests.get') as mock_request:
216+
with mock.patch('requests.Session.get') as mock_request:
217217
mock_request.return_value.status_code = 404
218218
c = KubeControllerManagerCheck(CHECK_NAME, {}, [instance])
219219
c.check(instance)
220220
assert instance["slis_available"] is False
221221

222222

223223
def test_detect_sli_endpoint_403(mock_metrics, mock_request, instance):
224-
with mock.patch('requests.get') as mock_request:
224+
with mock.patch('requests.Session.get') as mock_request:
225225
mock_request.return_value.status_code = 403
226226
c = KubeControllerManagerCheck(CHECK_NAME, {}, [instance])
227227
c.check(instance)

kube_dns/tests/test_kube_dns.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def mock_get():
2424
with open(f_name, 'r') as f:
2525
text_data = f.read()
2626
with mock.patch(
27-
'requests.get',
27+
'requests.Session.get',
2828
return_value=mock.MagicMock(
2929
status_code=200, iter_lines=lambda **kwargs: text_data.split("\n"), headers={'Content-Type': "text/plain"}
3030
),
@@ -96,13 +96,13 @@ def test_service_check_ok(self, monkeypatch):
9696
]
9797

9898
# successful health check
99-
with mock.patch("requests.get", return_value=mock.MagicMock(status_code=200)):
99+
with mock.patch('requests.Session.get', return_value=mock.MagicMock(status_code=200)):
100100
check._perform_service_check(instance)
101101

102102
# failed health check
103103
raise_error = mock.Mock()
104104
raise_error.side_effect = requests.HTTPError('health check failed')
105-
with mock.patch("requests.get", return_value=mock.MagicMock(raise_for_status=raise_error)):
105+
with mock.patch('requests.Session.get', return_value=mock.MagicMock(raise_for_status=raise_error)):
106106
check._perform_service_check(instance)
107107

108108
check.service_check.assert_has_calls(calls)

kube_metrics_server/tests/test_kube_metrics_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def mock_metrics():
2828
with open(f_name, 'r') as f:
2929
text_data = f.read()
3030
with mock.patch(
31-
'requests.get',
31+
'requests.Session.get',
3232
return_value=mock.MagicMock(
3333
status_code=200, iter_lines=lambda **kwargs: text_data.split("\n"), headers={'Content-Type': "text/plain"}
3434
),
@@ -76,13 +76,13 @@ def test_service_check_ok(monkeypatch):
7676
]
7777

7878
# successful health check
79-
with mock.patch("requests.get", return_value=mock.MagicMock(status_code=200)):
79+
with mock.patch('requests.Session.get', return_value=mock.MagicMock(status_code=200)):
8080
check._perform_service_check(instance)
8181

8282
# failed health check
8383
raise_error = mock.Mock()
8484
raise_error.side_effect = requests.HTTPError('health check failed')
85-
with mock.patch("requests.get", return_value=mock.MagicMock(raise_for_status=raise_error)):
85+
with mock.patch('requests.Session.get', return_value=mock.MagicMock(raise_for_status=raise_error)):
8686
check._perform_service_check(instance)
8787

8888
check.service_check.assert_has_calls(calls)

kube_proxy/tests/test_kube_proxy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def mock_iptables():
2525
with open(f_name, 'r') as f:
2626
text_data = f.read()
2727
mock_iptables = mock.patch(
28-
'requests.get',
28+
'requests.Session.get',
2929
return_value=mock.MagicMock(
3030
status_code=200, iter_lines=lambda **kwargs: text_data.split("\n"), headers={'Content-Type': "text/plain"}
3131
),
@@ -40,7 +40,7 @@ def mock_userspace():
4040
with open(f_name, 'r') as f:
4141
text_data = f.read()
4242
mock_userspace = mock.patch(
43-
'requests.get',
43+
'requests.Session.get',
4444
return_value=mock.MagicMock(
4545
status_code=200, iter_lines=lambda **kwargs: text_data.split("\n"), headers={'Content-Type': "text/plain"}
4646
),
@@ -119,13 +119,13 @@ def test_service_check_ok(monkeypatch):
119119
]
120120

121121
# successful health check
122-
with mock.patch("requests.get", return_value=mock.MagicMock(status_code=200)):
122+
with mock.patch('requests.Session.get', return_value=mock.MagicMock(status_code=200)):
123123
check._perform_service_check(instance)
124124

125125
# failed health check
126126
raise_error = mock.Mock()
127127
raise_error.side_effect = requests.HTTPError('health check failed')
128-
with mock.patch("requests.get", return_value=mock.MagicMock(raise_for_status=raise_error)):
128+
with mock.patch('requests.Session.get', return_value=mock.MagicMock(raise_for_status=raise_error)):
129129
check._perform_service_check(instance)
130130

131131
check.service_check.assert_has_calls(calls)

kube_scheduler/tests/test_kube_scheduler_1_14.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def mock_metrics():
2525
with open(f_name, 'r') as f:
2626
text_data = f.read()
2727
with mock.patch(
28-
'requests.get',
28+
'requests.Session.get',
2929
return_value=mock.MagicMock(
3030
status_code=200, iter_lines=lambda **kwargs: text_data.split("\n"), headers={'Content-Type': "text/plain"}
3131
),
@@ -118,13 +118,13 @@ def test_service_check_ok(monkeypatch):
118118
]
119119

120120
# successful health check
121-
with mock.patch("requests.get", return_value=mock.MagicMock(status_code=200)):
121+
with mock.patch('requests.Session.get', return_value=mock.MagicMock(status_code=200)):
122122
check._perform_service_check(instance)
123123

124124
# failed health check
125125
raise_error = mock.Mock()
126126
raise_error.side_effect = requests.HTTPError('health check failed')
127-
with mock.patch("requests.get", return_value=mock.MagicMock(raise_for_status=raise_error)):
127+
with mock.patch('requests.Session.get', return_value=mock.MagicMock(raise_for_status=raise_error)):
128128
check._perform_service_check(instance)
129129

130130
check.service_check.assert_has_calls(calls)

kube_scheduler/tests/test_sli_metrics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def mock_metrics():
2222
with open(f_name, 'r') as f:
2323
text_data = f.read()
2424
with mock.patch(
25-
'requests.get',
25+
'requests.Session.get',
2626
return_value=mock.MagicMock(
2727
status_code=200, iter_lines=lambda **kwargs: text_data.split("\n"), headers={'Content-Type': "text/plain"}
2828
),
@@ -97,23 +97,23 @@ def mock_request():
9797

9898

9999
def test_detect_sli_endpoint(mock_metrics, instance):
100-
with mock.patch('requests.get') as mock_request:
100+
with mock.patch('requests.Session.get') as mock_request:
101101
mock_request.return_value.status_code = 200
102102
c = KubeSchedulerCheck(CHECK_NAME, {}, [instance])
103103
c.check(instance)
104104
assert c._slis_available is True
105105

106106

107107
def test_detect_sli_endpoint_404(mock_metrics, instance):
108-
with mock.patch('requests.get') as mock_request:
108+
with mock.patch('requests.Session.get') as mock_request:
109109
mock_request.return_value.status_code = 404
110110
c = KubeSchedulerCheck(CHECK_NAME, {}, [instance])
111111
c.check(instance)
112112
assert c._slis_available is False
113113

114114

115115
def test_detect_sli_endpoint_403(mock_metrics, instance):
116-
with mock.patch('requests.get') as mock_request:
116+
with mock.patch('requests.Session.get') as mock_request:
117117
mock_request.return_value.status_code = 403
118118
c = KubeSchedulerCheck(CHECK_NAME, {}, [instance])
119119
c.check(instance)

kubelet/tests/test_kubelet.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ def test_kubelet_credentials_update(monkeypatch, aggregator):
563563
get = mock.MagicMock(
564564
status_code=200, iter_lines=lambda **kwargs: mock_from_file('kubelet_metrics_1_14.txt').splitlines()
565565
)
566-
with mock.patch('requests.get', return_value=get):
566+
with mock.patch('requests.Session.get', return_value=get):
567567
check.check(instance)
568568

569569
assert check._http_handlers[instance['kubelet_metrics_endpoint']].options['verify'] is True
@@ -573,7 +573,7 @@ def test_kubelet_credentials_update(monkeypatch, aggregator):
573573
status_code=200, iter_lines=lambda **kwargs: mock_from_file('kubelet_metrics_1_14.txt').splitlines()
574574
)
575575
kubelet_conn_info = {'url': 'http://127.0.0.1:10255', 'ca_cert': False}
576-
with mock.patch('requests.get', return_value=get), mock.patch(
576+
with mock.patch('requests.Session.get', return_value=get), mock.patch(
577577
'datadog_checks.kubelet.kubelet.get_connection_info', return_value=kubelet_conn_info
578578
):
579579
check.check(instance)
@@ -1049,7 +1049,7 @@ def test_perform_kubelet_check(monkeypatch):
10491049

10501050
instance_tags = ["one:1"]
10511051
get = MockedResponse()
1052-
with mock.patch("requests.get", side_effect=get):
1052+
with mock.patch('requests.Session.get', side_effect=get):
10531053
check._perform_kubelet_check(instance_tags)
10541054

10551055
get.assert_has_calls(
@@ -1093,7 +1093,7 @@ def test_report_node_metrics_kubernetes1_18(monkeypatch, aggregator):
10931093

10941094
get = mock.MagicMock(status_code=404, iter_lines=lambda **kwargs: "Error Code")
10951095
get.raise_for_status.side_effect = requests.HTTPError('error')
1096-
with mock.patch('requests.get', return_value=get):
1096+
with mock.patch('requests.Session.get', return_value=get):
10971097
check._report_node_metrics(['foo:bar'])
10981098
aggregator.assert_all_metrics_covered()
10991099

powerdns_recursor/tests/test_metadata.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ def test_metadata_unit(datadog_agent):
2424

2525
config_obj, tags = check._get_config(instance)
2626

27-
with mock.patch('requests.get', side_effect=requests.exceptions.Timeout()):
27+
with mock.patch('requests.Session.get', side_effect=requests.exceptions.Timeout()):
2828
check._collect_metadata(config_obj)
2929
datadog_agent.assert_metadata_count(0)
3030
check.log.debug.assert_called_with('Error collecting PowerDNS Recursor version: %s', '')
3131

3232
datadog_agent.reset()
33-
with mock.patch('requests.get', return_value=MockResponse()):
33+
with mock.patch('requests.Session.get', return_value=MockResponse()):
3434
check._collect_metadata(config_obj)
3535
datadog_agent.assert_metadata_count(0)
3636
check.log.debug.assert_called_with("Couldn't find the PowerDNS Recursor Server version header")
3737

3838
datadog_agent.reset()
39-
with mock.patch('requests.get', return_value=MockResponse(headers={'Server': 'wrong_stuff'})):
39+
with mock.patch('requests.Session.get', return_value=MockResponse(headers={'Server': 'wrong_stuff'})):
4040
check._collect_metadata(config_obj)
4141
datadog_agent.assert_metadata_count(0)
4242
check.log.debug.assert_called_with(

0 commit comments

Comments
 (0)