From bf3adbb11ef36591d80b9b6f4d9768caf516b4e3 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Thu, 14 Nov 2013 09:35:06 -0500 Subject: testsuite: fixed unit tests for Probes allowed_groups option --- .../Testlib/TestServer/TestPlugins/TestProbes.py | 36 +++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'testsuite/Testsrc') diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestProbes.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestProbes.py index a87628eaf..2face023f 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestProbes.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestProbes.py @@ -1,4 +1,5 @@ import os +import re import sys import copy import time @@ -295,7 +296,9 @@ text def inner(): return self.get_obj(core) - return inner() + rv = inner() + rv.allowed_cgroups = [re.compile("^.*$")] + return rv def test__init(self): mock_load_data = Mock() @@ -591,6 +594,37 @@ text self.assertEqual(probes.cgroups[cname], self.get_test_cgroups()[cname]) + # test again, with an explicit list of allowed groups + probes.allowed_cgroups = [re.compile(r'^.*s$')] + for cname, cdata in self.get_test_probedata().items(): + client = Mock() + client.hostname = cname + cgroups = [] + cprobedata = ClientProbeDataSet() + for pname, pdata in cdata.items(): + dataitem = lxml.etree.Element("Probe", name=pname) + if pname == "text": + # add some groups to the plaintext test to test + # group parsing + data = [pdata] + for group in self.get_test_cgroups()[cname]: + data.append("group:%s" % group) + dataitem.text = "\n".join(data) + else: + dataitem.text = str(pdata) + + probes.ReceiveDataItem(client, dataitem, cgroups, cprobedata) + + probes.cgroups[client.hostname] = cgroups + probes.probedata[client.hostname] = cprobedata + self.assertIn(client.hostname, probes.probedata) + self.assertIn(pname, probes.probedata[cname]) + self.assertEqual(pdata, probes.probedata[cname][pname]) + self.assertIn(client.hostname, probes.cgroups) + self.assertEqual(probes.cgroups[cname], + [g for g in self.get_test_cgroups()[cname] + if g.endswith("s")]) + def test_get_additional_groups(self): TestConnector.test_get_additional_groups(self) -- cgit v1.2.3-1-g7c22 From 1c7488e85200e50429b1248d36a6815bedc49f02 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Mon, 25 Nov 2013 12:55:50 -0500 Subject: testsuite: fixed unit tests for database fixes --- testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'testsuite/Testsrc') diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py index 5ae0dfcba..eac4faf90 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py @@ -7,6 +7,7 @@ import Bcfg2.Server from Bcfg2.Compat import reduce from mock import Mock, MagicMock, patch from Bcfg2.Server.Plugin.helpers import * +from Bcfg2.Server.Plugin.exceptions import PluginInitError # add all parent testsuite directories to sys.path to allow (most) # relative imports in python 2.4 @@ -90,13 +91,13 @@ class TestDatabaseBacked(TestPlugin): Bcfg2.Server.Plugin.helpers.HAS_DJANGO = False core = Mock() + core.setup.cfp.getboolean.return_value = False db = self.get_obj(core) self.assertFalse(db._use_db) core = Mock() core.setup.cfp.getboolean.return_value = True - db = self.get_obj(core) - self.assertFalse(db._use_db) + self.assertRaises(PluginInitError, self.get_obj, core) Bcfg2.Server.Plugin.helpers.HAS_DJANGO = True -- cgit v1.2.3-1-g7c22 From e614d5a90baa771d6cda3860352890fdeb86236d Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Mon, 25 Nov 2013 13:33:32 -0500 Subject: testsuite: make DatabaseBacked tests work without django installed --- testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'testsuite/Testsrc') diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py index eac4faf90..f0531d113 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py @@ -77,6 +77,14 @@ class TestFunctions(Bcfg2TestCase): class TestDatabaseBacked(TestPlugin): test_obj = DatabaseBacked + def get_obj(self, core=None): + if not HAS_DJANGO: + if core is None: + core = Mock() + # disable the database + core.setup.cfp.getboolean.return_value = False + return TestPlugin.get_obj(self, core=core) + @skipUnless(HAS_DJANGO, "Django not found") def test__use_db(self): core = Mock() -- cgit v1.2.3-1-g7c22 From 37b65a39545d7c5b64c2403a617a97d1d0f4a012 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Mon, 25 Nov 2013 14:27:58 -0500 Subject: testsuite: fixed DatabaseBacked default Core object --- testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'testsuite/Testsrc') diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py index f0531d113..f9296ac89 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py @@ -80,7 +80,7 @@ class TestDatabaseBacked(TestPlugin): def get_obj(self, core=None): if not HAS_DJANGO: if core is None: - core = Mock() + core = MagicMock() # disable the database core.setup.cfp.getboolean.return_value = False return TestPlugin.get_obj(self, core=core) -- cgit v1.2.3-1-g7c22 From 53f8eb67378f6a8054cb107e72b094f070d40c83 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Thu, 5 Dec 2013 09:58:18 -0500 Subject: Tools: new Augeas driver --- .../TestClient/TestTools/TestPOSIX/TestAugeas.py | 233 +++++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestAugeas.py (limited to 'testsuite/Testsrc') diff --git a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestAugeas.py b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestAugeas.py new file mode 100644 index 000000000..bfcb8a378 --- /dev/null +++ b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestAugeas.py @@ -0,0 +1,233 @@ +# -*- coding: utf-8 -*- +import os +import sys +import copy +import lxml.etree +import tempfile +from mock import Mock, MagicMock, patch +try: + from Bcfg2.Client.Tools.POSIX.Augeas import * + HAS_AUGEAS = True +except ImportError: + HAS_AUGEAS = False + +# add all parent testsuite directories to sys.path to allow (most) +# relative imports in python 2.4 +path = os.path.dirname(__file__) +while path != "/": + if os.path.basename(path).lower().startswith("test"): + sys.path.append(path) + if os.path.basename(path) == "testsuite": + break + path = os.path.dirname(path) +from TestPOSIX.Testbase import TestPOSIXTool +from common import * + + +test_data = """ + + content with spaces + + + + + + + one + two + + + same + same + same + same + + +""" + +test_xdata = lxml.etree.XML(test_data) + +class TestPOSIXAugeas(TestPOSIXTool): + test_obj = POSIXAugeas + + applied_commands = dict( + insert=lxml.etree.Element( + "Insert", label="Thing", + path='Test/Children[#attribute/identical = "true"]/Thing'), + set=lxml.etree.Element("Set", path="Test/Text/#text", + value="content with spaces"), + move=lxml.etree.Element( + "Move", source="Test/Foo", + destination='Test/Children[#attribute/identical = "false"]/Foo'), + remove=lxml.etree.Element("Remove", path="Test/Bar"), + clear=lxml.etree.Element("Clear", path="Test/Empty/#text"), + setm=lxml.etree.Element( + "SetMulti", sub="#text", value="same", + base='Test/Children[#attribute/multi = "true"]/Thing')) + + + @skipUnless(HAS_AUGEAS, "Python Augeas libraries not found") + def setUp(self): + fd, self.tmpfile = tempfile.mkstemp() + os.fdopen(fd, 'w').write(test_data) + + def tearDown(self): + tmpfile = getattr(self, "tmpfile", None) + if tmpfile: + os.unlink(tmpfile) + + def test_fully_specified(self): + ptool = self.get_obj() + + entry = lxml.etree.Element("Path", name="/test", type="augeas") + self.assertFalse(ptool.fully_specified(entry)) + + entry.text = "text" + self.assertTrue(ptool.fully_specified(entry)) + + def test_install(self): + # this is tested adequately by the other tests + pass + + def test_verify(self): + # this is tested adequately by the other tests + pass + + @patch("Bcfg2.Client.Tools.POSIX.Augeas.POSIXTool.verify") + def _verify(self, commands, mock_verify): + ptool = self.get_obj() + mock_verify.return_value = True + + entry = lxml.etree.Element("Path", name=self.tmpfile, type="augeas", + lens="Xml") + entry.extend(commands) + + modlist = [] + self.assertTrue(ptool.verify(entry, modlist)) + mock_verify.assert_called_with(ptool, entry, modlist) + self.assertXMLEqual(lxml.etree.parse(self.tmpfile).getroot(), + test_xdata) + + def test_verify_insert(self): + """ Test successfully verifying an Insert command """ + self._verify([self.applied_commands['insert']]) + + def test_verify_set(self): + """ Test successfully verifying a Set command """ + self._verify([self.applied_commands['set']]) + + def test_verify_move(self): + """ Test successfully verifying a Move command """ + self._verify([self.applied_commands['move']]) + + def test_verify_remove(self): + """ Test successfully verifying a Remove command """ + self._verify([self.applied_commands['remove']]) + + def test_verify_clear(self): + """ Test successfully verifying a Clear command """ + self._verify([self.applied_commands['clear']]) + + def test_verify_set_multi(self): + """ Test successfully verifying a SetMulti command """ + self._verify([self.applied_commands['setm']]) + + def test_verify_all(self): + """ Test successfully verifying multiple commands """ + self._verify(self.applied_commands.values()) + + @patch("Bcfg2.Client.Tools.POSIX.Augeas.POSIXTool.install") + def _install(self, commands, expected, mock_install): + ptool = self.get_obj() + mock_install.return_value = True + + entry = lxml.etree.Element("Path", name=self.tmpfile, type="augeas", + lens="Xml") + entry.extend(commands) + + self.assertTrue(ptool.install(entry)) + mock_install.assert_called_with(ptool, entry) + self.assertXMLEqual(lxml.etree.parse(self.tmpfile).getroot(), + expected) + + def test_install_set_existing(self): + """ Test setting the value of an existing node """ + expected = copy.deepcopy(test_xdata) + expected.find("Text").text = "Changed content" + self._install([lxml.etree.Element("Set", path="Test/Text/#text", + value="Changed content")], + expected) + + def test_install_set_new(self): + """ Test setting the value of an new node """ + expected = copy.deepcopy(test_xdata) + newtext = lxml.etree.SubElement(expected, "NewText") + newtext.text = "new content" + self._install([lxml.etree.Element("Set", path="Test/NewText/#text", + value="new content")], + expected) + + def test_install_remove(self): + """ Test removing a node """ + expected = copy.deepcopy(test_xdata) + expected.remove(expected.find("Attrs")) + self._install( + [lxml.etree.Element("Remove", + path='Test/*[#attribute/foo = "foo"]')], + expected) + + def test_install_move(self): + """ Test moving a node """ + expected = copy.deepcopy(test_xdata) + foo = expected.xpath("//Foo")[0] + expected.append(foo) + self._install( + [lxml.etree.Element("Move", source='Test/Children/Foo', + destination='Test/Foo')], + expected) + + def test_install_clear(self): + """ Test clearing a node """ + # TODO: clearing a node doesn't seem to work with the XML lens + # + # % augtool -b + # augtool> set /augeas/load/Xml/incl[3] "/tmp/test.xml" + # augtool> load + # augtool> clear '/files/tmp/test.xml/Test/Text/#text' + # augtool> save + # error: Failed to execute command + # saving failed (run 'print /augeas//error' for details) + # augtool> print /augeas//error + # + # The error isn't useful. + pass + + def test_install_set_multi(self): + """ Test setting multiple nodes at once """ + expected = copy.deepcopy(test_xdata) + for thing in expected.xpath("Children[@identical='true']/Thing"): + thing.text = "same" + self._install( + [lxml.etree.Element( + "SetMulti", value="same", + base='Test/Children[#attribute/identical = "true"]', + sub="Thing/#text")], + expected) + + def test_install_insert(self): + """ Test inserting a node """ + expected = copy.deepcopy(test_xdata) + children = expected.xpath("Children[@identical='true']")[0] + thing = lxml.etree.Element("Thing") + thing.text = "three" + children.append(thing) + self._install( + [lxml.etree.Element( + "Insert", + path='Test/Children[#attribute/identical = "true"]/Thing[2]', + label="Thing", where="after"), + lxml.etree.Element( + "Set", + path='Test/Children[#attribute/identical = "true"]/Thing[3]/#text', + value="three")], + expected) -- cgit v1.2.3-1-g7c22 From a15439cbfd0cdc9687e80216ec43ae941d4b5323 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Thu, 5 Dec 2013 10:18:41 -0500 Subject: testsuite: fixed Augeas unit tests in travis-ci --- .../TestClient/TestTools/TestPOSIX/TestAugeas.py | 368 ++++++++++----------- 1 file changed, 184 insertions(+), 184 deletions(-) (limited to 'testsuite/Testsrc') diff --git a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestAugeas.py b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestAugeas.py index bfcb8a378..8dbf51a1c 100644 --- a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestAugeas.py +++ b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestAugeas.py @@ -47,187 +47,187 @@ test_data = """ test_xdata = lxml.etree.XML(test_data) -class TestPOSIXAugeas(TestPOSIXTool): - test_obj = POSIXAugeas - - applied_commands = dict( - insert=lxml.etree.Element( - "Insert", label="Thing", - path='Test/Children[#attribute/identical = "true"]/Thing'), - set=lxml.etree.Element("Set", path="Test/Text/#text", - value="content with spaces"), - move=lxml.etree.Element( - "Move", source="Test/Foo", - destination='Test/Children[#attribute/identical = "false"]/Foo'), - remove=lxml.etree.Element("Remove", path="Test/Bar"), - clear=lxml.etree.Element("Clear", path="Test/Empty/#text"), - setm=lxml.etree.Element( - "SetMulti", sub="#text", value="same", - base='Test/Children[#attribute/multi = "true"]/Thing')) - - - @skipUnless(HAS_AUGEAS, "Python Augeas libraries not found") - def setUp(self): - fd, self.tmpfile = tempfile.mkstemp() - os.fdopen(fd, 'w').write(test_data) - - def tearDown(self): - tmpfile = getattr(self, "tmpfile", None) - if tmpfile: - os.unlink(tmpfile) - - def test_fully_specified(self): - ptool = self.get_obj() - - entry = lxml.etree.Element("Path", name="/test", type="augeas") - self.assertFalse(ptool.fully_specified(entry)) - - entry.text = "text" - self.assertTrue(ptool.fully_specified(entry)) - - def test_install(self): - # this is tested adequately by the other tests - pass - - def test_verify(self): - # this is tested adequately by the other tests - pass - - @patch("Bcfg2.Client.Tools.POSIX.Augeas.POSIXTool.verify") - def _verify(self, commands, mock_verify): - ptool = self.get_obj() - mock_verify.return_value = True - - entry = lxml.etree.Element("Path", name=self.tmpfile, type="augeas", - lens="Xml") - entry.extend(commands) - - modlist = [] - self.assertTrue(ptool.verify(entry, modlist)) - mock_verify.assert_called_with(ptool, entry, modlist) - self.assertXMLEqual(lxml.etree.parse(self.tmpfile).getroot(), - test_xdata) - - def test_verify_insert(self): - """ Test successfully verifying an Insert command """ - self._verify([self.applied_commands['insert']]) - - def test_verify_set(self): - """ Test successfully verifying a Set command """ - self._verify([self.applied_commands['set']]) - - def test_verify_move(self): - """ Test successfully verifying a Move command """ - self._verify([self.applied_commands['move']]) - - def test_verify_remove(self): - """ Test successfully verifying a Remove command """ - self._verify([self.applied_commands['remove']]) - - def test_verify_clear(self): - """ Test successfully verifying a Clear command """ - self._verify([self.applied_commands['clear']]) - - def test_verify_set_multi(self): - """ Test successfully verifying a SetMulti command """ - self._verify([self.applied_commands['setm']]) - - def test_verify_all(self): - """ Test successfully verifying multiple commands """ - self._verify(self.applied_commands.values()) - - @patch("Bcfg2.Client.Tools.POSIX.Augeas.POSIXTool.install") - def _install(self, commands, expected, mock_install): - ptool = self.get_obj() - mock_install.return_value = True - - entry = lxml.etree.Element("Path", name=self.tmpfile, type="augeas", - lens="Xml") - entry.extend(commands) - - self.assertTrue(ptool.install(entry)) - mock_install.assert_called_with(ptool, entry) - self.assertXMLEqual(lxml.etree.parse(self.tmpfile).getroot(), - expected) - - def test_install_set_existing(self): - """ Test setting the value of an existing node """ - expected = copy.deepcopy(test_xdata) - expected.find("Text").text = "Changed content" - self._install([lxml.etree.Element("Set", path="Test/Text/#text", - value="Changed content")], - expected) - - def test_install_set_new(self): - """ Test setting the value of an new node """ - expected = copy.deepcopy(test_xdata) - newtext = lxml.etree.SubElement(expected, "NewText") - newtext.text = "new content" - self._install([lxml.etree.Element("Set", path="Test/NewText/#text", - value="new content")], - expected) - - def test_install_remove(self): - """ Test removing a node """ - expected = copy.deepcopy(test_xdata) - expected.remove(expected.find("Attrs")) - self._install( - [lxml.etree.Element("Remove", - path='Test/*[#attribute/foo = "foo"]')], - expected) - - def test_install_move(self): - """ Test moving a node """ - expected = copy.deepcopy(test_xdata) - foo = expected.xpath("//Foo")[0] - expected.append(foo) - self._install( - [lxml.etree.Element("Move", source='Test/Children/Foo', - destination='Test/Foo')], - expected) - - def test_install_clear(self): - """ Test clearing a node """ - # TODO: clearing a node doesn't seem to work with the XML lens - # - # % augtool -b - # augtool> set /augeas/load/Xml/incl[3] "/tmp/test.xml" - # augtool> load - # augtool> clear '/files/tmp/test.xml/Test/Text/#text' - # augtool> save - # error: Failed to execute command - # saving failed (run 'print /augeas//error' for details) - # augtool> print /augeas//error - # - # The error isn't useful. - pass - - def test_install_set_multi(self): - """ Test setting multiple nodes at once """ - expected = copy.deepcopy(test_xdata) - for thing in expected.xpath("Children[@identical='true']/Thing"): - thing.text = "same" - self._install( - [lxml.etree.Element( - "SetMulti", value="same", - base='Test/Children[#attribute/identical = "true"]', - sub="Thing/#text")], - expected) - - def test_install_insert(self): - """ Test inserting a node """ - expected = copy.deepcopy(test_xdata) - children = expected.xpath("Children[@identical='true']")[0] - thing = lxml.etree.Element("Thing") - thing.text = "three" - children.append(thing) - self._install( - [lxml.etree.Element( - "Insert", - path='Test/Children[#attribute/identical = "true"]/Thing[2]', - label="Thing", where="after"), - lxml.etree.Element( - "Set", - path='Test/Children[#attribute/identical = "true"]/Thing[3]/#text', - value="three")], - expected) +if can_skip or HAS_AUGEAS: + class TestPOSIXAugeas(TestPOSIXTool): + test_obj = POSIXAugeas + + applied_commands = dict( + insert=lxml.etree.Element( + "Insert", label="Thing", + path='Test/Children[#attribute/identical = "true"]/Thing'), + set=lxml.etree.Element("Set", path="Test/Text/#text", + value="content with spaces"), + move=lxml.etree.Element( + "Move", source="Test/Foo", + destination='Test/Children[#attribute/identical = "false"]/Foo'), + remove=lxml.etree.Element("Remove", path="Test/Bar"), + clear=lxml.etree.Element("Clear", path="Test/Empty/#text"), + setm=lxml.etree.Element( + "SetMulti", sub="#text", value="same", + base='Test/Children[#attribute/multi = "true"]/Thing')) + + @skipUnless(HAS_AUGEAS, "Python Augeas libraries not found") + def setUp(self): + fd, self.tmpfile = tempfile.mkstemp() + os.fdopen(fd, 'w').write(test_data) + + def tearDown(self): + tmpfile = getattr(self, "tmpfile", None) + if tmpfile: + os.unlink(tmpfile) + + def test_fully_specified(self): + ptool = self.get_obj() + + entry = lxml.etree.Element("Path", name="/test", type="augeas") + self.assertFalse(ptool.fully_specified(entry)) + + entry.text = "text" + self.assertTrue(ptool.fully_specified(entry)) + + def test_install(self): + # this is tested adequately by the other tests + pass + + def test_verify(self): + # this is tested adequately by the other tests + pass + + @patch("Bcfg2.Client.Tools.POSIX.Augeas.POSIXTool.verify") + def _verify(self, commands, mock_verify): + ptool = self.get_obj() + mock_verify.return_value = True + + entry = lxml.etree.Element("Path", name=self.tmpfile, + type="augeas", lens="Xml") + entry.extend(commands) + + modlist = [] + self.assertTrue(ptool.verify(entry, modlist)) + mock_verify.assert_called_with(ptool, entry, modlist) + self.assertXMLEqual(lxml.etree.parse(self.tmpfile).getroot(), + test_xdata) + + def test_verify_insert(self): + """ Test successfully verifying an Insert command """ + self._verify([self.applied_commands['insert']]) + + def test_verify_set(self): + """ Test successfully verifying a Set command """ + self._verify([self.applied_commands['set']]) + + def test_verify_move(self): + """ Test successfully verifying a Move command """ + self._verify([self.applied_commands['move']]) + + def test_verify_remove(self): + """ Test successfully verifying a Remove command """ + self._verify([self.applied_commands['remove']]) + + def test_verify_clear(self): + """ Test successfully verifying a Clear command """ + self._verify([self.applied_commands['clear']]) + + def test_verify_set_multi(self): + """ Test successfully verifying a SetMulti command """ + self._verify([self.applied_commands['setm']]) + + def test_verify_all(self): + """ Test successfully verifying multiple commands """ + self._verify(self.applied_commands.values()) + + @patch("Bcfg2.Client.Tools.POSIX.Augeas.POSIXTool.install") + def _install(self, commands, expected, mock_install): + ptool = self.get_obj() + mock_install.return_value = True + + entry = lxml.etree.Element("Path", name=self.tmpfile, + type="augeas", lens="Xml") + entry.extend(commands) + + self.assertTrue(ptool.install(entry)) + mock_install.assert_called_with(ptool, entry) + self.assertXMLEqual(lxml.etree.parse(self.tmpfile).getroot(), + expected) + + def test_install_set_existing(self): + """ Test setting the value of an existing node """ + expected = copy.deepcopy(test_xdata) + expected.find("Text").text = "Changed content" + self._install([lxml.etree.Element("Set", path="Test/Text/#text", + value="Changed content")], + expected) + + def test_install_set_new(self): + """ Test setting the value of an new node """ + expected = copy.deepcopy(test_xdata) + newtext = lxml.etree.SubElement(expected, "NewText") + newtext.text = "new content" + self._install([lxml.etree.Element("Set", path="Test/NewText/#text", + value="new content")], + expected) + + def test_install_remove(self): + """ Test removing a node """ + expected = copy.deepcopy(test_xdata) + expected.remove(expected.find("Attrs")) + self._install( + [lxml.etree.Element("Remove", + path='Test/*[#attribute/foo = "foo"]')], + expected) + + def test_install_move(self): + """ Test moving a node """ + expected = copy.deepcopy(test_xdata) + foo = expected.xpath("//Foo")[0] + expected.append(foo) + self._install( + [lxml.etree.Element("Move", source='Test/Children/Foo', + destination='Test/Foo')], + expected) + + def test_install_clear(self): + """ Test clearing a node """ + # TODO: clearing a node doesn't seem to work with the XML lens + # + # % augtool -b + # augtool> set /augeas/load/Xml/incl[3] "/tmp/test.xml" + # augtool> load + # augtool> clear '/files/tmp/test.xml/Test/Text/#text' + # augtool> save + # error: Failed to execute command + # saving failed (run 'print /augeas//error' for details) + # augtool> print /augeas//error + # + # The error isn't useful. + pass + + def test_install_set_multi(self): + """ Test setting multiple nodes at once """ + expected = copy.deepcopy(test_xdata) + for thing in expected.xpath("Children[@identical='true']/Thing"): + thing.text = "same" + self._install( + [lxml.etree.Element( + "SetMulti", value="same", + base='Test/Children[#attribute/identical = "true"]', + sub="Thing/#text")], + expected) + + def test_install_insert(self): + """ Test inserting a node """ + expected = copy.deepcopy(test_xdata) + children = expected.xpath("Children[@identical='true']")[0] + thing = lxml.etree.Element("Thing") + thing.text = "three" + children.append(thing) + self._install( + [lxml.etree.Element( + "Insert", + path='Test/Children[#attribute/identical = "true"]/Thing[2]', + label="Thing", where="after"), + lxml.etree.Element( + "Set", + path='Test/Children[#attribute/identical = "true"]/Thing[3]/#text', + value="three")], + expected) -- cgit v1.2.3-1-g7c22 From 5050cdeb3e7635b1d32d354c30c7acef5f1c9c43 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Thu, 5 Dec 2013 14:48:35 -0500 Subject: Augeas: Only install unverified commands --- .../TestClient/TestTools/TestPOSIX/TestAugeas.py | 31 +++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'testsuite/Testsrc') diff --git a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestAugeas.py b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestAugeas.py index 8dbf51a1c..9b25499fe 100644 --- a/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestAugeas.py +++ b/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestAugeas.py @@ -9,6 +9,7 @@ try: from Bcfg2.Client.Tools.POSIX.Augeas import * HAS_AUGEAS = True except ImportError: + POSIXAugeas = None HAS_AUGEAS = False # add all parent testsuite directories to sys.path to allow (most) @@ -155,7 +156,8 @@ if can_skip or HAS_AUGEAS: expected = copy.deepcopy(test_xdata) expected.find("Text").text = "Changed content" self._install([lxml.etree.Element("Set", path="Test/Text/#text", - value="Changed content")], + value="Changed content", + verified="false")], expected) def test_install_set_new(self): @@ -164,16 +166,30 @@ if can_skip or HAS_AUGEAS: newtext = lxml.etree.SubElement(expected, "NewText") newtext.text = "new content" self._install([lxml.etree.Element("Set", path="Test/NewText/#text", - value="new content")], + value="new content", + verified="false")], expected) + def test_install_only_verified(self): + """ Test that only unverified commands are installed """ + expected = copy.deepcopy(test_xdata) + newtext = lxml.etree.SubElement(expected, "NewText") + newtext.text = "new content" + self._install( + [lxml.etree.Element("Set", path="Test/NewText/#text", + value="new content", verified="false"), + lxml.etree.Element("Set", path="Test/Bogus/#text", + value="bogus", verified="true")], + expected) + def test_install_remove(self): """ Test removing a node """ expected = copy.deepcopy(test_xdata) expected.remove(expected.find("Attrs")) self._install( [lxml.etree.Element("Remove", - path='Test/*[#attribute/foo = "foo"]')], + path='Test/*[#attribute/foo = "foo"]', + verified="false")], expected) def test_install_move(self): @@ -183,7 +199,8 @@ if can_skip or HAS_AUGEAS: expected.append(foo) self._install( [lxml.etree.Element("Move", source='Test/Children/Foo', - destination='Test/Foo')], + destination='Test/Foo', + verified="false")], expected) def test_install_clear(self): @@ -211,7 +228,7 @@ if can_skip or HAS_AUGEAS: [lxml.etree.Element( "SetMulti", value="same", base='Test/Children[#attribute/identical = "true"]', - sub="Thing/#text")], + sub="Thing/#text", verified="false")], expected) def test_install_insert(self): @@ -225,9 +242,9 @@ if can_skip or HAS_AUGEAS: [lxml.etree.Element( "Insert", path='Test/Children[#attribute/identical = "true"]/Thing[2]', - label="Thing", where="after"), + label="Thing", where="after", verified="false"), lxml.etree.Element( "Set", path='Test/Children[#attribute/identical = "true"]/Thing[3]/#text', - value="three")], + value="three", verified="false")], expected) -- cgit v1.2.3-1-g7c22 From 2695e7a9af097596527edb52a722d17ea44601cc Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Sun, 8 Dec 2013 21:21:11 -0500 Subject: Cfg: let EncryptedGenerator load setup object whenever the plugin is imported Previously, if CfgEncryptedGenerator was imported before the Cfg object was instantiated, it would finalize the Bcfg2.Server.Plugins.Cfg.SETUP object with a value of None, and would be unable to access the options dict. --- .../TestServer/TestPlugins/TestCfg/TestCfgEncryptedGenerator.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'testsuite/Testsrc') diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgEncryptedGenerator.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgEncryptedGenerator.py index 71a7410da..2bfec0e2d 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgEncryptedGenerator.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgEncryptedGenerator.py @@ -1,6 +1,7 @@ import os import sys import lxml.etree +import Bcfg2.Server.Plugins.Cfg from mock import Mock, MagicMock, patch from Bcfg2.Server.Plugins.Cfg.CfgEncryptedGenerator import * from Bcfg2.Server.Plugin import PluginExecutionError @@ -47,9 +48,10 @@ if can_skip or HAS_CRYPTO: ceg = self.get_obj() ceg.handle_event(event) mock_handle_event.assert_called_with(ceg, event) - mock_decrypt.assert_called_with("encrypted", - setup=SETUP, - algorithm=mock_get_algorithm.return_value) + mock_decrypt.assert_called_with( + "encrypted", + setup=Bcfg2.Server.Plugins.Cfg.SETUP, + algorithm=mock_get_algorithm.return_value) self.assertEqual(ceg.data, "plaintext") reset() -- cgit v1.2.3-1-g7c22 From 91558c72f6905991c7fb3f24057ab9e41ecce434 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Sun, 8 Dec 2013 21:21:42 -0500 Subject: XMLSrc: Load XML in one step instead of separate read and parse --- .../Testlib/TestServer/TestPlugin/Testhelpers.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'testsuite/Testsrc') diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py index f9296ac89..ce17cb076 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py @@ -1,5 +1,4 @@ import os -import re import sys import copy import lxml.etree @@ -29,6 +28,7 @@ def tostring(el): class FakeElementTree(lxml.etree._ElementTree): xinclude = Mock() + parse = Mock class TestFunctions(Bcfg2TestCase): @@ -1132,14 +1132,14 @@ class TestXMLSrc(TestXMLFileBacked): # ensure that the node object has the necessary interface self.assertTrue(hasattr(self.test_obj.__node__, "Match")) - @patch("%s.open" % builtins) - def test_HandleEvent(self, mock_open): + @patch("lxml.etree.parse") + def test_HandleEvent(self, mock_parse): xdata = lxml.etree.Element("Test") lxml.etree.SubElement(xdata, "Path", name="path", attr="whatever") xsrc = self.get_obj("/test/foo.xml") xsrc.__node__ = Mock() - mock_open.return_value.read.return_value = tostring(xdata) + mock_parse.return_value = xdata.getroottree() if xsrc.__priority_required__: # test with no priority at all @@ -1148,20 +1148,20 @@ class TestXMLSrc(TestXMLFileBacked): # test with bogus priority xdata.set("priority", "cow") - mock_open.return_value.read.return_value = tostring(xdata) + mock_parse.return_value = xdata.getroottree() self.assertRaises(PluginExecutionError, - xsrc.HandleEvent, Mock()) + xsrc.HandleEvent, Mock()) # assign a priority to use in future tests xdata.set("priority", "10") - mock_open.return_value.read.return_value = tostring(xdata) + mock_parse.return_value = xdata.getroottree() - mock_open.reset_mock() + mock_parse.reset_mock() xsrc = self.get_obj("/test/foo.xml") xsrc.__node__ = Mock() xsrc.HandleEvent(Mock()) - mock_open.assert_called_with("/test/foo.xml") - mock_open.return_value.read.assert_any_call() + mock_parse.assert_called_with("/test/foo.xml", + parser=Bcfg2.Server.XMLParser) self.assertXMLEqual(xsrc.__node__.call_args[0][0], xdata) self.assertEqual(xsrc.__node__.call_args[0][1], dict()) self.assertEqual(xsrc.pnode, xsrc.__node__.return_value) -- cgit v1.2.3-1-g7c22