diff options
author | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2012-08-15 09:06:43 -0400 |
---|---|---|
committer | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2012-08-15 09:06:43 -0400 |
commit | b862090945322d5ba4b42e180bba92afb860df21 (patch) | |
tree | 1c482adfa9561bad14d82fc442f8f319b33b1d4f /testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py | |
parent | 7890fd0aa5331541c71b893c313553765ca1628e (diff) | |
download | bcfg2-b862090945322d5ba4b42e180bba92afb860df21.tar.gz bcfg2-b862090945322d5ba4b42e180bba92afb860df21.tar.bz2 bcfg2-b862090945322d5ba4b42e180bba92afb860df21.zip |
POSIX:
refactored POSIX tool into multiple files to make it more manageable
Added unit tests for POSIX tool and sub-tools
fixed ACL handling for filesystems mounted noacl
Diffstat (limited to 'testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py')
-rw-r--r-- | testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py new file mode 100644 index 000000000..021ed8113 --- /dev/null +++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py @@ -0,0 +1,154 @@ +import os +import stat +import copy +import unittest +import lxml.etree +from mock import Mock, MagicMock, patch +from Bcfg2.Client.Tools.POSIX.Directory import * +from Test__init import get_posix_object + +def call(*args, **kwargs): + """ the Mock call object is a fairly recent addition, but it's + very very useful, so we create our own function to create Mock + calls """ + return (args, kwargs) + +def get_directory_object(posix=None): + if posix is None: + posix = get_posix_object() + return POSIXDirectory(posix.logger, posix.setup, posix.config) + +class TestPOSIXDirectory(unittest.TestCase): + @patch("Bcfg2.Client.Tools.POSIX.base.POSIXTool.verify") + @patch("Bcfg2.Client.Tools.POSIX.Directory.POSIXDirectory._exists") + def test_verify(self, mock_exists, mock_verify): + entry = lxml.etree.Element("Path", name="/test", type="directory", + perms='0644', owner='root', group='root') + ptool = get_directory_object() + + mock_exists.return_value = False + self.assertFalse(ptool.verify(entry, [])) + mock_exists.assert_called_with(entry) + + mock_exists.reset_mock() + exists_rv = MagicMock() + exists_rv.__getitem__.return_value = stat.S_IFREG | 0644 + mock_exists.return_value = exists_rv + self.assertFalse(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 | 0644 + self.assertFalse(ptool.verify(entry, [])) + mock_exists.assert_called_with(entry) + mock_verify.assert_called_with(ptool, entry, []) + + mock_exists.reset_mock() + mock_verify.reset_mock() + mock_verify.return_value = True + self.assertTrue(ptool.verify(entry, [])) + mock_exists.assert_called_with(entry) + mock_verify.assert_called_with(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) + + 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) + + @patch("os.unlink") + @patch("shutil.rmtree") + @patch("Bcfg2.Client.Tools.POSIX.base.POSIXTool.install") + @patch("Bcfg2.Client.Tools.POSIX.Directory.POSIXDirectory._exists") + @patch("Bcfg2.Client.Tools.POSIX.Directory.POSIXDirectory._makedirs") + def test_install(self, mock_makedirs, mock_exists, mock_install, + mock_rmtree, mock_unlink): + entry = lxml.etree.Element("Path", name="/test/foo/bar", + type="directory", perms='0644', + owner='root', group='root') + ptool = get_directory_object() + + def reset(): + mock_exists.reset_mock() + mock_install.reset_mock() + mock_unlink.reset_mock() + mock_rmtree.reset_mock() + mock_rmtree.mock_makedirs() + + mock_makedirs.return_value = True + mock_exists.return_value = False + mock_install.return_value = True + self.assertTrue(ptool.install(entry)) + mock_exists.assert_called_with(entry) + mock_install.assert_called_with(ptool, entry) + mock_makedirs.assert_called_with(entry) + + reset() + exists_rv = MagicMock() + exists_rv.__getitem__.return_value = stat.S_IFREG | 0644 + mock_exists.return_value = exists_rv + self.assertTrue(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) + + reset() + exists_rv.__getitem__.return_value = stat.S_IFDIR | 0644 + mock_install.return_value = True + self.assertTrue(ptool.install(entry)) + mock_exists.assert_called_with(entry) + mock_install.assert_called_with(ptool, entry) + + reset() + mock_install.return_value = False + self.assertFalse(ptool.install(entry)) + mock_install.assert_called_with(ptool, entry) + + entry.set("prune", "true") + prune = ["/test/foo/bar/prune1", "/test/foo/bar/prune2"] + for path in prune: + lxml.etree.SubElement(entry, "Prune", path=path) + + 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") |