From e2d8f32d31d5a5ee1d4d87d3af50694d707a796d Mon Sep 17 00:00:00 2001 From: James Page Date: Thu, 16 Dec 2021 09:20:28 +0000 Subject: [PATCH] Use unittest.mock instead of mock The mock third party library was needed for mock support in py2 runtimes. Since we now only support py36 and later, we can use the standard lib unittest.mock module instead. Change-Id: Idffdcf1153821c3d9514f3410e5609ea8c99fe74 --- .zuul.yaml | 1 - test-requirements.txt | 5 ----- unit_tests/test_action_change_osd_weight.py | 2 +- unit_tests/test_action_list_inconsistent.py | 2 +- unit_tests/test_action_list_pools.py | 12 ++++++++---- unit_tests/test_action_purge_osd.py | 2 +- unit_tests/test_actions_mon.py | 2 +- unit_tests/test_ceph_actions.py | 2 +- unit_tests/test_ceph_hooks.py | 2 +- unit_tests/test_ceph_utils.py | 4 ++-- unit_tests/test_check_ceph_status.py | 2 +- unit_tests/test_status.py | 2 +- unit_tests/test_upgrade.py | 2 +- unit_tests/test_utils.py | 2 +- 14 files changed, 20 insertions(+), 22 deletions(-) diff --git a/.zuul.yaml b/.zuul.yaml index b3037e94..0eed1965 100644 --- a/.zuul.yaml +++ b/.zuul.yaml @@ -1,5 +1,4 @@ - project: templates: - - python35-charm-jobs - openstack-python3-ussuri-jobs - openstack-cover-jobs diff --git a/test-requirements.txt b/test-requirements.txt index f853625d..0aabe171 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -13,11 +13,6 @@ setuptools<50.0.0 # https://github.com/pypa/setuptools/commit/04e3df22df840c6bb requests>=2.18.4 -# Newer mock seems to have some syntax which is newer than python3.5 (e.g. -# f'{something}' -mock>=1.2,<4.0.0; python_version < '3.6' -mock>=1.2; python_version >= '3.6' - stestr>=2.2.0 # Dependency of stestr. Workaround for diff --git a/unit_tests/test_action_change_osd_weight.py b/unit_tests/test_action_change_osd_weight.py index d3ce3ff4..fbe26bd7 100644 --- a/unit_tests/test_action_change_osd_weight.py +++ b/unit_tests/test_action_change_osd_weight.py @@ -14,7 +14,7 @@ """Tests for reweight_osd action.""" from actions import change_osd_weight as action -from mock import mock +import unittest.mock as mock from test_utils import CharmTestCase diff --git a/unit_tests/test_action_list_inconsistent.py b/unit_tests/test_action_list_inconsistent.py index 6f006ce6..6a3694cf 100644 --- a/unit_tests/test_action_list_inconsistent.py +++ b/unit_tests/test_action_list_inconsistent.py @@ -14,7 +14,7 @@ """Tests for the list_inconsistent_objs action.""" from actions import list_inconsistent_objs as action -from mock import mock +import unittest.mock as mock from test_utils import CharmTestCase diff --git a/unit_tests/test_action_list_pools.py b/unit_tests/test_action_list_pools.py index f42ba439..2491085d 100644 --- a/unit_tests/test_action_list_pools.py +++ b/unit_tests/test_action_list_pools.py @@ -107,9 +107,13 @@ class ListPoolsTestCase(CharmTestCase): def test_getting_list_pools_with_details(self): """Test getting list of pools with details.""" + self.pools = None + + def _function_set(message): + self.pools = json.loads(message['message']) + self.function_set.side_effect = _function_set list_pools.main() self.function_get.assert_called_once_with("format") - pools = json.loads(self.function_set.call_args.args[0]["message"]) - self.assertEqual(pools[0]["pool"], 1) - self.assertEqual(pools[0]["size"], 3) - self.assertEqual(pools[0]["min_size"], 2) + self.assertEqual(self.pools[0]["pool"], 1) + self.assertEqual(self.pools[0]["size"], 3) + self.assertEqual(self.pools[0]["min_size"], 2) diff --git a/unit_tests/test_action_purge_osd.py b/unit_tests/test_action_purge_osd.py index 64d4f6fd..2146f9e8 100644 --- a/unit_tests/test_action_purge_osd.py +++ b/unit_tests/test_action_purge_osd.py @@ -14,7 +14,7 @@ """Tests for purge_osd action.""" from actions import purge_osd as action -from mock import mock +import unittest.mock as mock from test_utils import CharmTestCase diff --git a/unit_tests/test_actions_mon.py b/unit_tests/test_actions_mon.py index fb749bf9..edbb4561 100644 --- a/unit_tests/test_actions_mon.py +++ b/unit_tests/test_actions_mon.py @@ -12,7 +12,7 @@ # limitations under the License. import json import sys -from mock import mock +import unittest.mock as mock from test_utils import CharmTestCase diff --git a/unit_tests/test_ceph_actions.py b/unit_tests/test_ceph_actions.py index c54830fa..81f31e7c 100644 --- a/unit_tests/test_ceph_actions.py +++ b/unit_tests/test_ceph_actions.py @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import mock +import unittest.mock as mock import subprocess import test_utils diff --git a/unit_tests/test_ceph_hooks.py b/unit_tests/test_ceph_hooks.py index 707ee52c..59a58a52 100644 --- a/unit_tests/test_ceph_hooks.py +++ b/unit_tests/test_ceph_hooks.py @@ -3,7 +3,7 @@ import json import unittest import sys -from mock import patch, MagicMock, DEFAULT, call +from unittest.mock import patch, MagicMock, DEFAULT, call # python-apt is not installed as part of test-requirements but is imported by # some charmhelpers modules so create a fake import. diff --git a/unit_tests/test_ceph_utils.py b/unit_tests/test_ceph_utils.py index 0ea4c626..4332c71a 100644 --- a/unit_tests/test_ceph_utils.py +++ b/unit_tests/test_ceph_utils.py @@ -12,7 +12,7 @@ # limitations under the License. import json -import mock +import unittest.mock as mock import test_utils @@ -250,7 +250,7 @@ class CephUtilsTestCase(test_utils.CharmTestCase): def test_set_require_osd_release_raise_call_error(self, log, check_call): release = 'luminous' check_call.side_effect = utils.subprocess.CalledProcessError( - 0, mock.mock.MagicMock() + 0, mock.MagicMock() ) expected_call = mock.call( ['ceph', 'osd', 'require-osd-release', release] diff --git a/unit_tests/test_check_ceph_status.py b/unit_tests/test_check_ceph_status.py index b7b06155..5342ce55 100644 --- a/unit_tests/test_check_ceph_status.py +++ b/unit_tests/test_check_ceph_status.py @@ -16,7 +16,7 @@ import unittest import os import sys -from mock import patch +from unittest.mock import patch # import the module we want to test os.sys.path.insert(1, os.path.join(sys.path[0], 'files/nagios')) diff --git a/unit_tests/test_status.py b/unit_tests/test_status.py index 71b845eb..ff181e16 100644 --- a/unit_tests/test_status.py +++ b/unit_tests/test_status.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import mock +import unittest.mock as mock import sys import test_utils diff --git a/unit_tests/test_upgrade.py b/unit_tests/test_upgrade.py index f784f7cb..f60bb43b 100644 --- a/unit_tests/test_upgrade.py +++ b/unit_tests/test_upgrade.py @@ -1,4 +1,4 @@ -from mock import patch +from unittest.mock import patch from ceph_hooks import check_for_upgrade from test_utils import CharmTestCase diff --git a/unit_tests/test_utils.py b/unit_tests/test_utils.py index 83fe5ae2..ce139de5 100644 --- a/unit_tests/test_utils.py +++ b/unit_tests/test_utils.py @@ -17,7 +17,7 @@ import unittest import os import yaml -from mock import patch +from unittest.mock import patch def load_config():