From d0efac8fde1f2bfc77b45899073c225201c64a2c Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Mon, 20 Aug 2012 17:26:22 -0400 Subject: attempt to get tests working under python 2.6 --- .../TestClient/TestTools/TestPOSIX/Testbase.py | 14 ++-- testsuite/Testlib/TestServer/TestPlugin.py | 2 +- .../Testlib/TestServer/TestPlugins/TestMetadata.py | 20 ++++-- .../Testlib/TestServer/TestPlugins/TestProbes.py | 18 +++-- testsuite/common.py | 83 ++++++++++++++++++++-- 5 files changed, 109 insertions(+), 28 deletions(-) (limited to 'testsuite') diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py index 4fbc2485f..442641c0b 100644 --- a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py +++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py @@ -285,7 +285,7 @@ class TestPOSIXTool(Bcfg2TestCase): mock_set_secontext.assert_called_with(entry, path=entry.get("name")) mock_set_acls.assert_called_with(entry, path=entry.get("name")) - @unittest.skipUnless(has_acls, "ACLS not found, skipping") + @skipUnless(has_acls, "ACLS not found, skipping") @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" % @@ -429,7 +429,7 @@ class TestPOSIXTool(Bcfg2TestCase): self.assertItemsEqual(added_acls, [(fileacl_rv, posix1e.ACL_GROUP, 100, 5)]) - @unittest.skipUnless(has_selinux, "SELinux not found, skipping") + @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() @@ -577,7 +577,7 @@ class TestPOSIXTool(Bcfg2TestCase): Bcfg2.Client.Tools.POSIX.base.has_acls = states mock_stat.assert_called_with(path) - @unittest.skipUnless(has_selinux, "SELinux not found, skipping") + @skipUnless(has_selinux, "SELinux not found, skipping") def test__gather_data_selinux(self): context = 'system_u:object_r:root_t:s0' path = '/test' @@ -595,7 +595,7 @@ class TestPOSIXTool(Bcfg2TestCase): @patch("Bcfg2.Client.Tools.POSIX.base.%s._list_file_acls" % test_obj.__name__) - @unittest.skipUnless(has_acls, "ACLS not found, skipping") + @skipUnless(has_acls, "ACLS not found, skipping") def test__gather_data_acls(self, mock_list_file_acls): acls = {("default", posix1e.ACL_USER, "testuser"): "rwx", ("access", posix1e.ACL_GROUP, "testgroup"): "rx"} @@ -779,7 +779,7 @@ class TestPOSIXTool(Bcfg2TestCase): self.assertEqual(entry.get(attr), val) self.assertEqual(entry.get("current_mtime"), str(mtime)) - @unittest.skipUnless(has_acls, "ACLS not found, skipping") + @skipUnless(has_acls, "ACLS not found, skipping") def test_list_entry_acls(self): entry = lxml.etree.Element("Path", name="/test", type="file") lxml.etree.SubElement(entry, "ACL", scope="user", type="default", @@ -791,7 +791,7 @@ class TestPOSIXTool(Bcfg2TestCase): {("default", posix1e.ACL_USER, "user"): 7, ("access", posix1e.ACL_GROUP, "group"): 5}) - @unittest.skipUnless(has_acls, "ACLS not found, skipping") + @skipUnless(has_acls, "ACLS not found, skipping") @patch("pwd.getpwuid") @patch("grp.getgrgid") def test_list_file_acls(self, mock_getgrgid, mock_getpwuid): @@ -876,7 +876,7 @@ class TestPOSIXTool(Bcfg2TestCase): self.assertItemsEqual(mock_ACL.call_args_list, [call(file=path), call(filedef=path)]) - @unittest.skipUnless(has_acls, "ACLS not found, skipping") + @skipUnless(has_acls, "ACLS not found, skipping") @patch("Bcfg2.Client.Tools.POSIX.base.%s._list_file_acls" % test_obj.__name__) @patch("Bcfg2.Client.Tools.POSIX.base.%s._list_entry_acls" % diff --git a/testsuite/Testlib/TestServer/TestPlugin.py b/testsuite/Testlib/TestServer/TestPlugin.py index 1f2f236dd..d59b19a32 100644 --- a/testsuite/Testlib/TestServer/TestPlugin.py +++ b/testsuite/Testlib/TestServer/TestPlugin.py @@ -135,7 +135,7 @@ class TestPlugin(TestDebuggable): class TestDatabaseBacked(TestPlugin): test_obj = DatabaseBacked - @unittest.skipUnless(has_django, "Django not found") + @skipUnless(has_django, "Django not found") def test__use_db(self): core = Mock() core.setup.cfp.getboolean.return_value = True diff --git a/testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py b/testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py index 4990ca8f9..ee39eb2c9 100644 --- a/testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py +++ b/testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py @@ -370,7 +370,7 @@ class TestMetadata(_TestMetadata, TestStatistics, TestDatabaseBacked): return get_metadata_object(core=core, watch_clients=watch_clients, use_db=self.use_db) - @unittest.skipUnless(has_django, "Django not found") + @skipUnless(has_django, "Django not found") def test__use_db(self): # with the way we've set up our metadata tests, it's unweildy # to test _use_db. however, given the way get_obj works, if @@ -1128,15 +1128,13 @@ class TestMetadata(_TestMetadata, TestStatistics, TestDatabaseBacked): pass - class TestMetadataBase(TestMetadata): """ base test object for testing Metadata with database enabled """ __test__ = False use_db = True + @skipUnless(has_django, "Django not found") def setUp(self): - if not has_django: - self.skipTest("Django not found, skipping") syncdb(TestMetadataDB) def load_clients_data(self, metadata=None, xdata=None): @@ -1238,7 +1236,12 @@ class TestMetadataBase(TestMetadata): class TestMetadata_NoClientsXML(TestMetadataBase): """ test Metadata without a clients.xml. we have to disable or override tests that rely on client options """ - __test__ = True + # only run these tests if it's possible to skip tests or if we + # have django. otherwise they'll all get run because our fake + # skipping decorators for python < 2.7 won't work when they + # decorate setUp() + if can_skip or has_django: + __test__ = True def load_groups_data(self, metadata=None, xdata=None): if metadata is None: @@ -1398,7 +1401,12 @@ class TestMetadata_NoClientsXML(TestMetadataBase): class TestMetadata_ClientsXML(TestMetadataBase): """ test Metadata with a clients.xml. """ - __test__ = True + # only run these tests if it's possible to skip tests or if we + # have django. otherwise they'll all get run because our fake + # skipping decorators for python < 2.7 won't work when they + # decorate setUp() + if can_skip or has_django: + __test__ = True def load_clients_data(self, metadata=None, xdata=None): if metadata is None: diff --git a/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py b/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py index 960697f70..3cff4ac73 100644 --- a/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py +++ b/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py @@ -57,17 +57,15 @@ class TestProbeData(Bcfg2TestCase): self.assertIsNotNone(data.xdata) self.assertIsNotNone(data.xdata.find("test2")) + @skipUnless(has_json, "JSON libraries not found, skipping JSON tests") def test_json(self): - if not has_json: - self.skipTest("JSON libraries not found, skipping JSON tests") jdata = json.dumps(test_data) data = ProbeData(jdata) self.assertIsNotNone(data.json) self.assertItemsEqual(test_data, data.json) + @skipUnless(has_yaml, "YAML libraries not found, skipping YAML tests") def test_yaml(self): - if not has_yaml: - self.skipTest("YAML libraries not found, skipping YAML tests") jdata = yaml.dump(test_data) data = ProbeData(jdata) self.assertIsNotNone(data.yaml) @@ -252,7 +250,7 @@ text "use_database", default=False) - @unittest.skipUnless(has_django, "Django not found, skipping") + @skipUnless(has_django, "Django not found, skipping") @patch("Bcfg2.Server.Plugins.Probes.Probes._write_data_db", Mock()) @patch("Bcfg2.Server.Plugins.Probes.Probes._write_data_xml", Mock()) def test_write_data_xml(self): @@ -261,7 +259,7 @@ text probes._write_data_xml.assert_called_with("test") self.assertFalse(probes._write_data_db.called) - @unittest.skipUnless(has_django, "Django not found, skipping") + @skipUnless(has_django, "Django not found, skipping") @patch("Bcfg2.Server.Plugins.Probes.Probes._write_data_db", Mock()) @patch("Bcfg2.Server.Plugins.Probes.Probes._write_data_xml", Mock()) def test_write_data_db(self): @@ -326,7 +324,7 @@ text self.assertIsNotNone(jdata.get("value")) self.assertItemsEqual(test_data, json.loads(jdata.get("value"))) - @unittest.skipUnless(has_django, "Django not found, skipping") + @skipUnless(has_django, "Django not found, skipping") def test__write_data_db(self): syncdb(TestProbesDB) probes = self.get_probes_object(use_db=True) @@ -378,7 +376,7 @@ text pgroups = ProbesGroupsModel.objects.filter(hostname=cname).all() self.assertEqual(len(pgroups), len(probes.cgroups[cname])) - @unittest.skipUnless(has_django, "Django not found, skipping") + @skipUnless(has_django, "Django not found, skipping") @patch("Bcfg2.Server.Plugins.Probes.Probes._load_data_db", Mock()) @patch("Bcfg2.Server.Plugins.Probes.Probes._load_data_xml", Mock()) def test_load_data_xml(self): @@ -387,7 +385,7 @@ text probes._load_data_xml.assert_any_call() self.assertFalse(probes._load_data_db.called) - @unittest.skipUnless(has_django, "Django not found, skipping") + @skipUnless(has_django, "Django not found, skipping") @patch("Bcfg2.Server.Plugins.Probes.Probes._load_data_db", Mock()) @patch("Bcfg2.Server.Plugins.Probes.Probes._load_data_xml", Mock()) def test_load_data_db(self): @@ -419,7 +417,7 @@ text self.assertItemsEqual(probes.probedata, self.get_test_probedata()) self.assertItemsEqual(probes.cgroups, self.get_test_cgroups()) - @unittest.skipUnless(has_django, "Django not found, skipping") + @skipUnless(has_django, "Django not found, skipping") def test__load_data_db(self): syncdb(TestProbesDB) probes = self.get_probes_object(use_db=True) diff --git a/testsuite/common.py b/testsuite/common.py index 006b45970..e0a27b2c7 100644 --- a/testsuite/common.py +++ b/testsuite/common.py @@ -1,8 +1,6 @@ import os import unittest - -__all__ = ['call', 'datastore', 'Bcfg2TestCase', 'DBModelTestCase', 'syncdb', - 'XI', 'XI_NAMESPACE'] +from functools import wraps datastore = "/" @@ -32,7 +30,84 @@ except ImportError: return (args, kwargs) -class Bcfg2TestCase(unittest.TestCase): +if hasattr(unittest.TestCase, "assertItemsEqual"): + TestCase = unittest.TestCase +else: + def assertion(predicate, default_msg=None): + @wraps(predicate) + def inner(*args, **kwargs): + if 'msg' in kwargs: + msg = kwargs['msg'] + del kwargs['msg'] + else: + msg = default_msg % args + assert predicate(*args, **kwargs), msg + return inner + + class TestCase(unittest.TestCase): + # versions of TestCase before python 2.7 lacked a lot of the + # really handy convenience methods, so we provide them -- at + # least the easy ones and the ones we use. + assertIs = assertion(lambda a, b: a is b, "%s is not %s") + assertIsNot = assertion(lambda a, b: a is not b, "%s is %s") + assertIsNone = assertion(lambda x: x is None, "%s is not None") + assertIsNotNone = assertion(lambda x: x is not None, "%s is None") + assertIn = assertion(lambda a, b: a in b, "%s is not in %s") + assertNotIn = assertion(lambda a, b: a not in b, "%s is in %s") + assertIsInstance = assertion(isinstance, "%s is not %s") + assertNotIsInstance = assertion(lambda a, b: not isinstance(a, b), + "%s is %s") + assertGreater = assertion(lambda a, b: a > b, + "%s is not greater than %s") + assertGreaterEqual = assertion(lambda a, b: a >= b, + "%s is not greater than or equal to %s") + assertLess = assertion(lambda a, b: a < b, "%s is not less than %s") + assertLessEqual = assertion(lambda a, b: a <= b, + "%s is not less than or equal to %s") + assertItemsEqual = assertion(lambda a, b: sorted(a) == sorted(b), + "Items do not match:\n%s\n%s") + +if hasattr(unittest, "skip"): + can_skip = True + skip = unittest.skip + skipIf = unittest.skipIf + skipUnless = unittest.skipUnless +else: + # we can't actually skip tests, we just make them pass + can_skip = False + + def skip(msg): + def decorator(func): + @wraps(func) + def inner(*args, **kwargs): + pass + return inner + return decorator + + def skipIf(condition, msg): + def decorator(func): + if condition: + return func + + @wraps(func) + def inner(*args, **kwargs): + pass + return inner + return decorator + + def skipUnless(condition, msg): + def decorator(func): + if not condition: + return func + + @wraps(func) + def inner(*args, **kwargs): + pass + return inner + return decorator + + +class Bcfg2TestCase(TestCase): def assertXMLEqual(self, el1, el2, msg=None): self.assertEqual(el1.tag, el2.tag, msg=msg) self.assertEqual(el1.text, el2.text, msg=msg) -- cgit v1.2.3-1-g7c22