From 6851a2931869aa9d9181b7b2d95f048aa5415a23 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 22 Jul 2015 18:39:32 +0200 Subject: Rules: New options replace_name to replace %{name} in attributes If you use the regex feature of Rules/Defaults you may need the real name of the matched entry in an attribute (for example home of POSIXUser). You can now enable replace_name for rules or defaults and %{name} will be replaces in the attribues of the Element before adding them to the target entry. This allows you to write something like that in Defaults to assing a default home directory somewhere else to all users with unset home: --- .../Testlib/TestServer/TestPlugins/TestDefaults.py | 9 +++++++++ .../Testlib/TestServer/TestPlugins/TestRules.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) (limited to 'testsuite') diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestDefaults.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestDefaults.py index 9b4a6af88..3c660099e 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestDefaults.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestDefaults.py @@ -22,6 +22,10 @@ from Testinterfaces import TestGoalValidator class TestDefaults(TestRules, TestGoalValidator): test_obj = Defaults + def setUp(self): + TestRules.setUp(self) + set_setup_default("defaults_replace_name", True) + def get_obj(self, *args, **kwargs): return TestRules.get_obj(self, *args, **kwargs) @@ -91,3 +95,8 @@ class TestDefaults(TestRules, TestGoalValidator): def test_regex(self): self._do_test('regex') + + def test_replace_name(self): + Bcfg2.Options.setup.defaults_replace_name = True + self._do_test('replace_name') + Bcfg2.Options.setup.defaults_replace_name = False diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestRules.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestRules.py index 45f3671e8..88b334edf 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestRules.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestRules.py @@ -32,6 +32,7 @@ class TestRules(TestPrioDir): group=lxml.etree.Element("SEPort", name="6789/tcp"), children=lxml.etree.Element("Path", name="/etc/child-entries"), regex=lxml.etree.Element("Package", name="regex"), + replace_name=lxml.etree.Element("POSIXUser", name="regex"), slash=lxml.etree.Element("Path", name="/etc/trailing/slash"), no_slash=lxml.etree.Element("Path", name="/etc/no/trailing/slash/")) @@ -53,6 +54,8 @@ class TestRules(TestPrioDir): group="root", mode="0775"), regex=lxml.etree.Element("Package", name="regex", type="yum", version="any"), + replace_name=lxml.etree.Element("POSIXUser", name="regex", + home="/foobar%{bar}/regex"), slash=lxml.etree.Element("Path", name="/etc/trailing/slash", type="directory", owner="root", group="root", mode="0600"), @@ -70,6 +73,8 @@ class TestRules(TestPrioDir): in_file = copy.deepcopy(concrete) in_file['regex'].set("name", ".*") + in_file['replace_name'].set("home", "/foobar%{bar}/%{name}") + in_file['replace_name'].set("name", ".*") in_file['slash'].set("name", "/etc/trailing/slash/") in_file['no_slash'].set("name", "/etc/no/trailing/slash") @@ -91,6 +96,7 @@ class TestRules(TestPrioDir): rules3 = lxml.etree.Element("Rules", priority="10") rules3.append(in_file['duplicate']) rules3.append(in_file['regex']) + rules3.append(in_file['replace_name']) rules3.append(in_file['slash']) rules = {"rules1.xml": rules1, "rules2.xml": rules2, "rules3.xml": rules3} @@ -99,6 +105,7 @@ class TestRules(TestPrioDir): TestPrioDir.setUp(self) set_setup_default("lax_decryption", True) set_setup_default("rules_regex", False) + set_setup_default("rules_replace_name", False) def get_child(self, name): """ Turn one of the XML documents in `rules` into a child @@ -169,6 +176,17 @@ class TestRules(TestPrioDir): self._do_test('regex') Bcfg2.Options.setup.rules_regex = False + def test_replace_name(self): + """ Test that Rules handles replaces name in attribues with regular expressions """ + Bcfg2.Options.setup.rules_regex = False + Bcfg2.Options.setup.rules_replace_name = False + self._do_test_failure('replace_name', handles=False) + Bcfg2.Options.setup.rules_regex = True + Bcfg2.Options.setup.rules_replace_name = True + self._do_test('replace_name') + Bcfg2.Options.setup.rules_regex = False + Bcfg2.Options.setup.rules_replace_name = False + def test_slash(self): """ Test that Rules handles trailing slashes on Path entries """ self._do_test('slash') -- cgit v1.2.3-1-g7c22 From 3259a1611edf97241b3a2d1bd585185907fe4e9c Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 26 Aug 2015 19:33:00 +0200 Subject: testsuite: Fix debug messages to handle xml with a '%' inside --- testsuite/common.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'testsuite') diff --git a/testsuite/common.py b/testsuite/common.py index 4c7337e0d..bdf0c01cb 100644 --- a/testsuite/common.py +++ b/testsuite/common.py @@ -171,21 +171,22 @@ class Bcfg2TestCase(TestCase): msg = "XML trees are not equal: %s" else: msg += ": %s" - fullmsg = msg + "\nFirst: %s" % lxml.etree.tostring(el1) + \ + msg += "\n%s" + fullmsg = "First: %s" % lxml.etree.tostring(el1) + \ "\nSecond: %s" % lxml.etree.tostring(el2) - self.assertEqual(el1.tag, el2.tag, msg=fullmsg % "Tags differ") + self.assertEqual(el1.tag, el2.tag, msg=msg % ("Tags differ", fullmsg)) if el1.text is not None and el2.text is not None: self.assertEqual(el1.text.strip(), el2.text.strip(), - msg=fullmsg % "Text content differs") + msg=msg % ("Text content differs", fullmsg)) else: self.assertEqual(el1.text, el2.text, - msg=fullmsg % "Text content differs") + msg=msg % ("Text content differs", fullmsg)) self.assertItemsEqual(el1.attrib.items(), el2.attrib.items(), - msg=fullmsg % "Attributes differ") + msg=msg % ("Attributes differ", fullmsg)) self.assertEqual(len(el1.getchildren()), len(el2.getchildren()), - msg=fullmsg % "Different numbers of children") + msg=msg % ("Different numbers of children", fullmsg)) matched = [] for child1 in el1.getchildren(): for child2 in el2.xpath(child1.tag): @@ -199,10 +200,10 @@ class Bcfg2TestCase(TestCase): continue else: assert False, \ - fullmsg % ("Element %s is missing from second" % - lxml.etree.tostring(child1)) + msg % ("Element %s is missing from second" % + lxml.etree.tostring(child1), fullmsg) self.assertItemsEqual(el2.getchildren(), matched, - msg=fullmsg % "Second has extra element(s)") + msg=msg % ("Second has extra element(s)", fullmsg)) class DBModelTestCase(Bcfg2TestCase): -- cgit v1.2.3-1-g7c22