1
0
Fork 0

Replace python flexmock module with builtin mock

Also replace dependency in specfile and alter dockerfile

Signed-off-by: Tomas Korbar <tkorbar@redhat.com>
This commit is contained in:
Tomas Korbar 2019-11-25 13:29:52 +01:00
parent d704dadb0b
commit 140d982354
19 changed files with 33 additions and 49 deletions

View file

@ -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

View file

@ -0,0 +1 @@
import tests.unit.globals

View file

@ -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)

View file

@ -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

View file

@ -0,0 +1 @@
import tests.unit.globals

View file

@ -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)

View file

@ -0,0 +1 @@
import tests.unit.globals

View file

@ -1,5 +1,4 @@
import unittest2
import tests.unit.globals
import tuned.monitors.base
class MockMonitor(tuned.monitors.base.Monitor):

View file

@ -0,0 +1 @@
import tests.unit.globals

View file

@ -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()

View file

@ -0,0 +1 @@
import tests.unit.globals

View file

@ -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'

View file

@ -0,0 +1 @@
import tests.unit.globals

View file

@ -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")

View file

@ -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)

View file

@ -0,0 +1 @@
import tests.unit.globals

View file

@ -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()

View file

@ -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'\

View file

@ -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