Skip to content

Commit

Permalink
Remove use of mock
Browse files Browse the repository at this point in the history
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
  • Loading branch information
s-t-e-v-e-n-k committed May 24, 2022
1 parent 95f68cd commit 20c97e8
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 41 deletions.
1 change: 0 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/unit/test_authv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_command_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 33 additions & 34 deletions test/unit/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -215,9 +214,9 @@ def _consume(sr):

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):
Expand Down Expand Up @@ -272,7 +271,7 @@ def test_delete_segment(self):
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',
Expand All @@ -298,7 +297,7 @@ def test_delete_segment_exception(self):
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
Expand Down Expand Up @@ -346,7 +345,7 @@ def test_delete_object_version(self, mock_connection_class):
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
Expand All @@ -369,7 +368,7 @@ def test_delete_object_with_headers(self):
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,
Expand Down Expand Up @@ -402,7 +401,7 @@ def test_delete_object_slo_support(self):
# 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({
Expand Down Expand Up @@ -435,10 +434,10 @@ def test_delete_object_dlo_support(self):
# 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, {})]
Expand Down Expand Up @@ -495,7 +494,7 @@ def test_delete_empty_container_with_headers(self):

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,
Expand Down Expand Up @@ -825,7 +824,7 @@ def test_list_account(self):
(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',
Expand All @@ -841,12 +840,12 @@ def test_list_account(self):
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',
Expand All @@ -868,7 +867,7 @@ def test_list_account_with_headers(self):
(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',
Expand All @@ -892,7 +891,7 @@ def test_list_account_with_headers(self):
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,
Expand All @@ -918,7 +917,7 @@ def test_list_container(self):
(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',
Expand All @@ -935,12 +934,12 @@ def test_list_container(self):
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',
Expand All @@ -964,7 +963,7 @@ def test_list_container_marker(self):
(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({
Expand Down Expand Up @@ -998,7 +997,7 @@ def test_list_container_with_headers(self):
(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',
Expand Down Expand Up @@ -1027,7 +1026,7 @@ def test_list_container_with_headers(self):
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',
Expand Down Expand Up @@ -1120,7 +1119,7 @@ def test_list_queue_size(self, mock_get_conn):
(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)
Expand Down Expand Up @@ -2218,7 +2217,7 @@ def fake_sub_page(*args):

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,
Expand Down Expand Up @@ -2257,7 +2256,7 @@ def __str__(self):
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,
Expand Down Expand Up @@ -2329,7 +2328,7 @@ def test_download_object_job(self):
})

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()
Expand Down Expand Up @@ -2373,7 +2372,7 @@ def test_download_object_job_with_mtime(self):

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()
Expand Down Expand Up @@ -2419,7 +2418,7 @@ def test_download_object_job_bad_mtime(self):

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()
Expand Down Expand Up @@ -2464,7 +2463,7 @@ def test_download_object_job_ignore_mtime(self):

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()
Expand Down Expand Up @@ -2492,7 +2491,7 @@ def test_download_object_job_ignore_mtime(self):

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,
Expand Down Expand Up @@ -3026,7 +3025,7 @@ def test_object_post(self, res_iter, thread_manager):
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"]})
Expand Down Expand Up @@ -3071,7 +3070,7 @@ def test_object_copy(self, inter_compl, thread_manager):
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"]})
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
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

Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_swiftclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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
Expand Down
2 changes: 1 addition & 1 deletion test/unit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
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
Expand Down

0 comments on commit 20c97e8

Please sign in to comment.