Browse Source

run jobs as unit-test

master
Ulrich Carmesin 3 years ago
parent
commit
803d3deef2
  1. 18
      basic/componentHandling.py
  2. 21
      basic/message.py
  3. 37
      basic/program.py
  4. 4
      components/component.py
  5. 4
      components/testa/Testa.py
  6. 3
      components/testa1/Testa1.py
  7. 3
      components/testa11/Testa11.py
  8. 3
      components/testa2/Testa2.py
  9. 3
      components/testa21/Testa21.py
  10. 3
      components/testb/Testb.py
  11. 3
      components/testb1/Testb1.py
  12. 21
      components/testexec.py
  13. 20
      test/test_job.py

18
basic/componentHandling.py

@ -70,7 +70,7 @@ class ComponentManager:
job.debug(verify, "getComponents " + compobjname) job.debug(verify, "getComponents " + compobjname)
return comps[compobjname] return comps[compobjname]
def getComponents(mainfct): def getComponents(self, mainfct):
job = basic.program.Job.getInstance() job = basic.program.Job.getInstance()
verify = -2 + job.getDebugLevel("job_tool") verify = -2 + job.getDebugLevel("job_tool")
job.debug(verify, "getComponents " + mainfct) job.debug(verify, "getComponents " + mainfct)
@ -135,8 +135,9 @@ class ComponentManager:
print(vars(c.m)) print(vars(c.m))
if readedPar is not None: if readedPar is not None:
print("createComponent 5 a " + name + " : " + str(readedPar)) print("createComponent 5 a " + name + " : " + str(readedPar))
for k in readedPar["components"][name].keys(): if name in readedPar["comps"]:
c.conf[k] = readedPar["components"][name][k] for k in readedPar["comps"][name].keys():
c.conf[k] = readedPar["comps"][name][k]
comps[name] = c comps[name] = c
self.createComponent(c, nr, suffix) self.createComponent(c, nr, suffix)
else: else:
@ -153,8 +154,11 @@ class ComponentManager:
print("createComponent 4 b " + componentName) print("createComponent 4 b " + componentName)
print(vars(c)) print(vars(c))
if readedPar is not None: if readedPar is not None:
for k in readedPar["components"][name].keys(): if name in readedPar["comps"]:
c.conf[k] = readedPar["components"][name][k] for k in readedPar["comps"][name].keys():
c.conf[k] = readedPar["comps"][name][k]
else:
print("comp fehlt "+name)
comps[name] = c comps[name] = c
self.createComponent(c, i, suffix) self.createComponent(c, i, suffix)
i = i + 1 i = i + 1
@ -195,11 +199,11 @@ def getComponentDict():
for c in comps: for c in comps:
out[comps[c].name] = {} out[comps[c].name] = {}
print("getCompDict " + comps[c].name) print("getCompDict " + comps[c].name)
for k in comps[c].confs.keys(): for k in comps[c].conf.keys():
print("getCompDict " + k) print("getCompDict " + k)
if isParameterSubnode(k): # "artifact" in k or "components" in k or "instance" in k: if isParameterSubnode(k): # "artifact" in k or "components" in k or "instance" in k:
print("getCompDict -b- " + k) print("getCompDict -b- " + k)
out[comps[c].name][k] = copy.deepcopy(comps[c].confs[k]) out[comps[c].name][k] = copy.deepcopy(comps[c].conf[k])
return out return out

21
basic/message.py

