Skip to content

Commit c958fab

Browse files
Balazs Gibizerstephenfin
authored andcommitted
Avoid modifying the Mock class in test
The rdb unit test defines a shutdown field on the Mock class unintentionally. This causes that a later test in the same executor expecting that mock.Mock().shutdown is a newly auto generated mock.Mock() but it founds that is an already used (called) object. This causes that the later test fails when asserting the number of calls on that mock. The original intention of the rbd unit test was to catch the instantiation of the Rados object in test so it set Rados = mock.Mock() so when the code under test called Rados() it actually called Mock(). It worked but it has that huge side effect. Instead of this a proper mocking of the constructor can be done in two steps: rados_inst = mock.Mock() Rados = mock.Mock(return_value=rados_inst) This makes sure that every Rados() call will return rados_inst that is a mock without causing Mock class level side effect. Change-Id: If71620e808744736cb4fe3abda76d81a6335311b Closes-Bug: #1936849 (cherry picked from commit 930b7c9)
1 parent 088addf commit c958fab

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

nova/tests/unit/storage/test_rbd.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ def setUp(self):
119119
rados_patcher = mock.patch.object(rbd_utils, 'rados')
120120
self.mock_rados = rados_patcher.start()
121121
self.addCleanup(rados_patcher.stop)
122-
self.mock_rados.Rados = mock.Mock
123-
self.mock_rados.Rados.ioctx = mock.Mock()
124-
self.mock_rados.Rados.connect = mock.Mock()
125-
self.mock_rados.Rados.shutdown = mock.Mock()
126-
self.mock_rados.Rados.open_ioctx = mock.Mock()
127-
self.mock_rados.Rados.open_ioctx.return_value = \
128-
self.mock_rados.Rados.ioctx
122+
self.mock_rados.Rados = mock.Mock()
123+
self.rados_inst = mock.Mock()
124+
self.mock_rados.Rados.return_value = self.rados_inst
125+
self.rados_inst.ioctx = mock.Mock()
126+
self.rados_inst.connect = mock.Mock()
127+
self.rados_inst.shutdown = mock.Mock()
128+
self.rados_inst.open_ioctx = mock.Mock()
129+
self.rados_inst.open_ioctx.return_value = \
130+
self.rados_inst.ioctx
129131
self.mock_rados.Error = Exception
130132

131133
rbd_patcher = mock.patch.object(rbd_utils, 'rbd')
@@ -339,33 +341,31 @@ def test_rbd_volume_proxy_init(self, mock_connect_from_rados,
339341

340342
def test_connect_to_rados_default(self):
341343
ret = self.driver._connect_to_rados()
342-
self.mock_rados.Rados.connect.assert_called_once_with(
344+
self.rados_inst.connect.assert_called_once_with(
343345
timeout=self.rbd_connect_timeout)
344-
self.assertTrue(self.mock_rados.Rados.open_ioctx.called)
345-
self.assertIsInstance(ret[0], self.mock_rados.Rados)
346-
self.assertEqual(self.mock_rados.Rados.ioctx, ret[1])
347-
self.mock_rados.Rados.open_ioctx.assert_called_with(self.rbd_pool)
346+
self.assertTrue(self.rados_inst.open_ioctx.called)
347+
self.assertEqual(self.rados_inst.ioctx, ret[1])
348+
self.rados_inst.open_ioctx.assert_called_with(self.rbd_pool)
348349

349350
def test_connect_to_rados_different_pool(self):
350351
ret = self.driver._connect_to_rados('alt_pool')
351-
self.mock_rados.Rados.connect.assert_called_once_with(
352+
self.rados_inst.connect.assert_called_once_with(
352353
timeout=self.rbd_connect_timeout)
353-
self.assertTrue(self.mock_rados.Rados.open_ioctx.called)
354-
self.assertIsInstance(ret[0], self.mock_rados.Rados)
355-
self.assertEqual(self.mock_rados.Rados.ioctx, ret[1])
356-
self.mock_rados.Rados.open_ioctx.assert_called_with('alt_pool')
354+
self.assertTrue(self.rados_inst.open_ioctx.called)
355+
self.assertEqual(self.rados_inst.ioctx, ret[1])
356+
self.rados_inst.open_ioctx.assert_called_with('alt_pool')
357357

358358
def test_connect_to_rados_error(self):
359-
self.mock_rados.Rados.open_ioctx.side_effect = self.mock_rados.Error
359+
self.rados_inst.open_ioctx.side_effect = self.mock_rados.Error
360360
self.assertRaises(self.mock_rados.Error,
361361
self.driver._connect_to_rados)
362-
self.mock_rados.Rados.open_ioctx.assert_called_once_with(
362+
self.rados_inst.open_ioctx.assert_called_once_with(
363363
self.rbd_pool)
364-
self.mock_rados.Rados.shutdown.assert_called_once_with()
364+
self.rados_inst.shutdown.assert_called_once_with()
365365

366366
def test_connect_to_rados_unicode_arg(self):
367367
self.driver._connect_to_rados(u'unicode_pool')
368-
self.mock_rados.Rados.open_ioctx.assert_called_with(
368+
self.rados_inst.open_ioctx.assert_called_with(
369369
test.MatchType(str))
370370

371371
def test_ceph_args_none(self):

0 commit comments

Comments
 (0)