From f2b0737249281213c0f1df18510eeb1da1cbba38 Mon Sep 17 00:00:00 2001 From: "Rafael.Timmerberg" Date: Fri, 28 Jun 2013 01:01:41 +0200 Subject: [PATCH] test --- test/PingTest.py | 10 +++++++ test/__init__.py | 1 + test/lib/__init__.py | 1 + test/lib/dummy.py | 9 ++++++ test/parsertest.py | 69 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 test/PingTest.py create mode 100644 test/__init__.py create mode 100644 test/lib/__init__.py create mode 100644 test/lib/dummy.py create mode 100755 test/parsertest.py diff --git a/test/PingTest.py b/test/PingTest.py new file mode 100644 index 0000000..2e468f0 --- /dev/null +++ b/test/PingTest.py @@ -0,0 +1,10 @@ +__author__ = 'rafael' + + +from lib.services.ping import PingService +from lib.config.hostgroups import HostGroup +from lib.config.members import Member + +member = Member() +ps = PingService(fails={"warn": 100}) +hg = HostGroup("pingGroup", members=[]) diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..f399501 --- /dev/null +++ b/test/__init__.py @@ -0,0 +1 @@ +__author__ = 'rafael' diff --git a/test/lib/__init__.py b/test/lib/__init__.py new file mode 100644 index 0000000..f399501 --- /dev/null +++ b/test/lib/__init__.py @@ -0,0 +1 @@ +__author__ = 'rafael' diff --git a/test/lib/dummy.py b/test/lib/dummy.py new file mode 100644 index 0000000..75fc3aa --- /dev/null +++ b/test/lib/dummy.py @@ -0,0 +1,9 @@ +__author__ = 'rafael' + + +class Dummy(object): + def __init__(self, **kwargs): + self.args = kwargs + +def create(args): + return Dummy(**args) diff --git a/test/parsertest.py b/test/parsertest.py new file mode 100755 index 0000000..73d0b8a --- /dev/null +++ b/test/parsertest.py @@ -0,0 +1,69 @@ +import json +import imp +import os.path as path + +class DummyParent(object): + def __init__(self, name=None, argsToReplace=None): + self.name = name + self.argsToReplace = argsToReplace + + def get_args_to_replace(self): + return self.argsToReplace + + +def replace_with_import(objList, items_func): + """ + replaces configuration dicts with their objects by importing and creating it in the first step. + In the second step the original list of json config dicts gets replaced by the loaded objects + + :param objList: the list of objects which is iterated on + :param modPart: the folder from the module (i.e tasks, parsers) + :param items_func: function to get a pointer on the list of json-config-objects to replace. Takes one argument and + should return a list of + :param class_check: currently unsupported + """ + for obj in objList: + repl = [] + items = items_func(obj) + for clazzItem in items: + try: + + clazz = clazzItem["class"] + p = path.join("lib", clazz + ".py") + mod = imp.load_source(clazz, p) + item = mod.create(clazzItem) + items.append(item) + except ImportError, err: + print "could not import " + clazz + ": " + str(clazzItem) + "! reason" + print str(err) + except KeyError, k: + print "Key " + str(k) + " not in classItem " + str(clazzItem) + except Exception, e: + print "Error while replacing class ( " + clazz + " ):" + str(e) + + del items[:] + items.extend(repl) + +jsonFile = ''' +{ + "someDummy": { + "argsToReplace": + [ + {"class": "dummy", "args": {"someargs":"ok"}}, + {"class": "dummy", "args": {"someother":"blah"}} + ] + } +} +''' + +jsonDict = json.loads(jsonFile) + +dummys=[] +for key, val in jsonDict.items(): + dummys.append(DummyParent(key, **val)) + +items_func = lambda dummy: dummy.get_args_to_replace() +replace_with_import(dummys, items_func) + + +