From da0918e71e82c407e1dc7f5cb80f33cad93263af Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Tue, 13 Aug 2013 09:31:49 -0400 Subject: testsuite: fixed unit tests for new SSLCA stuff --- .../Testlib/TestServer/TestPlugin/Testhelpers.py | 14 ++-- .../TestCfg/TestCfgAuthorizedKeysGenerator.py | 6 +- .../TestCfg/TestCfgPrivateKeyCreator.py | 12 ++-- .../TestServer/TestPlugins/TestCfg/Test_init.py | 76 +++++++++------------- 4 files changed, 45 insertions(+), 63 deletions(-) (limited to 'testsuite') diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py index 7515b5e97..b2f2e4e11 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testhelpers.py @@ -24,9 +24,8 @@ from TestServer.TestPlugin.Testinterfaces import TestGenerator try: from Bcfg2.Server.Encryption import EVPError - HAS_CRYPTO = True except: - HAS_CRYPTO = False + pass def tostring(el): @@ -625,6 +624,10 @@ class TestXMLFileBacked(TestFileBacked): class TestStructFile(TestXMLFileBacked): test_obj = StructFile + def setUp(self): + TestXMLFileBacked.setUp(self) + set_setup_default("lax_decryption", False) + def _get_test_data(self): """ build a very complex set of test data """ # top-level group and client elements @@ -707,11 +710,10 @@ class TestStructFile(TestXMLFileBacked): @patch("genshi.template.TemplateLoader") def test_Index(self, mock_TemplateLoader): - has_crypto = Bcfg2.Server.Plugin.helpers.HAS_CRYPTO - Bcfg2.Server.Plugin.helpers.HAS_CRYPTO = False TestXMLFileBacked.test_Index(self) sf = self.get_obj() + sf.encryption = False sf.encoding = Mock() (xdata, groups, subgroups, children, subchildren, standalone) = \ self._get_test_data() @@ -736,10 +738,10 @@ class TestStructFile(TestXMLFileBacked): self.assertEqual(sf.template, loader.load.return_value) - Bcfg2.Server.Plugin.helpers.HAS_CRYPTO = has_crypto - @skipUnless(HAS_CRYPTO, "No crypto libraries found, skipping") def test_Index_crypto(self): + if not self.test_obj.encryption: + return Bcfg2.Options.setup.lax_decryption = False sf = self.get_obj() sf._decrypt = Mock() diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgAuthorizedKeysGenerator.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgAuthorizedKeysGenerator.py index e5cef8fa2..7e96b618c 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgAuthorizedKeysGenerator.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgAuthorizedKeysGenerator.py @@ -27,12 +27,12 @@ class TestCfgAuthorizedKeysGenerator(TestCfgGenerator, TestStructFile): TestCfgGenerator.setUp(self) TestStructFile.setUp(self) - def get_obj(self, name=None, core=None, fam=None): + @patch("Bcfg2.Server.Plugins.Cfg.CfgAuthorizedKeysGenerator.get_cfg") + def get_obj(self, mock_get_cfg, name=None, core=None, fam=None): if name is None: name = self.path - Bcfg2.Server.Plugins.Cfg.CfgAuthorizedKeysGenerator.CFG = Mock() if core is not None: - Bcfg2.Server.Plugins.Cfg.CfgAuthorizedKeysGenerator.CFG.core = core + mock_get_cfg.return_value.core = core return self.test_obj(name) @patch("Bcfg2.Server.Plugins.Cfg.CfgGenerator.handle_event") diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgPrivateKeyCreator.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgPrivateKeyCreator.py index ea0853a8d..2967a23b6 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgPrivateKeyCreator.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgPrivateKeyCreator.py @@ -22,16 +22,15 @@ while path != "/": break path = os.path.dirname(path) from common import * -from TestServer.TestPlugins.TestCfg.Test_init import TestCfgCreator -from TestServer.TestPlugin.Testhelpers import TestStructFile +from TestServer.TestPlugins.TestCfg.Test_init import TestXMLCfgCreator -class TestCfgPrivateKeyCreator(TestCfgCreator, TestStructFile): +class TestCfgPrivateKeyCreator(TestXMLCfgCreator): test_obj = CfgPrivateKeyCreator should_monitor = False def get_obj(self, name=None, fam=None): - return TestCfgCreator.get_obj(self, name=name) + return TestXMLCfgCreator.get_obj(self, name=name) @patch("shutil.rmtree") @patch("tempfile.mkdtemp") @@ -93,7 +92,7 @@ class TestCfgPrivateKeyCreator(TestCfgCreator, TestStructFile): # the get_specificity() return value is being used # appropriately, we put some dummy data in it and test for # that data - pkc.get_specificity.side_effect = lambda m, s: dict(group="foo") + pkc.get_specificity.side_effect = lambda m: dict(group="foo") pkc._gen_keypair = Mock() privkey = os.path.join(datastore, "privkey") pkc._gen_keypair.return_value = privkey @@ -137,8 +136,7 @@ class TestCfgPrivateKeyCreator(TestCfgCreator, TestStructFile): ("ssh-rsa publickey pubkey.filename\n", "privatekey")) pkc.XMLMatch.assert_called_with(metadata) - pkc.get_specificity.assert_called_with(metadata, - pkc.XMLMatch.return_value) + pkc.get_specificity.assert_called_with(metadata) pkc._gen_keypair.assert_called_with(metadata, pkc.XMLMatch.return_value) self.assertItemsEqual(mock_open.call_args_list, diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/Test_init.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/Test_init.py index 170a31c3f..307461918 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/Test_init.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/Test_init.py @@ -177,6 +177,7 @@ class TestCfgCreator(TestCfgBaseFileMatcher): def setUp(self): TestCfgBaseFileMatcher.setUp(self) set_setup_default("filemonitor", MagicMock()) + set_setup_default("cfg_passphrase", None) def get_obj(self, name=None): if name is None: @@ -249,6 +250,10 @@ class TestCfgCreator(TestCfgBaseFileMatcher): class TestXMLCfgCreator(TestCfgCreator, TestStructFile): test_obj = XMLCfgCreator + def setUp(self): + TestCfgCreator.setUp(self) + TestStructFile.setUp(self) + @patch("Bcfg2.Server.Plugins.Cfg.CfgCreator.handle_event") @patch("Bcfg2.Server.Plugin.helpers.StructFile.HandleEvent") def test_handle_event(self, mock_HandleEvent, mock_handle_event): @@ -310,36 +315,6 @@ class TestXMLCfgCreator(TestCfgCreator, TestStructFile): inner2() - def _test_cfg_property(self, name): - """ generic test function to test both category and passphrase - properties """ - cc = self.get_obj() - cc.setup = Mock() - cc.setup.cfp = ConfigParser.ConfigParser() - self.assertIsNone(getattr(cc, name)) - - cc.setup.reset_mock() - cc.setup.cfp.add_section("cfg") - cc.setup.cfp.set("cfg", name, "foo") - self.assertEqual(getattr(cc, name), "foo") - - if cc.cfg_section: - cc.setup.cfp.add_section(cc.cfg_section) - cc.setup.cfp.set(cc.cfg_section, name, "bar") - self.assertEqual(getattr(cc, name), "bar") - - def test_category(self): - self._test_cfg_property("category") - - @patchIf(HAS_CRYPTO, "Bcfg2.Server.Encryption.get_passphrases") - def test_passphrase(self, mock_get_passphrases): - cc = self.get_obj() - if HAS_CRYPTO and cc.encryptable: - mock_get_passphrases.return_value = dict(foo="foo", bar="bar") - self._test_cfg_property("passphrase") - else: - self.assertIsNone(getattr(cc, name)) - class TestCfgDefaultInfo(TestCfgInfo): test_obj = CfgDefaultInfo @@ -372,6 +347,7 @@ class TestCfgEntrySet(TestEntrySet): def setUp(self): TestEntrySet.setUp(self) set_setup_default("cfg_validation", False) + set_setup_default("cfg_handlers", []) def test__init(self): pass @@ -379,14 +355,14 @@ class TestCfgEntrySet(TestEntrySet): def test_handle_event(self): eset = self.get_obj() eset.entry_init = Mock() - eset._handlers = [Mock(), Mock(), Mock()] - for hdlr in eset._handlers: + Bcfg2.Options.setup.cfg_handlers = [Mock(), Mock(), Mock()] + for hdlr in Bcfg2.Options.setup.cfg_handlers: hdlr.__name__ = "handler" eset.entries = dict() def reset(): eset.entry_init.reset_mock() - for hdlr in eset._handlers: + for hdlr in Bcfg2.Options.setup.cfg_handlers: hdlr.reset_mock() # test that a bogus deleted event is discarded @@ -396,18 +372,19 @@ class TestCfgEntrySet(TestEntrySet): eset.handle_event(evt) self.assertFalse(eset.entry_init.called) self.assertItemsEqual(eset.entries, dict()) - for hdlr in eset._handlers: + for hdlr in Bcfg2.Options.setup.cfg_handlers: self.assertFalse(hdlr.handles.called) self.assertFalse(hdlr.ignore.called) # test creation of a new file for action in ["exists", "created", "changed"]: + print("Testing handling of %s events" % action) evt = Mock() evt.code2str.return_value = action evt.filename = os.path.join(datastore, "test.txt") # test with no handler that handles - for hdlr in eset._handlers: + for hdlr in Bcfg2.Options.setup.cfg_handlers: hdlr.handles.return_value = False hdlr.ignore.return_value = False @@ -415,16 +392,16 @@ class TestCfgEntrySet(TestEntrySet): eset.handle_event(evt) self.assertFalse(eset.entry_init.called) self.assertItemsEqual(eset.entries, dict()) - for hdlr in eset._handlers: + for hdlr in Bcfg2.Options.setup.cfg_handlers: hdlr.handles.assert_called_with(evt, basename=eset.path) hdlr.ignore.assert_called_with(evt, basename=eset.path) # test with a handler that handles the entry reset() - eset._handlers[-1].handles.return_value = True + Bcfg2.Options.setup.cfg_handlers[-1].handles.return_value = True eset.handle_event(evt) - eset.entry_init.assert_called_with(evt, eset._handlers[-1]) - for hdlr in eset._handlers: + eset.entry_init.assert_called_with(evt, Bcfg2.Options.setup.cfg_handlers[-1]) + for hdlr in Bcfg2.Options.setup.cfg_handlers: hdlr.handles.assert_called_with(evt, basename=eset.path) if not hdlr.return_value: hdlr.ignore.assert_called_with(evt, basename=eset.path) @@ -432,14 +409,14 @@ class TestCfgEntrySet(TestEntrySet): # test with a handler that ignores the entry before one # that handles it reset() - eset._handlers[0].ignore.return_value = True + Bcfg2.Options.setup.cfg_handlers[0].ignore.return_value = True eset.handle_event(evt) self.assertFalse(eset.entry_init.called) - eset._handlers[0].handles.assert_called_with(evt, - basename=eset.path) - eset._handlers[0].ignore.assert_called_with(evt, - basename=eset.path) - for hdlr in eset._handlers[1:]: + Bcfg2.Options.setup.cfg_handlers[0].handles.assert_called_with( + evt, basename=eset.path) + Bcfg2.Options.setup.cfg_handlers[0].ignore.assert_called_with( + evt, basename=eset.path) + for hdlr in Bcfg2.Options.setup.cfg_handlers[1:]: self.assertFalse(hdlr.handles.called) self.assertFalse(hdlr.ignore.called) @@ -451,7 +428,7 @@ class TestCfgEntrySet(TestEntrySet): eset.entries[evt.filename] = Mock() eset.handle_event(evt) self.assertFalse(eset.entry_init.called) - for hdlr in eset._handlers: + for hdlr in Bcfg2.Options.setup.cfg_handlers: self.assertFalse(hdlr.handles.called) self.assertFalse(hdlr.ignore.called) eset.entries[evt.filename].handle_event.assert_called_with(evt) @@ -461,7 +438,7 @@ class TestCfgEntrySet(TestEntrySet): evt.code2str.return_value = "deleted" eset.handle_event(evt) self.assertFalse(eset.entry_init.called) - for hdlr in eset._handlers: + for hdlr in Bcfg2.Options.setup.cfg_handlers: self.assertFalse(hdlr.handles.called) self.assertFalse(hdlr.ignore.called) self.assertItemsEqual(eset.entries, dict()) @@ -826,6 +803,11 @@ class TestCfgEntrySet(TestEntrySet): class TestCfg(TestGroupSpool, TestPullTarget): test_obj = Cfg + def setUp(self): + TestGroupSpool.setUp(self) + TestPullTarget.setUp(self) + set_setup_default("cfg_handlers", []) + def get_obj(self, core=None): if core is None: core = Mock() -- cgit v1.2.3-1-g7c22