summaryrefslogtreecommitdiffstats
path: root/testsuite/Testlib
diff options
context:
space:
mode:
Diffstat (limited to 'testsuite/Testlib')
-rw-r--r--testsuite/Testlib/TestServer/TestPlugin.py178
-rw-r--r--testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py110
-rw-r--r--testsuite/Testlib/TestServer/TestPlugins/TestProbes.py15
3 files changed, 190 insertions, 113 deletions
diff --git a/testsuite/Testlib/TestServer/TestPlugin.py b/testsuite/Testlib/TestServer/TestPlugin.py
index 1d9c004d5..5c3c98502 100644
--- a/testsuite/Testlib/TestServer/TestPlugin.py
+++ b/testsuite/Testlib/TestServer/TestPlugin.py
@@ -66,14 +66,19 @@ class TestPluginExecutionError(Bcfg2TestCase):
class TestDebuggable(Bcfg2TestCase):
+ test_obj = Debuggable
+
+ def get_obj(self):
+ return self.test_obj()
+
def test__init(self):
- d = Debuggable()
+ d = self.get_obj()
self.assertIsInstance(d.logger, logging.Logger)
self.assertFalse(d.debug_flag)
- @patch("Bcfg2.Server.Plugin.Debuggable.debug_log")
+ @patch("Bcfg2.Server.Plugin.%s.debug_log" % test_obj.__name__)
def test_toggle_debug(self, mock_debug):
- d = Debuggable()
+ d = self.get_obj()
orig = d.debug_flag
d.toggle_debug()
self.assertNotEqual(orig, d.debug_flag)
@@ -88,7 +93,7 @@ class TestDebuggable(Bcfg2TestCase):
self.assertTrue(mock_debug.called)
def test_debug_log(self):
- d = Debuggable()
+ d = self.get_obj()
d.logger = Mock()
d.debug_flag = False
d.debug_log("test")
@@ -105,40 +110,50 @@ class TestDebuggable(Bcfg2TestCase):
class TestPlugin(TestDebuggable):
+ test_obj = Plugin
+
+ def get_obj(self, core=None):
+ if core is None:
+ core = Mock()
+ return self.test_obj(core, datastore)
+
def test__init(self):
core = Mock()
- p = Plugin(core, datastore)
+ p = self.get_obj(core=core)
self.assertEqual(p.data, os.path.join(datastore, p.name))
self.assertEqual(p.core, core)
self.assertIsInstance(p, Debuggable)
@patch("os.makedirs")
def test_init_repo(self, mock_makedirs):
- Plugin.init_repo(datastore)
- mock_makedirs.assert_called_with(os.path.join(datastore, Plugin.name))
+ self.test_obj.init_repo(datastore)
+ mock_makedirs.assert_called_with(os.path.join(datastore,
+ self.test_obj.name))
class TestDatabaseBacked(TestPlugin):
+ test_obj = DatabaseBacked
+
@unittest.skipUnless(has_django, "Django not found")
def test__use_db(self):
core = Mock()
core.setup.cfp.getboolean.return_value = True
- db = DatabaseBacked(core, datastore)
+ db = self.get_obj(core)
self.assertTrue(db._use_db)
core = Mock()
core.setup.cfp.getboolean.return_value = False
- db = DatabaseBacked(core, datastore)
+ db = self.get_obj(core)
self.assertFalse(db._use_db)
Bcfg2.Server.Plugin.has_django = False
core = Mock()
- db = DatabaseBacked(core, datastore)
+ db = self.get_obj(core)
self.assertFalse(db._use_db)
core = Mock()
core.setup.cfp.getboolean.return_value = True
- db = DatabaseBacked(core, datastore)
+ db = self.get_obj(core)
self.assertFalse(db._use_db)
Bcfg2.Server.Plugin.has_django = True
@@ -190,25 +205,25 @@ class TestProbing(Bcfg2TestCase):
class TestStatistics(TestPlugin):
- """ placeholder """
- pass
+ test_obj = Statistics
class TestThreadedStatistics(TestStatistics):
+ test_obj = ThreadedStatistics
data = [("foo.example.com", "<foo/>"),
("bar.example.com", "<bar/>")]
@patch("threading.Thread.start")
def test__init(self, mock_start):
core = Mock()
- ts = ThreadedStatistics(core, datastore)
+ ts = self.get_obj(core)
mock_start.assert_any_call()
@patch("__builtin__.open")
@patch("Bcfg2.Server.Plugin.ThreadedStatistics.run", Mock())
def test_save(self, mock_open):
core = Mock()
- ts = ThreadedStatistics(core, datastore)
+ ts = self.get_obj(core)
queue = Mock()
queue.empty = Mock(side_effect=Empty)
ts.work_queue = queue
@@ -253,7 +268,7 @@ class TestThreadedStatistics(TestStatistics):
def test_load(self, mock_XML, mock_open, mock_exists, mock_unlink):
core = Mock()
core.terminate.isSet.return_value = False
- ts = ThreadedStatistics(core, datastore)
+ ts = self.get_obj(core)
with patch("%s.load" % cPickle.__name__) as mock_load:
ts.work_queue = Mock()
@@ -311,7 +326,7 @@ class TestThreadedStatistics(TestStatistics):
@patch("Bcfg2.Server.Plugin.ThreadedStatistics.handle_statistic")
def test_run(self, mock_handle, mock_save, mock_load):
core = Mock()
- ts = ThreadedStatistics(core, datastore)
+ ts = self.get_obj(core)
mock_load.return_value = True
ts.work_queue = Mock()
@@ -350,7 +365,7 @@ class TestThreadedStatistics(TestStatistics):
@patch("Bcfg2.Server.Plugin.ThreadedStatistics.run", Mock())
def test_process_statistics(self):
core = Mock()
- ts = ThreadedStatistics(core, datastore)
+ ts = self.get_obj(core)
ts.work_queue = Mock()
ts.process_statistics(*self.data[0])
ts.work_queue.put_nowait.assert_called_with(self.data[0])
@@ -415,14 +430,19 @@ class TestClientRunHooks(Bcfg2TestCase):
class TestFileBacked(Bcfg2TestCase):
+ test_obj = FileBacked
+
+ def get_obj(self, path=datastore, fam=None):
+ return self.test_obj(path, fam=fam)
+
@patch("__builtin__.open")
- @patch("Bcfg2.Server.Plugin.FileBacked.Index")
- def test_HandleEvent(self, mock_Index, mock_open):
+ def test_HandleEvent(self, mock_open):
path = "/test"
- fb = FileBacked(path)
+ fb = self.get_obj(path)
+ fb.Index = Mock()
def reset():
- mock_Index.reset_mock()
+ fb.Index.reset_mock()
mock_open.reset_mock()
for evt in ["exists", "changed", "created"]:
@@ -432,14 +452,14 @@ class TestFileBacked(Bcfg2TestCase):
fb.HandleEvent(event)
mock_open.assert_called_with(path)
mock_open.return_value.read.assert_any_call()
- mock_Index.assert_any_call()
+ fb.Index.assert_any_call()
reset()
event = Mock()
event.code2str.return_value = "endExist"
fb.HandleEvent(event)
self.assertFalse(mock_open.called)
- self.assertFalse(mock_Index.called)
+ self.assertFalse(fb.Index.called)
class TestDirectoryBacked(Bcfg2TestCase):
@@ -637,28 +657,33 @@ class TestDirectoryBacked(Bcfg2TestCase):
class TestXMLFileBacked(TestFileBacked):
+ test_obj = XMLFileBacked
+
+ def get_obj(self, path=datastore, fam=None, should_monitor=False):
+ return self.test_obj(path, fam=fam, should_monitor=should_monitor)
+
def test__init(self):
fam = Mock()
fname = "/test"
- xfb = XMLFileBacked(fname)
+ xfb = self.get_obj(fname)
self.assertIsNone(xfb.fam)
- xfb = XMLFileBacked(fname, fam=fam)
+ xfb = self.get_obj(fname, fam=fam)
self.assertFalse(fam.AddMonitor.called)
fam.reset_mock()
- xfb = XMLFileBacked(fname, fam=fam, should_monitor=True)
+ xfb = self.get_obj(fname, fam=fam, should_monitor=True)
fam.AddMonitor.assert_called_with(fname, xfb)
@patch("os.path.exists")
@patch("lxml.etree.parse")
- @patch("Bcfg2.Server.Plugin.XMLFileBacked.add_monitor")
- def test_follow_xincludes(self, mock_add_monitor, mock_parse, mock_exists):
+ def test_follow_xincludes(self, mock_parse, mock_exists):
fname = "/test/test1.xml"
- xfb = XMLFileBacked(fname)
+ xfb = self.get_obj(fname)
+ xfb.add_monitor = Mock()
def reset():
- mock_add_monitor.reset_mock()
+ xfb.add_monitor.reset_mock()
mock_parse.reset_mock()
mock_exists.reset_mock()
xfb.extras = []
@@ -670,17 +695,21 @@ class TestXMLFileBacked(TestFileBacked):
# basic functionality
xdata['/test/test2.xml'] = lxml.etree.Element("Test").getroottree()
xfb._follow_xincludes(xdata=xdata['/test/test2.xml'])
- self.assertFalse(mock_add_monitor.called)
+ self.assertFalse(xfb.add_monitor.called)
- reset()
- xfb.xdata = xdata['/test/test2.xml'].getroot()
- xfb._follow_xincludes()
- self.assertFalse(mock_add_monitor.called)
- xfb.xdata = None
+ if (not hasattr(self.test_obj, "xdata") or
+ not isinstance(self.test_obj.xdata, property)):
+ # if xdata is settable, test that method of getting data
+ # to _follow_xincludes
+ reset()
+ xfb.xdata = xdata['/test/test2.xml'].getroot()
+ xfb._follow_xincludes()
+ self.assertFalse(xfb.add_monitor.called)
+ xfb.xdata = None
reset()
xfb._follow_xincludes(fname="/test/test2.xml")
- self.assertFalse(mock_add_monitor.called)
+ self.assertFalse(xfb.add_monitor.called)
# test one level of xinclude
xdata[fname] = lxml.etree.Element("Test").getroottree()
@@ -689,14 +718,14 @@ class TestXMLFileBacked(TestFileBacked):
href="/test/test2.xml")
reset()
xfb._follow_xincludes(fname=fname)
- mock_add_monitor.assert_called_with("/test/test2.xml")
+ xfb.add_monitor.assert_called_with("/test/test2.xml")
self.assertItemsEqual(mock_parse.call_args_list,
[call(f) for f in xdata.keys()])
mock_exists.assert_called_with("/test/test2.xml")
reset()
xfb._follow_xincludes(xdata=xdata[fname])
- mock_add_monitor.assert_called_with("/test/test2.xml")
+ xfb.add_monitor.assert_called_with("/test/test2.xml")
self.assertItemsEqual(mock_parse.call_args_list,
[call(f) for f in xdata.keys()
if f != fname])
@@ -728,7 +757,7 @@ class TestXMLFileBacked(TestFileBacked):
reset()
xfb._follow_xincludes(fname=fname)
- self.assertItemsEqual(mock_add_monitor.call_args_list,
+ self.assertItemsEqual(xfb.add_monitor.call_args_list,
[call(f) for f in xdata.keys() if f != fname])
self.assertItemsEqual(mock_parse.call_args_list,
[call(f) for f in xdata.keys()])
@@ -737,7 +766,7 @@ class TestXMLFileBacked(TestFileBacked):
reset()
xfb._follow_xincludes(xdata=xdata[fname])
- self.assertItemsEqual(mock_add_monitor.call_args_list,
+ self.assertItemsEqual(xfb.add_monitor.call_args_list,
[call(f) for f in xdata.keys() if f != fname])
self.assertItemsEqual(mock_parse.call_args_list,
[call(f) for f in xdata.keys() if f != fname])
@@ -745,10 +774,10 @@ class TestXMLFileBacked(TestFileBacked):
[call(f) for f in xdata.keys() if f != fname])
@patch("lxml.etree._ElementTree", FakeElementTree)
- @patch("Bcfg2.Server.Plugin.XMLFileBacked._follow_xincludes")
+ @patch("Bcfg2.Server.Plugin.%s._follow_xincludes" % test_obj.__name__)
def test_Index(self, mock_follow):
fname = "/test/test1.xml"
- xfb = XMLFileBacked(fname)
+ xfb = self.get_obj(fname)
def reset():
mock_follow.reset_mock()
@@ -805,25 +834,27 @@ class TestXMLFileBacked(TestFileBacked):
def test_add_monitor(self):
fname = "/test/test1.xml"
- xfb = XMLFileBacked(fname)
+ xfb = self.get_obj(fname)
xfb.add_monitor("/test/test2.xml")
self.assertIn("/test/test2.xml", xfb.extras)
fam = Mock()
- xfb = XMLFileBacked(fname, fam=fam)
+ xfb = self.get_obj(fname, fam=fam)
fam.reset_mock()
xfb.add_monitor("/test/test3.xml")
self.assertFalse(fam.AddMonitor.called)
self.assertIn("/test/test3.xml", xfb.extras)
fam.reset_mock()
- xfb = XMLFileBacked(fname, fam=fam, should_monitor=True)
+ xfb = self.get_obj(fname, fam=fam, should_monitor=True)
xfb.add_monitor("/test/test4.xml")
fam.AddMonitor.assert_called_with("/test/test4.xml", xfb)
self.assertIn("/test/test4.xml", xfb.extras)
class TestStructFile(TestXMLFileBacked):
+ test_obj = StructFile
+
def _get_test_data(self):
""" build a very complex set of test data """
# top-level group and client elements
@@ -894,7 +925,7 @@ class TestStructFile(TestXMLFileBacked):
return (xdata, groups, subgroups, children, subchildren, standalone)
def test_include_element(self):
- sf = StructFile("/test/test.xml")
+ sf = self.get_obj("/test/test.xml")
metadata = Mock()
metadata.groups = ["group1", "group2"]
metadata.hostname = "foo.example.com"
@@ -925,9 +956,9 @@ class TestStructFile(TestXMLFileBacked):
self.assertTrue(inc("Other"))
- @patch("Bcfg2.Server.Plugin.StructFile._include_element")
+ @patch("Bcfg2.Server.Plugin.%s._include_element" % test_obj.__name__)
def test__match(self, mock_include):
- sf = StructFile("/test/test.xml")
+ sf = self.get_obj("/test/test.xml")
metadata = Mock()
(xdata, groups, subgroups, children, subchildren, standalone) = \
@@ -953,9 +984,9 @@ class TestStructFile(TestXMLFileBacked):
for el in standalone:
self.assertXMLEqual(el, sf._match(el, metadata)[0])
- @patch("Bcfg2.Server.Plugin.StructFile._match")
+ @patch("Bcfg2.Server.Plugin.%s._match" % test_obj.__name__)
def test_Match(self, mock_match):
- sf = StructFile("/test/test.xml")
+ sf = self.get_obj("/test/test.xml")
metadata = Mock()
(xdata, groups, subgroups, children, subchildren, standalone) = \
@@ -983,9 +1014,9 @@ class TestStructFile(TestXMLFileBacked):
xexpected.extend(expected)
self.assertXMLEqual(xactual, xexpected)
- @patch("Bcfg2.Server.Plugin.StructFile._include_element")
+ @patch("Bcfg2.Server.Plugin.%s._include_element" % test_obj.__name__)
def test__xml_match(self, mock_include):
- sf = StructFile("/test/test.xml")
+ sf = self.get_obj("/test/test.xml")
metadata = Mock()
(xdata, groups, subgroups, children, subchildren, standalone) = \
@@ -1005,9 +1036,9 @@ class TestStructFile(TestXMLFileBacked):
expected.extend(standalone)
self.assertXMLEqual(actual, expected)
- @patch("Bcfg2.Server.Plugin.StructFile._xml_match")
+ @patch("Bcfg2.Server.Plugin.%s._xml_match" % test_obj.__name__)
def test_Match(self, mock_xml_match):
- sf = StructFile("/test/test.xml")
+ sf = self.get_obj("/test/test.xml")
metadata = Mock()
(sf.xdata, groups, subgroups, children, subchildren, standalone) = \
@@ -1308,7 +1339,7 @@ class TestXMLSrc(TestXMLFileBacked):
xdata = lxml.etree.Element("Test")
lxml.etree.SubElement(xdata, "Path", name="path", attr="whatever")
- xsrc = self.test_obj("/test/foo.xml")
+ xsrc = self.get_obj("/test/foo.xml")
xsrc.__node__ = Mock()
mock_open.return_value.read.return_value = lxml.etree.tostring(xdata)
@@ -1324,7 +1355,7 @@ class TestXMLSrc(TestXMLFileBacked):
mock_open.return_value.read.return_value = lxml.etree.tostring(xdata)
mock_open.reset_mock()
- xsrc = self.test_obj("/test/foo.xml")
+ xsrc = self.get_obj("/test/foo.xml")
xsrc.__node__ = Mock()
xsrc.HandleEvent(Mock())
mock_open.assert_called_with("/test/foo.xml")
@@ -1336,7 +1367,7 @@ class TestXMLSrc(TestXMLFileBacked):
@patch("Bcfg2.Server.Plugin.XMLSrc.HandleEvent")
def test_Cache(self, mock_HandleEvent):
- xsrc = self.test_obj("/test/foo.xml")
+ xsrc = self.get_obj("/test/foo.xml")
metadata = Mock()
xsrc.Cache(metadata)
mock_HandleEvent.assert_any_call()
@@ -1366,8 +1397,28 @@ class TestXMLDirectoryBacked(TestDirectoryBacked):
class TestPrioDir(TestPlugin, TestGenerator, TestXMLDirectoryBacked):
- pass
+ test_obj = PrioDir
+
+ def get_obj(self, core=None):
+ if core is None:
+ core = Mock()
+ return self.test_obj(core, datastore)
+
+ def test_HandleEvent(self):
+ pass
+
+ def test__iter(self):
+ pass
+
+ def test__getitem(self):
+ pass
+
+ def test_add_entry(self):
+ pass
+ def test_add_directory_monitor(self):
+ pass
+
class SpecificityError(Bcfg2TestCase):
""" placeholder for future tests """
@@ -1383,8 +1434,13 @@ class SpecificData(Bcfg2TestCase):
class TestEntrySet(TestDebuggable):
- pass
+ test_obj = EntrySet
+
+ def get_obj(self, basename="test", path=datastore, entry_type=SpecificData,
+ encoding=None):
+ return self.test_obj(basename, path, entry_type, encoding)
class GroupSpool(TestPlugin, TestGenerator):
+ test_obj = GroupSpool
pass
diff --git a/testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py b/testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py
index 3eb242d89..6aab04a89 100644
--- a/testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py
+++ b/testsuite/Testlib/TestServer/TestPlugins/TestMetadata.py
@@ -10,8 +10,8 @@ from ....common import *
import Bcfg2.Server
import Bcfg2.Server.Plugin
from Bcfg2.Server.Plugins.Metadata import *
-from ..TestPlugin import TestXMLFileBacked, TestMetadata, TestStatistics, \
- TestDatabaseBacked
+from ..TestPlugin import TestXMLFileBacked, TestMetadata as _TestMetadata, \
+ TestStatistics, TestDatabaseBacked
clients_test_tree = lxml.etree.XML('''
<Clients>
@@ -77,8 +77,7 @@ groups_test_tree = lxml.etree.XML('''
def get_metadata_object(core=None, watch_clients=False, use_db=False):
if core is None:
core = Mock()
- core.setup.cfp.getboolean = Mock()
- core.setup.cfp.getboolean.return_value = use_db
+ core.setup.cfp.getboolean = Mock(return_value=use_db)
return Metadata(core, datastore, watch_clients=watch_clients)
@@ -167,24 +166,29 @@ class TestClientVersions(Bcfg2TestCase):
class TestXMLMetadataConfig(TestXMLFileBacked):
+ test_obj = XMLMetadataConfig
groups_test_tree = groups_test_tree
clients_test_tree = clients_test_tree
- def get_config_object(self, basefile="clients.xml", core=None,
- watch_clients=False):
+ def get_obj(self, basefile="clients.xml", core=None, watch_clients=False):
self.metadata = get_metadata_object(core=core,
watch_clients=watch_clients)
return XMLMetadataConfig(self.metadata, watch_clients, basefile)
+ def test__init(self):
+ xmc = self.get_obj()
+ self.assertEqual(self.metadata.core.fam, xmc.fam)
+ self.assertFalse(xmc.fam.AddMonitor.called)
+
def test_xdata(self):
- config = self.get_config_object()
+ config = self.get_obj()
with self.assertRaises(Bcfg2.Server.Plugin.MetadataRuntimeError):
config.xdata
config.data = "<test/>"
self.assertEqual(config.xdata, "<test/>")
def test_base_xdata(self):
- config = self.get_config_object()
+ config = self.get_obj()
# we can't use assertRaises here because base_xdata is a property
with self.assertRaises(Bcfg2.Server.Plugin.MetadataRuntimeError):
config.base_xdata
@@ -193,7 +197,7 @@ class TestXMLMetadataConfig(TestXMLFileBacked):
def test_add_monitor(self):
core = Mock()
- config = self.get_config_object(core=core)
+ config = self.get_obj(core=core)
fname = "test.xml"
fpath = os.path.join(self.metadata.data, fname)
@@ -203,15 +207,19 @@ class TestXMLMetadataConfig(TestXMLFileBacked):
self.assertFalse(core.fam.AddMonitor.called)
self.assertEqual(config.extras, [fpath])
- config = self.get_config_object(core=core, watch_clients=True)
+ config = self.get_obj(core=core, watch_clients=True)
config.add_monitor(fpath)
core.fam.AddMonitor.assert_called_with(fpath, config.metadata)
self.assertItemsEqual(config.extras, [fpath])
+ def test_Index(self):
+ # Index() isn't used on XMLMetadataConfig objects
+ pass
+
@patch("Bcfg2.Server.Plugins.Metadata.XMLMetadataConfig.add_monitor")
@patch("lxml.etree.parse")
def test_load_xml(self, mock_parse, mock_add_monitor):
- config = self.get_config_object("clients.xml")
+ config = self.get_obj("clients.xml")
mock_parse.side_effect = lxml.etree.XMLSyntaxError(None, None, None,
None)
config.load_xml()
@@ -243,7 +251,7 @@ class TestXMLMetadataConfig(TestXMLFileBacked):
@patch("Bcfg2.Server.Plugins.Metadata.XMLMetadataConfig.write_xml")
def test_write(self, mock_write_xml):
- config = self.get_config_object("clients.xml")
+ config = self.get_obj("clients.xml")
config.basedata = "<test/>"
config.write()
mock_write_xml.assert_called_with(os.path.join(self.metadata.data,
@@ -260,7 +268,7 @@ class TestXMLMetadataConfig(TestXMLFileBacked):
def test_write_xml(self, mock_readlink, mock_islink, mock_rename,
mock_unlink, mock_open):
fname = "clients.xml"
- config = self.get_config_object(fname)
+ config = self.get_obj(fname)
fpath = os.path.join(self.metadata.data, fname)
tmpfile = "%s.new" % fpath
linkdest = os.path.join(self.metadata.data, "client-link.xml")
@@ -294,7 +302,7 @@ class TestXMLMetadataConfig(TestXMLFileBacked):
@patch("Bcfg2.Server.Plugins.Metadata.XMLMetadataConfig.load_xml", Mock())
@patch('lxml.etree.parse')
def test_find_xml_for_xpath(self, mock_parse):
- config = self.get_config_object("groups.xml")
+ config = self.get_obj("groups.xml")
config.basedata = self.groups_test_tree
xpath = "//Group[@name='group1']"
self.assertItemsEqual(config.find_xml_for_xpath(xpath),
@@ -324,7 +332,7 @@ class TestXMLMetadataConfig(TestXMLFileBacked):
@patch("Bcfg2.Server.Plugins.Metadata.XMLMetadataConfig.load_xml")
def test_HandleEvent(self, mock_load_xml):
- config = self.get_config_object("groups.xml")
+ config = self.get_obj("groups.xml")
evt = Mock()
evt.filename = os.path.join(self.metadata.data, "groups.xml")
evt.code2str = Mock(return_value="changed")
@@ -340,15 +348,24 @@ class TestClientMetadata(Bcfg2TestCase):
self.assertFalse(cm.inGroup("group3"))
-class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
+class TestMetadata(_TestMetadata, TestStatistics, TestDatabaseBacked):
+ test_obj = Metadata
groups_test_tree = groups_test_tree
clients_test_tree = clients_test_tree
use_db = False
- def get_metadata_object(self, core=None, watch_clients=False):
+ def get_obj(self, core=None, watch_clients=False):
return get_metadata_object(core=core, watch_clients=watch_clients,
use_db=self.use_db)
+ @unittest.skipUnless(has_django, "Django not found")
+ def test__use_db(self):
+ # with the way we've set up our metadata tests, it's unweildy
+ # to test _use_db. however, given the way get_obj works, if
+ # there was a bug in _use_db it'd be almost certain to shake
+ # out in the rest of the testing.
+ pass
+
def get_nonexistent_client(self, metadata, prefix="client"):
if metadata is None:
metadata = self.load_clients_data()
@@ -362,7 +379,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
def test__init(self):
# test with watch_clients=False
core = Mock()
- metadata = self.get_metadata_object(core=core)
+ metadata = self.get_obj(core=core)
self.assertIsInstance(metadata, Bcfg2.Server.Plugin.Plugin)
self.assertIsInstance(metadata, Bcfg2.Server.Plugin.Metadata)
self.assertIsInstance(metadata, Bcfg2.Server.Plugin.Statistics)
@@ -373,7 +390,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
# test with watch_clients=True
core.fam = Mock()
- metadata = self.get_metadata_object(core=core, watch_clients=True)
+ metadata = self.get_obj(core=core, watch_clients=True)
self.assertEqual(len(metadata.states), 2)
core.fam.AddMonitor.assert_any_call(os.path.join(metadata.data,
"groups.xml"),
@@ -385,8 +402,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
core.fam.reset_mock()
core.fam.AddMonitor = Mock(side_effect=IOError)
self.assertRaises(Bcfg2.Server.Plugin.PluginInitError,
- self.get_metadata_object,
- core=core, watch_clients=True)
+ self.get_obj, core=core, watch_clients=True)
@patch('os.makedirs', Mock())
@patch('__builtin__.open')
@@ -400,21 +416,21 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
def test_search_xdata(self):
# test finding a node with the proper name
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
tree = self.groups_test_tree
res = metadata._search_xdata("Group", "group1", tree)
self.assertIsInstance(res, lxml.etree._Element)
self.assertEqual(res.get("name"), "group1")
# test finding a node with the wrong name but correct alias
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
tree = self.clients_test_tree
res = metadata._search_xdata("Client", "alias3", tree, alias=True)
self.assertIsInstance(res, lxml.etree._Element)
self.assertNotEqual(res.get("name"), "alias3")
# test failure finding a node
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
tree = self.clients_test_tree
res = metadata._search_xdata("Client",
self.get_nonexistent_client(metadata),
@@ -422,7 +438,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
self.assertIsNone(res)
def search_xdata(self, tag, name, tree, alias=False):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
res = metadata._search_xdata(tag, name, tree, alias=alias)
self.assertIsInstance(res, lxml.etree._Element)
if not alias:
@@ -445,7 +461,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
self.search_xdata("Client", "alias1", tree, alias=True)
def test_add_group(self):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
metadata.groups_xml.write = Mock()
metadata.groups_xml.data = lxml.etree.XML('<Groups/>').getroottree()
metadata.groups_xml.basedata = copy.copy(metadata.groups_xml.data)
@@ -477,7 +493,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
self.assertFalse(metadata.groups_xml.write.called)
def test_update_group(self):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
metadata.groups_xml.write_xml = Mock()
metadata.groups_xml.data = copy.deepcopy(self.groups_test_tree)
metadata.groups_xml.basedata = copy.copy(metadata.groups_xml.data)
@@ -494,7 +510,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
"bogus_group", dict())
def test_remove_group(self):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
metadata.groups_xml.write_xml = Mock()
metadata.groups_xml.data = copy.deepcopy(self.groups_test_tree)
metadata.groups_xml.basedata = copy.copy(metadata.groups_xml.data)
@@ -509,7 +525,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
"bogus_group")
def test_add_bundle(self):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
metadata.groups_xml.write = Mock()
metadata.groups_xml.data = lxml.etree.XML('<Groups/>').getroottree()
metadata.groups_xml.basedata = copy.copy(metadata.groups_xml.data)
@@ -532,7 +548,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
self.assertFalse(metadata.groups_xml.write.called)
def test_remove_bundle(self):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
metadata.groups_xml.write_xml = Mock()
metadata.groups_xml.data = copy.deepcopy(self.groups_test_tree)
metadata.groups_xml.basedata = copy.copy(metadata.groups_xml.data)
@@ -547,7 +563,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
"bogus_bundle")
def test_add_client(self):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
metadata.clients_xml.write = Mock()
metadata.clients_xml.data = lxml.etree.XML('<Clients/>').getroottree()
metadata.clients_xml.basedata = copy.copy(metadata.clients_xml.data)
@@ -582,7 +598,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
self.assertFalse(metadata.clients_xml.write.called)
def test_update_client(self):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
metadata.clients_xml.write_xml = Mock()
metadata.clients_xml.data = copy.deepcopy(self.clients_test_tree)
metadata.clients_xml.basedata = copy.copy(metadata.clients_xml.data)
@@ -601,7 +617,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
def load_clients_data(self, metadata=None, xdata=None):
if metadata is None:
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
metadata.clients_xml.data = \
xdata or copy.deepcopy(self.clients_test_tree)
metadata.clients_xml.basedata = copy.copy(metadata.clients_xml.data)
@@ -613,7 +629,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
@patch("Bcfg2.Server.Plugins.Metadata.XMLMetadataConfig.load_xml")
def test_clients_xml_event(self, mock_load_xml):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
metadata.profiles = ["group1", "group2"]
self.load_clients_data(metadata=metadata)
mock_load_xml.assert_any_call()
@@ -657,7 +673,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
def load_groups_data(self, metadata=None, xdata=None):
if metadata is None:
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
metadata.groups_xml.data = xdata or copy.deepcopy(self.groups_test_tree)
metadata.groups_xml.basedata = copy.copy(metadata.groups_xml.data)
evt = Mock()
@@ -710,7 +726,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
@patch("Bcfg2.Server.Plugins.Metadata.XMLMetadataConfig.load_xml", Mock())
def test_set_profile(self):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
if 'clients.xml' in metadata.states:
metadata.states['clients.xml'] = False
self.assertRaises(Bcfg2.Server.Plugin.MetadataRuntimeError,
@@ -811,7 +827,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
@patch("Bcfg2.Server.Plugins.Metadata.XMLMetadataConfig.write_xml", Mock())
@patch("Bcfg2.Server.Plugins.Metadata.ClientMetadata")
def test_get_initial_metadata(self, mock_clientmetadata):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
if 'clients.xml' in metadata.states:
metadata.states['clients.xml'] = False
self.assertRaises(Bcfg2.Server.Plugin.MetadataRuntimeError,
@@ -886,7 +902,7 @@ class TestMetadata(TestMetadata, TestStatistics, TestDatabaseBacked):
@patch("Bcfg2.Server.Plugins.Metadata.XMLMetadataConfig.load_xml", Mock())
def test_merge_groups(self):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
self.load_groups_data(metadata=metadata)
self.load_clients_data(metadata=metadata)
@@ -1113,7 +1129,7 @@ class TestMetadataBase(TestMetadata):
def load_clients_data(self, metadata=None, xdata=None):
if metadata is None:
- metadata = get_metadata_object()
+ metadata = get_obj()
for client in clients_test_tree.findall("Client"):
metadata.add_client(client.get("name"))
return metadata
@@ -1132,7 +1148,7 @@ class TestMetadataBase(TestMetadata):
core = Mock()
core.fam = Mock()
mock_exists.return_value = False
- metadata = self.get_metadata_object(core=core, watch_clients=True)
+ metadata = self.get_obj(core=core, watch_clients=True)
self.assertIsInstance(metadata, Bcfg2.Server.Plugin.DatabaseBacked)
core.fam.AddMonitor.assert_called_once_with(os.path.join(metadata.data,
"groups.xml"),
@@ -1140,7 +1156,7 @@ class TestMetadataBase(TestMetadata):
mock_exists.return_value = True
core.fam.reset_mock()
- metadata = self.get_metadata_object(core=core, watch_clients=True)
+ metadata = self.get_obj(core=core, watch_clients=True)
core.fam.AddMonitor.assert_any_call(os.path.join(metadata.data,
"groups.xml"),
metadata)
@@ -1155,7 +1171,7 @@ class TestMetadataBase(TestMetadata):
pass
def test_add_client(self):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
hostname = self.get_nonexistent_client(metadata)
client = metadata.add_client(hostname)
self.assertIsInstance(client, MetadataClientModel)
@@ -1176,7 +1192,7 @@ class TestMetadataBase(TestMetadata):
pass
def test_list_clients(self):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
self.assertItemsEqual(metadata.list_clients(),
[c.hostname
for c in MetadataClientModel.objects.all()])
@@ -1188,7 +1204,7 @@ class TestMetadataBase(TestMetadata):
pass
def test_remove_client(self):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
client_name = self.get_nonexistent_client(metadata)
self.assertRaises(Bcfg2.Server.Plugin.MetadataConsistencyError,
@@ -1225,7 +1241,7 @@ class TestMetadata_NoClientsXML(TestMetadataBase):
@patch("Bcfg2.Server.Plugins.Metadata.XMLMetadataConfig.write_xml", Mock())
@patch("Bcfg2.Server.Plugins.Metadata.ClientMetadata")
def test_get_initial_metadata(self, mock_clientmetadata):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
if 'clients.xml' in metadata.states:
metadata.states['clients.xml'] = False
self.assertRaises(Bcfg2.Server.Plugin.MetadataRuntimeError,
@@ -1364,7 +1380,7 @@ class TestMetadata_ClientsXML(TestMetadataBase):
def load_clients_data(self, metadata=None, xdata=None):
if metadata is None:
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
metadata.core.fam = Mock()
metadata._handle_file("clients.xml")
metadata = TestMetadata.load_clients_data(self, metadata=metadata,
@@ -1377,7 +1393,7 @@ class TestMetadata_ClientsXML(TestMetadataBase):
@patch("Bcfg2.Server.Plugins.Metadata.Metadata.list_clients")
def test_clients_xml_event(self, mock_list_clients, mock_handle_event,
mock_load_xml):
- metadata = self.get_metadata_object()
+ metadata = self.get_obj()
metadata.profiles = ["group1", "group2"]
evt = Mock()
evt.filename = os.path.join(datastore, "Metadata", "clients.xml")
diff --git a/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py b/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py
index 816f9c370..f90096210 100644
--- a/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py
+++ b/testsuite/Testlib/TestServer/TestPlugins/TestProbes.py
@@ -74,19 +74,22 @@ class TestProbeData(Bcfg2TestCase):
class TestProbeSet(TestEntrySet):
- def get_probeset_object(self, fam=None):
+ test_obj = ProbeSet
+
+ def get_obj(self, path=datastore, fam=None, encoding=None,
+ plugin_name="Probes"):
if fam is None:
fam = Mock()
- return ProbeSet(datastore, fam, None, "Probes")
+ return self.test_obj(path, fam, encoding, plugin_name)
def test__init(self):
fam = Mock()
- ps = self.get_probeset_object(fam)
+ ps = self.get_obj(fam=fam)
self.assertEqual(ps.plugin_name, "Probes")
fam.AddMonitor.assert_called_with(datastore, ps)
def test_HandleEvent(self):
- ps = self.get_probeset_object()
+ ps = self.get_obj()
ps.handle_event = Mock()
# test that events on the data store itself are skipped
@@ -109,7 +112,7 @@ class TestProbeSet(TestEntrySet):
@patch("__builtin__.list", FakeList)
def test_get_probe_data(self):
- ps = self.get_probeset_object()
+ ps = self.get_obj()
# build some fairly complex test data for this. in the end,
# we want the probe data to include only the most specific
@@ -170,6 +173,8 @@ group-specific"""
class TestProbes(TestProbing, TestConnector, TestDatabaseBacked):
+ test_obj = Probes
+
def get_test_probedata(self):
test_xdata = lxml.etree.Element("test")
lxml.etree.SubElement(test_xdata, "test", foo="foo")