@ -84,19 +84,23 @@ class Message:
elif job.program == "unit": elif job.program == "unit":
self.logfile = self.debugfile self.logfile = self.debugfile
else: else:
basedir = job.par.basedir self.setLogdir(logTime)
basedir = basedir.replace("base", "log")
# basedir = utils.path_tool.composePath(basedir, None)
basedir = utils.path_tool.composePath(basedir, None)
os.makedirs(basedir, exist_ok=True)
logpath = basedir + "/protokoll_" + logTime + ".txt"
self.logDebug("logfile " + logpath)
self.logfile = open(logpath, "w")
self.topmessage = "" self.topmessage = ""
self.messages = [] self.messages = []
print("message initialisiert mit level " + str(self.level)) print("message initialisiert mit level " + str(self.level))
def setLogdir(self, logTime):
job = basic.program.Job.getInstance()
basedir = job.par.basedir
basedir = basedir.replace("base", "log")
# basedir = utils.path_tool.composePath(basedir, None)
basedir = utils.path_tool.composePath(basedir, None)
os.makedirs(basedir, exist_ok=True)
logpath = basedir + "/protokoll_" + logTime + ".txt"
self.logDebug("logfile " + logpath)
self.logfile = open(logpath, "w")
def initErrorTyp(self): def initErrorTyp(self):
self.CONST_ERRTYP = { self.CONST_ERRTYP = {
"fatal": "4", # wird immer in debug und log ausgegeben, setzt RC "fatal": "4", # wird immer in debug und log ausgegeben, setzt RC
@ -225,7 +229,6 @@ class Message:
def debug(self, prio, text): def debug(self, prio, text):
""" eigentliche Schreibroutine: hierin wird debug-Level beruecksichtgigt""" """ eigentliche Schreibroutine: hierin wird debug-Level beruecksichtgigt"""
if (int(prio) < int(self.level)+1): if (int(prio) < int(self.level)+1):
print ("debugfile " + str(self.debugfile))
self.debugfile.write(text + "\n") self.debugfile.write(text + "\n")
def merge(self, submsg): def merge(self, submsg):

37
basic/program.py

@ -20,42 +20,49 @@ jobdef = {
"pfilesource" : "", "pfilesource" : "",
"pfiletarget" : "envparfile", "pfiletarget" : "envparfile",
"basedir": "envbase", "basedir": "envbase",
"dirname": "envdir",
"logdir": "{job.par.envdir}/{log}/log_{time}.txt" }, "logdir": "{job.par.envdir}/{log}/log_{time}.txt" },
"check_environment": { "check_environment": {
"pardef": "", "pardef": "",
"pfilesource" : "", "pfilesource" : "",
"pfiletarget" : "envparfile", "pfiletarget" : "envparfile",
"basedir": "envbase", "basedir": "envbase",
"dirname": "envdir",
"logdir": "{job.par.envdir}/{log}/log_{time}.txt" }, "logdir": "{job.par.envdir}/{log}/log_{time}.txt" },
"init_testset": { "init_testset": {
"pardef": "tsdir,tdtyp,tdsrc,tdname", "pardef": "tsdir,tdtyp,tdsrc,tdname",
"pfilesource" : "envparfile", "pfilesource" : "envparfile",
"pfiletarget" : "tsparfile", "pfiletarget" : "tsparfile",
"basedir": "tsbase", "basedir": "tsbase",
"dirname": "tsdir",
"logdir": "{job.par.tsdir}/{log}/log_{tstime}.txt" }, "logdir": "{job.par.tsdir}/{log}/log_{tstime}.txt" },
"init_testcase": { "init_testcase": {
"pardef": "tcdir,tdtyp,tdsrc,tdname", "pardef": "tcdir,tdtyp,tdsrc,tdname",
"pfilesource" : "tsparfile", "pfilesource" : "envparfile",
"pfiletarget" : "tcparfile", "pfiletarget" : "tcparfile",
"basedir": "tcbase", "basedir": "tcbase",
"dirname": "tcdir",
"logdir": "{job.par.tcdir}/{log}/log_{tctime}.txt" }, "logdir": "{job.par.tcdir}/{log}/log_{tctime}.txt" },
"test_system": { "test_system": {
"pardef": "tcdir,tdtyp,tdsrc,tdname", "pardef": "tcdir,tdtyp,tdsrc,tdname",
"pfilesource": "tsparfile", "pfilesource": "tsparfile",
"pfiletarget": "tcparfile", "pfiletarget": "tcparfile",
"basedir": "tcbase", "basedir": "tcbase",
"dirname": "tcdir",
"logdir": "{job.par.tcdir}/{log}/log_{tctime}.txt"}, "logdir": "{job.par.tcdir}/{log}/log_{tctime}.txt"},
"finish_testcase": { "finish_testcase": {
"pardef": "tcdir,tdtyp,tdsrc,tdname", "pardef": "tcdir,tdtyp,tdsrc,tdname",
"pfilesource": "tsparfile", "pfilesource": "tsparfile",
"pfiletarget": "tcparfile", "pfiletarget": "tcparfile",
"basedir": "tcbase", "basedir": "tcbase",
"dirname": "tcdir",
"logdir": "{job.par.tcdir}/{log}/log_{tctime}.txt"}, "logdir": "{job.par.tcdir}/{log}/log_{tctime}.txt"},
"finish_testset": { "finish_testset": {
"pardef": "tcdir,tdtyp,tdsrc,tdname", "pardef": "tcdir,tdtyp,tdsrc,tdname",
"pfilesource": "tsparfile", "pfilesource": "tsparfile",
"pfiletarget": "tcparfile", "pfiletarget": "tcparfile",
"basedir": "tcbase", "basedir": "tcbase",
"dirname": "tsdir",
"logdir": "{job.par.tcdir}/{log}/log_{tctime}.txt"} "logdir": "{job.par.tcdir}/{log}/log_{tctime}.txt"}
} }
@ -96,6 +103,12 @@ class Job:
Job(program) Job(program)
return Job.__instance return Job.__instance
def setProgram(self, program):
self.program = program
self.par.program = program
self.par.setBasedir(program)
self.m.setLogdir("logTime")
def getInstance(): def getInstance():
if (Job.__instance is not None): if (Job.__instance is not None):
return Job.__instance return Job.__instance
@ -196,21 +209,18 @@ class Parameter:
def __init__ (self, program): def __init__ (self, program):
print ("# init Parameter for " + program) print ("# init Parameter for " + program)
self.program = program self.program = program
if program == "unit": self.basedir = "debugs"
self.basedir = "debugs" self.setBasedir(program)
return print (f"# Parameter initialisiert {self.program} mit basedir {self.basedir}")
elif jobdef[program]: if (program != "unit"): self.setParameter()
def setBasedir(self, program):
if jobdef[program]:
self.basedir = jobdef[program]["basedir"] self.basedir = jobdef[program]["basedir"]
elif "estcase" in program: if hasattr(self, jobdef[program]["dirname"]):
self.basedir = "tcbase" utils.path_tool.extractPath(self.basedir, getattr(self, jobdef[program]["dirname"]))
elif "estset" in program:
self.basedir = "tsbase"
elif "nviron" in program:
self.basedir = "envbase"
else: else:
self.basedir = "debugs" self.basedir = "debugs"
print (f"# Parameter initialisiert {self.program} mit basedir {self.basedir}")
self.setParameter()
def checkParameter(self): def checkParameter(self):
job = Job.getInstance() job = Job.getInstance()
@ -220,7 +230,6 @@ class Parameter:
print(p) print(p)
if len(p) > 1 and not hasattr(self, p): if len(p) > 1 and not hasattr(self, p):
job.m.setFatal("Parameter " + p + " is not set!") job.m.setFatal("Parameter " + p + " is not set!")
print (f"job initialisiert {self.program}")
def setParameter(self): def setParameter(self):
""" """

