From 20c97e83d3af855e5e238dfa4b10b7bf29533d57 Mon Sep 17 00:00:00 2001 From: Steve Kowalik Date: Tue, 24 May 2022 11:56:35 +1000 Subject: Remove use of mock Since Python 3.4, the unittest module has provided mock, negating the need for the external dependancy. Switch to using unittest.mock. Change-Id: Idec3aaed2fddd1ece3ed86ee0bcc48f7616d56fa --- test-requirements.txt | 1 - test/unit/test_authv1.py | 2 +- test/unit/test_command_helpers.py | 2 +- test/unit/test_service.py | 67 +++++++++++++++++++-------------------- test/unit/test_shell.py | 2 +- test/unit/test_swiftclient.py | 2 +- test/unit/test_utils.py | 2 +- test/unit/utils.py | 2 +- 8 files changed, 39 insertions(+), 41 deletions(-) diff --git a/test-requirements.txt b/test-requirements.txt index c2fb2c6..b0633eb 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -3,6 +3,5 @@ hacking>=3.2.0,<3.3.0;python_version>='3.0' # Apache-2.0 coverage!=4.4,>=4.0 # Apache-2.0 keystoneauth1>=3.4.0 # Apache-2.0 -mock>=1.2.0 # BSD stestr>=2.0.0,!=3.0.0 # Apache-2.0 openstacksdk>=0.11.0 # Apache-2.0 diff --git a/test/unit/test_authv1.py b/test/unit/test_authv1.py index c16227f..b4de7e0 100644 --- a/test/unit/test_authv1.py +++ b/test/unit/test_authv1.py @@ -14,8 +14,8 @@ import datetime import json -import mock import unittest +from unittest import mock from keystoneauth1 import plugin from keystoneauth1 import loading from keystoneauth1 import exceptions diff --git a/test/unit/test_command_helpers.py b/test/unit/test_command_helpers.py index 1cb3bb1..3e51aa9 100644 --- a/test/unit/test_command_helpers.py +++ b/test/unit/test_command_helpers.py @@ -13,9 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import mock from io import StringIO import unittest +from unittest import mock from swiftclient import command_helpers as h from swiftclient.multithreading import OutputManager diff --git a/test/unit/test_service.py b/test/unit/test_service.py index 0ad5378..1176a1f 100644 --- a/test/unit/test_service.py +++ b/test/unit/test_service.py @@ -16,17 +16,16 @@ import builtins import contextlib import io -import mock import os import tempfile import unittest import time import json from io import BytesIO +from unittest import mock from concurrent.futures import Future from hashlib import md5 -from mock import Mock, PropertyMock from queue import Queue, Empty as QueueEmptyError from time import sleep @@ -215,9 +214,9 @@ class TestSwiftReader(unittest.TestCase): class _TestServiceBase(unittest.TestCase): def _get_mock_connection(self, attempts=2): - m = Mock(spec=Connection) - type(m).attempts = PropertyMock(return_value=attempts) - type(m).auth_end_time = PropertyMock(return_value=4) + m = mock.Mock(spec=Connection) + type(m).attempts = mock.PropertyMock(return_value=attempts) + type(m).auth_end_time = mock.PropertyMock(return_value=4) return m def _get_queue(self, q): @@ -272,7 +271,7 @@ class TestServiceDelete(_TestServiceBase): def test_delete_segment_exception(self): mock_q = Queue() mock_conn = self._get_mock_connection() - mock_conn.delete_object = Mock(side_effect=self.exc) + mock_conn.delete_object = mock.Mock(side_effect=self.exc) expected_r = self._get_expected({ 'action': 'delete_segment', 'object': 'test_s', @@ -298,7 +297,7 @@ class TestServiceDelete(_TestServiceBase): def test_delete_object(self): mock_q = Queue() mock_conn = self._get_mock_connection() - mock_conn.head_object = Mock(return_value={}) + mock_conn.head_object = mock.Mock(return_value={}) expected_r = self._get_expected({ 'action': 'delete_object', 'success': True @@ -346,7 +345,7 @@ class TestServiceDelete(_TestServiceBase): def test_delete_object_with_headers(self): mock_q = Queue() mock_conn = self._get_mock_connection() - mock_conn.head_object = Mock(return_value={}) + mock_conn.head_object = mock.Mock(return_value={}) expected_r = self._get_expected({ 'action': 'delete_object', 'success': True @@ -369,7 +368,7 @@ class TestServiceDelete(_TestServiceBase): def test_delete_object_exception(self): mock_q = Queue() mock_conn = self._get_mock_connection() - mock_conn.delete_object = Mock(side_effect=self.exc) + mock_conn.delete_object = mock.Mock(side_effect=self.exc) expected_r = self._get_expected({ 'action': 'delete_object', 'success': False, @@ -402,7 +401,7 @@ class TestServiceDelete(_TestServiceBase): # additional query string to cause the right delete server side mock_q = Queue() mock_conn = self._get_mock_connection() - mock_conn.head_object = Mock( + mock_conn.head_object = mock.Mock( return_value={'x-static-large-object': True} ) expected_r = self._get_expected({ @@ -435,10 +434,10 @@ class TestServiceDelete(_TestServiceBase): # A DLO object is determined in _delete_object by heading the object # and checking for the existence of a x-object-manifest header. # Mock that here. - mock_conn.head_object = Mock( + mock_conn.head_object = mock.Mock( return_value={'x-object-manifest': 'manifest_c/manifest_p'} ) - mock_conn.get_container = Mock( + mock_conn.get_container = mock.Mock( side_effect=[(None, [{'name': 'test_seg_1'}, {'name': 'test_seg_2'}]), (None, {})] @@ -495,7 +494,7 @@ class TestServiceDelete(_TestServiceBase): def test_delete_empty_container_exception(self): mock_conn = self._get_mock_connection() - mock_conn.delete_container = Mock(side_effect=self.exc) + mock_conn.delete_container = mock.Mock(side_effect=self.exc) expected_r = self._get_expected({ 'action': 'delete_container', 'success': False, @@ -825,7 +824,7 @@ class TestServiceList(_TestServiceBase): (None, [{'name': 'test_c'}]), (None, []) ] - mock_conn.get_account = Mock(side_effect=get_account_returns) + mock_conn.get_account = mock.Mock(side_effect=get_account_returns) expected_r = self._get_expected({ 'action': 'list_account_part', @@ -841,12 +840,12 @@ class TestServiceList(_TestServiceBase): self.assertIsNone(self._get_queue(mock_q)) long_opts = dict(self.opts, **{'long': True}) - mock_conn.head_container = Mock(return_value={'test_m': '1'}) + mock_conn.head_container = mock.Mock(return_value={'test_m': '1'}) get_account_returns = [ (None, [{'name': 'test_c'}]), (None, []) ] - mock_conn.get_account = Mock(side_effect=get_account_returns) + mock_conn.get_account = mock.Mock(side_effect=get_account_returns) expected_r_long = self._get_expected({ 'action': 'list_account_part', @@ -868,7 +867,7 @@ class TestServiceList(_TestServiceBase): (None, [{'name': 'test_c'}]), (None, []) ] - mock_conn.get_account = Mock(side_effect=get_account_returns) + mock_conn.get_account = mock.Mock(side_effect=get_account_returns) expected_r = self._get_expected({ 'action': 'list_account_part', @@ -892,7 +891,7 @@ class TestServiceList(_TestServiceBase): def test_list_account_exception(self): mock_q = Queue() mock_conn = self._get_mock_connection() - mock_conn.get_account = Mock(side_effect=self.exc) + mock_conn.get_account = mock.Mock(side_effect=self.exc) expected_r = self._get_expected({ 'action': 'list_account_part', 'success': False, @@ -918,7 +917,7 @@ class TestServiceList(_TestServiceBase): (None, [{'name': 'test_o'}]), (None, []) ] - mock_conn.get_container = Mock(side_effect=get_container_returns) + mock_conn.get_container = mock.Mock(side_effect=get_container_returns) expected_r = self._get_expected({ 'action': 'list_container_part', @@ -935,12 +934,12 @@ class TestServiceList(_TestServiceBase): self.assertIsNone(self._get_queue(mock_q)) long_opts = dict(self.opts, **{'long': True}) - mock_conn.head_container = Mock(return_value={'test_m': '1'}) + mock_conn.head_container = mock.Mock(return_value={'test_m': '1'}) get_container_returns = [ (None, [{'name': 'test_o'}]), (None, []) ] - mock_conn.get_container = Mock(side_effect=get_container_returns) + mock_conn.get_container = mock.Mock(side_effect=get_container_returns) expected_r_long = self._get_expected({ 'action': 'list_container_part', @@ -964,7 +963,7 @@ class TestServiceList(_TestServiceBase): (None, [{'name': 'b'}, {'name': 'c'}]), (None, []) ] - mock_get_cont = Mock(side_effect=get_container_returns) + mock_get_cont = mock.Mock(side_effect=get_container_returns) mock_conn.get_container = mock_get_cont expected_r = self._get_expected({ @@ -998,7 +997,7 @@ class TestServiceList(_TestServiceBase): (None, [{'name': 'test_o'}]), (None, []) ] - mock_conn.get_container = Mock(side_effect=get_container_returns) + mock_conn.get_container = mock.Mock(side_effect=get_container_returns) expected_r = self._get_expected({ 'action': 'list_container_part', @@ -1027,7 +1026,7 @@ class TestServiceList(_TestServiceBase): def test_list_container_exception(self): mock_q = Queue() mock_conn = self._get_mock_connection() - mock_conn.get_container = Mock(side_effect=self.exc) + mock_conn.get_container = mock.Mock(side_effect=self.exc) expected_r = self._get_expected({ 'action': 'list_container_part', 'container': 'test_c', @@ -1120,7 +1119,7 @@ class TestServiceList(_TestServiceBase): (None, [{'name': 'container14'}]), (None, []) ] - mock_conn.get_account = Mock(side_effect=get_account_returns) + mock_conn.get_account = mock.Mock(side_effect=get_account_returns) mock_get_conn.return_value = mock_conn s = SwiftService(options=self.opts) @@ -2218,7 +2217,7 @@ class TestServiceDownload(_TestServiceBase): sub_page.side_effect = fake_sub_page - r = Mock(spec=Future) + r = mock.Mock(spec=Future) r.result.return_value = self._get_expected({ 'success': True, 'start_time': 1, @@ -2257,7 +2256,7 @@ class TestServiceDownload(_TestServiceBase): return repr(self.value) def _make_result(): - r = Mock(spec=Future) + r = mock.Mock(spec=Future) r.result.return_value = self._get_expected({ 'success': True, 'start_time': 1, @@ -2329,7 +2328,7 @@ class TestServiceDownload(_TestServiceBase): }) with mock.patch.object(builtins, 'open') as mock_open: - written_content = Mock() + written_content = mock.Mock() mock_open.return_value = written_content s = SwiftService() _opts = self.opts.copy() @@ -2373,7 +2372,7 @@ class TestServiceDownload(_TestServiceBase): with mock.patch.object(builtins, 'open') as mock_open, \ mock.patch('swiftclient.service.utime') as mock_utime: - written_content = Mock() + written_content = mock.Mock() mock_open.return_value = written_content s = SwiftService() _opts = self.opts.copy() @@ -2419,7 +2418,7 @@ class TestServiceDownload(_TestServiceBase): with mock.patch.object(builtins, 'open') as mock_open, \ mock.patch('swiftclient.service.utime') as mock_utime: - written_content = Mock() + written_content = mock.Mock() mock_open.return_value = written_content s = SwiftService() _opts = self.opts.copy() @@ -2464,7 +2463,7 @@ class TestServiceDownload(_TestServiceBase): with mock.patch.object(builtins, 'open') as mock_open, \ mock.patch('swiftclient.service.utime') as mock_utime: - written_content = Mock() + written_content = mock.Mock() mock_open.return_value = written_content s = SwiftService() _opts = self.opts.copy() @@ -2492,7 +2491,7 @@ class TestServiceDownload(_TestServiceBase): def test_download_object_job_exception(self): mock_conn = self._get_mock_connection() - mock_conn.get_object = Mock(side_effect=self.exc) + mock_conn.get_object = mock.Mock(side_effect=self.exc) expected_r = self._get_expected({ 'success': False, 'error': self.exc, @@ -3026,7 +3025,7 @@ class TestServicePost(_TestServiceBase): Check post method translates strings and objects to _post_object_job calls correctly """ - tm_instance = Mock() + tm_instance = mock.Mock() thread_manager.return_value = tm_instance self.opts.update({'meta': ["meta1:test1"], "header": ["hdr1:test1"]}) @@ -3071,7 +3070,7 @@ class TestServiceCopy(_TestServiceBase): Check copy method translates strings and objects to _copy_object_job calls correctly """ - tm_instance = Mock() + tm_instance = mock.Mock() thread_manager.return_value = tm_instance self.opts.update({'meta': ["meta1:test1"], "header": ["hdr1:test1"]}) diff --git a/test/unit/test_shell.py b/test/unit/test_shell.py index eb70a92..94168b5 100644 --- a/test/unit/test_shell.py +++ b/test/unit/test_shell.py @@ -20,10 +20,10 @@ import getpass import hashlib import json import logging -import mock import os import tempfile import unittest +from unittest import mock import textwrap from time import localtime, mktime, strftime, strptime diff --git a/test/unit/test_swiftclient.py b/test/unit/test_swiftclient.py index 3b59166..ad2af50 100644 --- a/test/unit/test_swiftclient.py +++ b/test/unit/test_swiftclient.py @@ -16,11 +16,11 @@ import gzip import json import logging -import mock import io import socket import string import unittest +from unittest import mock import warnings import tempfile from hashlib import md5 diff --git a/test/unit/test_utils.py b/test/unit/test_utils.py index 3833154..33fd415 100644 --- a/test/unit/test_utils.py +++ b/test/unit/test_utils.py @@ -17,7 +17,7 @@ import gzip import io import json import unittest -import mock +from unittest import mock import tempfile from time import gmtime, localtime, mktime, strftime, strptime from hashlib import md5, sha1 diff --git a/test/unit/utils.py b/test/unit/utils.py index 0dc0b3c..87d3210 100644 --- a/test/unit/utils.py +++ b/test/unit/utils.py @@ -20,10 +20,10 @@ import os import sys from time import sleep import unittest +from unittest import mock from requests import RequestException from requests.structures import CaseInsensitiveDict -import mock from urllib.parse import urlparse, ParseResult from swiftclient import client as c from swiftclient import shell as s -- cgit v1.2.1