From 9f422a8dfd3dad9072d391e68fb7fb14b6a4cedb Mon Sep 17 00:00:00 2001 From: Julia Kreger Date: Tue, 4 Apr 2023 10:35:04 -0700 Subject: Fix requests calls with timeouts Bandit 1.7.5 dropped with logic to check requests invocations. Specifically if a timeout is not explicitly set, then it results in an error. This should cause our bandit job to go green. Closes-Bug: 2015284 Change-Id: I1dcb3075de63aae97bb22012a54736c293393185 --- ironic/tests/unit/common/test_kickstart_utils.py | 3 +- ironic/tests/unit/common/test_molds.py | 35 ++++++++++++++++-------- 2 files changed, 25 insertions(+), 13 deletions(-) (limited to 'ironic/tests') diff --git a/ironic/tests/unit/common/test_kickstart_utils.py b/ironic/tests/unit/common/test_kickstart_utils.py index 0dd1ac572..db6123b9d 100644 --- a/ironic/tests/unit/common/test_kickstart_utils.py +++ b/ironic/tests/unit/common/test_kickstart_utils.py @@ -129,4 +129,5 @@ echo $CONTENT | /usr/bin/base64 --decode > {file_path}\n\ task.node.instance_info = i_info task.node.save() self.assertEqual(expected, ks_utils.prepare_config_drive(task)) - mock_get.assert_called_with('http://server/fake-configdrive-url') + mock_get.assert_called_with('http://server/fake-configdrive-url', + timeout=60) diff --git a/ironic/tests/unit/common/test_molds.py b/ironic/tests/unit/common/test_molds.py index 810dd61bc..2323c2fa8 100644 --- a/ironic/tests/unit/common/test_molds.py +++ b/ironic/tests/unit/common/test_molds.py @@ -46,7 +46,8 @@ class ConfigurationMoldTestCase(db_base.DbTestCase): molds.save_configuration(task, url, data) mock_put.assert_called_once_with(url, '{\n "key": "value"\n}', - headers={'X-Auth-Token': 'token'}) + headers={'X-Auth-Token': 'token'}, + timeout=60) @mock.patch.object(swift, 'get_swift_session', autospec=True) @mock.patch.object(requests, 'put', autospec=True) @@ -77,7 +78,8 @@ class ConfigurationMoldTestCase(db_base.DbTestCase): mock_put.assert_called_once_with( url, '{\n "key": "value"\n}', - headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}) + headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}, + timeout=60) @mock.patch.object(requests, 'put', autospec=True) def test_save_configuration_http_noauth(self, mock_put): @@ -91,7 +93,8 @@ class ConfigurationMoldTestCase(db_base.DbTestCase): molds.save_configuration(task, url, data) mock_put.assert_called_once_with( url, '{\n "key": "value"\n}', - headers=None) + headers=None, + timeout=60) @mock.patch.object(requests, 'put', autospec=True) def test_save_configuration_http_error(self, mock_put): @@ -112,7 +115,8 @@ class ConfigurationMoldTestCase(db_base.DbTestCase): {'key': 'value'}) mock_put.assert_called_once_with( 'https://example.com/file2', '{\n "key": "value"\n}', - headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}) + headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}, + timeout=60) @mock.patch.object(requests, 'put', autospec=True) def test_save_configuration_connection_error(self, mock_put): @@ -132,7 +136,8 @@ class ConfigurationMoldTestCase(db_base.DbTestCase): task, 'https://example.com/file2', {'key': 'value'}) mock_put.assert_called_with( 'https://example.com/file2', '{\n "key": "value"\n}', - headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}) + headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}, + timeout=60) self.assertEqual(mock_put.call_count, 3) @mock.patch.object(requests, 'put', autospec=True) @@ -155,7 +160,8 @@ class ConfigurationMoldTestCase(db_base.DbTestCase): {'key': 'value'}) mock_put.assert_called_with( 'https://example.com/file2', '{\n "key": "value"\n}', - headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}) + headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}, + timeout=60) self.assertEqual(mock_put.call_count, 2) @mock.patch.object(swift, 'get_swift_session', autospec=True) @@ -176,7 +182,8 @@ class ConfigurationMoldTestCase(db_base.DbTestCase): result = molds.get_configuration(task, url) mock_get.assert_called_once_with( - url, headers={'X-Auth-Token': 'token'}) + url, headers={'X-Auth-Token': 'token'}, + timeout=60) self.assertJsonEqual({'key': 'value'}, result) @mock.patch.object(swift, 'get_swift_session', autospec=True) @@ -210,7 +217,8 @@ class ConfigurationMoldTestCase(db_base.DbTestCase): result = molds.get_configuration(task, url) mock_get.assert_called_once_with( - url, headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}) + url, headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}, + timeout=60) self.assertJsonEqual({"key": "value"}, result) @mock.patch.object(requests, 'get', autospec=True) @@ -228,7 +236,7 @@ class ConfigurationMoldTestCase(db_base.DbTestCase): with task_manager.acquire(self.context, self.node.uuid) as task: result = molds.get_configuration(task, url) - mock_get.assert_called_once_with(url, headers=None) + mock_get.assert_called_once_with(url, headers=None, timeout=60) self.assertJsonEqual({"key": "value"}, result) @mock.patch.object(requests, 'get', autospec=True) @@ -249,7 +257,8 @@ class ConfigurationMoldTestCase(db_base.DbTestCase): 'https://example.com/file2') mock_get.assert_called_once_with( 'https://example.com/file2', - headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}) + headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}, + timeout=60) @mock.patch.object(requests, 'get', autospec=True) def test_get_configuration_connection_error(self, mock_get): @@ -269,7 +278,8 @@ class ConfigurationMoldTestCase(db_base.DbTestCase): task, 'https://example.com/file2') mock_get.assert_called_with( 'https://example.com/file2', - headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}) + headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}, + timeout=60) self.assertEqual(mock_get.call_count, 3) @mock.patch.object(requests, 'get', autospec=True) @@ -291,7 +301,8 @@ class ConfigurationMoldTestCase(db_base.DbTestCase): 'https://example.com/file2') mock_get.assert_called_with( 'https://example.com/file2', - headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}) + headers={'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}, + timeout=60) self.assertEqual(mock_get.call_count, 2) @mock.patch.object(requests, 'get', autospec=True) -- cgit v1.2.1