summaryrefslogtreecommitdiffstats
path: root/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2012-08-21 17:06:35 -0400
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2012-08-21 17:06:35 -0400
commit556ac5660c22eb8316bff03cc3ccec5c82005b7c (patch)
treecd1829f50f535e4580b1a2ca6de89e45d5280526 /testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py
parent52b01a5a0a308f05affad9b72e2c1406cb807021 (diff)
downloadbcfg2-556ac5660c22eb8316bff03cc3ccec5c82005b7c.tar.gz
bcfg2-556ac5660c22eb8316bff03cc3ccec5c82005b7c.tar.bz2
bcfg2-556ac5660c22eb8316bff03cc3ccec5c82005b7c.zip
removed context managers from unit tests
Diffstat (limited to 'testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py')
-rw-r--r--testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py494
1 files changed, 246 insertions, 248 deletions
diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py
index e8e5dbe23..e7772d508 100644
--- a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py
+++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py
@@ -1,7 +1,6 @@
import os
import copy
import stat
-import unittest
import lxml.etree
from mock import Mock, MagicMock, patch
import Bcfg2.Client.Tools
@@ -29,137 +28,140 @@ class TestPOSIXTool(Bcfg2TestCase):
posix = get_posix_object()
return self.test_obj(posix.logger, posix.setup, posix.config)
+ def setUp(self):
+ self.ptool = self.get_obj()
+
+ def tearDown(self):
+ # just to guarantee that we start fresh each time
+ self.ptool = None
+
def test_fully_specified(self):
# fully_specified should do no checking on the abstract
# POSIXTool object
- ptool = self.get_obj()
- self.assertTrue(ptool.fully_specified(Mock()))
+ self.assertTrue(self.ptool.fully_specified(Mock()))
+ @patch('os.stat')
+ @patch('os.walk')
@patch("Bcfg2.Client.Tools.POSIX.base.%s._verify_metadata" %
test_obj.__name__)
- def test_verify(self, mock_verify):
+ def test_verify(self, mock_verify, mock_walk, mock_stat):
entry = lxml.etree.Element("Path", name="/test", type="file")
- ptool = self.get_obj()
- with patch('os.stat') as mock_stat, patch('os.walk') as mock_walk:
- mock_stat.return_value = MagicMock()
-
- mock_verify.return_value = False
- self.assertFalse(ptool.verify(entry, []))
- mock_verify.assert_called_with(entry)
-
- mock_verify.reset_mock()
- mock_verify.return_value = True
- self.assertTrue(ptool.verify(entry, []))
- mock_verify.assert_called_with(entry)
-
- mock_verify.reset_mock()
- entry.set("recursive", "true")
- walk_rv = [("/", ["dir1", "dir2"], ["file1", "file2"]),
- ("/dir1", ["dir3"], []),
- ("/dir2", [], ["file3", "file4"])]
- mock_walk.return_value = walk_rv
- self.assertTrue(ptool.verify(entry, []))
- mock_walk.assert_called_with(entry.get("name"))
- all_verifies = [call(entry)]
- for root, dirs, files in walk_rv:
- all_verifies.extend([call(entry, path=os.path.join(root, p))
- for p in dirs + files])
- self.assertItemsEqual(mock_verify.call_args_list, all_verifies)
-
+
+ mock_stat.return_value = MagicMock()
+ mock_verify.return_value = False
+ self.assertFalse(self.ptool.verify(entry, []))
+ mock_verify.assert_called_with(entry)
+
+ mock_verify.reset_mock()
+ mock_verify.return_value = True
+ self.assertTrue(self.ptool.verify(entry, []))
+ mock_verify.assert_called_with(entry)
+
+ mock_verify.reset_mock()
+ entry.set("recursive", "true")
+ walk_rv = [("/", ["dir1", "dir2"], ["file1", "file2"]),
+ ("/dir1", ["dir3"], []),
+ ("/dir2", [], ["file3", "file4"])]
+ mock_walk.return_value = walk_rv
+ self.assertTrue(self.ptool.verify(entry, []))
+ mock_walk.assert_called_with(entry.get("name"))
+ all_verifies = [call(entry)]
+ for root, dirs, files in walk_rv:
+ all_verifies.extend([call(entry, path=os.path.join(root, p))
+ for p in dirs + files])
+ self.assertItemsEqual(mock_verify.call_args_list, all_verifies)
+
+ @patch('os.walk')
@patch("Bcfg2.Client.Tools.POSIX.base.%s._set_perms" % test_obj.__name__)
- def test_install(self, mock_set_perms):
+ def test_install(self, mock_set_perms, mock_walk):
entry = lxml.etree.Element("Path", name="/test", type="file")
- ptool = self.get_obj()
mock_set_perms.return_value = True
- self.assertTrue(ptool.install(entry))
+ self.assertTrue(self.ptool.install(entry))
mock_set_perms.assert_called_with(entry)
mock_set_perms.reset_mock()
entry.set("recursive", "true")
- with patch('os.walk') as mock_walk:
- walk_rv = [("/", ["dir1", "dir2"], ["file1", "file2"]),
- ("/dir1", ["dir3"], []),
- ("/dir2", [], ["file3", "file4"])]
- mock_walk.return_value = walk_rv
-
- mock_set_perms.return_value = True
- self.assertTrue(ptool.install(entry))
- mock_walk.assert_called_with(entry.get("name"))
- all_set_perms = [call(entry)]
- for root, dirs, files in walk_rv:
- all_set_perms.extend([call(entry,
- path=os.path.join(root, p))
- for p in dirs + files])
- self.assertItemsEqual(mock_set_perms.call_args_list,
- all_set_perms)
-
- mock_walk.reset_mock()
- mock_set_perms.reset_mock()
+ walk_rv = [("/", ["dir1", "dir2"], ["file1", "file2"]),
+ ("/dir1", ["dir3"], []),
+ ("/dir2", [], ["file3", "file4"])]
+ mock_walk.return_value = walk_rv
- def set_perms_rv(entry, path=None):
- if path == '/dir2/file3':
- return False
- else:
- return True
- mock_set_perms.side_effect = set_perms_rv
-
- self.assertFalse(ptool.install(entry))
- mock_walk.assert_called_with(entry.get("name"))
- self.assertItemsEqual(mock_set_perms.call_args_list,
- all_set_perms)
+ mock_set_perms.return_value = True
+ self.assertTrue(self.ptool.install(entry))
+ mock_walk.assert_called_with(entry.get("name"))
+ all_set_perms = [call(entry)]
+ for root, dirs, files in walk_rv:
+ all_set_perms.extend([call(entry, path=os.path.join(root, p))
+ for p in dirs + files])
+ self.assertItemsEqual(mock_set_perms.call_args_list,
+ all_set_perms)
+
+ mock_walk.reset_mock()
+ mock_set_perms.reset_mock()
+
+ def set_perms_rv(entry, path=None):
+ if path == '/dir2/file3':
+ return False
+ else:
+ return True
+ mock_set_perms.side_effect = set_perms_rv
+
+ self.assertFalse(self.ptool.install(entry))
+ mock_walk.assert_called_with(entry.get("name"))
+ self.assertItemsEqual(mock_set_perms.call_args_list, all_set_perms)
+ @patch('os.lstat')
@patch("os.unlink")
+ @patch("os.path.isdir")
@patch("shutil.rmtree")
- def test_exists(self, mock_rmtree, mock_unlink):
+ def test_exists(self, mock_rmtree, mock_isdir, mock_unlink, mock_lstat):
entry = lxml.etree.Element("Path", name="/etc/foo", type="file")
- ptool = self.get_obj()
- with patch('os.lstat') as mock_lstat, \
- patch("os.path.isdir") as mock_isdir:
- mock_lstat.side_effect = OSError
- self.assertFalse(ptool._exists(entry))
- mock_lstat.assert_called_with(entry.get('name'))
- self.assertFalse(mock_unlink.called)
-
- mock_lstat.reset_mock()
- mock_unlink.reset_mock()
- rv = MagicMock()
- mock_lstat.return_value = rv
- mock_lstat.side_effect = None
- self.assertEqual(ptool._exists(entry), rv)
- mock_lstat.assert_called_with(entry.get('name'))
- self.assertFalse(mock_unlink.called)
-
- mock_lstat.reset_mock()
- mock_unlink.reset_mock()
- mock_isdir.return_value = False
- self.assertFalse(ptool._exists(entry, remove=True))
- mock_isdir.assert_called_with(entry.get('name'))
- mock_lstat.assert_called_with(entry.get('name'))
- mock_unlink.assert_called_with(entry.get('name'))
- self.assertFalse(mock_rmtree.called)
-
- mock_lstat.reset_mock()
- mock_isdir.reset_mock()
- mock_unlink.reset_mock()
- mock_rmtree.reset_mock()
- mock_isdir.return_value = True
- self.assertFalse(ptool._exists(entry, remove=True))
- mock_isdir.assert_called_with(entry.get('name'))
- mock_lstat.assert_called_with(entry.get('name'))
- mock_rmtree.assert_called_with(entry.get('name'))
- self.assertFalse(mock_unlink.called)
-
- mock_isdir.reset_mock()
- mock_lstat.reset_mock()
- mock_unlink.reset_mock()
- mock_rmtree.reset_mock()
- mock_rmtree.side_effect = OSError
- self.assertEqual(ptool._exists(entry, remove=True), rv)
- mock_isdir.assert_called_with(entry.get('name'))
- mock_lstat.assert_called_with(entry.get('name'))
- mock_rmtree.assert_called_with(entry.get('name'))
- self.assertFalse(mock_unlink.called)
+
+ mock_lstat.side_effect = OSError
+ self.assertFalse(self.ptool._exists(entry))
+ mock_lstat.assert_called_with(entry.get('name'))
+ self.assertFalse(mock_unlink.called)
+
+ mock_lstat.reset_mock()
+ mock_unlink.reset_mock()
+ rv = MagicMock()
+ mock_lstat.return_value = rv
+ mock_lstat.side_effect = None
+ self.assertEqual(self.ptool._exists(entry), rv)
+ mock_lstat.assert_called_with(entry.get('name'))
+ self.assertFalse(mock_unlink.called)
+
+ mock_lstat.reset_mock()
+ mock_unlink.reset_mock()
+ mock_isdir.return_value = False
+ self.assertFalse(self.ptool._exists(entry, remove=True))
+ mock_isdir.assert_called_with(entry.get('name'))
+ mock_lstat.assert_called_with(entry.get('name'))
+ mock_unlink.assert_called_with(entry.get('name'))
+ self.assertFalse(mock_rmtree.called)
+
+ mock_lstat.reset_mock()
+ mock_isdir.reset_mock()
+ mock_unlink.reset_mock()
+ mock_rmtree.reset_mock()
+ mock_isdir.return_value = True
+ self.assertFalse(self.ptool._exists(entry, remove=True))
+ mock_isdir.assert_called_with(entry.get('name'))
+ mock_lstat.assert_called_with(entry.get('name'))
+ mock_rmtree.assert_called_with(entry.get('name'))
+ self.assertFalse(mock_unlink.called)
+
+ mock_isdir.reset_mock()
+ mock_lstat.reset_mock()
+ mock_unlink.reset_mock()
+ mock_rmtree.reset_mock()
+ mock_rmtree.side_effect = OSError
+ self.assertEqual(self.ptool._exists(entry, remove=True), rv)
+ mock_isdir.assert_called_with(entry.get('name'))
+ mock_lstat.assert_called_with(entry.get('name'))
+ mock_rmtree.assert_called_with(entry.get('name'))
+ self.assertFalse(mock_unlink.called)
@patch("os.chown")
@patch("os.chmod")
@@ -173,8 +175,6 @@ class TestPOSIXTool(Bcfg2TestCase):
test_obj.__name__)
def test_set_perms(self, mock_set_secontext, mock_set_acls, mock_norm_gid,
mock_norm_uid, mock_utime, mock_chmod, mock_chown):
- ptool = self.get_obj()
-
def reset():
mock_set_secontext.reset_mock()
mock_set_acls.reset_mock()
@@ -188,7 +188,7 @@ class TestPOSIXTool(Bcfg2TestCase):
type="symlink")
mock_set_acls.return_value = True
mock_set_secontext.return_value = True
- self.assertTrue(ptool._set_perms(entry))
+ self.assertTrue(self.ptool._set_perms(entry))
mock_set_secontext.assert_called_with(entry, path=entry.get("name"))
mock_set_acls.assert_called_with(entry, path=entry.get("name"))
@@ -198,7 +198,7 @@ class TestPOSIXTool(Bcfg2TestCase):
mock_norm_gid.return_value = 100
reset()
- self.assertTrue(ptool._set_perms(entry))
+ self.assertTrue(self.ptool._set_perms(entry))
mock_norm_uid.assert_called_with(entry)
mock_norm_gid.assert_called_with(entry)
mock_chown.assert_called_with(entry.get("name"), 10, 100)
@@ -211,7 +211,7 @@ class TestPOSIXTool(Bcfg2TestCase):
reset()
mtime = 1344459042
entry.set("mtime", str(mtime))
- self.assertTrue(ptool._set_perms(entry))
+ self.assertTrue(self.ptool._set_perms(entry))
mock_norm_uid.assert_called_with(entry)
mock_norm_gid.assert_called_with(entry)
mock_chown.assert_called_with(entry.get("name"), 10, 100)
@@ -222,7 +222,7 @@ class TestPOSIXTool(Bcfg2TestCase):
mock_set_acls.assert_called_with(entry, path=entry.get("name"))
reset()
- self.assertTrue(ptool._set_perms(entry, path='/etc/bar'))
+ self.assertTrue(self.ptool._set_perms(entry, path='/etc/bar'))
mock_norm_uid.assert_called_with(entry)
mock_norm_gid.assert_called_with(entry)
mock_chown.assert_called_with('/etc/bar', 10, 100)
@@ -241,7 +241,7 @@ class TestPOSIXTool(Bcfg2TestCase):
os.chown.side_effect = chown_rv
entry.set("type", "device")
entry.set("dev_type", list(device_map.keys())[0])
- self.assertFalse(ptool._set_perms(entry))
+ self.assertFalse(self.ptool._set_perms(entry))
mock_norm_uid.assert_called_with(entry)
mock_norm_gid.assert_called_with(entry)
mock_chown.assert_called_with(entry.get("name"), 0, 0)
@@ -257,7 +257,7 @@ class TestPOSIXTool(Bcfg2TestCase):
os.chmod.side_effect = OSError
entry.set("type", "file")
del entry.attrib["dev_type"]
- self.assertFalse(ptool._set_perms(entry))
+ self.assertFalse(self.ptool._set_perms(entry))
mock_norm_uid.assert_called_with(entry)
mock_norm_gid.assert_called_with(entry)
mock_chown.assert_called_with(entry.get("name"), 10, 100)
@@ -275,7 +275,7 @@ class TestPOSIXTool(Bcfg2TestCase):
os.utime.side_effect = OSError
mock_set_acls.return_value = False
mock_set_secontext.return_value = False
- self.assertFalse(ptool._set_perms(entry))
+ self.assertFalse(self.ptool._set_perms(entry))
mock_norm_uid.assert_called_with(entry)
mock_norm_gid.assert_called_with(entry)
mock_chown.assert_called_with(entry.get("name"), 10, 100)
@@ -286,17 +286,18 @@ class TestPOSIXTool(Bcfg2TestCase):
mock_set_acls.assert_called_with(entry, path=entry.get("name"))
@skipUnless(has_acls, "ACLS not found, skipping")
+ @patch("os.path.isdir")
@patch("Bcfg2.Client.Tools.POSIX.base.%s._norm_uid" % test_obj.__name__)
@patch("Bcfg2.Client.Tools.POSIX.base.%s._norm_gid" % test_obj.__name__)
@patch("Bcfg2.Client.Tools.POSIX.base.%s._list_entry_acls" %
test_obj.__name__)
- def test_set_acls(self, mock_list_entry_acls, mock_norm_gid, mock_norm_uid):
+ def test_set_acls(self, mock_list_entry_acls, mock_norm_gid, mock_norm_uid,
+ mock_isdir):
entry = lxml.etree.Element("Path", name="/etc/foo", type="file")
- ptool = self.get_obj()
# disable acls for the initial test
Bcfg2.Client.Tools.POSIX.base.has_acls = False
- self.assertTrue(ptool._set_acls(entry))
+ self.assertTrue(self.ptool._set_acls(entry))
Bcfg2.Client.Tools.POSIX.base.has_acls = True
# build a set of file ACLs to return from posix1e.ACL(file=...)
@@ -323,9 +324,9 @@ class TestPOSIXTool(Bcfg2TestCase):
mock_norm_uid.return_value = 10
mock_norm_gid.return_value = 100
- with patch("posix1e.ACL") as mock_ACL, \
- patch("posix1e.Entry") as mock_Entry, \
- patch("os.path.isdir") as mock_isdir:
+ @patch("posix1e.ACL")
+ @patch("posix1e.Entry")
+ def inner(mock_Entry, mock_ACL):
# set up the unreasonably complex return value for
# posix1e.ACL(), which has three separate uses
fileacl_rv = MagicMock()
@@ -364,17 +365,17 @@ class TestPOSIXTool(Bcfg2TestCase):
# test fs mounted noacl
mock_ACL.side_effect = IOError(95, "Operation not permitted")
- self.assertFalse(ptool._set_acls(entry))
+ self.assertFalse(self.ptool._set_acls(entry))
# test other error
reset()
mock_ACL.side_effect = IOError
- self.assertFalse(ptool._set_acls(entry))
+ self.assertFalse(self.ptool._set_acls(entry))
reset()
mock_ACL.side_effect = mock_acl_rv
mock_isdir.return_value = True
- self.assertTrue(ptool._set_acls(entry))
+ self.assertTrue(self.ptool._set_acls(entry))
self.assertItemsEqual(mock_ACL.call_args_list,
[call(file=entry.get("name")),
call(filedef=entry.get("name"))])
@@ -412,7 +413,7 @@ class TestPOSIXTool(Bcfg2TestCase):
mock_norm_gid.reset_mock()
mock_isdir.return_value = False
acl_entries = []
- self.assertTrue(ptool._set_acls(entry, path="/bin/bar"))
+ self.assertTrue(self.ptool._set_acls(entry, path="/bin/bar"))
mock_ACL.assert_called_with(file="/bin/bar")
self.assertItemsEqual(fileacl_rv.delete_entry.call_args_list,
[call(a) for a in remove_acls])
@@ -429,27 +430,29 @@ class TestPOSIXTool(Bcfg2TestCase):
self.assertItemsEqual(added_acls,
[(fileacl_rv, posix1e.ACL_GROUP, 100, 5)])
+ inner()
+
@skipUnless(has_selinux, "SELinux not found, skipping")
def test_set_secontext(self):
entry = lxml.etree.Element("Path", name="/etc/foo", type="file")
- ptool = self.get_obj()
# disable selinux for the initial test
Bcfg2.Client.Tools.POSIX.base.has_selinux = False
- self.assertTrue(ptool._set_secontext(entry))
+ self.assertTrue(self.ptool._set_secontext(entry))
Bcfg2.Client.Tools.POSIX.base.has_selinux = True
- with patch("selinux.restorecon") as mock_restorecon, \
- patch("selinux.lsetfilecon") as mock_lsetfilecon:
+ @patch("selinux.restorecon")
+ @patch("selinux.lsetfilecon")
+ def inner(mock_lsetfilecon, mock_restorecon):
# no context given
- self.assertTrue(ptool._set_secontext(entry))
+ self.assertTrue(self.ptool._set_secontext(entry))
self.assertFalse(mock_restorecon.called)
self.assertFalse(mock_lsetfilecon.called)
mock_restorecon.reset_mock()
mock_lsetfilecon.reset_mock()
entry.set("secontext", "__default__")
- self.assertTrue(ptool._set_secontext(entry))
+ self.assertTrue(self.ptool._set_secontext(entry))
mock_restorecon.assert_called_with(entry.get("name"))
self.assertFalse(mock_lsetfilecon.called)
@@ -457,159 +460,152 @@ class TestPOSIXTool(Bcfg2TestCase):
mock_lsetfilecon.reset_mock()
mock_lsetfilecon.return_value = 0
entry.set("secontext", "foo_t")
- self.assertTrue(ptool._set_secontext(entry))
+ self.assertTrue(self.ptool._set_secontext(entry))
self.assertFalse(mock_restorecon.called)
mock_lsetfilecon.assert_called_with(entry.get("name"), "foo_t")
mock_restorecon.reset_mock()
mock_lsetfilecon.reset_mock()
mock_lsetfilecon.return_value = 1
- self.assertFalse(ptool._set_secontext(entry))
+ self.assertFalse(self.ptool._set_secontext(entry))
self.assertFalse(mock_restorecon.called)
mock_lsetfilecon.assert_called_with(entry.get("name"), "foo_t")
+ inner()
+
@patch("grp.getgrnam")
def test_norm_gid(self, mock_getgrnam):
- ptool = self.get_obj()
- self.assertEqual(5, ptool._norm_gid("5"))
+ self.assertEqual(5, self.ptool._norm_gid("5"))
self.assertFalse(mock_getgrnam.called)
mock_getgrnam.reset_mock()
mock_getgrnam.return_value = ("group", "x", 5, [])
- self.assertEqual(5, ptool._norm_gid("group"))
+ self.assertEqual(5, self.ptool._norm_gid("group"))
mock_getgrnam.assert_called_with("group")
@patch("Bcfg2.Client.Tools.POSIX.base.%s._norm_gid" % test_obj.__name__)
def test_norm_entry_gid(self, mock_norm_gid):
entry = lxml.etree.Element("Path", name="/test", type="file",
group="group", owner="user")
- ptool = self.get_obj()
mock_norm_gid.return_value = 10
- self.assertEqual(10, ptool._norm_entry_gid(entry))
+ self.assertEqual(10, self.ptool._norm_entry_gid(entry))
mock_norm_gid.assert_called_with(entry.get("group"))
mock_norm_gid.reset_mock()
mock_norm_gid.side_effect = KeyError
- self.assertEqual(0, ptool._norm_entry_gid(entry))
+ self.assertEqual(0, self.ptool._norm_entry_gid(entry))
mock_norm_gid.assert_called_with(entry.get("group"))
@patch("pwd.getpwnam")
def test_norm_uid(self, mock_getpwnam):
- ptool = self.get_obj()
- self.assertEqual(5, ptool._norm_uid("5"))
+ self.assertEqual(5, self.ptool._norm_uid("5"))
self.assertFalse(mock_getpwnam.called)
mock_getpwnam.reset_mock()
mock_getpwnam.return_value = ("user", "x", 5, 5, "User", "/home/user",
"/bin/zsh")
- self.assertEqual(5, ptool._norm_uid("user"))
+ self.assertEqual(5, self.ptool._norm_uid("user"))
mock_getpwnam.assert_called_with("user")
@patch("Bcfg2.Client.Tools.POSIX.base.%s._norm_uid" % test_obj.__name__)
def test_norm_entry_uid(self, mock_norm_uid):
entry = lxml.etree.Element("Path", name="/test", type="file",
group="group", owner="user")
- ptool = self.get_obj()
mock_norm_uid.return_value = 10
- self.assertEqual(10, ptool._norm_entry_uid(entry))
+ self.assertEqual(10, self.ptool._norm_entry_uid(entry))
mock_norm_uid.assert_called_with(entry.get("owner"))
mock_norm_uid.reset_mock()
mock_norm_uid.side_effect = KeyError
- self.assertEqual(0, ptool._norm_entry_uid(entry))
+ self.assertEqual(0, self.ptool._norm_entry_uid(entry))
mock_norm_uid.assert_called_with(entry.get("owner"))
def test_norm_acl_perms(self):
- ptool = self.get_obj()
# there's basically no reasonably way to test the Permset
# object parsing feature without writing our own Mock object
# that re-implements Permset.test(). silly pylibacl won't let
# us create standalone Entry or Permset objects.
- ptool = self.get_obj()
- self.assertEqual(5, ptool._norm_acl_perms("5"))
- self.assertEqual(0, ptool._norm_acl_perms("55"))
- self.assertEqual(5, ptool._norm_acl_perms("rx"))
- self.assertEqual(5, ptool._norm_acl_perms("r-x"))
- self.assertEqual(6, ptool._norm_acl_perms("wr-"))
- self.assertEqual(0, ptool._norm_acl_perms("rwrw"))
- self.assertEqual(0, ptool._norm_acl_perms("-"))
- self.assertEqual(0, ptool._norm_acl_perms("a"))
- self.assertEqual(6, ptool._norm_acl_perms("rwa"))
- self.assertEqual(4, ptool._norm_acl_perms("rr"))
-
- def test__gather_data(self):
+ self.assertEqual(5, self.ptool._norm_acl_perms("5"))
+ self.assertEqual(0, self.ptool._norm_acl_perms("55"))
+ self.assertEqual(5, self.ptool._norm_acl_perms("rx"))
+ self.assertEqual(5, self.ptool._norm_acl_perms("r-x"))
+ self.assertEqual(6, self.ptool._norm_acl_perms("wr-"))
+ self.assertEqual(0, self.ptool._norm_acl_perms("rwrw"))
+ self.assertEqual(0, self.ptool._norm_acl_perms("-"))
+ self.assertEqual(0, self.ptool._norm_acl_perms("a"))
+ self.assertEqual(6, self.ptool._norm_acl_perms("rwa"))
+ self.assertEqual(4, self.ptool._norm_acl_perms("rr"))
+
+ @patch('os.stat')
+ def test__gather_data(self, mock_stat):
path = '/test'
- ptool = self.get_obj()
-
- # have to use context manager version of patch here because
- # os.stat must be unpatched when we instantiate the object to
- # make pkgutil.walk_packages() work
- with patch('os.stat') as mock_stat:
- mock_stat.side_effect = OSError
- self.assertFalse(ptool._gather_data(path)[0])
- mock_stat.assert_called_with(path)
-
- mock_stat.reset_mock()
- mock_stat.side_effect = None
- # create a return value
- stat_rv = MagicMock()
- def stat_getitem(key):
- if int(key) == stat.ST_UID:
- return 0
- elif int(key) == stat.ST_GID:
- return 10
- elif int(key) == stat.ST_MODE:
- # return extra bits in the mode to emulate a device
- # and ensure that they're stripped
- return int('060660', 8)
- stat_rv.__getitem__ = Mock(side_effect=stat_getitem)
- mock_stat.return_value = stat_rv
-
- # disable selinux and acls for this call -- we test them
- # separately so that we can skip those tests as appropriate
- states = (Bcfg2.Client.Tools.POSIX.base.has_selinux,
- Bcfg2.Client.Tools.POSIX.base.has_acls)
- Bcfg2.Client.Tools.POSIX.base.has_selinux = False
- Bcfg2.Client.Tools.POSIX.base.has_acls = False
- self.assertEqual(ptool._gather_data(path),
- (stat_rv, '0', '10', '0660', None, None))
- Bcfg2.Client.Tools.POSIX.base.has_selinux, \
- Bcfg2.Client.Tools.POSIX.base.has_acls = states
- mock_stat.assert_called_with(path)
+ mock_stat.side_effect = OSError
+ self.assertFalse(self.ptool._gather_data(path)[0])
+ mock_stat.assert_called_with(path)
+
+ mock_stat.reset_mock()
+ mock_stat.side_effect = None
+ # create a return value
+ stat_rv = MagicMock()
+ def stat_getitem(key):
+ if int(key) == stat.ST_UID:
+ return 0
+ elif int(key) == stat.ST_GID:
+ return 10
+ elif int(key) == stat.ST_MODE:
+ # return extra bits in the mode to emulate a device
+ # and ensure that they're stripped
+ return int('060660', 8)
+ stat_rv.__getitem__ = Mock(side_effect=stat_getitem)
+ mock_stat.return_value = stat_rv
+
+ # disable selinux and acls for this call -- we test them
+ # separately so that we can skip those tests as appropriate
+ states = (Bcfg2.Client.Tools.POSIX.base.has_selinux,
+ Bcfg2.Client.Tools.POSIX.base.has_acls)
+ Bcfg2.Client.Tools.POSIX.base.has_selinux = False
+ Bcfg2.Client.Tools.POSIX.base.has_acls = False
+ self.assertEqual(self.ptool._gather_data(path),
+ (stat_rv, '0', '10', '0660', None, None))
+ Bcfg2.Client.Tools.POSIX.base.has_selinux, \
+ Bcfg2.Client.Tools.POSIX.base.has_acls = states
+ mock_stat.assert_called_with(path)
@skipUnless(has_selinux, "SELinux not found, skipping")
def test__gather_data_selinux(self):
context = 'system_u:object_r:root_t:s0'
path = '/test'
- ptool = self.get_obj()
- with patch("selinux.getfilecon") as mock_getfilecon, \
- patch('os.stat') as mock_stat:
+
+ @patch("selinux.getfilecon")
+ @patch('os.stat')
+ def inner(mock_stat, mock_getfilecon):
mock_getfilecon.return_value = [len(context) + 1, context]
mock_stat.return_value = MagicMock()
# disable acls for this call and test them separately
state = Bcfg2.Client.Tools.POSIX.base.has_acls
Bcfg2.Client.Tools.POSIX.base.has_acls = False
- self.assertEqual(ptool._gather_data(path)[4], 'root_t')
+ self.assertEqual(self.ptool._gather_data(path)[4], 'root_t')
Bcfg2.Client.Tools.POSIX.base.has_acls = state
mock_getfilecon.assert_called_with(path)
+
+ inner()
+ @skipUnless(has_acls, "ACLS not found, skipping")
+ @patch('os.stat')
@patch("Bcfg2.Client.Tools.POSIX.base.%s._list_file_acls" %
test_obj.__name__)
- @skipUnless(has_acls, "ACLS not found, skipping")
- def test__gather_data_acls(self, mock_list_file_acls):
+ def test__gather_data_acls(self, mock_list_file_acls, mock_stat):
acls = {("default", posix1e.ACL_USER, "testuser"): "rwx",
("access", posix1e.ACL_GROUP, "testgroup"): "rx"}
mock_list_file_acls.return_value = acls
path = '/test'
- ptool = self.get_obj()
- with patch('os.stat') as mock_stat:
- mock_stat.return_value = MagicMock()
- # disable selinux for this call and test it separately
- state = Bcfg2.Client.Tools.POSIX.base.has_selinux
- Bcfg2.Client.Tools.POSIX.base.has_selinux = False
- self.assertItemsEqual(ptool._gather_data(path)[5], acls)
- Bcfg2.Client.Tools.POSIX.base.has_selinux = state
- mock_list_file_acls.assert_called_with(path)
+ mock_stat.return_value = MagicMock()
+ # disable selinux for this call and test it separately
+ state = Bcfg2.Client.Tools.POSIX.base.has_selinux
+ Bcfg2.Client.Tools.POSIX.base.has_selinux = False
+ self.assertItemsEqual(self.ptool._gather_data(path)[5], acls)
+ Bcfg2.Client.Tools.POSIX.base.has_selinux = state
+ mock_list_file_acls.assert_called_with(path)
@patch("Bcfg2.Client.Tools.POSIX.base.%s._verify_acls" % test_obj.__name__)
@patch("Bcfg2.Client.Tools.POSIX.base.%s._gather_data" % test_obj.__name__)
@@ -626,18 +622,16 @@ class TestPOSIXTool(Bcfg2TestCase):
# can start fresh every time
orig_entry = copy.deepcopy(entry)
- ptool = self.get_obj()
-
def reset():
mock_gather_data.reset_mock()
+ mock_verify_acls.reset_mock()
mock_norm_uid.reset_mock()
mock_norm_gid.reset_mock()
return copy.deepcopy(orig_entry)
-
# test nonexistent file
mock_gather_data.return_value = (False, None, None, None, None, None)
- self.assertFalse(ptool._verify_metadata(entry))
+ self.assertFalse(self.ptool._verify_metadata(entry))
self.assertEqual(entry.get("current_exists", "").lower(), "false")
mock_gather_data.assert_called_with(entry.get("name"))
@@ -654,7 +648,7 @@ class TestPOSIXTool(Bcfg2TestCase):
entry = reset()
mock_gather_data.return_value = tuple(gather_data_rv)
- self.assertTrue(ptool._verify_metadata(entry))
+ self.assertTrue(self.ptool._verify_metadata(entry))
mock_gather_data.assert_called_with(entry.get("name"))
mock_verify_acls.assert_called_with(entry, path=entry.get("name"))
self.assertEqual(entry.get("current_exists", 'true'), 'true')
@@ -667,7 +661,7 @@ class TestPOSIXTool(Bcfg2TestCase):
sestate = Bcfg2.Client.Tools.POSIX.base.has_selinux
Bcfg2.Client.Tools.POSIX.base.has_selinux = False
mock_gather_data.return_value = tuple(gather_data_rv)
- self.assertTrue(ptool._verify_metadata(entry))
+ self.assertTrue(self.ptool._verify_metadata(entry))
mock_gather_data.assert_called_with(entry.get("name"))
mock_verify_acls.assert_called_with(entry, path=entry.get("name"))
self.assertEqual(entry.get("current_exists", 'true'), 'true')
@@ -688,7 +682,7 @@ class TestPOSIXTool(Bcfg2TestCase):
stat_rv.__getitem__.return_value = mtime
gather_data_rv[0] = stat_rv
mock_gather_data.return_value = tuple(gather_data_rv)
- self.assertTrue(ptool._verify_metadata(entry))
+ self.assertTrue(self.ptool._verify_metadata(entry))
mock_gather_data.assert_called_with(entry.get("name"))
mock_verify_acls.assert_called_with(entry, path=entry.get("name"))
self.assertEqual(entry.get("current_exists", 'true'), 'true')
@@ -712,7 +706,7 @@ class TestPOSIXTool(Bcfg2TestCase):
gather_data_rv[idx] = val
gather_data_rv[fail_idx] = fail_val
mock_gather_data.return_value = tuple(gather_data_rv)
- self.assertFalse(ptool._verify_metadata(entry))
+ self.assertFalse(self.ptool._verify_metadata(entry))
mock_gather_data.assert_called_with(entry.get("name"))
mock_verify_acls.assert_called_with(entry, path=entry.get("name"))
self.assertEqual(entry.get("current_exists", 'true'), 'true')
@@ -732,7 +726,7 @@ class TestPOSIXTool(Bcfg2TestCase):
for attr, idx, val in expected:
gather_data_rv[idx] = val
mock_gather_data.return_value = tuple(gather_data_rv)
- self.assertFalse(ptool._verify_metadata(entry))
+ self.assertFalse(self.ptool._verify_metadata(entry))
mock_gather_data.assert_called_with(entry.get("name"))
mock_verify_acls.assert_called_with(entry, path=entry.get("name"))
self.assertEqual(entry.get("current_exists", 'true'), 'true')
@@ -745,7 +739,9 @@ class TestPOSIXTool(Bcfg2TestCase):
entry = reset()
entry.set("mtime", str(mtime))
entry.set("secontext", "__default__")
- with patch("selinux.matchpathcon") as mock_matchpathcon:
+
+ @patch("selinux.matchpathcon")
+ def inner(entry, mock_matchpathcon):
context1 = "system_u:object_r:etc_t:s0"
context2 = "system_u:object_r:root_t:s0"
mock_matchpathcon.return_value = [1 + len(context1),
@@ -754,7 +750,7 @@ class TestPOSIXTool(Bcfg2TestCase):
for attr, idx, val in expected:
gather_data_rv[idx] = val
mock_gather_data.return_value = tuple(gather_data_rv)
- self.assertTrue(ptool._verify_metadata(entry))
+ self.assertTrue(self.ptool._verify_metadata(entry))
mock_gather_data.assert_called_with(entry.get("name"))
mock_verify_acls.assert_called_with(entry,
path=entry.get("name"))
@@ -769,7 +765,7 @@ class TestPOSIXTool(Bcfg2TestCase):
entry.set("secontext", "__default__")
mock_matchpathcon.return_value = [1 + len(context2),
context2]
- self.assertFalse(ptool._verify_metadata(entry))
+ self.assertFalse(self.ptool._verify_metadata(entry))
mock_gather_data.assert_called_with(entry.get("name"))
mock_verify_acls.assert_called_with(entry,
path=entry.get("name"))
@@ -779,6 +775,8 @@ class TestPOSIXTool(Bcfg2TestCase):
self.assertEqual(entry.get(attr), val)
self.assertEqual(entry.get("current_mtime"), str(mtime))
+ inner(entry)
+
@skipUnless(has_acls, "ACLS not found, skipping")
def test_list_entry_acls(self):
entry = lxml.etree.Element("Path", name="/test", type="file")
@@ -786,19 +784,19 @@ class TestPOSIXTool(Bcfg2TestCase):
user="user", perms="rwx")
lxml.etree.SubElement(entry, "ACL", scope="group", type="access",
group="group", perms="5")
- ptool = self.get_obj()
- self.assertItemsEqual(ptool._list_entry_acls(entry),
+ self.assertItemsEqual(self.ptool._list_entry_acls(entry),
{("default", posix1e.ACL_USER, "user"): 7,
("access", posix1e.ACL_GROUP, "group"): 5})
@skipUnless(has_acls, "ACLS not found, skipping")
@patch("pwd.getpwuid")
@patch("grp.getgrgid")
- def test_list_file_acls(self, mock_getgrgid, mock_getpwuid):
+ @patch("os.path.isdir")
+ def test_list_file_acls(self, mock_isdir, mock_getgrgid, mock_getpwuid):
path = '/test'
- ptool = self.get_obj()
- with patch("posix1e.ACL") as mock_ACL, \
- patch("os.path.isdir") as mock_isdir:
+
+ @patch("posix1e.ACL")
+ def inner(mock_ACL):
# build a set of file ACLs to return from posix1e.ACL(file=...)
file_acls = []
acl = Mock()
@@ -845,15 +843,15 @@ class TestPOSIXTool(Bcfg2TestCase):
mock_ACL.reset_mock()
mock_ACL.side_effect = IOError(95, "Operation not supported")
- self.assertItemsEqual(ptool._list_file_acls(path), dict())
+ self.assertItemsEqual(self.ptool._list_file_acls(path), dict())
reset()
mock_ACL.side_effect = IOError
- self.assertItemsEqual(ptool._list_file_acls(path), dict())
+ self.assertItemsEqual(self.ptool._list_file_acls(path), dict())
reset()
mock_ACL.side_effect = mock_acl_rv
- self.assertItemsEqual(ptool._list_file_acls(path), acls)
+ self.assertItemsEqual(self.ptool._list_file_acls(path), acls)
mock_isdir.assert_called_with(path)
mock_getgrgid.assert_called_with(100)
mock_getpwuid.assert_called_with(10)
@@ -867,7 +865,7 @@ class TestPOSIXTool(Bcfg2TestCase):
defacls = acls
for akey, perms in acls.items():
defacls[('default', akey[1], akey[2])] = perms
- self.assertItemsEqual(ptool._list_file_acls(path), defacls)
+ self.assertItemsEqual(self.ptool._list_file_acls(path), defacls)
mock_isdir.assert_called_with(path)
self.assertItemsEqual(mock_getgrgid.call_args_list,
[call(100), call(100)])
@@ -876,6 +874,8 @@ class TestPOSIXTool(Bcfg2TestCase):
self.assertItemsEqual(mock_ACL.call_args_list,
[call(file=path), call(filedef=path)])
+ inner()
+
@skipUnless(has_acls, "ACLS not found, skipping")
@patch("Bcfg2.Client.Tools.POSIX.base.%s._list_file_acls" %
test_obj.__name__)
@@ -883,7 +883,6 @@ class TestPOSIXTool(Bcfg2TestCase):
test_obj.__name__)
def test_verify_acls(self, mock_list_entry_acls, mock_list_file_acls):
entry = lxml.etree.Element("Path", name="/test", type="file")
- ptool = self.get_obj()
# we can't test to make sure that errors get properly sorted
# into (missing, extra, wrong) without refactoring the
# _verify_acls code, and I don't feel like doing that, so eff
@@ -897,7 +896,7 @@ class TestPOSIXTool(Bcfg2TestCase):
mock_list_entry_acls.return_value = acls
mock_list_file_acls.return_value = acls
- self.assertTrue(ptool._verify_acls(entry))
+ self.assertTrue(self.ptool._verify_acls(entry))
mock_list_entry_acls.assert_called_with(entry)
mock_list_file_acls.assert_called_with(entry.get("name"))
@@ -905,7 +904,7 @@ class TestPOSIXTool(Bcfg2TestCase):
mock_list_entry_acls.reset_mock()
mock_list_file_acls.reset_mock()
mock_list_file_acls.return_value = extra_acls
- self.assertFalse(ptool._verify_acls(entry))
+ self.assertFalse(self.ptool._verify_acls(entry))
mock_list_entry_acls.assert_called_with(entry)
mock_list_file_acls.assert_called_with(entry.get("name"))
@@ -914,7 +913,7 @@ class TestPOSIXTool(Bcfg2TestCase):
mock_list_file_acls.reset_mock()
mock_list_entry_acls.return_value = extra_acls
mock_list_file_acls.return_value = acls
- self.assertFalse(ptool._verify_acls(entry))
+ self.assertFalse(self.ptool._verify_acls(entry))
mock_list_entry_acls.assert_called_with(entry)
mock_list_file_acls.assert_called_with(entry.get("name"))
@@ -925,7 +924,7 @@ class TestPOSIXTool(Bcfg2TestCase):
mock_list_file_acls.reset_mock()
mock_list_entry_acls.return_value = extra_acls
mock_list_file_acls.return_value = wrong_acls
- self.assertFalse(ptool._verify_acls(entry))
+ self.assertFalse(self.ptool._verify_acls(entry))
mock_list_entry_acls.assert_called_with(entry)
mock_list_file_acls.assert_called_with(entry.get("name"))
@@ -941,7 +940,6 @@ class TestPOSIXTool(Bcfg2TestCase):
mock_set_perms.reset_mock()
mock_makedirs.reset_mock()
- ptool = self.get_obj()
mock_set_perms.return_value = True
def path_exists_rv(path):
if path == "/test":
@@ -949,7 +947,7 @@ class TestPOSIXTool(Bcfg2TestCase):
else:
return False
mock_exists.side_effect = path_exists_rv
- self.assertTrue(ptool._makedirs(entry))
+ self.assertTrue(self.ptool._makedirs(entry))
self.assertItemsEqual(mock_exists.call_args_list,
[call("/test"), call("/test/foo"),
call("/test/foo/bar")])
@@ -960,7 +958,7 @@ class TestPOSIXTool(Bcfg2TestCase):
reset()
mock_makedirs.side_effect = OSError
- self.assertFalse(ptool._makedirs(entry))
+ self.assertFalse(self.ptool._makedirs(entry))
self.assertItemsEqual(mock_set_perms.call_args_list,
[call(entry, path="/test/foo"),
call(entry, path="/test/foo/bar")])
@@ -973,7 +971,7 @@ class TestPOSIXTool(Bcfg2TestCase):
else:
return True
mock_set_perms.side_effect = set_perms_rv
- self.assertFalse(ptool._makedirs(entry))
+ self.assertFalse(self.ptool._makedirs(entry))
self.assertItemsEqual(mock_exists.call_args_list,
[call("/test"), call("/test/foo"),
call("/test/foo/bar")])