From 140d982354055cca4f23a1188146b2ccd03a4db7 Mon Sep 17 00:00:00 2001 From: Tomas Korbar Date: Mon, 25 Nov 2019 13:29:52 +0100 Subject: [PATCH] Replace python flexmock module with builtin mock Also replace dependency in specfile and alter dockerfile Signed-off-by: Tomas Korbar --- tests/unit/Dockerfile | 2 +- tests/unit/exports/__init__.py | 1 + tests/unit/exports/test_controller.py | 6 +++--- tests/unit/globals.py | 3 +-- tests/unit/hardware/__init__.py | 1 + tests/unit/hardware/test_inventory.py | 6 +++--- tests/unit/monitors/__init__.py | 1 + tests/unit/monitors/test_base.py | 1 - tests/unit/plugins/__init__.py | 1 + tests/unit/plugins/test_base.py | 5 ----- tests/unit/profiles/__init__.py | 1 + tests/unit/profiles/test_loader.py | 4 ---- tests/unit/storage/__init__.py | 1 + tests/unit/storage/test_factory.py | 6 +++--- tests/unit/storage/test_storage.py | 23 ++++++++++++++--------- tests/unit/utils/__init__.py | 1 + tests/unit/utils/test_commands.py | 5 ----- tests/unit/utils/test_global_config.py | 5 ----- tuned.spec | 9 +-------- 19 files changed, 33 insertions(+), 49 deletions(-) diff --git a/tests/unit/Dockerfile b/tests/unit/Dockerfile index 2e4e258..14a74b6 100644 --- a/tests/unit/Dockerfile +++ b/tests/unit/Dockerfile @@ -15,7 +15,7 @@ RUN if [[ $OS == "centos:7" ]]; then yum install -y epel-release; fi; RUN ${PKM} install -y virt-what ethtool gawk hdparm util-linux dbus polkit make ARG PYTHON -RUN ${PKM} install -y python$PYTHON-flexmock +RUN ${PKM} install -y python$PYTHON-mock ARG OS ARG PYTHON diff --git a/tests/unit/exports/__init__.py b/tests/unit/exports/__init__.py index e69de29..2b05622 100644 --- a/tests/unit/exports/__init__.py +++ b/tests/unit/exports/__init__.py @@ -0,0 +1 @@ +import tests.unit.globals diff --git a/tests/unit/exports/test_controller.py b/tests/unit/exports/test_controller.py index ce1def9..af2e017 100644 --- a/tests/unit/exports/test_controller.py +++ b/tests/unit/exports/test_controller.py @@ -1,5 +1,5 @@ import unittest2 -import flexmock +from unittest.mock import Mock from tuned.exports.controller import ExportsController import tuned.exports as exports @@ -60,12 +60,12 @@ class MockExporter(object): self.is_running = False def export(self,method,*args,**kwargs): - object_to_export = flexmock.flexmock(\ + object_to_export = Mock(\ method = method, args = args, kwargs = kwargs) self.exported_methods.append(object_to_export) def signal(self,method,*args,**kwargs): - object_to_export = flexmock.flexmock(\ + object_to_export = Mock(\ method = method, args = args, kwargs = kwargs) self.exported_signals.append(object_to_export) diff --git a/tests/unit/globals.py b/tests/unit/globals.py index 5491a54..aa8f803 100644 --- a/tests/unit/globals.py +++ b/tests/unit/globals.py @@ -1,4 +1,3 @@ -import flexmock import logging import tuned.logs @@ -6,4 +5,4 @@ logger = logging.getLogger() handler = logging.NullHandler() logger.addHandler(handler) -flexmock.flexmock(tuned.logs).should_receive("get").and_return(logger) +tuned.logs.get = lambda: logger diff --git a/tests/unit/hardware/__init__.py b/tests/unit/hardware/__init__.py index e69de29..2b05622 100644 --- a/tests/unit/hardware/__init__.py +++ b/tests/unit/hardware/__init__.py @@ -0,0 +1 @@ +import tests.unit.globals diff --git a/tests/unit/hardware/test_inventory.py b/tests/unit/hardware/test_inventory.py index 83a02dd..00f8def 100644 --- a/tests/unit/hardware/test_inventory.py +++ b/tests/unit/hardware/test_inventory.py @@ -1,5 +1,5 @@ import unittest2 -from flexmock import flexmock +from unittest.mock import Mock import pyudev from tuned.hardware.inventory import Inventory @@ -32,7 +32,7 @@ class InventoryTestCase(unittest2.TestCase): self._dummy.TestCallback) self._inventory.subscribe(self._dummier,subsystem_name, self._dummier.TestCallback) - device = flexmock(subsystem = subsystem_name) + device = Mock(subsystem = subsystem_name) self._inventory._handle_udev_event("test event", device) self.assertTrue(self._dummy.CallbackWasCalled) self.assertTrue(self._dummier.CallbackWasCalled) @@ -41,7 +41,7 @@ class InventoryTestCase(unittest2.TestCase): self._dummy.CallbackWasCalled = False self._dummier.CallbackWasCalled = False self._inventory.unsubscribe(self._dummy) - device = flexmock(subsystem = subsystem_name) + device = Mock(subsystem = subsystem_name) self._inventory._handle_udev_event("test event", device) self.assertFalse(self._dummy.CallbackWasCalled) self.assertTrue(self._dummier.CallbackWasCalled) diff --git a/tests/unit/monitors/__init__.py b/tests/unit/monitors/__init__.py index e69de29..2b05622 100644 --- a/tests/unit/monitors/__init__.py +++ b/tests/unit/monitors/__init__.py @@ -0,0 +1 @@ +import tests.unit.globals diff --git a/tests/unit/monitors/test_base.py b/tests/unit/monitors/test_base.py index 7ee9bcf..2d6e82c 100644 --- a/tests/unit/monitors/test_base.py +++ b/tests/unit/monitors/test_base.py @@ -1,5 +1,4 @@ import unittest2 -import tests.unit.globals import tuned.monitors.base class MockMonitor(tuned.monitors.base.Monitor): diff --git a/tests/unit/plugins/__init__.py b/tests/unit/plugins/__init__.py index e69de29..2b05622 100644 --- a/tests/unit/plugins/__init__.py +++ b/tests/unit/plugins/__init__.py @@ -0,0 +1 @@ +import tests.unit.globals diff --git a/tests/unit/plugins/test_base.py b/tests/unit/plugins/test_base.py index ba6a6e4..b49f98e 100644 --- a/tests/unit/plugins/test_base.py +++ b/tests/unit/plugins/test_base.py @@ -4,7 +4,6 @@ except ImportError: from collections import Mapping import tempfile import unittest2 -import flexmock from tuned.monitors.repository import Repository import tuned.plugins.decorators as decorators @@ -17,10 +16,6 @@ import tuned.consts as consts from tuned import storage import tuned.plugins.base -tuned.plugins.base.log = flexmock.flexmock(info = lambda *args: None,\ - error = lambda *args: None,debug = lambda *args: None,\ - warn = lambda *args: None) - temp_storage_file = tempfile.TemporaryFile(mode = 'r') consts.DEFAULT_STORAGE_FILE = temp_storage_file.name monitors_repository = monitors.Repository() diff --git a/tests/unit/profiles/__init__.py b/tests/unit/profiles/__init__.py index e69de29..2b05622 100644 --- a/tests/unit/profiles/__init__.py +++ b/tests/unit/profiles/__init__.py @@ -0,0 +1 @@ +import tests.unit.globals diff --git a/tests/unit/profiles/test_loader.py b/tests/unit/profiles/test_loader.py index c9c1810..1f4ed65 100644 --- a/tests/unit/profiles/test_loader.py +++ b/tests/unit/profiles/test_loader.py @@ -1,5 +1,4 @@ import unittest2 -import flexmock import tempfile import shutil import os @@ -10,9 +9,6 @@ from tuned.profiles.exceptions import InvalidProfileException class LoaderTestCase(unittest2.TestCase): @classmethod def setUpClass(cls): - profiles.loader.log = flexmock.flexmock(info = lambda *args: None,\ - error = lambda *args: None,debug = lambda *args: None,\ - warn = lambda *args: None) cls._test_dir = tempfile.mkdtemp() cls._profiles_dir = cls._test_dir + '/test_profiles' cls._dummy_profile_dir = cls._profiles_dir + '/dummy' diff --git a/tests/unit/storage/__init__.py b/tests/unit/storage/__init__.py index e69de29..2b05622 100644 --- a/tests/unit/storage/__init__.py +++ b/tests/unit/storage/__init__.py @@ -0,0 +1 @@ +import tests.unit.globals diff --git a/tests/unit/storage/test_factory.py b/tests/unit/storage/test_factory.py index 8bd253a..3c700ab 100644 --- a/tests/unit/storage/test_factory.py +++ b/tests/unit/storage/test_factory.py @@ -1,16 +1,16 @@ import unittest -from flexmock import flexmock +from unittest.mock import Mock import tuned.storage class StorageFactoryTestCase(unittest.TestCase): def test_create(self): - mock_provider = flexmock() + mock_provider = Mock() factory = tuned.storage.Factory(mock_provider) self.assertEqual(mock_provider, factory.provider) def test_create_storage(self): - mock_provider = flexmock() + mock_provider = Mock() factory = tuned.storage.Factory(mock_provider) storage_foo = factory.create("foo") diff --git a/tests/unit/storage/test_storage.py b/tests/unit/storage/test_storage.py index 17eac37..2d2709a 100644 --- a/tests/unit/storage/test_storage.py +++ b/tests/unit/storage/test_storage.py @@ -1,26 +1,31 @@ import unittest -from flexmock import flexmock +from unittest.mock import Mock +from unittest.mock import call import tuned.storage class StorageStorageTestCase(unittest.TestCase): def test_set(self): - mock_provider = flexmock() + mock_provider = Mock() factory = tuned.storage.Factory(mock_provider) storage = factory.create("foo") - mock_provider.should_receive("set").with_args("foo", "optname", "optval").once storage.set("optname", "optval") + mock_provider.set.assert_called_once_with("foo", "optname", "optval") def test_get(self): - mock_provider = flexmock() + mock_provider = Mock() + mock_provider.get.side_effect = [ None, "defval", "somevalue" ] factory = tuned.storage.Factory(mock_provider) storage = factory.create("foo") - mock_provider.should_receive("get").with_args("foo", "optname", None).and_return(None).once.ordered - mock_provider.should_receive("get").with_args("foo", "optname", "defval").and_return("defval").once.ordered - mock_provider.should_receive("get").with_args("foo", "existing", None).and_return("somevalue").once.ordered - - self.assertIsNone(storage.get("optname")) + self.assertEqual(None, storage.get("optname")) self.assertEqual("defval", storage.get("optname", "defval")) self.assertEqual("somevalue", storage.get("existing")) + calls = [ + call("foo", "optname", None), + call("foo", "optname", "defval"), + call("foo", "existing", None) + ] + + mock_provider.get.assert_has_calls(calls) diff --git a/tests/unit/utils/__init__.py b/tests/unit/utils/__init__.py index e69de29..2b05622 100644 --- a/tests/unit/utils/__init__.py +++ b/tests/unit/utils/__init__.py @@ -0,0 +1 @@ +import tests.unit.globals diff --git a/tests/unit/utils/test_commands.py b/tests/unit/utils/test_commands.py index fef2a27..0950203 100644 --- a/tests/unit/utils/test_commands.py +++ b/tests/unit/utils/test_commands.py @@ -1,6 +1,5 @@ import unittest2 import tempfile -import flexmock import shutil import re import os @@ -10,10 +9,6 @@ import tuned.consts as consts from tuned.exceptions import TunedException import tuned.utils.commands -tuned.utils.commands.log = flexmock.flexmock(info = lambda *args: None,\ - error = lambda *args: None,debug = lambda *args: None,\ - warn = lambda *args: None) - class CommandsTestCase(unittest2.TestCase): def setUp(self): self._commands = commands() diff --git a/tests/unit/utils/test_global_config.py b/tests/unit/utils/test_global_config.py index d6e9d5e..d2a9889 100644 --- a/tests/unit/utils/test_global_config.py +++ b/tests/unit/utils/test_global_config.py @@ -1,5 +1,4 @@ import unittest2 -import flexmock import tempfile import shutil import os @@ -10,10 +9,6 @@ import tuned.utils.global_config as global_config class GlobalConfigTestCase(unittest2.TestCase): @classmethod def setUpClass(cls): - global_config.log = flexmock.flexmock(info = lambda *args: None,\ - error = lambda *args: None,debug = lambda *args: None,\ - warn = lambda *args: None) - cls.test_dir = tempfile.mkdtemp() with open(cls.test_dir + '/test_config','w') as f: f.write('test_option = hello\ntest_bool = 1\ntest_size = 12MB\n'\ diff --git a/tuned.spec b/tuned.spec index 3264d44..eb64501 100644 --- a/tuned.spec +++ b/tuned.spec @@ -60,11 +60,7 @@ Requires(preun): systemd Requires(postun): systemd BuildRequires: %{_py}, %{_py}-devel # BuildRequires for 'make test' -BuildRequires: %{_py}-unittest2, %{_py}-configobj -# No flexmock on RHEL -%if ! 0%{?rhel} -BuildRequires: %{_py}-flexmock -%endif +BuildRequires: %{_py}-unittest2, %{_py}-configobj, %{_py}-mock BuildRequires: %{_py}-decorator, %{_py}-pyudev Requires: %{_py}-decorator, %{_py}-pyudev, %{_py}-configobj Requires: %{_py}-schedutils, %{_py}-linux-procfs, %{_py}-perf @@ -272,10 +268,7 @@ touch %{buildroot}%{_sysconfdir}/modprobe.d/kvm.rt.tuned.conf desktop-file-validate %{buildroot}%{_datadir}/applications/tuned-gui.desktop %check -# Unit tests not working on RHEL due to missing flexmock dependency -%if ! 0%{?rhel} make test -%endif %post %systemd_post tuned.service