4
components/component.py

@ -95,6 +95,10 @@ class Component():
(5) the summary result is returned (5) the summary result is returned
:return: :return:
""" """
job = basic.program.Job.getInstance()
verify = job.getDebugLevel(self.name)
self.m.debug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() " + str(self.name))
pass pass
def getHitscore(self, typ, rs, tg): def getHitscore(self, typ, rs, tg):

4
components/testa/Testa.py

@ -1,7 +1,7 @@
import components.component import components.component
import components.testexec
class Testa(components.component.Component, components.testexec.Testexecuter):
class Testa(components.component.Component):
def __init__(self): def __init__(self):
print('init Testa') print('init Testa')

3
components/testa1/Testa1.py

@ -1,6 +1,7 @@
import components.component import components.component
import components.testexec
class Testa1(components.component.Component): class Testa1(components.component.Component, components.testexec.Testexecuter):
def __init__(self): def __init__(self):
print('init Testa1') print('init Testa1')

3
components/testa11/Testa11.py

@ -1,6 +1,7 @@
import components.component import components.component
import components.testexec
class Testa11(components.component.Component): class Testa11(components.component.Component, components.testexec.Testexecuter):
def __init__(self): def __init__(self):
print('init Testa1') print('init Testa1')

3
components/testa2/Testa2.py

@ -1,6 +1,7 @@
import components.component import components.component
import components.testexec
class Testa2(components.component.Component): class Testa2(components.component.Component, components.testexec.Testexecuter):
def __init__(self): def __init__(self):
print('init Testa1') print('init Testa1')

3
components/testa21/Testa21.py

@ -1,5 +1,6 @@
import components.component import components.component
import components.testexec
class Testa21(components.component.Component): class Testa21(components.component.Component, components.testexec.Testexecuter):
def __init__(self): def __init__(self):
print('init Testa1') print('init Testa1')

3
components/testb/Testb.py

@ -1,5 +1,6 @@
import components.component import components.component
import components.testexec
class Testb(components.component.Component): class Testb(components.component.Component, components.testexec.Testexecuter):
def __init__(self): def __init__(self):
print('init Testb') print('init Testb')

