test
This commit is contained in:
parent
a1ca7da5ad
commit
f2b0737249
5 changed files with 90 additions and 0 deletions
10
test/PingTest.py
Normal file
10
test/PingTest.py
Normal file
|
|
@ -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=[])
|
||||
1
test/__init__.py
Normal file
1
test/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
__author__ = 'rafael'
|
||||
1
test/lib/__init__.py
Normal file
1
test/lib/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
__author__ = 'rafael'
|
||||
9
test/lib/dummy.py
Normal file
9
test/lib/dummy.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
__author__ = 'rafael'
|
||||
|
||||
|
||||
class Dummy(object):
|
||||
def __init__(self, **kwargs):
|
||||
self.args = kwargs
|
||||
|
||||
def create(args):
|
||||
return Dummy(**args)
|
||||
69
test/parsertest.py
Executable file
69
test/parsertest.py
Executable file
|
|
@ -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)
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue