diff options
author | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2012-08-21 17:06:35 -0400 |
---|---|---|
committer | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2012-08-21 17:06:35 -0400 |
commit | 556ac5660c22eb8316bff03cc3ccec5c82005b7c (patch) | |
tree | cd1829f50f535e4580b1a2ca6de89e45d5280526 /testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py | |
parent | 52b01a5a0a308f05affad9b72e2c1406cb807021 (diff) | |
download | bcfg2-556ac5660c22eb8316bff03cc3ccec5c82005b7c.tar.gz bcfg2-556ac5660c22eb8316bff03cc3ccec5c82005b7c.tar.bz2 bcfg2-556ac5660c22eb8316bff03cc3ccec5c82005b7c.zip |
removed context managers from unit tests
Diffstat (limited to 'testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py')
-rw-r--r-- | testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py | 122 |
1 files changed, 60 insertions, 62 deletions
diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py index 79879afed..34d47d8d3 100644 --- a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py +++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py @@ -1,7 +1,6 @@ import os import stat import copy -import unittest import lxml.etree from mock import Mock, MagicMock, patch from Bcfg2.Client.Tools.POSIX.Directory import * @@ -12,81 +11,80 @@ from .....common import * class TestPOSIXDirectory(TestPOSIXTool): test_obj = POSIXDirectory + @patch("os.listdir") @patch("Bcfg2.Client.Tools.POSIX.base.POSIXTool.verify") @patch("Bcfg2.Client.Tools.POSIX.Directory.%s._exists" % test_obj.__name__) - def test_verify(self, mock_exists, mock_verify): + def test_verify(self, mock_exists, mock_verify, mock_listdir): entry = lxml.etree.Element("Path", name="/test", type="directory", perms='0644', owner='root', group='root') - ptool = self.get_obj() mock_exists.return_value = False - self.assertFalse(ptool.verify(entry, [])) + self.assertFalse(self.ptool.verify(entry, [])) mock_exists.assert_called_with(entry) mock_exists.reset_mock() exists_rv = MagicMock() exists_rv.__getitem__.return_value = stat.S_IFREG | 420 # 0o644 mock_exists.return_value = exists_rv - self.assertFalse(ptool.verify(entry, [])) + self.assertFalse(self.ptool.verify(entry, [])) mock_exists.assert_called_with(entry) mock_exists.reset_mock() mock_verify.return_value = False exists_rv.__getitem__.return_value = stat.S_IFDIR | 420 # 0o644 - self.assertFalse(ptool.verify(entry, [])) + self.assertFalse(self.ptool.verify(entry, [])) mock_exists.assert_called_with(entry) - mock_verify.assert_called_with(ptool, entry, []) + mock_verify.assert_called_with(self.ptool, entry, []) mock_exists.reset_mock() mock_verify.reset_mock() mock_verify.return_value = True - self.assertTrue(ptool.verify(entry, [])) + self.assertTrue(self.ptool.verify(entry, [])) mock_exists.assert_called_with(entry) - mock_verify.assert_called_with(ptool, entry, []) + mock_verify.assert_called_with(self.ptool, entry, []) - with patch("os.listdir") as mock_listdir: - mock_exists.reset_mock() - mock_verify.reset_mock() - entry.set("prune", "true") - orig_entry = copy.deepcopy(entry) + mock_exists.reset_mock() + mock_verify.reset_mock() + entry.set("prune", "true") + orig_entry = copy.deepcopy(entry) - entries = ["foo", "bar", "bar/baz"] - mock_listdir.return_value = entries - modlist = [os.path.join(entry.get("name"), entries[0])] - self.assertFalse(ptool.verify(entry, modlist)) - mock_exists.assert_called_with(entry) - mock_verify.assert_called_with(ptool, entry, modlist) - mock_listdir.assert_called_with(entry.get("name")) - expected = [os.path.join(entry.get("name"), e) - for e in entries - if os.path.join(entry.get("name"), e) not in modlist] - actual = [e.get("path") for e in entry.findall("Prune")] - self.assertItemsEqual(expected, actual) - - mock_verify.reset_mock() - mock_exists.reset_mock() - mock_listdir.reset_mock() - entry = copy.deepcopy(orig_entry) - modlist = [os.path.join(entry.get("name"), e) - for e in entries] - self.assertTrue(ptool.verify(entry, modlist)) - mock_exists.assert_called_with(entry) - mock_verify.assert_called_with(ptool, entry, modlist) - mock_listdir.assert_called_with(entry.get("name")) - self.assertEqual(len(entry.findall("Prune")), 0) + entries = ["foo", "bar", "bar/baz"] + mock_listdir.return_value = entries + modlist = [os.path.join(entry.get("name"), entries[0])] + self.assertFalse(self.ptool.verify(entry, modlist)) + mock_exists.assert_called_with(entry) + mock_verify.assert_called_with(self.ptool, entry, modlist) + mock_listdir.assert_called_with(entry.get("name")) + expected = [os.path.join(entry.get("name"), e) + for e in entries + if os.path.join(entry.get("name"), e) not in modlist] + actual = [e.get("path") for e in entry.findall("Prune")] + self.assertItemsEqual(expected, actual) + + mock_verify.reset_mock() + mock_exists.reset_mock() + mock_listdir.reset_mock() + entry = copy.deepcopy(orig_entry) + modlist = [os.path.join(entry.get("name"), e) + for e in entries] + self.assertTrue(self.ptool.verify(entry, modlist)) + mock_exists.assert_called_with(entry) + mock_verify.assert_called_with(self.ptool, entry, modlist) + mock_listdir.assert_called_with(entry.get("name")) + self.assertEqual(len(entry.findall("Prune")), 0) @patch("os.unlink") + @patch("os.path.isdir") @patch("shutil.rmtree") @patch("Bcfg2.Client.Tools.POSIX.base.POSIXTool.install") @patch("Bcfg2.Client.Tools.POSIX.Directory.%s._exists" % test_obj.__name__) @patch("Bcfg2.Client.Tools.POSIX.Directory.%s._makedirs" % test_obj.__name__) def test_install(self, mock_makedirs, mock_exists, mock_install, - mock_rmtree, mock_unlink): + mock_rmtree, mock_isdir, mock_unlink): entry = lxml.etree.Element("Path", name="/test/foo/bar", type="directory", perms='0644', owner='root', group='root') - ptool = self.get_obj() def reset(): mock_exists.reset_mock() @@ -98,32 +96,32 @@ class TestPOSIXDirectory(TestPOSIXTool): mock_makedirs.return_value = True mock_exists.return_value = False mock_install.return_value = True - self.assertTrue(ptool.install(entry)) + self.assertTrue(self.ptool.install(entry)) mock_exists.assert_called_with(entry) - mock_install.assert_called_with(ptool, entry) + mock_install.assert_called_with(self.ptool, entry) mock_makedirs.assert_called_with(entry) reset() exists_rv = MagicMock() exists_rv.__getitem__.return_value = stat.S_IFREG | 420 # 0o644 mock_exists.return_value = exists_rv - self.assertTrue(ptool.install(entry)) + self.assertTrue(self.ptool.install(entry)) mock_unlink.assert_called_with(entry.get("name")) mock_exists.assert_called_with(entry) mock_makedirs.assert_called_with(entry) - mock_install.assert_called_with(ptool, entry) + mock_install.assert_called_with(self.ptool, entry) reset() exists_rv.__getitem__.return_value = stat.S_IFDIR | 420 # 0o644 mock_install.return_value = True - self.assertTrue(ptool.install(entry)) + self.assertTrue(self.ptool.install(entry)) mock_exists.assert_called_with(entry) - mock_install.assert_called_with(ptool, entry) + mock_install.assert_called_with(self.ptool, entry) reset() mock_install.return_value = False - self.assertFalse(ptool.install(entry)) - mock_install.assert_called_with(ptool, entry) + self.assertFalse(self.ptool.install(entry)) + mock_install.assert_called_with(self.ptool, entry) entry.set("prune", "true") prune = ["/test/foo/bar/prune1", "/test/foo/bar/prune2"] @@ -132,17 +130,17 @@ class TestPOSIXDirectory(TestPOSIXTool): reset() mock_install.return_value = True - with patch("os.path.isdir") as mock_isdir: - def isdir_rv(path): - if path.endswith("prune2"): - return True - else: - return False - mock_isdir.side_effect = isdir_rv - self.assertTrue(ptool.install(entry)) - mock_exists.assert_called_with(entry) - mock_install.assert_called_with(ptool, entry) - self.assertItemsEqual(mock_isdir.call_args_list, - [call(p) for p in prune]) - mock_unlink.assert_called_with("/test/foo/bar/prune1") - mock_rmtree.assert_called_with("/test/foo/bar/prune2") + + def isdir_rv(path): + if path.endswith("prune2"): + return True + else: + return False + mock_isdir.side_effect = isdir_rv + self.assertTrue(self.ptool.install(entry)) + mock_exists.assert_called_with(entry) + mock_install.assert_called_with(self.ptool, entry) + self.assertItemsEqual(mock_isdir.call_args_list, + [call(p) for p in prune]) + mock_unlink.assert_called_with("/test/foo/bar/prune1") + mock_rmtree.assert_called_with("/test/foo/bar/prune2") |