3
components/testb1/Testb1.py

@ -1,5 +1,6 @@
import components.component import components.component
import components.testexec
class Testb1(components.component.Component): class Testb1(components.component.Component, components.testexec.Testexecuter):
def __init__(self): def __init__(self):
print('init Testa1') print('init Testa1')

21
components/testexec.py

@ -46,8 +46,8 @@ class Testexecuter():
job = basic.program.Job.getInstance() job = basic.program.Job.getInstance()
verify = -1+job.getDebugLevel(self.name) verify = -1+job.getDebugLevel(self.name)
self.m.logInfo("--- " + str(inspect.currentframe().f_code.co_name) + "() started at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper()) self.m.logInfo("--- " + str(inspect.currentframe().f_code.co_name) + "() started at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper())
self.reset_TData(granularity)
self.m.setMsg("checkInstance for " + self.name + " is OK") self.m.setMsg("prepare_system for " + self.name +" "+ granularity + " is OK")
self.m.logInfo("--- " + str(inspect.currentframe().f_code.co_name) + "() finished at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper()) self.m.logInfo("--- " + str(inspect.currentframe().f_code.co_name) + "() finished at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper())
pass pass
@ -60,7 +60,8 @@ class Testexecuter():
""" """
job = basic.program.Job.getInstance() job = basic.program.Job.getInstance()
verify = -1+job.getDebugLevel(self.name) verify = -1+job.getDebugLevel(self.name)
self.m.logDebug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() started at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper()) self.m.debug(verify, "--- "+str(inspect.currentframe().f_code.co_name)+"() started at "
+ datetime.now().strftime("%Y%m%d_%H%M%S")+" for " + str(self.name).upper())
if "log" in self.conf["artifact"]: if "log" in self.conf["artifact"]:
self.m.logInfo("log rotate in "+ self.name) self.m.logInfo("log rotate in "+ self.name)
if "flaskdb" in self.conf["artifact"]: if "flaskdb" in self.conf["artifact"]:
@ -69,8 +70,8 @@ class Testexecuter():
self.m.logInfo("lob is deleted with flaskdb "+ self.name) self.m.logInfo("lob is deleted with flaskdb "+ self.name)
if "file" in self.conf["artifact"]: if "file" in self.conf["artifact"]:
self.m.logInfo("rm files in "+ self.name) self.m.logInfo("rm files in "+ self.name)
self.m.setMsg("checkInstance for " + self.name + " is OK") self.m.setMsg("resetInstance for " + self.name + " is OK")
self.m.logDebug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() finished at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper()) self.m.debug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() finished at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper())
def load_TData(self, granularity, testdata): def load_TData(self, granularity, testdata):
""" """
@ -82,12 +83,12 @@ class Testexecuter():
""" """
job = basic.program.Job.getInstance() job = basic.program.Job.getInstance()
verify = -1+job.getDebugLevel(self.name) verify = -1+job.getDebugLevel(self.name)
self.m.logDebug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() started at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper()) self.m.debug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() started at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper())
if "testdata" in self.conf: if "testdata" in self.conf:
if self.conf["testdata"] == "flaskdb": if self.conf["testdata"] == "flaskdb":
self.m.logInfo("insert flaskdb-content " + self.name) self.m.logInfo("insert flaskdb-content " + self.name)
self.m.setMsg("data loaded for " + self.name + " is OK") self.m.setMsg("data loaded for " + self.name + " is OK")
self.m.logDebug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() finished at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper()) self.m.debug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() finished at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper())
def read_TData(self, granularity): def read_TData(self, granularity):
""" """
@ -97,13 +98,13 @@ class Testexecuter():
""" """
job = basic.program.Job.getInstance() job = basic.program.Job.getInstance()
verify = -1+job.getDebugLevel(self.name) verify = -1+job.getDebugLevel(self.name)
self.m.logDebug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() started at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper()) self.m.debug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() started at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper())
if "flaskdb" in self.conf["artifact"]: if "flaskdb" in self.conf["artifact"]:
self.m.logInfo("select flaskdb-content "+ self.name) self.m.logInfo("select flaskdb-content "+ self.name)
if "lob" in self.conf["artifact"]: if "lob" in self.conf["artifact"]:
self.m.logInfo("check lob if is deleted with flaskdb "+ self.name) self.m.logInfo("check lob if is deleted with flaskdb "+ self.name)
self.m.setMsg("checkInstance for " + self.name + " is OK") self.m.setMsg("readInstance for " + self.name + " is OK")
self.m.logDebug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() finished at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper()) self.m.debug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() finished at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper())
def test_System(self, granularity): def test_System(self, granularity):
""" """

