From 93d3a75039b767d6c02cf89ac8e8d82df4715278 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Thu, 25 Apr 2019 16:06:25 +0200 Subject: [PATCH] Tests: replace mocking loopingcall with zero interval We mock sleep(), but LoopingCall, depending on version either uses a private wrapper around eventlet.Event or one from oslo_utils. Since mocking them correctly requires mocking private methods, this change sets the loop interval to zero, bypassing waiting. This reduces the time required for running disk_partitioner tests from 20 seconds to less than 1 second. Change-Id: I49340bfedd3e63316a79f166ce0874d5020b862a --- ironic_lib/tests/test_disk_partitioner.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ironic_lib/tests/test_disk_partitioner.py b/ironic_lib/tests/test_disk_partitioner.py index 3cf296a2..059ec340 100644 --- a/ironic_lib/tests/test_disk_partitioner.py +++ b/ironic_lib/tests/test_disk_partitioner.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import eventlet import mock from testtools.matchers import HasLength @@ -23,6 +22,9 @@ from ironic_lib.tests import base from ironic_lib import utils +CONF = disk_partitioner.CONF + + class DiskPartitionerTestCase(base.IronicLibTestCase): def test_add_partition(self): @@ -99,12 +101,12 @@ class DiskPartitionerTestCase(base.IronicLibTestCase): 'fuser', '/dev/fake', run_as_root=True, check_exit_code=[0, 1]) - @mock.patch.object(eventlet.greenthread, 'sleep', lambda seconds: None) @mock.patch.object(disk_partitioner.DiskPartitioner, '_exec', autospec=True) @mock.patch.object(utils, 'execute', autospec=True) def test_commit_with_device_is_busy_once(self, mock_utils_exc, mock_disk_partitioner_exec): + CONF.set_override('check_device_interval', 0, group='disk_partitioner') dp = disk_partitioner.DiskPartitioner('/dev/fake') fake_parts = [(1, {'boot_flag': None, 'extra_flags': None, @@ -134,12 +136,12 @@ class DiskPartitionerTestCase(base.IronicLibTestCase): check_exit_code=[0, 1]) self.assertEqual(2, mock_utils_exc.call_count) - @mock.patch.object(eventlet.greenthread, 'sleep', lambda seconds: None) @mock.patch.object(disk_partitioner.DiskPartitioner, '_exec', autospec=True) @mock.patch.object(utils, 'execute', autospec=True) def test_commit_with_device_is_always_busy(self, mock_utils_exc, mock_disk_partitioner_exec): + CONF.set_override('check_device_interval', 0, group='disk_partitioner') dp = disk_partitioner.DiskPartitioner('/dev/fake') fake_parts = [(1, {'boot_flag': None, 'extra_flags': None, @@ -169,13 +171,12 @@ class DiskPartitionerTestCase(base.IronicLibTestCase): check_exit_code=[0, 1]) self.assertEqual(20, mock_utils_exc.call_count) - # Mock the eventlet.greenthread.sleep for the looping_call - @mock.patch.object(eventlet.greenthread, 'sleep', lambda seconds: None) @mock.patch.object(disk_partitioner.DiskPartitioner, '_exec', autospec=True) @mock.patch.object(utils, 'execute', autospec=True) def test_commit_with_device_disconnected(self, mock_utils_exc, mock_disk_partitioner_exec): + CONF.set_override('check_device_interval', 0, group='disk_partitioner') dp = disk_partitioner.DiskPartitioner('/dev/fake') fake_parts = [(1, {'boot_flag': None, 'extra_flags': None,