def test_result_badprobe(): """check push result bad probe """ global app global conf global lDB lDB.cleanDB() c = app.test_client() aJobs = [{"id" : 1, "job" : "health", "freq" : 10, "version" : 1, "data" : {}}] uid = insertOneHost("p1", "test_res", aJobs, "127.2.0.1", ":2:1") # without data rv = c.post("/results", data=dict(uid=uid+1, data='')) j = json.loads(rv.data) print j if j['answer'] != 'KO': assert False, "action error"
def test_result_2(): """check push result with compression """ global app global conf global lDB lDB.cleanDB() c = app.test_client() aJobs = [{"id": 1, "job": "health", "freq": 10, "version": 1, "data": {}}] uid = insertOneHost("p1", "test_res", aJobs, "127.2.0.1", ":2:1") d = {} d['timestamp'] = time.time() d['probeuid'] = "aa" d['probename'] = "test_res" d['date'] = time.time() rv = c.post("/results", data=dict(uid=uid, compressed="yes", data=b64encode(zlib.compress(json.dumps([d]))))) j = json.loads(rv.data) print j if j['answer'] != 'OK': assert False, "action error"
def test_result_2(): """check push result with compression """ global app global conf global lDB lDB.cleanDB() c = app.test_client() aJobs = [{"id" : 1, "job" : "health", "freq" : 10, "version" : 1, "data" : {}}] uid = insertOneHost("p1", "test_res", aJobs, "127.2.0.1", ":2:1") d = {} d['timestamp'] = time.time() d['probeuid'] = "aa" d['probename'] = "test_res" d['date'] = time.time() rv = c.post("/results", data=dict(uid=uid, compressed="yes", data=b64encode(zlib.compress(json.dumps([d]))))) j = json.loads(rv.data) print j if j['answer'] != 'OK': assert False, "action error"
def test_restart(): """check restart """ global app global conf global lDB lDB.cleanDB() c = app.test_client() aJobs = [{"id" : 1, "job" : "health", "freq" : 10, "version" : 1, "data" : {}}] uid = insertOneHost("p1", "test_push1", aJobs, "127.2.0.1", ":2:1") rv = c.post("/pushAction", data=dict(uid=uid, action="restart")) j = json.loads(rv.data) if j['answer'] != 'KO': assert False, "action error" # check restart job without job specification rv = c.post("/pushAction", data=dict(uid=uid, action="restart", module="job")) j = json.loads(rv.data) if j['answer'] != 'KO': assert False, "action error"
def test_push_2(): """check action ws : restart all """ global app global conf global lDB lDB.cleanDB() c = app.test_client() aJobs = [{"id" : 1, "job" : "health", "freq" : 10, "version" : 1, "data" : {}}] uid = insertOneHost("p2", "test_push2", aJobs, "127.2.0.2", ":2:2") rv = c.post("/pushAction", data=dict(uid=uid, action="restart", module="all")) j = json.loads(rv.data) if j['answer'] != 'OK': assert False, "action error {}".format(j) # ping the server and check the return rv = c.post("/ping", data=dict(uid=uid, hostId="p2")) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "ping known host" a = j['action'] if a['name'] != "restart" or type(a['args']) != dict: assert False, "action not in ping reply"
def insertOneHost(id, probename, jobs, ipv4, ipv6): """adds one host """ global app global conf conf.addHost({"id": id, "probename": probename, "jobs": jobs}) c = app.test_client() rv = c.post("/discover", data=dict(hostId=id, ipv4=ipv4, ipv6=ipv6, version="0.0")) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" rv = c.get("/admin/getProbes") j = json.loads(rv.data) if j['answer'] != "OK": assert False, "error in getProbes" uid = -1 for p in j['probes']: if p['ipv4'] == ipv4: uid = p['uid'] if uid == -1: assert False, "bad uid for {}".format(probename) return uid
def test_result_nob64(): """check push result wo base64 data """ global app global conf global lDB lDB.cleanDB() c = app.test_client() aJobs = [{"id" : 1, "job" : "health", "freq" : 10, "version" : 1, "data" : {}}] uid = insertOneHost("p1", "test_res", aJobs, "127.2.0.1", ":2:1") # without base64 d = {} rv = c.post("/results", data=dict(uid=uid, data=json.dumps(d))) j = json.loads(rv.data) if j['answer'] != 'KO': assert False, "action error"
def test_discover_4(): """/discover 127.1.0.4 in probe list with good version check with ws getProbes """ global app global conf global lDB lDB.cleanDB() conf.addHost( {"id" : "x4", "probename": "test_db4", "jobs" : [{"id" : 1, "job" : "health", "freq" : 10, "version" : 1, "data" : {}}]}) c = app.test_client() rv = c.post("/discover", data=dict(hostId="x4",ipv4="127.1.0.4",ipv6="::4",version="1.3")) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" rv = c.get("/admin/getProbes") j = json.loads(rv.data) if j['answer'] != "OK" or j['probes'][0]['version'] != "1.3": assert False, "probe not found with good version"
def test_upgrade_6(): """upgrade with unknown probe """ global app global conf global lDB lDB.cleanDB() insertConf() c = app.test_client() aJobs = [{"id" : 1, "job" : "health", "freq" : 10, "version" : 1, "data" : {}}] uid = insertOneHost("p6", "test_up6", aJobs, "127.2.0.6", ":2:6", "0.6.6") rv = c.post("/upgrade", data=dict(uid=123)) j = json.loads(rv.data) if rv.status_code != 404 and j['reason'] != "probe not found": assert False, "probe not found"
def test_upgrade_4(): """upgrade fw version with pprod 0.6.2 -> 0.6.3 """ global app global conf global lDB lDB.cleanDB() insertConf() c = app.test_client() aJobs = [{"id" : 1, "job" : "health", "freq" : 10, "version" : 1, "data" : {}}] uid = insertOneHost("p4", "test_up4", aJobs, "127.2.0.4", ":2:4", "0.6.2", "preprod") rv = c.post("/upgrade", data=dict(uid=uid)) s = rv.data s = s.strip() if s != '0.6.3': assert False, "bad version downloaded"
def test_upgrade_5(): """upgrade fw version with not found """ global app global conf global lDB lDB.cleanDB() insertConf() c = app.test_client() aJobs = [{"id" : 1, "job" : "health", "freq" : 10, "version" : 1, "data" : {}}] uid = insertOneHost("p5", "test_up5", aJobs, "127.2.0.5", ":2:5", "0.6.1", "not_found") rv = c.post("/upgrade", data=dict(uid=uid)) if rv.status_code != 404: assert False, "file not found {}".format(rv.status_code)
def insertOneHost(id, probename, jobs, ipv4, ipv6, version="1.0", fw="prod"): """check action ws with bad action """ global app global conf conf.addHost( {"id" : id, "probename": probename, "firmware": fw, "jobs" : jobs} ) c = app.test_client() rv = c.post("/discover", data=dict(hostId=id,ipv4=ipv4,ipv6=ipv6,version=version)) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "should have found this host" rv = c.get("/admin/getProbes") j = json.loads(rv.data) if j['answer'] != "OK": assert False, "error in getProbes" uid = -1 for p in j['probes']: if p['ipv4'] == ipv4: uid = p['uid'] if uid == -1: assert False, "bad uid for {}".format(probename) return uid
def test_upgrade_3(): """upgrade fw version with prod 0.6.2 -> 0.6.2 """ global app global conf global lDB lDB.cleanDB() insertConf() c = app.test_client() aJobs = [{"id" : 1, "job" : "health", "freq" : 10, "version" : 1, "data" : {}}] uid = insertOneHost("p3", "test_up3", aJobs, "127.2.0.3", ":2:3", "0.6.2", "prod") rv = c.post("/upgrade", data=dict(uid=uid)) j = json.loads(rv.data) if rv.status_code != 201 and j['reason'] != "no need for upgrade": assert False, "no need for upgrade"
def test_upgrade_4(): """upgrade fw version with pprod 0.6.2 -> 0.6.3 """ global app global conf global lDB lDB.cleanDB() insertConf() c = app.test_client() aJobs = [{"id": 1, "job": "health", "freq": 10, "version": 1, "data": {}}] uid = insertOneHost("p4", "test_up4", aJobs, "127.2.0.4", ":2:4", "0.6.2", "preprod") rv = c.post("/upgrade", data=dict(uid=uid)) s = rv.data s = s.strip() if s != '0.6.3': assert False, "bad version downloaded"
def test_discover_2(): """/discover 127.1.0.2 in db """ global app global conf global lDB lDB.cleanDB() installConfig() conf.addHost( {"id" : "x2", "probename": "test_db2", "jobs" : [{"id" : 1, "job" : "health", "freq" : 10, "version" : 1, "data" : {}}]}) c = app.test_client() rv = c.post("/discover", data=dict(hostId="x2",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"
def test_ping_get(): """/ping GET """ global app c = app.test_client() rv = c.get("/ping") if rv.status_code != 500: assert False, "ping GET working"
def test_ping_put_ukn(): """/ping POST unknown """ global app c = app.test_client() rv = c.post("/ping", data=dict(uid="12", hostId="test")) j = json.loads(rv.data) if rv.status_code != 404 and j['answer'] != "host not found": assert False, "ping POST ukn not working"
def test_version(): """/version GET """ global app c = app.test_client() rv = c.get("/version") j = json.loads(rv.data) if j['answer'] != "OK": assert False, "version GET not working"
def test_ping_put_empty(): """/ping POST empty """ global app c = app.test_client() rv = c.post("/ping", data=dict()) j = json.loads(rv.data) if j['answer'] != "KO" and j['reason'] != "missing uid": assert False, "missing uid not working"
def test_ping_bad_hostid(): """/ping with bad hostid """ global app c = app.test_client() rv = c.post("/ping", data=dict(uid="1", hostId="xx2")) j = json.loads(rv.data) if j['answer'] != "bad probe matching id and hostid": assert False, "ping with bad hostid"
def test_404(): """get 404""" global app global lDB lDB.cleanDB() c = app.test_client() # get /unknown rv = c.get("/unknown") if rv.status_code != 404: assert False, "404 expected"
def test_ping_no_host(): """/ping without host parameter """ global app c = app.test_client() rv = c.post("/ping", data=dict(uid="12")) j = json.loads(rv.data) # print j, rv.status_code if rv.status_code != 400 and j['answer'] != "KO" and j['reason'] != "missing hostId": assert False, "ping missing host id not working"
def test_main(): """get /""" global app global lDB lDB.cleanDB() c = app.test_client() # get / rv = c.get("/") j = json.loads(rv.data) if j['status'] != "OK": assert False, "get / failed"
def test_ping_no_host(): """/ping without host parameter """ global app c = app.test_client() rv = c.post("/ping", data=dict(uid="12")) j = json.loads(rv.data) # print j, rv.status_code if rv.status_code != 400 and j['answer'] != "KO" and j[ 'reason'] != "missing hostId": assert False, "ping missing host id not working"
def test_discover_empty(): """/discover empty """ global app global lDB lDB.cleanDB() c = app.test_client() rv = c.post("/discover", data=dict()) j = json.loads(rv.data) if j['answer'] != "missing argument": assert False, "discover empty not detected"
def test_discover_get(): """/discover GET """ global app global lDB lDB.cleanDB() c = app.test_client() rv = c.get("/discover") print rv.status if rv.status != "405 METHOD NOT ALLOWED": assert False, "discover GET should not be allowed"
def test_pingHost_ukn(): """/ping host ukn """ global lDB lDB.cleanDB() lDB.updateHost("xx1", {'uid' : 1}) global app c = app.test_client() rv = c.post("/ping", data=dict(uid="1", hostId="xx2")) j = json.loads(rv.data) if j['answer'] != "bad probe matching id and hostid": assert False, "ping known ukn id"
def test_pingHost_ukn(): """/ping host ukn """ global lDB lDB.cleanDB() lDB.updateHost("xx1", {'uid': 1}) global app c = app.test_client() rv = c.post("/ping", data=dict(uid="1", hostId="xx2")) j = json.loads(rv.data) if j['answer'] != "bad probe matching id and hostid": assert False, "ping known ukn id"
def test_discover_1(): """/discover 127.1.0.1 not in db """ global app global lDB lDB.cleanDB() installConfig() c = app.test_client() rv = c.post("/discover", data=dict(hostId="x1",ipv4="127.1.0.1",ipv6="::1",version="0.0")) j = json.loads(rv.data) if j['answer'] != "KO" and j['reason'] != "not found": assert False, "should not found this host"
def test_pingHost_ok(): """/ping host ok """ global conf conf.addHost({"id": "xx1", "probename": "test", "jobs": []}) global lDB lDB.cleanDB() lDB.updateHost("xx1", {'uid': 1}) global app c = app.test_client() rv = c.post("/ping", data=dict(uid="1", hostId="xx1")) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "ping known host"
def test_getProbes(): """/admin/getProbes """ global app global lDB # clean DB first lDB.cleanDB() c = app.test_client() rv = c.get("/admin/getProbes") j = json.loads(rv.data) if not j.__contains__('answer'): print(j) assert False, "no answer" if j['answer'] != "OK": assert False, "db should be browsable"
def test_pingHost_ok(): """/ping host ok """ global conf conf.addHost( {"id" : "xx1", "probename": "test", "jobs" : []} ) global lDB lDB.cleanDB() lDB.updateHost("xx1", {'uid' : 1}) global app c = app.test_client() rv = c.post("/ping", data=dict(uid="1", hostId="xx1")) j = json.loads(rv.data) if j['answer'] != "OK": assert False, "ping known host"
def test_result_nob64(): """check push result wo base64 data """ global app global conf global lDB lDB.cleanDB() c = app.test_client() aJobs = [{"id": 1, "job": "health", "freq": 10, "version": 1, "data": {}}] uid = insertOneHost("p1", "test_res", aJobs, "127.2.0.1", ":2:1") # without base64 d = {} rv = c.post("/results", data=dict(uid=uid, data=json.dumps(d))) j = json.loads(rv.data) if j['answer'] != 'KO': assert False, "action error"
def test_upgrade_6(): """upgrade with unknown probe """ global app global conf global lDB lDB.cleanDB() insertConf() c = app.test_client() aJobs = [{"id": 1, "job": "health", "freq": 10, "version": 1, "data": {}}] uid = insertOneHost("p6", "test_up6", aJobs, "127.2.0.6", ":2:6", "0.6.6") rv = c.post("/upgrade", data=dict(uid=123)) j = json.loads(rv.data) if rv.status_code != 404 and j['reason'] != "probe not found": assert False, "probe not found"
def test_result_badprobe(): """check push result bad probe """ global app global conf global lDB lDB.cleanDB() c = app.test_client() aJobs = [{"id": 1, "job": "health", "freq": 10, "version": 1, "data": {}}] uid = insertOneHost("p1", "test_res", aJobs, "127.2.0.1", ":2:1") # without data rv = c.post("/results", data=dict(uid=uid + 1, data='')) j = json.loads(rv.data) print j if j['answer'] != 'KO': assert False, "action error"
def test_upgrade_5(): """upgrade fw version with not found """ global app global conf global lDB lDB.cleanDB() insertConf() c = app.test_client() aJobs = [{"id": 1, "job": "health", "freq": 10, "version": 1, "data": {}}] uid = insertOneHost("p5", "test_up5", aJobs, "127.2.0.5", ":2:5", "0.6.1", "not_found") rv = c.post("/upgrade", data=dict(uid=uid)) if rv.status_code != 404: assert False, "file not found {}".format(rv.status_code)
def test_push_1(): """check action ws with bad action """ global app global conf global lDB lDB.cleanDB() c = app.test_client() aJobs = [{"id" : 1, "job" : "health", "freq" : 10, "version" : 1, "data" : {}}] uid = insertOneHost("p1", "test_push1", aJobs, "127.2.0.1", ":2:1") rv = c.post("/pushAction", data=dict(uid=uid, action="no action")) j = json.loads(rv.data) if j['answer'] != 'KO': assert False, "action error"
def test_upgrade_3(): """upgrade fw version with prod 0.6.2 -> 0.6.2 """ global app global conf global lDB lDB.cleanDB() insertConf() c = app.test_client() aJobs = [{"id": 1, "job": "health", "freq": 10, "version": 1, "data": {}}] uid = insertOneHost("p3", "test_up3", aJobs, "127.2.0.3", ":2:3", "0.6.2", "prod") rv = c.post("/upgrade", data=dict(uid=uid)) j = json.loads(rv.data) if rv.status_code != 201 and j['reason'] != "no need for upgrade": assert False, "no need for upgrade"
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"
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_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'])