From ac65ec6a6eaf4a537e36ad1598e8ce5b818639b1 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Mon, 12 Mar 2018 12:32:38 +0100 Subject: [PATCH] Use more granular mocking in test_utils oslo.config will start using the same functions that we mock in its next release, breaking our assumptions on how many times they are called. Change-Id: Ief2c3d3089916e021cfef38f45f2cc2c78bc4f9b Closes-Bug: #1754026 --- ironic/tests/unit/common/test_utils.py | 28 +++++++++++++++----------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/ironic/tests/unit/common/test_utils.py b/ironic/tests/unit/common/test_utils.py index 30fd3d1ab8..6add5cd2cc 100644 --- a/ironic/tests/unit/common/test_utils.py +++ b/ironic/tests/unit/common/test_utils.py @@ -418,27 +418,31 @@ class TempFilesTestCase(base.TestCase): mock_dir_writable.assert_called_once_with('/fake/path') mock_free_space.assert_called_once_with('/fake/path', 5) - @mock.patch.object(os.path, 'exists', autospec=True) @mock.patch.object(utils, '_check_dir_writable', autospec=True) @mock.patch.object(utils, '_check_dir_free_space', autospec=True) - def test_check_dir_no_dir(self, mock_free_space, mock_dir_writable, - mock_exists): - mock_exists.return_value = False + def test_check_dir_no_dir(self, mock_free_space, mock_dir_writable): self.config(tempdir='/fake/path') - self.assertRaises(exception.PathNotFound, utils.check_dir) - mock_exists.assert_called_once_with(CONF.tempdir) + # NOTE(dtantsur): self.config uses os.path.exists, so we cannot mock + # on the method level. + with mock.patch.object(os.path, 'exists', + autospec=True) as mock_exists: + mock_exists.return_value = False + self.assertRaises(exception.PathNotFound, utils.check_dir) + mock_exists.assert_called_once_with(CONF.tempdir) self.assertFalse(mock_free_space.called) self.assertFalse(mock_dir_writable.called) - @mock.patch.object(os.path, 'exists', autospec=True) @mock.patch.object(utils, '_check_dir_writable', autospec=True) @mock.patch.object(utils, '_check_dir_free_space', autospec=True) - def test_check_dir_ok(self, mock_free_space, mock_dir_writable, - mock_exists): - mock_exists.return_value = True + def test_check_dir_ok(self, mock_free_space, mock_dir_writable): self.config(tempdir='/fake/path') - utils.check_dir() - mock_exists.assert_called_once_with(CONF.tempdir) + # NOTE(dtantsur): self.config uses os.path.exists, so we cannot mock + # on the method level. + with mock.patch.object(os.path, 'exists', + autospec=True) as mock_exists: + mock_exists.return_value = True + utils.check_dir() + mock_exists.assert_called_once_with(CONF.tempdir) mock_dir_writable.assert_called_once_with(CONF.tempdir) mock_free_space.assert_called_once_with(CONF.tempdir, 1)