From 22016ee593d6047855964734a17120d2aea6b5a5 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Tue, 21 Aug 2012 13:32:51 -0400 Subject: lots of various py3k fixes --- .../TestClient/TestTools/TestPOSIX/TestDevice.py | 12 +- .../TestTools/TestPOSIX/TestDirectory.py | 12 +- .../TestClient/TestTools/TestPOSIX/TestFile.py | 42 +++--- .../TestClient/TestTools/TestPOSIX/TestHardlink.py | 4 +- .../TestTools/TestPOSIX/TestNonexistent.py | 4 +- .../TestTools/TestPOSIX/TestPermissions.py | 4 +- .../TestClient/TestTools/TestPOSIX/TestSymlink.py | 4 +- .../TestClient/TestTools/TestPOSIX/Testbase.py | 6 +- testsuite/Testlib/TestServer/TestPlugin.py | 126 ++++++++-------- .../Testlib/TestServer/TestPlugins/TestProbes.py | 8 +- testsuite/common.py | 165 ++++++++++++++++----- 11 files changed, 241 insertions(+), 146 deletions(-) (limited to 'testsuite') diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDevice.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDevice.py index f32da9c37..b737cfe1e 100644 --- a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDevice.py +++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDevice.py @@ -4,8 +4,8 @@ import unittest import lxml.etree from mock import Mock, MagicMock, patch from Bcfg2.Client.Tools.POSIX.Device import * -from Test__init import get_posix_object -from Testbase import TestPOSIXTool +from .Test__init import get_posix_object +from .Testbase import TestPOSIXTool from .....common import * class TestPOSIXDevice(TestPOSIXTool): @@ -97,8 +97,8 @@ class TestPOSIXDevice(TestPOSIXTool): self.assertTrue(ptool.install(entry)) mock_exists.assert_called_with(entry, remove=True) mock_makedev.assert_called_with(0, 10) - mock_mknod.assert_called_with(entry.get("name"), - device_map[entry.get("dev_type")] | 0644, + mock_mknod.assert_called_with(entry.get("name"), # 0o644 + device_map[entry.get("dev_type")] | 420, mock_makedev.return_value) mock_install.assert_called_with(ptool, entry) @@ -127,6 +127,6 @@ class TestPOSIXDevice(TestPOSIXTool): self.assertTrue(ptool.install(entry)) mock_exists.assert_called_with(entry, remove=True) - mock_mknod.assert_called_with(entry.get("name"), - device_map[entry.get("dev_type")] | 0644) + mock_mknod.assert_called_with(entry.get("name"), # 0o644 + device_map[entry.get("dev_type")] | 420) mock_install.assert_called_with(ptool, entry) diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py index 34c98fa9c..79879afed 100644 --- a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py +++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDirectory.py @@ -5,8 +5,8 @@ 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 -from Testbase import TestPOSIXTool +from .Test__init import get_posix_object +from .Testbase import TestPOSIXTool from .....common import * class TestPOSIXDirectory(TestPOSIXTool): @@ -25,14 +25,14 @@ class TestPOSIXDirectory(TestPOSIXTool): mock_exists.reset_mock() exists_rv = MagicMock() - exists_rv.__getitem__.return_value = stat.S_IFREG | 0644 + exists_rv.__getitem__.return_value = stat.S_IFREG | 420 # 0o644 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 + exists_rv.__getitem__.return_value = stat.S_IFDIR | 420 # 0o644 self.assertFalse(ptool.verify(entry, [])) mock_exists.assert_called_with(entry) mock_verify.assert_called_with(ptool, entry, []) @@ -105,7 +105,7 @@ class TestPOSIXDirectory(TestPOSIXTool): reset() exists_rv = MagicMock() - exists_rv.__getitem__.return_value = stat.S_IFREG | 0644 + exists_rv.__getitem__.return_value = stat.S_IFREG | 420 # 0o644 mock_exists.return_value = exists_rv self.assertTrue(ptool.install(entry)) mock_unlink.assert_called_with(entry.get("name")) @@ -114,7 +114,7 @@ class TestPOSIXDirectory(TestPOSIXTool): mock_install.assert_called_with(ptool, entry) reset() - exists_rv.__getitem__.return_value = stat.S_IFDIR | 0644 + exists_rv.__getitem__.return_value = stat.S_IFDIR | 420 # 0o644 mock_install.return_value = True self.assertTrue(ptool.install(entry)) mock_exists.assert_called_with(entry) diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py index 06912c19c..084268959 100644 --- a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py +++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py @@ -5,10 +5,11 @@ import difflib import binascii import unittest import lxml.etree +from Bcfg2.Bcfg2Py3k import b64encode, b64decode from mock import Mock, MagicMock, patch from Bcfg2.Client.Tools.POSIX.File import * -from Test__init import get_posix_object -from Testbase import TestPOSIXTool +from .Test__init import get_posix_object +from .Testbase import TestPOSIXTool from .....common import * def get_file_object(posix=None): @@ -33,17 +34,18 @@ class TestPOSIXFile(TestPOSIXTool): def test_is_string(self): ptool = self.get_obj() - for char in range(8) + range(14, 32): + for char in list(range(8)) + list(range(14, 32)): self.assertFalse(ptool._is_string("foo" + chr(char) + "bar", - 'utf_8')) - for char in range(9, 14) + range(33, 128): + 'UTF-8')) + for char in list(range(9, 14)) + list(range(33, 128)): self.assertTrue(ptool._is_string("foo" + chr(char) + "bar", - 'utf_8')) - self.assertFalse(ptool._is_string("foo" + chr(128) + "bar", - 'ascii')) + 'UTF-8')) ustr = 'é' - self.assertTrue(ptool._is_string(ustr, 'utf_8')) - self.assertFalse(ptool._is_string(ustr, 'ascii')) + self.assertTrue(ptool._is_string(ustr, 'UTF-8')) + if not inPy3k: + self.assertFalse(ptool._is_string("foo" + chr(128) + "bar", + 'ascii')) + self.assertFalse(ptool._is_string(ustr, 'ascii')) def test_get_data(self): orig_entry = lxml.etree.Element("Path", name="/test", type="file") @@ -51,7 +53,7 @@ class TestPOSIXFile(TestPOSIXTool): ptool = self.get_obj(posix=get_posix_object(setup=setup)) entry = copy.deepcopy(orig_entry) - entry.text = binascii.b2a_base64("test") + entry.text = b64encode("test") entry.set("encoding", "base64") self.assertEqual(ptool._get_data(entry), ("test", True)) @@ -63,7 +65,7 @@ class TestPOSIXFile(TestPOSIXTool): entry.text = "test" self.assertEqual(ptool._get_data(entry), ("test", False)) - ustr = u'é' + ustr = u('é') entry = copy.deepcopy(orig_entry) entry.text = ustr self.assertEqual(ptool._get_data(entry), (ustr, False)) @@ -207,7 +209,7 @@ class TestPOSIXFile(TestPOSIXTool): mock_rename.assert_called_with(newfile, entry.get("name")) mock_unlink.assert_called_with(newfile) - @patch("%.open" % builtins) + @patch("%s.open" % builtins) @patch("Bcfg2.Client.Tools.POSIX.File.%s._diff" % test_obj.__name__) @patch("Bcfg2.Client.Tools.POSIX.File.%s._get_data" % test_obj.__name__) @patch("Bcfg2.Client.Tools.POSIX.File.%s._is_string" % test_obj.__name__) @@ -239,8 +241,7 @@ class TestPOSIXFile(TestPOSIXTool): mock_open.assert_called_with(entry.get("name")) mock_open.return_value.read.assert_any_call() self.assertFalse(mock_diff.called) - self.assertEqual(entry.get("current_bfile"), - binascii.b2a_base64(ondisk)) + self.assertEqual(entry.get("current_bfile"), b64encode(ondisk)) # binary data on disk entry = reset() @@ -248,8 +249,7 @@ class TestPOSIXFile(TestPOSIXTool): ptool._get_diffs(entry, content=ondisk) self.assertFalse(mock_open.called) self.assertFalse(mock_diff.called) - self.assertEqual(entry.get("current_bfile"), - binascii.b2a_base64(ondisk)) + self.assertEqual(entry.get("current_bfile"), b64encode(ondisk)) # sensitive, non-interactive -- do nothing entry = reset() @@ -278,7 +278,7 @@ class TestPOSIXFile(TestPOSIXTool): filename=entry.get("name")) self.assertIsNone(entry.get("qtext")) self.assertEqual(entry.get("current_bdiff"), - binascii.b2a_base64("\n".join(mock_diff.return_value))) + b64encode("\n".join(mock_diff.return_value))) del entry.attrib["current_bdiff"] self.assertItemsEqual(orig_entry.attrib, entry.attrib) @@ -297,14 +297,14 @@ class TestPOSIXFile(TestPOSIXTool): self.assertIsNotNone(entry.get("qtext")) self.assertTrue(entry.get("qtext").startswith("test\n")) self.assertEqual(entry.get("current_bdiff"), - binascii.b2a_base64("\n".join(mock_diff.return_value))) + b64encode("\n".join(mock_diff.return_value))) del entry.attrib['qtext'] del entry.attrib["current_bdiff"] self.assertItemsEqual(orig_entry.attrib, entry.attrib) # non-sensitive, interactive with unicode data entry = reset() - entry.text = u"tëst" + entry.text = u("tëst") encoded = entry.text.encode(setup['encoding']) mock_get_data.return_value = (encoded, False) ptool._get_diffs(entry, interactive=True) @@ -317,7 +317,7 @@ class TestPOSIXFile(TestPOSIXTool): filename=entry.get("name"))]) self.assertIsNotNone(entry.get("qtext")) self.assertEqual(entry.get("current_bdiff"), - binascii.b2a_base64("\n".join(mock_diff.return_value))) + b64encode("\n".join(mock_diff.return_value))) del entry.attrib['qtext'] del entry.attrib["current_bdiff"] self.assertItemsEqual(orig_entry.attrib, entry.attrib) diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestHardlink.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestHardlink.py index 663c98af2..221da9ea5 100644 --- a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestHardlink.py +++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestHardlink.py @@ -4,8 +4,8 @@ import unittest import lxml.etree from mock import Mock, MagicMock, patch from Bcfg2.Client.Tools.POSIX.Hardlink import * -from Test__init import get_posix_object -from Testbase import TestPOSIXTool +from .Test__init import get_posix_object +from .Testbase import TestPOSIXTool from .....common import * class TestPOSIXHardlink(TestPOSIXTool): diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestNonexistent.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestNonexistent.py index 8d959a15f..2b46bd9e6 100644 --- a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestNonexistent.py +++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestNonexistent.py @@ -4,8 +4,8 @@ import unittest import lxml.etree from mock import Mock, MagicMock, patch from Bcfg2.Client.Tools.POSIX.Nonexistent import * -from Test__init import get_config, get_posix_object -from Testbase import TestPOSIXTool +from .Test__init import get_config, get_posix_object +from .Testbase import TestPOSIXTool from .....common import * class TestPOSIXNonexistent(TestPOSIXTool): diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestPermissions.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestPermissions.py index 9d8130658..008f0c839 100644 --- a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestPermissions.py +++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestPermissions.py @@ -3,8 +3,8 @@ import unittest import lxml.etree from mock import Mock, MagicMock, patch from Bcfg2.Client.Tools.POSIX.Permissions import * -from Test__init import get_posix_object -from Testbase import TestPOSIXTool +from .Test__init import get_posix_object +from .Testbase import TestPOSIXTool from .....common import * class TestPOSIXPermissions(TestPOSIXTool): diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestSymlink.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestSymlink.py index 03504ca61..7ce8a4437 100644 --- a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestSymlink.py +++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestSymlink.py @@ -4,8 +4,8 @@ import unittest import lxml.etree from mock import Mock, MagicMock, patch from Bcfg2.Client.Tools.POSIX.Symlink import * -from Test__init import get_posix_object -from Testbase import TestPOSIXTool +from .Test__init import get_posix_object +from .Testbase import TestPOSIXTool from .....common import * class TestPOSIXSymlink(TestPOSIXTool): diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py index 442641c0b..e8e5dbe23 100644 --- a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py +++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/Testbase.py @@ -6,7 +6,7 @@ import lxml.etree from mock import Mock, MagicMock, patch import Bcfg2.Client.Tools from Bcfg2.Client.Tools.POSIX.base import * -from Test__init import get_posix_object +from .Test__init import get_posix_object from .....common import * try: @@ -240,13 +240,13 @@ class TestPOSIXTool(Bcfg2TestCase): raise KeyError os.chown.side_effect = chown_rv entry.set("type", "device") - entry.set("dev_type", device_map.keys()[0]) + entry.set("dev_type", list(device_map.keys())[0]) self.assertFalse(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) mock_chmod.assert_called_with(entry.get("name"), - int(entry.get("perms"), 8) | device_map.values()[0]) + int(entry.get("perms"), 8) | list(device_map.values())[0]) mock_utime.assert_called_with(entry.get("name"), (mtime, mtime)) mock_set_secontext.assert_called_with(entry, path=entry.get("name")) mock_set_acls.assert_called_with(entry, path=entry.get("name")) diff --git a/testsuite/Testlib/TestServer/TestPlugin.py b/testsuite/Testlib/TestServer/TestPlugin.py index d1904a2e6..c00a7d91d 100644 --- a/testsuite/Testlib/TestServer/TestPlugin.py +++ b/testsuite/Testlib/TestServer/TestPlugin.py @@ -4,9 +4,10 @@ import copy import logging import unittest import lxml.etree +import Bcfg2.Server +from Bcfg2.Bcfg2Py3k import reduce from mock import Mock, MagicMock, patch from Bcfg2.Server.Plugin import * -import Bcfg2.Server from ...common import * class FakeElementTree(lxml.etree._ElementTree): @@ -228,8 +229,9 @@ class TestThreadedStatistics(TestStatistics): mock_start.assert_any_call() @patch("%s.open" % builtins) + @patch("%s.dump" % cPickle.__name__) @patch("Bcfg2.Server.Plugin.ThreadedStatistics.run", Mock()) - def test_save(self, mock_open): + def test_save(self, mock_dump, mock_open): core = Mock() ts = self.get_obj(core) queue = Mock() @@ -255,78 +257,76 @@ class TestThreadedStatistics(TestStatistics): queue.get_nowait = Mock(side_effect=lambda: queue.data.pop()) mock_open.side_effect = None - # oh, the joy of working around different package names in - # py3k... - with patch("%s.dump" % cPickle.__name__) as mock_dump: - ts.save() - queue.empty.assert_any_call() - queue.get_nowait.assert_any_call() - mock_open.assert_called_with(ts.pending_file, 'w') - mock_open.return_value.close.assert_any_call() - # the order of the queue data gets changed, so we have to - # verify this call in an ugly way - self.assertItemsEqual(mock_dump.call_args[0][0], self.data) - self.assertEqual(mock_dump.call_args[0][1], mock_open.return_value) + ts.save() + queue.empty.assert_any_call() + queue.get_nowait.assert_any_call() + mock_open.assert_called_with(ts.pending_file, 'w') + mock_open.return_value.close.assert_any_call() + # the order of the queue data gets changed, so we have to + # verify this call in an ugly way + self.assertItemsEqual(mock_dump.call_args[0][0], self.data) + self.assertEqual(mock_dump.call_args[0][1], mock_open.return_value) @patch("os.unlink") @patch("os.path.exists") @patch("%s.open" % builtins) @patch("lxml.etree.XML") + @patch("%s.load" % cPickle.__name__) @patch("Bcfg2.Server.Plugin.ThreadedStatistics.run", Mock()) - def test_load(self, mock_XML, mock_open, mock_exists, mock_unlink): + def test_load(self, mock_load, mock_XML, mock_open, mock_exists, + mock_unlink): core = Mock() core.terminate.isSet.return_value = False ts = self.get_obj(core) - with patch("%s.load" % cPickle.__name__) as mock_load: - ts.work_queue = Mock() + ts.work_queue = Mock() + ts.work_queue.data = [] + def reset(): + core.reset_mock() + mock_open.reset_mock() + mock_exists.reset_mock() + mock_unlink.reset_mock() + mock_load.reset_mock() + mock_XML.reset_mock() + ts.work_queue.reset_mock() ts.work_queue.data = [] - def reset(): - core.reset_mock() - mock_open.reset_mock() - mock_exists.reset_mock() - mock_unlink.reset_mock() - mock_load.reset_mock() - mock_XML.reset_mock() - ts.work_queue.reset_mock() - ts.work_queue.data = [] - - mock_exists.return_value = False - self.assertTrue(ts.load()) - mock_exists.assert_called_with(ts.pending_file) - reset() - mock_exists.return_value = True - mock_open.side_effect = OSError - self.assertFalse(ts.load()) - mock_exists.assert_called_with(ts.pending_file) - mock_open.assert_called_with(ts.pending_file, 'r') + mock_exists.return_value = False + self.assertTrue(ts.load()) + mock_exists.assert_called_with(ts.pending_file) - reset() - mock_open.side_effect = None - mock_load.return_value = self.data - ts.work_queue.put_nowait.side_effect = Full - self.assertTrue(ts.load()) - mock_exists.assert_called_with(ts.pending_file) - mock_open.assert_called_with(ts.pending_file, 'r') - mock_open.return_value.close.assert_any_call() - mock_load.assert_called_with(mock_open.return_value) + reset() + mock_exists.return_value = True + mock_open.side_effect = OSError + self.assertFalse(ts.load()) + mock_exists.assert_called_with(ts.pending_file) + mock_open.assert_called_with(ts.pending_file, 'r') - reset() - core.build_metadata.side_effect = lambda x: x - mock_XML.side_effect = lambda x, parser=None: x - ts.work_queue.put_nowait.side_effect = None - self.assertTrue(ts.load()) - mock_exists.assert_called_with(ts.pending_file) - mock_open.assert_called_with(ts.pending_file, 'r') - mock_open.return_value.close.assert_any_call() - mock_load.assert_called_with(mock_open.return_value) - self.assertItemsEqual(mock_XML.call_args_list, - [call(x, parser=Bcfg2.Server.XMLParser) - for h, x in self.data]) - self.assertItemsEqual(ts.work_queue.put_nowait.call_args_list, - [call((h, x)) for h, x in self.data]) - mock_unlink.assert_called_with(ts.pending_file) + reset() + mock_open.side_effect = None + mock_load.return_value = self.data + ts.work_queue.put_nowait.side_effect = Full + self.assertTrue(ts.load()) + mock_exists.assert_called_with(ts.pending_file) + mock_open.assert_called_with(ts.pending_file, 'r') + mock_open.return_value.close.assert_any_call() + mock_load.assert_called_with(mock_open.return_value) + + reset() + core.build_metadata.side_effect = lambda x: x + mock_XML.side_effect = lambda x, parser=None: x + ts.work_queue.put_nowait.side_effect = None + self.assertTrue(ts.load()) + mock_exists.assert_called_with(ts.pending_file) + mock_open.assert_called_with(ts.pending_file, 'r') + mock_open.return_value.close.assert_any_call() + mock_load.assert_called_with(mock_open.return_value) + self.assertItemsEqual(mock_XML.call_args_list, + [call(x, parser=Bcfg2.Server.XMLParser) + for h, x in self.data]) + self.assertItemsEqual(ts.work_queue.put_nowait.call_args_list, + [call((h, x)) for h, x in self.data]) + mock_unlink.assert_called_with(ts.pending_file) @patch("threading.Thread.start", Mock()) @patch("Bcfg2.Server.Plugin.ThreadedStatistics.load") @@ -1018,7 +1018,7 @@ class TestStructFile(TestXMLFileBacked): mock_match.side_effect = match_rv actual = sf.Match(metadata) expected = reduce(lambda x, y: x + y, - children.values() + subgroups.values()) + list(children.values()) + list(subgroups.values())) self.assertEqual(len(actual), len(expected)) # easiest way to compare the values is actually to make # them into an XML document and let assertXMLEqual compare @@ -1047,7 +1047,7 @@ class TestStructFile(TestXMLFileBacked): expected = lxml.etree.Element(xdata.tag, **xdata.attrib) expected.text = xdata.text expected.extend(reduce(lambda x, y: x + y, - children.values() + subchildren.values())) + list(children.values()) + list(subchildren.values()))) expected.extend(standalone) self.assertXMLEqual(actual, expected) @@ -1064,7 +1064,7 @@ class TestStructFile(TestXMLFileBacked): for call in mock_xml_match.call_args_list: actual.append(call[0][0]) self.assertEqual(call[0][1], metadata) - expected = groups.values() + standalone + expected = list(groups.values()) + standalone # easiest way to compare the values is actually to make # them into an XML document and let assertXMLEqual compare # them diff --git a/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py b/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py index a779a7707..df9248e50 100644 --- a/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py +++ b/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py @@ -53,7 +53,7 @@ class TestProbeData(Bcfg2TestCase): def test_xdata(self): xdata = lxml.etree.Element("test") lxml.etree.SubElement(xdata, "test2") - data = ProbeData(lxml.etree.tostring(xdata)) + data = ProbeData(lxml.etree.tostring(xdata, encoding='unicode')) self.assertIsNotNone(data.xdata) self.assertIsNotNone(data.xdata.find("test2")) @@ -193,7 +193,7 @@ class TestProbes(TestProbing, TestConnector, TestDatabaseBacked): rv = dict() rv["foo.example.com"] = ClientProbeDataSet(timestamp=time.time()) rv["foo.example.com"]["xml"] = \ - ProbeData(lxml.etree.tostring(test_xdata)) + ProbeData(lxml.etree.tostring(test_xdata, encoding='unicode')) rv["foo.example.com"]["text"] = ProbeData("freeform text") rv["foo.example.com"]["multiline"] = ProbeData("""multiple lines @@ -277,7 +277,8 @@ text mock_open.assert_called_with(os.path.join(datastore, probes.name, "probed.xml"), "w") - data = lxml.etree.XML(str(mock_open.return_value.write.call_args[0][0])) + print("data=%s" % mock_open.return_value.write.call_args[0][0]) + data = lxml.etree.XML(mock_open.return_value.write.call_args[0][0]) self.assertEqual(len(data.xpath("//Client")), 2) foodata = data.find("Client[@name='foo.example.com']") @@ -290,6 +291,7 @@ text xml = foodata.find("Probe[@name='xml']") self.assertIsNotNone(xml) self.assertIsNotNone(xml.get("value")) + print("value=%s" % xml.get("value")) xdata = lxml.etree.XML(xml.get("value")) self.assertIsNotNone(xdata) self.assertIsNotNone(xdata.find("test")) diff --git a/testsuite/common.py b/testsuite/common.py index cc24112e7..98ee0c6f5 100644 --- a/testsuite/common.py +++ b/testsuite/common.py @@ -37,45 +37,16 @@ except ImportError: if inPy3k: builtins = "builtins" + + def u(x): + return x else: builtins = "__builtin__" -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 + import codecs + def u(x): + return codecs.unicode_escape_decode(x)[0] - 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 @@ -117,7 +88,129 @@ else: return decorator -class Bcfg2TestCase(TestCase): +needs_assertItemsEqual = False +needs_others = False +if not hasattr(unittest.TestCase, "assertItemsEqual"): + # TestCase in Py3k lacks assertItemsEqual, but has the other + # convenience methods. this code is cribbed from the py2.7 + # unittest library + import collections + needs_assertItemsEqual = True + + def _count_diff_all_purpose(actual, expected): + '''Returns list of (cnt_act, cnt_exp, elem) triples where the + counts differ''' + # elements need not be hashable + s, t = list(actual), list(expected) + m, n = len(s), len(t) + NULL = object() + result = [] + for i, elem in enumerate(s): + if elem is NULL: + continue + cnt_s = cnt_t = 0 + for j in range(i, m): + if s[j] == elem: + cnt_s += 1 + s[j] = NULL + for j, other_elem in enumerate(t): + if other_elem == elem: + cnt_t += 1 + t[j] = NULL + if cnt_s != cnt_t: + diff = _Mismatch(cnt_s, cnt_t, elem) + result.append(diff) + + for i, elem in enumerate(t): + if elem is NULL: + continue + cnt_t = 0 + for j in range(i, n): + if t[j] == elem: + cnt_t += 1 + t[j] = NULL + diff = _Mismatch(0, cnt_t, elem) + result.append(diff) + return result + + def _count_diff_hashable(actual, expected): + '''Returns list of (cnt_act, cnt_exp, elem) triples where the + counts differ''' + # elements must be hashable + s, t = _ordered_count(actual), _ordered_count(expected) + result = [] + for elem, cnt_s in s.items(): + cnt_t = t.get(elem, 0) + if cnt_s != cnt_t: + diff = _Mismatch(cnt_s, cnt_t, elem) + result.append(diff) + for elem, cnt_t in t.items(): + if elem not in s: + diff = _Mismatch(0, cnt_t, elem) + result.append(diff) + return result + +if not hasattr(unittest.TestCase, "assertIn"): + # versions of TestCase before python 2.7 and python 3.1 lacked a + # lot of the really handy convenience methods, so we provide them + # -- at least the easy ones and the ones we use. + needs_others = True + + def _assertion(predicate, default_msg=None): + @wraps(predicate) + def inner(self, *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 Bcfg2TestCase(unittest.TestCase): + if needs_assertItemsEqual: + def assertItemsEqual(self, expected_seq, actual_seq, msg=None): + first_seq, second_seq = list(actual_seq), list(expected_seq) + try: + first = collections.Counter(first_seq) + second = collections.Counter(second_seq) + except TypeError: + # Handle case with unhashable elements + differences = _count_diff_all_purpose(first_seq, second_seq) + else: + if first == second: + return + differences = _count_diff_hashable(first_seq, second_seq) + + if differences: + standardMsg = 'Element counts were not equal:\n' + lines = ['First has %d, Second has %d: %r' % diff + for diff in differences] + diffMsg = '\n'.join(lines) + standardMsg = self._truncateMessage(standardMsg, diffMsg) + msg = self._formatMessage(msg, standardMsg) + self.fail(msg) + + if needs_others: + 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 instance of %s") + assertNotIsInstance = _assertion(lambda a, b: not isinstance(a, b), + "%s is instance of %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") + + 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