summaryrefslogtreecommitdiffstats
path: root/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestTemplateHelper.py
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2012-09-04 09:52:57 -0400
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2012-09-04 09:52:57 -0400
commitd9d4391b211c0a13cbfeadc9fa63e5bdeba9d2f6 (patch)
treecf59b7bcef389ca76f09c7f804db9d893b918e3b /testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestTemplateHelper.py
parent6697481f7bed646b4c66c54c9a46d11aa075af4a (diff)
downloadbcfg2-d9d4391b211c0a13cbfeadc9fa63e5bdeba9d2f6.tar.gz
bcfg2-d9d4391b211c0a13cbfeadc9fa63e5bdeba9d2f6.tar.bz2
bcfg2-d9d4391b211c0a13cbfeadc9fa63e5bdeba9d2f6.zip
reorganized testsuite to allow tests on stuff outside of src
Diffstat (limited to 'testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestTemplateHelper.py')
-rw-r--r--testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestTemplateHelper.py120
1 files changed, 120 insertions, 0 deletions
diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestTemplateHelper.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestTemplateHelper.py
new file mode 100644
index 000000000..556487288
--- /dev/null
+++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestTemplateHelper.py
@@ -0,0 +1,120 @@
+import os
+import sys
+import Bcfg2.Server.Plugin
+from mock import Mock, MagicMock, patch
+from Bcfg2.Server.Plugins.TemplateHelper 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 XI_NAMESPACE, XI, inPy3k, call, builtins, u, can_skip, \
+ skip, skipIf, skipUnless, Bcfg2TestCase, DBModelTestCase, syncdb, \
+ patchIf, datastore
+from TestPlugin import TestDirectoryBacked, TestConnector, TestPlugin, \
+ TestFileBacked
+
+
+class TestHelperModule(TestFileBacked):
+ test_obj = HelperModule
+ path = os.path.join(datastore, "test.py")
+
+ def test__init(self):
+ hm = self.get_obj()
+ self.assertEqual(hm._module_name, "test")
+ self.assertEqual(hm._attrs, [])
+
+ @patch("imp.load_source")
+ def test_Index(self, mock_load_source):
+ hm = self.get_obj()
+
+ mock_load_source.side_effect = ImportError
+ attrs = dir(hm)
+ hm.Index()
+ mock_load_source.assert_called_with(hm._module_name, hm.name)
+ self.assertEqual(attrs, dir(hm))
+ self.assertEqual(hm._attrs, [])
+
+ mock_load_source.reset()
+ mock_load_source.side_effect = None
+ # a regular Mock (not a MagicMock) won't automatically create
+ # __export__, so this triggers a failure condition in Index
+ mock_load_source.return_value = Mock()
+ attrs = dir(hm)
+ hm.Index()
+ mock_load_source.assert_called_with(hm._module_name, hm.name)
+ self.assertEqual(attrs, dir(hm))
+ self.assertEqual(hm._attrs, [])
+
+ # test reserved attributes
+ module = Mock()
+ module.__export__ = ["_attrs", "Index", "__init__"]
+ mock_load_source.reset()
+ mock_load_source.return_value = module
+ attrs = dir(hm)
+ hm.Index()
+ mock_load_source.assert_called_with(hm._module_name, hm.name)
+ self.assertEqual(attrs, dir(hm))
+ self.assertEqual(hm._attrs, [])
+
+ # test adding attributes
+ module = Mock()
+ module.__export__ = ["foo", "bar", "baz", "Index"]
+ mock_load_source.reset()
+ mock_load_source.return_value = module
+ hm.Index()
+ mock_load_source.assert_called_with(hm._module_name, hm.name)
+ self.assertTrue(hasattr(hm, "foo"))
+ self.assertTrue(hasattr(hm, "bar"))
+ self.assertTrue(hasattr(hm, "baz"))
+ self.assertEqual(hm._attrs, ["foo", "bar", "baz"])
+
+ # test removing attributes
+ module = Mock()
+ module.__export__ = ["foo", "bar", "quux", "Index"]
+ mock_load_source.reset()
+ mock_load_source.return_value = module
+ hm.Index()
+ mock_load_source.assert_called_with(hm._module_name, hm.name)
+ self.assertTrue(hasattr(hm, "foo"))
+ self.assertTrue(hasattr(hm, "bar"))
+ self.assertTrue(hasattr(hm, "quux"))
+ self.assertFalse(hasattr(hm, "baz"))
+ self.assertEqual(hm._attrs, ["foo", "bar", "quux"])
+
+
+
+class TestHelperSet(TestDirectoryBacked):
+ test_obj = HelperSet
+ testfiles = ['foo.py', 'foo_bar.py', 'foo.bar.py']
+ ignore = ['fooo.py~', 'fooo.pyc', 'fooo.pyo']
+ badevents = ['foo']
+
+
+class TestTemplateHelper(TestPlugin, TestConnector):
+ test_obj = TemplateHelper
+
+ def test__init(self):
+ TestPlugin.test__init(self)
+
+ th = self.get_obj()
+ self.assertIsInstance(th.helpers, HelperSet)
+
+ def test_get_additional_data(self):
+ TestConnector.test_get_additional_data(self)
+
+ th = self.get_obj()
+ modules = ['foo', 'bar']
+ rv = dict()
+ for mname in modules:
+ module = Mock()
+ module._module_name = mname
+ rv[mname] = module
+ th.helpers.entries['%s.py' % mname] = module
+ actual = th.get_additional_data(Mock())
+ self.assertItemsEqual(actual, rv)