From 4462816a4a2c26ef7fc94f51b6485feb1ab44c27 Mon Sep 17 00:00:00 2001 From: Gordon Messmer Date: Thu, 11 Sep 2014 11:22:03 -0700 Subject: First pass at Jinja2 support for Cfg. --- .../TestCfg/TestCfgEncryptedJinja2Generator.py | 47 ++++++++++++++++++++++ .../TestPlugins/TestCfg/TestCfgJinja2Generator.py | 44 ++++++++++++++++++++ testsuite/Testsrc/test_code_checks.py | 3 ++ testsuite/install.sh | 2 +- 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgEncryptedJinja2Generator.py create mode 100644 testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgJinja2Generator.py (limited to 'testsuite') diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgEncryptedJinja2Generator.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgEncryptedJinja2Generator.py new file mode 100644 index 000000000..281ecb2e7 --- /dev/null +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgEncryptedJinja2Generator.py @@ -0,0 +1,47 @@ +import os +import sys +from Bcfg2.Server.Plugins.Cfg.CfgEncryptedJinja2Generator import * + +# 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 common import * + +try: + from TestServer.TestPlugins.TestCfg.TestCfgJinja2Generator import \ + TestCfgJinja2Generator + from Bcfg2.Server.Plugins.Cfg.CfgJinja2Generator import HAS_JINJA2 +except ImportError: + TestCfgJinja2Generator = object + HAS_JINJA2 = False + +try: + from TestServer.TestPlugins.TestCfg.TestCfgEncryptedGenerator import \ + TestCfgEncryptedGenerator + from Bcfg2.Server.Plugins.Cfg.CfgEncryptedGenerator import HAS_CRYPTO +except ImportError: + TestCfgEncryptedGenerator = object + HAS_CRYPTO = False + + +if can_skip or (HAS_CRYPTO and HAS_JINJA2): + class TestCfgEncryptedJinja2Generator(TestCfgJinja2Generator, + TestCfgEncryptedGenerator): + test_obj = CfgEncryptedJinja2Generator + + @skipUnless(HAS_CRYPTO, "Encryption libraries not found, skipping") + @skipUnless(HAS_JINJA2, "Jinja2 libraries not found, skipping") + def setUp(self): + pass + + def test_handle_event(self): + TestCfgEncryptedGenerator.test_handle_event(self) + + def test_get_data(self): + TestCfgJinja2Generator.test_get_data(self) diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgJinja2Generator.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgJinja2Generator.py new file mode 100644 index 000000000..333d6b978 --- /dev/null +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgJinja2Generator.py @@ -0,0 +1,44 @@ +import os +import sys +import lxml.etree +from mock import Mock, MagicMock, patch +from Bcfg2.Server.Plugins.Cfg.CfgJinja2Generator import * + +# 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 common import * +from TestServer.TestPlugins.TestCfg.Test_init import TestCfgGenerator + + +if HAS_JINJA2 or can_skip: + class TestCfgJinja2Generator(TestCfgGenerator): + test_obj = CfgJinja2Generator + + @skipUnless(HAS_JINJA2, "Jinja2 libraries not found, skipping") + def setUp(self): + pass + + @patch("Bcfg2.Server.Plugins.Cfg.CfgJinja2Generator.Template") + def test_get_data(self, mock_Template): + ccg = self.get_obj(encoding='UTF-8') + ccg.data = "data" + entry = lxml.etree.Element("Path", name="/test.txt") + metadata = Mock() + Bcfg2.Server.Plugins.Cfg.CfgJinja2Generator.SETUP = MagicMock() + + self.assertEqual(ccg.get_data(entry, metadata), + mock_Template.return_value.render.return_value) + Bcfg2.Server.Plugins.Cfg.CfgJinja2Generator.SETUP.__getitem__.assert_called_with("repo") + mock_Template.assert_called_with("data".decode(ccg.encoding)) + tmpl = mock_Template.return_value + name = entry.get("name") + tmpl.render.assert_called_with(metadata=metadata, name=name, path=name, + source_path=name, + repo=Bcfg2.Server.Plugins.Cfg.CfgJinja2Generator.SETUP.__getitem__.return_value) diff --git a/testsuite/Testsrc/test_code_checks.py b/testsuite/Testsrc/test_code_checks.py index 2b8b05926..77b170809 100644 --- a/testsuite/Testsrc/test_code_checks.py +++ b/testsuite/Testsrc/test_code_checks.py @@ -38,6 +38,7 @@ contingent_checks = { ("yum",): {"lib/Bcfg2/Client/Tools": ["YUM.py"]}, ("genshi",): {"lib/Bcfg2/Server/Plugins/Cfg": ["CfgGenshiGenerator.py"]}, ("Cheetah",): {"lib/Bcfg2/Server/Plugins/Cfg": ["CfgCheetahGenerator.py"]}, + ("jinja2",): {"lib/Bcfg2/Server/Plugins/Cfg": ["CfgJinja2Generator.py"]}, ("M2Crypto",): {"lib/Bcfg2": ["Encryption.py"], "lib/Bcfg2/Server/Plugins/Cfg": ["CfgEncryptedGenerator.py"]}, @@ -45,6 +46,8 @@ contingent_checks = { ["CfgEncryptedGenshiGenerator.py"]}, ("M2Crypto", "Cheetah"): {"lib/Bcfg2/Server/Plugins/Cfg": ["CfgEncryptedCheetahGenerator.py"]}, + ("M2Crypto", "jinja2"): {"lib/Bcfg2/Server/Plugins/Cfg": + ["CfgEncryptedJinja2Generator.py"]}, } # perform only error checking on the listed files diff --git a/testsuite/install.sh b/testsuite/install.sh index 50b91a4d2..6f36d4bef 100755 --- a/testsuite/install.sh +++ b/testsuite/install.sh @@ -11,7 +11,7 @@ if [[ ${PYVER:0:1} == "2" && $PYVER != "2.7" ]]; then fi if [[ "$WITH_OPTIONAL_DEPS" == "yes" ]]; then - pip install --use-mirrors PyYAML pyinotify boto pylibacl 'django<1.5' + pip install --use-mirrors PyYAML pyinotify boto pylibacl 'django<1.5' Jinja2 easy_install https://fedorahosted.org/released/python-augeas/python-augeas-0.4.1.tar.gz if [[ ${PYVER:0:1} == "2" ]]; then # django supports py3k, but South doesn't, and the django bits -- cgit v1.2.3-1-g7c22