diff options
-rw-r--r-- | testsuite/TestFrame.py | 29 | ||||
-rw-r--r-- | testsuite/TestOptions.py | 22 | ||||
-rw-r--r-- | testsuite/TestPlugin.py | 54 |
3 files changed, 62 insertions, 43 deletions
diff --git a/testsuite/TestFrame.py b/testsuite/TestFrame.py index 679e3ce27..a76c97eec 100644 --- a/testsuite/TestFrame.py +++ b/testsuite/TestFrame.py @@ -1,17 +1,21 @@ import lxml.etree -import Bcfg2.Client.Frame, Bcfg2.Client.Tools +import Bcfg2.Client.Frame +import Bcfg2.Client.Tools c1 = lxml.etree.XML("<Configuration><Bundle name='foo'><Configfile name='/tmp/test12' owner='root' group='root' empty='true' perms='644'/></Bundle></Configuration>") c2 = lxml.etree.XML("<Configuration><Bundle name='foo'><Configfile name='/tmp/test12' owner='root' group='root' empty='true' perms='644'/><Configfile name='/tmp/test12' owner='root' group='root' empty='true' perms='644'/></Bundle></Configuration>") + class DriverInitFail(object): def __init__(self, *args): raise Bcfg2.Client.Tools.toolInstantiationError + class DriverInventoryFail(object): __name__ = 'dif' + def __init__(self, logger, setup, config): self.config = config self.handled = [] @@ -49,40 +53,48 @@ class TestFrame(object): assert len(frame.tools) == 0 def test__Decide_Inventory(self): - setup = {'remove':'none', 'bundle':[], 'interactive':False} + setup = {'remove': 'none', + 'bundle': [], + 'interactive': False} times = {} frame = Bcfg2.Client.Frame.Frame(c2, setup, times, [DriverInventoryFail], False) assert len(frame.tools) == 1 frame.Inventory() - assert len([x for x in frame.states.values() if x]) == 0 + assert len([x for x in list(frame.states.values()) if x]) == 0 frame.Decide() assert len(frame.whitelist) def test__Decide_Bundle(self): - setup = {'remove':'none', 'bundle':['bar'], 'interactive':False} + setup = {'remove': 'none', + 'bundle': ['bar'], + 'interactive': False} times = {} frame = Bcfg2.Client.Frame.Frame(c2, setup, times, [DriverInventoryFail], False) assert len(frame.tools) == 1 frame.Inventory() - assert len([x for x in frame.states.values() if x]) == 0 + assert len([x for x in list(frame.states.values()) if x]) == 0 frame.Decide() assert len(frame.whitelist) == 0 def test__Decide_Dryrun(self): - setup = {'remove':'none', 'bundle':[], 'interactive':False} + setup = {'remove': 'none', + 'bundle': [], + 'interactive': False} times = {} frame = Bcfg2.Client.Frame.Frame(c2, setup, times, [DriverInventoryFail], True) assert len(frame.tools) == 1 frame.Inventory() - assert len([x for x in frame.states.values() if x]) == 0 + assert len([x for x in list(frame.states.values()) if x]) == 0 frame.Decide() assert len(frame.whitelist) == 0 def test__GenerateStats(self): - setup = {'remove':'none', 'bundle':[], 'interactive':False} + setup = {'remove': 'none', + 'bundle': [], + 'interactive': False} times = {} frame = Bcfg2.Client.Frame.Frame(c2, setup, times, [DriverInventoryFail], False) @@ -90,4 +102,3 @@ class TestFrame(object): frame.Decide() stats = frame.GenerateStats() assert len(stats.findall('.//Bad')[0].getchildren()) != 0 - diff --git a/testsuite/TestOptions.py b/testsuite/TestOptions.py index e7d2aeff0..735455d45 100644 --- a/testsuite/TestOptions.py +++ b/testsuite/TestOptions.py @@ -1,6 +1,9 @@ -import os, sys +import os +import sys + import Bcfg2.Options + class TestOption(object): def test__init(self): o = Bcfg2.Options.Option('foo', False, cmd='-F') @@ -22,11 +25,11 @@ class TestOption(object): assert o.value == 'test3' del os.environ['TEST2'] o.parse([], []) - print o.value + print(o.value) assert o.value == 'foobat' o.cf = ('communication', 'pwd') o.parse([], []) - print o.value + print(o.value) assert o.value == 'test4' o.cf = False o.parse([], []) @@ -41,11 +44,13 @@ class TestOption(object): o2.parse([('-F', '')], []) assert o2.value == True + class TestOptionSet(object): def test_buildGetopt(self): opts = [('foo', Bcfg2.Options.Option('foo', 'test1', cmd='-G')), ('bar', Bcfg2.Options.Option('foo', 'test2')), - ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H', odesc='1'))] + ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H', + odesc='1'))] os = Bcfg2.Options.OptionSet(opts) res = os.buildGetopt() assert 'H:' in res and 'G' in res and len(res) == 3 @@ -57,13 +62,14 @@ class TestOptionSet(object): odesc='1', long_arg=True))] os = Bcfg2.Options.OptionSet(opts) res = os.buildLongGetopt() - print res + print(res) assert 'H=' in res and len(res) == 1 def test_parse(self): opts = [('foo', Bcfg2.Options.Option('foo', 'test1', cmd='-G')), ('bar', Bcfg2.Options.Option('foo', 'test2')), - ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H', odesc='1'))] + ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H', + odesc='1'))] os = Bcfg2.Options.OptionSet(opts) try: os.parse(['-G', '-H']) @@ -80,11 +86,13 @@ class TestOptionSet(object): os3.parse(['-G']) assert os3['foo'] == True + class TestOptionParser(object): def test__init(self): opts = [('foo', Bcfg2.Options.Option('foo', 'test1', cmd='-h')), ('bar', Bcfg2.Options.Option('foo', 'test2')), - ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H', odesc='1'))] + ('baz', Bcfg2.Options.Option('foo', 'test1', cmd='-H', + odesc='1'))] os1 = Bcfg2.Options.OptionParser(opts) assert Bcfg2.Options.Option.cfpath == '/etc/bcfg2.conf' sys.argv = ['foo', '-C', '/usr/local/etc/bcfg2.conf'] diff --git a/testsuite/TestPlugin.py b/testsuite/TestPlugin.py index a6affbec2..aa619249a 100644 --- a/testsuite/TestPlugin.py +++ b/testsuite/TestPlugin.py @@ -1,6 +1,11 @@ -import os, Bcfg2.Server.Core, gamin, lxml.etree +import gamin +import lxml.etree +import os + +import Bcfg2.Server.Core from Bcfg2.Server.Plugin import EntrySet + class es_testtype(object): def __init__(self, name, properties, specific): self.name = name @@ -11,27 +16,30 @@ class es_testtype(object): def handle_event(self, event): self.handled += 1 - + def bind_entry(self, entry, metadata): entry.set('bound', '1') entry.set('name', self.name) self.built += 1 - + + class metadata(object): def __init__(self, hostname): self.hostname = hostname self.groups = ['base', 'debian'] - + #FIXME add test_specific + class test_entry_set(object): def __init__(self): self.dirname = '/tmp/estest-%d' % os.getpid() - os.path.isdir(self.dirname) or os.mkdir(self.dirname) + os.path.isdir(self.dirname) or os.mkdir(self.dirname) self.metadata = metadata('testhost') self.es = EntrySet('template', self.dirname, None, es_testtype) self.e = Bcfg2.Server.Core.GaminEvent(1, 'template', gamin.GAMExists) + def test_init(self): es = self.es e = self.e @@ -39,27 +47,26 @@ class test_entry_set(object): es.handle_event(e) es.handle_event(e) assert len(es.entries) == 1 - assert es.entries.values()[0].handled == 2 + assert list(es.entries.values())[0].handled == 2 e.action = 'changed' es.handle_event(e) - assert es.entries.values()[0].handled == 3 - - - def test_info(self): + assert list(es.entries.values())[0].handled == 3 + + def test_info(self): """Test info and info.xml handling.""" es = self.es - e = self.e + e = self.e dirname = self.dirname metadata = self.metadata - - # test 'info' handling + + # test 'info' handling assert es.metadata['group'] == 'root' self.mk_info(dirname) e.filename = 'info' e.action = 'exists' es.handle_event(e) assert es.metadata['group'] == 'sys' - e.action = 'deleted' + e.action = 'deleted' es.handle_event(e) assert es.metadata['group'] == 'root' @@ -73,17 +80,15 @@ class test_entry_set(object): e.action = 'deleted' es.handle_event(e) assert es.infoxml == None - def test_file_building(self): """Test file building.""" self.test_init() ent = lxml.etree.Element('foo') self.es.bind_entry(ent, self.metadata) - print self.es.entries.values()[0] - assert self.es.entries.values()[0].built == 1 - - + print(list(self.es.entries.values())[0]) + assert list(self.es.entries.values())[0].built == 1 + def test_host_specific_file_building(self): """Add a host-specific template and build it.""" self.e.filename = 'template.H_%s' % self.metadata.hostname @@ -93,9 +98,7 @@ class test_entry_set(object): ent = lxml.etree.Element('foo') self.es.bind_entry(ent, self.metadata) # FIXME need to test that it built the _right_ file here - - - + def test_deletion(self): """Test deletion of files.""" self.test_init() @@ -103,9 +106,9 @@ class test_entry_set(object): self.e.action = 'deleted' self.es.handle_event(self.e) assert len(self.es.entries) == 0 - + # TODO - how to clean up the temp dir & files after tests done? - + def mk_info(self, dir): i = open("%s/info" % dir, 'w') i.write('owner: root\n') @@ -117,6 +120,3 @@ class test_entry_set(object): i = open("%s/info.xml" % dir, 'w') i.write('<FileInfo><Info owner="root" group="other" perms="0600" /></FileInfo>\n') i.close - - - |