20
test/test_job.py

@ -2,9 +2,12 @@ import unittest
import os import os
from basic.program import Job from basic.program import Job
from basic.componentHandling import ComponentManager from basic.componentHandling import ComponentManager
import init_testcase
HOME_PATH = "/home/ulrich/6_Projekte/Programme/holtz"
PYTHON_CMD = "python"
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
def test_parameter(self): def xtest_parameter(self):
job = Job("unit") job = Job("unit")
args = { "application" : "TEST" , "environment" : "ENV01", "modus" : "unit", "loglevel" : "debug", "tool" : "job_tool"} args = { "application" : "TEST" , "environment" : "ENV01", "modus" : "unit", "loglevel" : "debug", "tool" : "job_tool"}
job.par.setParameterArgs(args) job.par.setParameterArgs(args)
@ -14,9 +17,9 @@ class MyTestCase(unittest.TestCase):
self.assertEqual(job.getDebugLevel("file_tool"), 23) self.assertEqual(job.getDebugLevel("file_tool"), 23)
self.assertEqual(job.getDebugLevel("job_tool"), 23) self.assertEqual(job.getDebugLevel("job_tool"), 23)
args = { "application" : "TEST" , "environment" : "ENV01", "modus" : "unit", "loglevel" : "debug", args = { "application" : "TEST" , "environment" : "ENV01", "modus" : "unit", "loglevel" : "debug",
"tool" : "job_tool", "tsdir": "/home/ulrich/6_Projekte/Programme/holtz/test/lauf/V0.1/startjob/2021-08-21_18-ß2-01"} "tool" : "job_tool", "tsdir": os.path.join(HOME_PATH, "test", "lauf", "V0.1", "startjob", "2021-08-21_18-ß2-01")}
job.par.setParameterArgs(args) job.par.setParameterArgs(args)
def test_components(self): def xtest_components(self):
print("# # # # tetsComponents # # # # #") print("# # # # tetsComponents # # # # #")
job = Job.resetInstance("unit") job = Job.resetInstance("unit")
args = { "application" : "TEST" , "environment" : "ENV01", "modus" : "unit", "loglevel" : "debug", "tool" : "job_tool"} args = { "application" : "TEST" , "environment" : "ENV01", "modus" : "unit", "loglevel" : "debug", "tool" : "job_tool"}
@ -26,10 +29,19 @@ class MyTestCase(unittest.TestCase):
cm.createComponents("testa", 1, "") cm.createComponents("testa", 1, "")
def test_run(self): def test_run(self):
os.system("python /home/ulrich/6_Projekte/Programme/holtz/check_environment.py -a TEST -e ENV01") # os.system("python /home/ulrich/6_Projekte/Programme/holtz/check_environment.py -a TEST -e ENV01")
# os.system("python /home/basic/6_Projekte/Programme/pythonProject/init_testset.py -a TEST -e ENV01 " # os.system("python /home/basic/6_Projekte/Programme/pythonProject/init_testset.py -a TEST -e ENV01 "
# "-ts /home/basic/6_Projekte/Programme/holtz/test/lauf/V0.1/implement_2021-08-28_23-50-51 -dt csv -ds implement -dn firstunit") # "-ts /home/basic/6_Projekte/Programme/holtz/test/lauf/V0.1/implement_2021-08-28_23-50-51 -dt csv -ds implement -dn firstunit")
#os.system(PYTHON_CMD+" "+os.path.join(HOME_PATH,"init_testcase.py")+" -a TEST -e ENV01 "
# "-tc "+os.path.join(HOME_PATH,"test","lauf","V0.1","TC0001","2021-08-28_23-50-51")+" -dt csv -ds implement -dn TC0001")
job = Job("unit")
args = { "application" : "TEST" , "environment" : "ENV01", "modus" : "unit", "loglevel" : "debug",
"tool" : "job_tool", "tcdir": os.path.join(HOME_PATH,"test","lauf","V0.1","TC0001","2021-08-28_23-50-51")}
job.par.setParameterArgs(args)
job.setProgram("init_testcase")
# os.system("python /home/basic/6_Projekte/Programme/pythonProject/init_testset.py") # os.system("python /home/basic/6_Projekte/Programme/pythonProject/init_testset.py")
init_testcase.start(job)
job.stopJob(1)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save