From fc29986ddf780202651053aeafa308653f05d2dd Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Thu, 16 Aug 2012 15:57:51 -0400 Subject: minor test fixes --- .../TestClient/TestTools/TestPOSIX/TestFile.py | 11 ++--- testsuite/Testlib/TestServer/TestPlugin.py | 3 +- .../Testlib/TestServer/TestPlugins/TestMetadata.py | 56 ++++++++++++++-------- .../Testlib/TestServer/TestPlugins/TestProbes.py | 36 ++++++++++---- 4 files changed, 67 insertions(+), 39 deletions(-) (limited to 'testsuite') diff --git a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py index d1f3ed14d..9582636fc 100644 --- a/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py +++ b/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py @@ -120,27 +120,24 @@ class TestPOSIXFile(TestPOSIXTool): mock_get_data.return_value = ("test", False) exists_rv.__getitem__.return_value = 4 entry.set("sensitive", "true") - open_rv = Mock() - open_rv.read.return_value = "tart" - mock_open.return_value = open_rv + mock_open.return_value.read.return_value = "tart" self.assertFalse(ptool.verify(entry, [])) mock_exists.assert_called_with(entry) mock_verify.assert_called_with(ptool, entry, []) mock_open.assert_called_with(entry.get("name")) - open_rv.assert_any_call() + mock_open.return_value.read.assert_called_with() mock_get_diffs.assert_called_with(entry, interactive=False, sensitive=True, is_binary=False, content="tart") reset() - open_rv.read.return_value = "test" - mock_open.return_value = open_rv + mock_open.return_value.read.return_value = "test" self.assertTrue(ptool.verify(entry, [])) mock_exists.assert_called_with(entry) mock_verify.assert_called_with(ptool, entry, []) mock_open.assert_called_with(entry.get("name")) - open_rv.assert_any_call() + mock_open.return_value.read.assert_called_with() self.assertFalse(mock_get_diffs.called) reset() diff --git a/testsuite/Testlib/TestServer/TestPlugin.py b/testsuite/Testlib/TestServer/TestPlugin.py index ab9754e00..678c65264 100644 --- a/testsuite/Testlib/TestServer/TestPlugin.py +++ b/testsuite/Testlib/TestServer/TestPlugin.py @@ -362,6 +362,7 @@ class TestThreadedStatistics(TestStatistics): return ts.work_queue.get_calls > 3 core.terminate.isSet.side_effect = terminate_isset + ts.work_queue.empty.return_value = False ts.run() mock_load.assert_any_call() self.assertGreaterEqual(ts.work_queue.get.call_count, len(self.data)) @@ -1720,7 +1721,7 @@ class TestEntrySet(TestDebuggable): spec.prio = prio spec.host = host if prio: - spec.__cmp__ = lambda o: cmp(spec.prio, o.prio) + spec.__cmp__ = lambda s, o: cmp(s.prio, o.prio) return spec self.assertRaises(PluginExecutionError, diff --git a/testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py b/testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py index 6aab04a89..4ea0e4fef 100644 --- a/testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py +++ b/testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py @@ -82,7 +82,8 @@ def get_metadata_object(core=None, watch_clients=False, use_db=False): class TestMetadataDB(DBModelTestCase): - models = [MetadataClientModel] + if has_django: + models = [MetadataClientModel] class TestClientVersions(Bcfg2TestCase): @@ -216,38 +217,51 @@ class TestXMLMetadataConfig(TestXMLFileBacked): # Index() isn't used on XMLMetadataConfig objects pass - @patch("Bcfg2.Server.Plugins.Metadata.XMLMetadataConfig.add_monitor") @patch("lxml.etree.parse") - def test_load_xml(self, mock_parse, mock_add_monitor): + @patch("Bcfg2.Server.Plugins.Metadata.XMLMetadataConfig._follow_xincludes") + def test_load_xml(self, mock_follow, mock_parse): config = self.get_obj("clients.xml") + + def reset(): + mock_parse.reset_mock() + mock_follow.reset_mock() + config.data = None + config.basedata = None + + reset() + config.load_xml() + mock_follow.assert_called_with(xdata=mock_parse.return_value) + mock_parse.assert_called_with(os.path.join(config.basedir, + "clients.xml"), + parser=Bcfg2.Server.XMLParser) + self.assertFalse(mock_parse.return_value.xinclude.called) + self.assertEqual(config.data, mock_parse.return_value) + self.assertIsNotNone(config.basedata) + + reset() mock_parse.side_effect = lxml.etree.XMLSyntaxError(None, None, None, None) config.load_xml() + mock_parse.assert_called_with(os.path.join(config.basedir, + "clients.xml"), + parser=Bcfg2.Server.XMLParser) self.assertIsNone(config.data) self.assertIsNone(config.basedata) - config.data = None - config.basedata = None + reset() mock_parse.side_effect = None - mock_parse.return_value.findall = Mock(return_value=[]) + def follow_xincludes(xdata=None): + config.extras = [Mock(), Mock()] + mock_follow.side_effect = follow_xincludes config.load_xml() - self.assertIsNotNone(config.data) + mock_follow.assert_called_with(xdata=mock_parse.return_value) + mock_parse.assert_called_with(os.path.join(config.basedir, + "clients.xml"), + parser=Bcfg2.Server.XMLParser) + mock_parse.return_value.xinclude.assert_any_call() + self.assertEqual(config.data, mock_parse.return_value) self.assertIsNotNone(config.basedata) - config.data = None - config.basedata = None - - def side_effect(*args): - def second_call(*args): - return [] - mock_parse.return_value.findall.side_effect = second_call - return [lxml.etree.Element(XI + "include", href="more.xml"), - lxml.etree.Element(XI + "include", href="evenmore.xml")] - - mock_parse.return_value.findall = Mock(side_effect=side_effect) - config.load_xml() - mock_add_monitor.assert_any_call("more.xml") - mock_add_monitor.assert_any_call("evenmore.xml") @patch("Bcfg2.Server.Plugins.Metadata.XMLMetadataConfig.write_xml") def test_write(self, mock_write_xml): diff --git a/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py b/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py index 0bcb65dc4..960697f70 100644 --- a/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py +++ b/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py @@ -15,11 +15,12 @@ from ..TestPlugin import TestEntrySet, TestProbing, TestConnector, \ test_data = dict(a=1, b=[1, 2, 3], c="test") class FakeList(list): - sort = Mock() + pass class TestProbesDB(DBModelTestCase): - models = [ProbesGroupsModel, ProbesDataModel] + if has_django: + models = [ProbesGroupsModel, ProbesDataModel] class TestClientProbeDataSet(Bcfg2TestCase): @@ -132,7 +133,8 @@ class TestProbeSet(TestEntrySet): # trust that Specificity is well-tested. Hah!) We also test # to make sure the interpreter is determined correctly. ps.get_matching = Mock() - matching = [] + matching = FakeList() + matching.sort = Mock() p1 = Mock() p1.specific = Bcfg2.Server.Plugin.Specificity(group=True, prio=10) @@ -164,7 +166,10 @@ group-specific""" metadata = Mock() pdata = ps.get_probe_data(metadata) ps.get_matching.assert_called_with(metadata) - FakeList.sort.assert_any_call() + # we can't create a matching operator.attrgetter object, and I + # don't feel the need to mock that out -- this is a good + # enough check + self.assertTrue(matching.sort.called) self.assertEqual(len(pdata), 3, "Found: %s" % [p.get("name") for p in pdata]) @@ -211,16 +216,27 @@ text "group-with-dashes"], "bar.example.com": []} - @patch("Bcfg2.Server.Plugins.Probes.Probes.load_data", Mock()) - def get_probes_object(self, use_db=False): + def get_probes_object(self, use_db=False, load_data=None): core = Mock() core.setup.cfp.getboolean = Mock() core.setup.cfp.getboolean.return_value = use_db - return Probes(core, datastore) + if load_data is None: + load_data = MagicMock() + # we have to patch load_data() in a funny way because + # different versions of Mock have different scopes for + # patching. in some versions, a patch applied to + # get_probes_object() would only apply to that function, while + # in others it would also apply to the calling function (e.g., + # test__init(), which relies on being able to check the calls + # of load_data(), and thus on load_data() being consistently + # mocked) + with patch("Bcfg2.Server.Plugins.Probes.Probes.load_data", + new=load_data): + return Probes(core, datastore) - @patch("Bcfg2.Server.Plugins.Probes.Probes.load_data") - def test__init(self, mock_load_data): - probes = self.get_probes_object() + def test__init(self): + mock_load_data = Mock() + probes = self.get_probes_object(load_data=mock_load_data) probes.core.fam.AddMonitor.assert_called_with(os.path.join(datastore, probes.name), probes.probes) -- cgit v1.2.3-1-g7c22