summaryrefslogtreecommitdiffstats
path: root/testsuite
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2013-08-08 14:02:12 -0400
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2013-08-09 08:54:01 -0400
commite29a400e609f3147dc3e0e3bab1054fe275e0bbd (patch)
treeb703ba0fa9c16953484e043001d0b1d4baa42669 /testsuite
parente254fa28ef0970a2ffe446f43ade10b4c5fccc29 (diff)
downloadbcfg2-e29a400e609f3147dc3e0e3bab1054fe275e0bbd.tar.gz
bcfg2-e29a400e609f3147dc3e0e3bab1054fe275e0bbd.tar.bz2
bcfg2-e29a400e609f3147dc3e0e3bab1054fe275e0bbd.zip
testsuite: fixed client tests
Diffstat (limited to 'testsuite')
-rw-r--r--testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py13
-rw-r--r--testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/Test__init.py32
-rw-r--r--testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py29
-rw-r--r--testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIXUsers.py11
-rw-r--r--testsuite/Testsrc/Testlib/TestClient/TestTools/Test_init.py27
-rw-r--r--testsuite/common.py6
6 files changed, 72 insertions, 46 deletions
diff --git a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py
index 8f933e08f..31e297888 100644
--- a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py
+++ b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py
@@ -55,8 +55,8 @@ class TestPOSIXFile(TestPOSIXTool):
def test_get_data(self):
orig_entry = lxml.etree.Element("Path", name="/test", type="file")
- ptool = self.get_obj(setup=dict(encoding="ascii", ppath='/',
- max_copies=5))
+ Bcfg2.Options.setup.encoding = "ascii"
+ ptool = self.get_obj()
entry = copy.deepcopy(orig_entry)
entry.text = b64encode("test")
@@ -91,8 +91,7 @@ class TestPOSIXFile(TestPOSIXTool):
@patch("Bcfg2.Client.Tools.POSIX.base.POSIXTool.verify")
def test_verify(self, mock_verify, mock_open):
entry = lxml.etree.Element("Path", name="/test", type="file")
- ptool = self.get_obj(setup=dict(interactive=False, ppath='/',
- max_copies=5))
+ ptool = self.get_obj()
ptool._exists = Mock()
ptool._get_data = Mock()
ptool._get_diffs = Mock()
@@ -223,8 +222,8 @@ class TestPOSIXFile(TestPOSIXTool):
group='root')
orig_entry.text = "test"
ondisk = "test2"
- ptool = self.get_obj(setup=dict(encoding="utf-8", ppath='/',
- max_copies=5))
+ Bcfg2.Options.setup.encoding = "utf-8"
+ ptool = self.get_obj()
ptool._get_data = Mock()
ptool._diff = Mock()
ptool._is_string = Mock()
@@ -312,7 +311,7 @@ class TestPOSIXFile(TestPOSIXTool):
# non-sensitive, interactive with unicode data
entry = reset()
entry.text = u("tëst")
- encoded = entry.text.encode(ptool.setup['encoding'])
+ encoded = entry.text.encode(Bcfg2.Options.setup.encoding)
ptool._diff.return_value = ["-test2", "+tëst"]
ptool._get_data.return_value = (encoded, False)
ptool._get_diffs(entry, interactive=True)
diff --git a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/Test__init.py b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/Test__init.py
index c2c6c5d4c..adc2032b7 100644
--- a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/Test__init.py
+++ b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/Test__init.py
@@ -119,15 +119,17 @@ class TestPOSIX(TestTool):
mock_verify.reset_mock()
mock_verify.return_value = False
- posix.setup.__getitem__.return_value = True
+ Bcfg2.Options.setup.interactive = True
self.assertFalse(posix.VerifyPath(entry, modlist))
self.assertIsNotNone(entry.get('qtext'))
@patch('os.remove')
def test_prune_old_backups(self, mock_remove):
entry = lxml.etree.Element("Path", name="/etc/foo", type="file")
- setup = dict(ppath='/', max_copies=5, paranoid=True)
- posix = self.get_obj(setup=setup)
+ Bcfg2.Options.setup.paranoid_path = '/'
+ Bcfg2.Options.setup.paranoid_copies = 5
+ Bcfg2.Options.setup.paranoid = True
+ posix = self.get_obj()
remove = ["_etc_foo_2012-07-20T04:13:22.364989",
"_etc_foo_2012-07-31T04:13:23.894958",
@@ -145,7 +147,7 @@ class TestPOSIX(TestTool):
mock_listdir.side_effect = OSError
posix._prune_old_backups(entry)
self.assertFalse(mock_remove.called)
- mock_listdir.assert_called_with(setup['ppath'])
+ mock_listdir.assert_called_with(Bcfg2.Options.setup.paranoid_path)
mock_listdir.reset_mock()
mock_remove.reset_mock()
@@ -153,9 +155,10 @@ class TestPOSIX(TestTool):
mock_listdir.return_value = keep + remove
posix._prune_old_backups(entry)
- mock_listdir.assert_called_with(setup['ppath'])
+ mock_listdir.assert_called_with(Bcfg2.Options.setup.paranoid_path)
self.assertItemsEqual(mock_remove.call_args_list,
- [call(os.path.join(setup['ppath'], p))
+ [call(os.path.join(Bcfg2.Options.setup.paranoid_path,
+ p))
for p in remove])
mock_listdir.reset_mock()
@@ -164,9 +167,10 @@ class TestPOSIX(TestTool):
# test to ensure that we call os.remove() for all files that
# need to be removed even if we get an error
posix._prune_old_backups(entry)
- mock_listdir.assert_called_with(setup['ppath'])
+ mock_listdir.assert_called_with(Bcfg2.Options.setup.paranoid_path)
self.assertItemsEqual(mock_remove.call_args_list,
- [call(os.path.join(setup['ppath'], p))
+ [call(os.path.join(Bcfg2.Options.setup.paranoid_path,
+ p))
for p in remove])
inner()
@@ -175,8 +179,10 @@ class TestPOSIX(TestTool):
@patch("os.path.isdir")
def test_paranoid_backup(self, mock_isdir, mock_copy):
entry = lxml.etree.Element("Path", name="/etc/foo", type="file")
- setup = dict(ppath='/', max_copies=5, paranoid=False)
- posix = self.get_obj(setup=setup)
+ Bcfg2.Options.setup.paranoid_path = '/'
+ Bcfg2.Options.setup.paranoid_copies = 5
+ Bcfg2.Options.setup.paranoid = False
+ posix = self.get_obj()
posix._prune_old_backups = Mock()
# paranoid false globally
@@ -185,9 +191,7 @@ class TestPOSIX(TestTool):
self.assertFalse(mock_copy.called)
# paranoid false on the entry
- setup['paranoid'] = True
- posix = self.get_obj(setup=setup)
- posix._prune_old_backups = Mock()
+ Bcfg2.Options.setup.paranoid = True
def reset():
mock_isdir.reset_mock()
@@ -227,6 +231,6 @@ class TestPOSIX(TestTool):
# just test it good enough
self.assertEqual(mock_copy.call_args[0][0],
entry.get("name"))
- bkupnam = os.path.join(setup['ppath'],
+ bkupnam = os.path.join(Bcfg2.Options.setup.paranoid_path,
entry.get('name').replace('/', '_')) + '_'
self.assertEqual(bkupnam, mock_copy.call_args[0][1][:len(bkupnam)])
diff --git a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py
index 8e7b58d30..bb7db5e14 100644
--- a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py
+++ b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py
@@ -197,7 +197,8 @@ class TestPOSIXTool(TestTool):
@patch("os.chown")
@patch("os.chmod")
@patch("os.utime")
- def test_set_perms(self, mock_utime, mock_chmod, mock_chown):
+ @patch("os.geteuid")
+ def test_set_perms(self, mock_geteuid, mock_utime, mock_chmod, mock_chown):
ptool = self.get_obj()
ptool._norm_entry_uid = Mock()
ptool._norm_entry_gid = Mock()
@@ -211,7 +212,12 @@ class TestPOSIXTool(TestTool):
mock_chmod.reset_mock()
mock_chown.reset_mock()
mock_utime.reset_mock()
+ mock_geteuid.reset_mock()
+ # pretend to run as root
+ mock_geteuid.return_value = 0
+
+ # test symlink -- no owner, group, permissions
entry = lxml.etree.Element("Path", name="/etc/foo", to="/etc/bar",
type="symlink")
ptool._set_acls.return_value = True
@@ -220,12 +226,12 @@ class TestPOSIXTool(TestTool):
ptool._set_secontext.assert_called_with(entry, path=entry.get("name"))
ptool._set_acls.assert_called_with(entry, path=entry.get("name"))
+ # test file with owner, group, permissions
+ reset()
entry = lxml.etree.Element("Path", name="/etc/foo", owner="owner",
group="group", mode="644", type="file")
ptool._norm_entry_uid.return_value = 10
ptool._norm_entry_gid.return_value = 100
-
- reset()
self.assertTrue(ptool._set_perms(entry))
ptool._norm_entry_uid.assert_called_with(entry)
ptool._norm_entry_gid.assert_called_with(entry)
@@ -236,6 +242,23 @@ class TestPOSIXTool(TestTool):
ptool._set_secontext.assert_called_with(entry, path=entry.get("name"))
ptool._set_acls.assert_called_with(entry, path=entry.get("name"))
+ # test file with owner, group, permissions, run as non-root
+ mock_geteuid.return_value = 1000
+ reset()
+ entry = lxml.etree.Element("Path", name="/etc/foo", owner="owner",
+ group="group", mode="644", type="file")
+ self.assertTrue(ptool._set_perms(entry))
+ self.assertFalse(ptool._norm_entry_uid.called)
+ self.assertFalse(ptool._norm_entry_gid.called)
+ self.assertFalse(mock_chown.called)
+ mock_chmod.assert_called_with(entry.get("name"),
+ int(entry.get("mode"), 8))
+ self.assertFalse(mock_utime.called)
+ ptool._set_secontext.assert_called_with(entry, path=entry.get("name"))
+ ptool._set_acls.assert_called_with(entry, path=entry.get("name"))
+ mock_geteuid.return_value = 0
+
+ # test with mtime
reset()
mtime = 1344459042
entry.set("mtime", str(mtime))
diff --git a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIXUsers.py b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIXUsers.py
index 57d8a6835..3d2e762ff 100644
--- a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIXUsers.py
+++ b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIXUsers.py
@@ -24,11 +24,12 @@ from TestTools.Test_init import TestTool
class TestPOSIXUsers(TestTool):
test_obj = POSIXUsers
- def get_obj(self, setup=None, config=None):
- if setup is None:
- setup = MagicMock()
- setup.__getitem__.return_value = []
- return TestTool.get_obj(self, setup, config)
+ def get_obj(self, config=None):
+ set_setup_default('uid_whitelist')
+ set_setup_default('uid_blacklist')
+ set_setup_default('gid_whitelist')
+ set_setup_default('gid_blacklist')
+ return TestTool.get_obj(self, config)
@patch("pwd.getpwall")
@patch("grp.getgrall")
diff --git a/testsuite/Testsrc/Testlib/TestClient/TestTools/Test_init.py b/testsuite/Testsrc/Testlib/TestClient/TestTools/Test_init.py
index df7b7c217..cbf8e4911 100644
--- a/testsuite/Testsrc/Testlib/TestClient/TestTools/Test_init.py
+++ b/testsuite/Testsrc/Testlib/TestClient/TestTools/Test_init.py
@@ -21,23 +21,15 @@ from common import *
class TestTool(Bcfg2TestCase):
test_obj = Tool
- def get_obj(self, setup=None, config=None):
+ def get_obj(self, config=None):
if config is None:
config = lxml.etree.Element("Configuration")
- if not setup:
- setup = MagicMock()
- if 'command_timeout' not in setup:
- setup['command_timeout'] = None
+ set_setup_default('command_timeout')
+ set_setup_default('interactive', False)
execs = self.test_obj.__execs__
self.test_obj.__execs__ = []
-
- @patch("Bcfg2.Options.get_option_parser")
- def inner(mock_option_parser):
- mock_option_parser.return_value = setup
- return self.test_obj(config)
-
- rv = inner()
+ rv = self.test_obj(config)
self.test_obj.__execs__ = execs
return rv
@@ -545,8 +537,9 @@ class TestSvcTool(TestTool):
@patch("Bcfg2.Client.prompt")
def test_BundleUpdated(self, mock_prompt):
- st = self.get_obj(setup=dict(interactive=False,
- servicemode='default'))
+ Bcfg2.Options.setup.service_mode = 'default'
+ Bcfg2.Options.setup.interactive = False
+ st = self.get_obj()
st.handlesEntry = Mock()
st.handlesEntry.side_effect = lambda e: e.tag == "Service"
st.stop_service = Mock()
@@ -598,7 +591,7 @@ class TestSvcTool(TestTool):
# test in interactive mode
reset()
mock_prompt.side_effect = lambda p: "interactive2" not in p
- st.setup['interactive'] = True
+ Bcfg2.Options.setup.interactive = True
states = st.BundleUpdated(bundle)
self.assertItemsEqual(st.handlesEntry.call_args_list,
[call(e) for e in entries])
@@ -611,8 +604,8 @@ class TestSvcTool(TestTool):
# test in build mode
reset()
- st.setup['interactive'] = False
- st.setup['servicemode'] = 'build'
+ Bcfg2.Options.setup.interactive = False
+ Bcfg2.Options.setup.service_mode = 'build'
states = st.BundleUpdated(bundle)
self.assertItemsEqual(st.handlesEntry.call_args_list,
[call(e) for e in entries])
diff --git a/testsuite/common.py b/testsuite/common.py
index 4bf5c85f3..baf119f5b 100644
--- a/testsuite/common.py
+++ b/testsuite/common.py
@@ -14,6 +14,7 @@ import sys
import codecs
import unittest
import lxml.etree
+import Bcfg2.Options
from mock import patch, MagicMock, _patch, DEFAULT
from Bcfg2.Compat import wraps
@@ -403,3 +404,8 @@ try:
except AttributeError:
re_type = type(re.compile(""))
+
+#: A function to set a default config option if it's not already set
+def set_setup_default(option, value=None):
+ if not hasattr(Bcfg2.Options.setup, option):
+ setattr(Bcfg2.Options.setup, option, value)