def test_noprobe(): """ no probe in the configuration """ global app global lDB lDB.cleanDB() dConf = {} sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf try: conf.loadFile('test_config.conf') except: return assert False, "test for no probe"
def test_template_empty(): """ try to create an empty template configuration """ global app global lDB lDB.cleanDB() dConf = { "template": [], "output": [{ "engine": "debug", "parameters": [], "active": "False" }], "probe": [{ "id": "temp01", "probename": "temp01", "template": ["T01"] }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf conf.loadFile('test_config.conf')
def test_template_syntaxerror(): """ check template syntax error trap """ global app global lDB lDB.cleanDB() dConf = { "template": [{ "name": "T01", "jobs": [{ "active": "True", "job": "health", "freq": 15, "version": 1, "data": {} }] }], "output": [{ "engine": "debug", "parameters": [], "active": "False" }], "probe": [{ "id": "temp01", "probename": "temp01", "template": ["T01"] }] } sConf = string.replace(str(dConf), "'", '"') sConf = string.replace(sConf, "],", ']') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf try: conf.loadFile('test_config.conf') except Exception as ex: return assert False, "configuration syntax error not trapped"
def test_template_noname(): """ template without name """ global app global lDB lDB.cleanDB() dConf = { "template": [{ "jobs": [{ "active": "True", "job": "health", "freq": 15, "version": 1, "data": {} }] }], "output": [{ "engine": "debug", "parameters": [], "active": "False" }], "probe": [{ "id": "temp01", "probename": "temp01", "template": ["T01"] }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf try: conf.loadFile('test_config.conf') except: return assert False, "should have a name in a template section"
def test_template_2loads(): """ load twice the conf """ global app global lDB lDB.cleanDB() dConf = { "template": [{ "name": "T01", "jobs": [{ "active": "True", "job": "health", "freq": 15, "version": 1, "data": {} }] }], "output": [{ "engine": "debug", "parameters": [], "active": "True" }], "probe": [{ "id": "temp01", "probename": "temp01", "hostname": "host", "template": ["T01"] }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf conf.loadFile('test_config.conf') conf.loadFile('test_config.conf')
def installConfig(): dConf = { "template" : [ { "name": "T01", "jobs" : [ { "active": "True", "job" : "health", "freq" : 15, "version" : 1, "data" : {} } ] } ], "output" : [ { "engine": "debug", "parameters" : [], "active" : "True" } ], "probe" : [ { "id" : "__temp01", "probename" : "__temp01", "template" : [ "T01" ] } ] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf conf.loadFile('test_config.conf')
def insertConf(): global app global lDB dConf = { "output" : [ { "engine": "debug", "parameters" : [], "active" : "True" } ], "global": { "firmware": { "current": "0.6.2", "prod": "0.6.2", "preprod" : "0.6.3", "test" : "0.7.0", "not_found" : "0.1.0" } }, "probe" : [ { "id" : "7374edd8c5d8bc023f29abb8e3fffb95c5a87adb712a5ea6270292850761b33b", "probename" : "test01", "firmware" : "test" } ] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf conf.loadFile('test_config.conf')
def insertConf(): global app global lDB dConf = { "output": [{ "engine": "debug", "parameters": [], "active": "True" }], "global": { "firmware": { "current": "0.6.2", "prod": "0.6.2", "preprod": "0.6.3", "test": "0.7.0", "not_found": "0.1.0" } }, "probe": [{ "id": "7374edd8c5d8bc023f29abb8e3fffb95c5a87adb712a5ea6270292850761b33b", "probename": "test01", "firmware": "test" }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf conf.loadFile('test_config.conf')
def test_noFile(): """ try to load a non missing file """ global conf try: conf.reload() except: None if conf.loadFile('test_config_no.conf') != False: assert False, "should not have loaded config file"
def test_engine_ukn(): """ unknown engine in conf """ global app global lDB lDB.cleanDB() dConf = { "output": [{ "engine": "ukn", "parameters": [], "active": "True" }], "probe": [{ "id": "temp01", "probename": "temp01" }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf try: conf.loadFile('test_config.conf') except: return assert False, "unknown output"
def test_outputers_empty(): """ coverage for outputers without parameters """ global app global lDB lDB.cleanDB() dConf = { "output": [{ "engine": "debug", "parameters": [], "active": "True" }, { "engine": "elastic", "active": "True" }, { "engine": "logstash", "active": "True" }], "probe": [{ "id": "temp01", "probename": "temp01" }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() try: conf.loadFile('test_config.conf') except: None dConf = { "output": [{ "engine": "logstash", "active": "True" }], "probe": [{ "id": "temp01", "probename": "temp01" }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() try: conf.loadFile('test_config.conf') except: None dConf = {"probe": [{"id": "temp01", "probename": "temp01"}]} sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() try: conf.loadFile('test_config.conf') except: return
def test_outputers(): """ coverage for outputers """ global app global lDB lDB.cleanDB() dConf = { "output": [{ "engine": "debug", "parameters": [], "active": "True" }, { "engine": "logstash", "parameters": [{ "server": "127.0.0.1", "port": 55514, "transport": "udp", "fields": [{ "ES_environnement": "PROD" }] }], "active": "True" }], "probe": [{ "id": "temp01", "probename": "temp01" }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf try: conf.loadFile('test_config.conf') except: None dConf = { "output": [{ "engine": "elastic", "parameters": [{ "server": "127.0.0.1", "index": "pyprobe", "shard": 5, "replica": 0 }], "active": "True" }], "probe": [{ "id": "temp01", "probename": "temp01" }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() try: conf.loadFile('test_config.conf') except: return
logLevel = logging.ERROR if args.log[0] == 'INFO': logLevel = logging.INFO logging.basicConfig(format=_logFormat, level=logLevel) # logging.basicConfig(level=logLevel) if not isinstance(args.debug, bool): logging.error('debug arg is not taking argument') exit() logging.info("starting server, version {}, {}".format(__version__, __date__)) logging.debug("pid {}".format(os.getpid())) try: if conf.loadFile(args.config) == False: logging.error('exiting') exit() except Exception as ex: logging.error("{}".format(" ".join(ex.args))) exit() if args.check == True: logging.info("check configuration : syntax seems ok") outs = [] for o in outputer: outs.append(o.getType()) print("outputs: {}".format(", ".join(outs)))
def test_template_missing(): """ try to apply a missing template to a host for job """ global app global lDB lDB.cleanDB() dConf = { "template": [{ "name": "T01", "jobs": [{ "active": "True", "job": "health", "freq": 15, "version": 1, "data": {} }] }], "output": [{ "engine": "debug", "parameters": [], "active": "False" }], "probe": [{ "id": "temp01", "probename": "temp01", "template": ["T_missing"] }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf conf.loadFile('test_config.conf') c = app.test_client() # register the probe rv = c.post("/discover", data=dict(hostId="temp01", ipv4="127.1.0.5", ipv6="::1", version="0.0")) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" # get the configuration rv = c.post("/myjobs", data=dict(uid=str(j['uid']))) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" if j['jobs'] != {}: assert False, "should not have template set {}".format(j['jobs'])
def test_template02(): """ multiple templates, 3 hosts with one, another or both """ global app global lDB lDB.cleanDB() dConf = { "template": [{ "name": "T01", "jobs": [{ "active": "True", "job": "health", "freq": 101, "version": 1, "data": {} }] }, { "name": "T02", "jobs": [{ "active": "True", "job": "health", "freq": 102, "version": 1, "data": {} }] }], "output": [{ "engine": "debug", "parameters": [], "active": "False" }], "probe": [{ "id": "temp01", "probename": "temp01", "template": ["T01"] }, { "id": "temp02", "probename": "temp02", "template": ["T02"] }, { "id": "temp03", "probename": "temp03", "template": ["T01", "T02"] }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf conf.loadFile('test_config.conf') c = app.test_client() # register the probe rv = c.post("/discover", data=dict(hostId="temp01", ipv4="127.1.0.6", ipv6="::1", version="0.0")) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" # get the configuration rv = c.post("/myjobs", data=dict(uid=str(j['uid']))) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" job = j['jobs'][0] if job['freq'] != 101: assert False, "job for temp01 should have freq 101" if job['id'] != 1000: assert False, "job for temp01 id != 1000" # register the probe rv = c.post("/discover", data=dict(hostId="temp02", ipv4="127.1.0.7", ipv6="::1", version="0.0")) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" # get the configuration rv = c.post("/myjobs", data=dict(uid=str(j['uid']))) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" job = j['jobs'][0] if job['freq'] != 102: assert False, "job for temp02 should have freq 102" if job['id'] != 1001: assert False, "job for temp02 id != 1001" # register the probe rv = c.post("/discover", data=dict(hostId="temp03", ipv4="127.1.0.8", ipv6="::1", version="0.0")) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" # get the configuration rv = c.post("/myjobs", data=dict(uid=str(j['uid']))) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" job = j['jobs'][0] if j['jobs'][0]['freq'] != 101: assert False, "job #1 for temp03 should have freq 101" if j['jobs'][1]['freq'] != 102: assert False, "job #2 for temp03 should have freq 101" if j['jobs'][0]['id'] != 1002: assert False, "job #1 for temp03 id != 1002" if j['jobs'][1]['id'] != 1003: assert False, "job #2 for temp03 id != 1003" # for coverage list(conf.getListProbes())
def test_template01(): """ apply a template to a host for job """ global app global lDB lDB.cleanDB() dConf = { "template": [{ "name": "T01", "jobs": [{ "active": "True", "job": "health", "freq": 15, "version": 1, "data": {} }] }], "output": [{ "engine": "debug", "parameters": [], "active": "False" }], "probe": [{ "id": "temp01", "probename": "temp01", "template": ["T01"] }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf conf.loadFile('test_config.conf') print("templates: {}".format(", ".join(conf.getListTemplate()))) c = app.test_client() # register the probe rv = c.post("/discover", data=dict(hostId="temp01", ipv4="127.1.0.5", ipv6="::1", version="0.0")) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" # get the configuration rv = c.post("/myjobs", data=dict(uid=str(j['uid']))) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" job = j['jobs'][0] if not job.__contains__('active'): assert False, "active not present" if job['active'] != "True": assert False, "active present but not True" if job['job'] != "health": assert False, "job should be health" if job['freq'] != 15: assert False, "job frequency should be 15" if job['id'] != 1000: assert False, "job id should be 1000"
def test_active_flag(): """ active flag on configuration if no flag, then active=True """ global app global lDB lDB.cleanDB() dConf = { "output": [{ "engine": "debug", "parameters": [], "active": "True" }], "probe": [{ "id": "xx6", "probename": "xx6", "jobs": [{ "id": 1, "job": "health", "freq": 15, "version": 1, "data": {} }] }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf conf.loadFile('test_config.conf') if conf.getJobsForHost("xx6")[0]['version'] != 1: assert False, "bad version at load" c = app.test_client() # register the probe rv = c.post("/discover", data=dict(hostId="xx6", ipv4="127.1.0.2", ipv6="::1", version="0.0")) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" # get the configuration rv = c.post("/myjobs", data=dict(uid=str(j['uid']))) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" if not j['jobs'][0].__contains__('active'): assert False, "active not present" if j['jobs'][0]['active'] != "True": assert False, "active present but not True" # -- change conf dConf = { "output": [{ "engine": "debug", "parameters": [], "active": "False" }], "probe": [{ "id": "xx6", "probename": "xx6", "jobs": [{ "id": 1, "active": 'False', "job": "health", "freq": 15, "version": 2, "data": {} }] }] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() rv = c.post("/admin/reload", data=dict()) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "reload not working" if conf.getJobsForHost("xx6")[0]['version'] != 2: assert False, "bad version at load" if conf.getJobsForHost("xx6")[0]['active'] != "False": assert False, "bad active status on reload"
def test_admin_reload(): """/admin/reload """ global app global lDB lDB.cleanDB() dConf = { "output" : [ { "engine": "debug", "parameters" : [], "active" : "False" } ], "probe" : [ { "id" : "test-01", "probename" : "test-01", "jobs" : [ { "id" : 1, "job" : "health", "freq" : 15, "version" : 1, "data" : {} } ] } ] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf conf.loadFile('test_config.conf') if conf.getJobsForHost("test-01")[0]['version'] != 1: assert False, "bad version at load" c = app.test_client() dConf = { "output" : [ { "engine": "debug", "parameters" : [], "active" : "False" } ], "probe" : [ { "id" : "test-01", "probename" : "test-01", "jobs" : [ { "id" : 1, "job" : "health", "freq" : 15, "version" : 2, "data" : {} } ] } ] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() rv = c.post("/admin/reload", data=dict()) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "reload not working" print conf.dump() if conf.getJobsForHost("test-01")[0]['version'] != 2: assert False, "bad version at load"
def test_myjobs(): """ try to apply a missing template to a host for job """ global app global lDB lDB.cleanDB() dConf = { "template" : [ { "name": "T01", "jobs" : [ { "active": "True", "job" : "health", "freq" : 15, "version" : 1, "data" : {} } ] } ], "output" : [ { "engine": "debug", "parameters" : [], "active" : "False" } ], "probe" : [ { "id" : "temp01", "probename" : "temp01", "template" : [ "T01" ] } ] } sConf = string.replace(str(dConf), "'", '"') try: f = file("test_config.conf", 'w') except IOError: logging.error("accessing config file {}".format(sFile)) return False f.write(sConf) f.close() global conf conf.loadFile('test_config.conf') c = app.test_client() # register the probe rv = c.post("/discover", data=dict(hostId="temp01",ipv4="127.1.0.5",ipv6="::1",version="0.0")) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" # get the configuration rv = c.post("/myjobs", data=dict(uid=str(j['uid']))) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" # get a config for unknown probe rv = c.post("/myjobs", data=dict(uid=str(99))) if rv.status_code != 404: assert False, "should not have found this host" # get a config for unknown probe rv = c.get("/myjobs") if rv.status_code != 400: assert False, "bad request not trapped"