def test_getHostContent(): """ getHostContentByUid """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.updateHost("test", {'uid': _id, "a": 1, "b": 2}) _sHost = lDB.getHostContentByUid(1) if _sHost['b'] != 2: assert False, "should return 2 as content of the b part" # check unknown id for coverage _sHost = lDB.getHostContentByUid(10) if _sHost != None: assert False, "show not found the host" lDB.cleanDB()
def test_getJobName(): """ getJobsForHost + getNameForHost """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") lDB.updateHost( "test", { 'uid': _id, "ipv4": 1, "ipv6": 2, "name": "test", "last": time.time(), "version": "1.1.2" }) _sJob = lDB.getJobsForHost(1) _sName = lDB.getNameForHost(1) _sList = lDB.getListProbes() lDB.cleanDB()
def test_getHostVersion(): """ getHostVersionByUid """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.updateHost("test", {'uid': _id, "a": 1, "b": 2}) _sVersion = lDB.getHostVersionByUid(1) if _sVersion != None: assert False, "host has no version" lDB.updateHost("test", {'uid': _id, "a": 1, "b": 2, "version": "1.1.2"}) _sVersion = lDB.getHostVersionByUid(1) if _sVersion != "1.1.2": assert False, "wrong version" _sVersion = lDB.getHostVersionByUid(2) if _sVersion != None: assert False, "host not found" lDB.cleanDB()
def test_getHostVersion(): """ getHostVersionByUid """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.updateHost("test", {'uid' : _id, "a":1, "b":2}) _sVersion = lDB.getHostVersionByUid(1) if _sVersion != None: assert False, "host has no version" lDB.updateHost("test", {'uid' : _id, "a":1, "b":2, "version": "1.1.2"}) _sVersion = lDB.getHostVersionByUid(1) if _sVersion != "1.1.2": assert False, "wrong version" _sVersion = lDB.getHostVersionByUid(2) if _sVersion != None: assert False, "host not found" lDB.cleanDB()
def test_getHostContent(): """ getHostContentByUid """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.updateHost("test", {'uid' : _id, "a":1, "b":2}) _sHost = lDB.getHostContentByUid(1) if _sHost['b'] != 2: assert False, "should return 2 as content of the b part" # check unknown id for coverage _sHost = lDB.getHostContentByUid(10) if _sHost != None: assert False, "show not found the host" lDB.cleanDB()
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_clean(): """ cleanOldProbes """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") lDB.updateHost("test", {'uid' : _id, "ipv4":1, "ipv6":2, "name": "test", "last": 0, "version": "1.1.2"}) lDB.cleanOldProbes() lDB.dump()
def ws_ping(): """ answers ok if everything is good """ logging.debug("/ping") # global lDB # global conf r = { "answer" : "OK" } if request.method == 'POST': _r = wsCheckParams(["uid", "hostId"]) if _r != None: return _r uid = int(request.form['uid']) host = wsCheckHostUID(uid) if not isinstance(host, unicode) and not isinstance(host, str): return host if host != request.form['hostId']: logging.error("bad probe {} {}".format(host, request.form['hostId'])) return make_response(jsonify({"answer" : "bad probe matching id and hostid"}), 400) lDB.updateHost(host, {"last" : time.time()}) a = lDB.getAction(host) if a != None: r['action'] = a return make_response(jsonify(r), 200) r = { "answer" : "KO" } return make_response(jsonify(r), 500)
def test_getAction(): """ getAction """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") lDB.updateHost( "test", { 'uid': _id, "ipv4": 1, "ipv6": 2, "name": "test", "last": time.time(), "version": "1.1.2" }) lDB.getAction(1) lDB.updateHost( "test", { 'uid': _id, "ipv4": 1, "ipv6": 2, "name": "test", "last": time.time(), "action": "test_action", "version": "1.1.2" }) a = lDB.getAction("test") if a != "test_action": assert False, "should have an action" a = lDB.getAction("test") if a != None: assert False, "should not have an action" lDB.cleanDB()
def test_getHost_not(): """ getHostByUid return false on unknown host """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.updateHost("test", {'uid' : _id}) _sHost = lDB.getHostByUid(2) if _sHost != None: assert False, "should return None" lDB.cleanDB()
def test_getHost(): """ getHostByUid """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.updateHost("test", {'uid' : _id}) _sHost = lDB.getHostByUid(1) if _sHost != "test": assert False, "should return name of the host" lDB.cleanDB()
def test_getUniqueId_2(): """ """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id : {}".format(_id) lDB.updateHost("test", {'uid' : _id}) _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.cleanDB()
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_getHost(): """ getHostByUid """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.updateHost("test", {'uid': _id}) _sHost = lDB.getHostByUid(1) if _sHost != "test": assert False, "should return name of the host" lDB.cleanDB()
def test_getHost_not(): """ getHostByUid return false on unknown host """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.updateHost("test", {'uid': _id}) _sHost = lDB.getHostByUid(2) if _sHost != None: assert False, "should return None" lDB.cleanDB()
def test_getUniqueId_2(): """ """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id : {}".format(_id) lDB.updateHost("test", {'uid': _id}) _id = lDB.getUniqueId("test") if _id != 1: assert False, "should return 1 as first id" lDB.cleanDB()
def ws_ping(): """ answers ok if everything is good """ logging.debug("/ping") # global lDB # global conf r = {"answer": "OK"} if request.method == 'POST': _r = wsCheckParams(["uid", "hostId"]) if _r != None: return _r uid = int(request.form['uid']) host = wsCheckHostUID(uid) if not isinstance(host, unicode) and not isinstance(host, str): return host if host != request.form['hostId']: logging.error("bad probe {} {}".format(host, request.form['hostId'])) return make_response( jsonify({"answer": "bad probe matching id and hostid"}), 400) lDB.updateHost(host, {"last": time.time()}) a = lDB.getAction(host) if a != None: r['action'] = a return make_response(jsonify(r), 200) r = {"answer": "KO"} return make_response(jsonify(r), 500)
def test_clean(): """ cleanOldProbes """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") lDB.updateHost( "test", { 'uid': _id, "ipv4": 1, "ipv6": 2, "name": "test", "last": 0, "version": "1.1.2" }) lDB.cleanOldProbes() lDB.dump()
def test_getJobName(): """ getJobsForHost + getNameForHost """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") lDB.updateHost("test", {'uid' : _id, "ipv4":1, "ipv6":2, "name": "test", "last": time.time(), "version": "1.1.2"}) _sJob = lDB.getJobsForHost(1) _sName = lDB.getNameForHost(1) _sList = lDB.getListProbes() lDB.cleanDB()
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_getAction(): """ getAction """ global lDB lDB.cleanDB() _id = lDB.getUniqueId("test") lDB.updateHost("test", {'uid' : _id, "ipv4":1, "ipv6":2, "name": "test", "last": time.time(), "version": "1.1.2"}) lDB.getAction(1) lDB.updateHost("test", {'uid' : _id, "ipv4":1, "ipv6":2, "name": "test", "last": time.time(), "action": "test_action", "version": "1.1.2"}) a = lDB.getAction("test") if a != "test_action": assert False, "should have an action" a = lDB.getAction("test") if a != None: assert False, "should not have an action" lDB.cleanDB()
def ws_pushAction(): """ add an action for the uid params : uid, action, module actions : restart """ logging.info("/pushAction") # global lDB _r = wsCheckParams(["uid", "action"]) if _r != None: return _r uid = int(request.form['uid']) action = str(request.form['action']) host = lDB.getHostByUid(uid) if host == None: return make_response( jsonify({ "answer": "KO", "reason": "host not found" }), 404) # global conf r = {"answer": "OK"} # ------------------------- if action == "restart": if request.form.__contains__('module') == False: return make_response( jsonify({ "answer": "KO", "reason": "missing module" }), 400) module = str(request.form['module']) r['action'] = "restart" r['target_uid'] = uid sAction = {"name": "restart"} if module == "all": sAction['args'] = {"module": module} lDB.updateHost(host, {"action": sAction}) if module == "job": if request.form.__contains__('job') == False: return make_response( jsonify({ "answer": "KO", "reason": "missing job" }), 400) job = str(request.form['job']) sAction['args'] = {"module": module, "job": job} lDB.updateHost(host, {"action": sAction}) return make_response(jsonify(r), 200) # ------------------------- if action == "upgrade": sAction = {"name": "upgrade"} sAction['args'] = {"when": "now"} lDB.updateHost(host, {"action": sAction}) return make_response(jsonify(r), 200) # exception r['answer'] = "KO" r['reason'] = "action not found" return make_response(jsonify(r), 400)
def ws_discover(): """ discover web service checks host in the configuration and returns its unique id """ logging.debug("/discover") # global conf # global lDB if request.method == 'POST': if not (request.form.__contains__('hostId') and request.form.__contains__('ipv4') and request.form.__contains__('ipv6') and request.form.__contains__('version') ): logging.error("probe passing bad args") return make_response(jsonify({"answer" : "missing argument"}), 400) _sHostId = request.form['hostId'] _sIpv4 = request.form['ipv4'] _sIpv6 = request.form['ipv6'] _sVersion = request.form['version'] # if the probe is in the configuration db # update the probe db # if conf.checkHost(_sHostId): _id = lDB.getUniqueId(_sHostId) _hostConf = conf.getConfigForHost(_sHostId) lDB.updateHost(_sHostId, {'uid' : _id, 'discoverTime': time.time(), 'last': time.time(), 'ipv4' : _sIpv4, 'ipv6' : _sIpv6, 'version' : _sVersion, 'name': _hostConf['probename']}) # push to outputer data = {} d = {} d['timestamp'] = datetime.datetime.utcfromtimestamp(time.time()).isoformat() d['probe_ipv4'] = str(_sIpv4) d['probe_ipv6'] = str(_sIpv6) d['probe_version'] = str(_sVersion) d['probe_hostid'] = str(_sHostId) data['date'] = time.time() data['probename'] = _hostConf['probename'] data['probeuid'] = _id data['name'] = str('DISCOVER') data['data'] = d for o in outputer: o.send(data) return make_response(jsonify({"answer" : "OK", "uid" : _id}), 200) else: logging.warning("probe not found {} {}".format(_sIpv4, _sIpv6)) # push to outputer data = {} d = {} d['timestamp'] = datetime.datetime.utcfromtimestamp(time.time()).isoformat() d['probe_ipv4'] = str(_sIpv4) d['probe_ipv6'] = str(_sIpv6) d['probe_version'] = str(_sVersion) d['probe_hostid'] = str(_sHostId) data['date'] = time.time() data['probename'] = str('unknown') data['probeuid'] = int(0) data['name'] = str('DISCOVER') data['data'] = d for o in outputer: o.send(data) # inform probe return make_response(jsonify({"answer" : "KO", "reason" : "not found"}), 404)
def ws_pushAction(): """ add an action for the uid params : uid, action, module actions : restart """ logging.info("/pushAction") # global lDB _r = wsCheckParams(["uid", "action"]) if _r != None: return _r uid = int(request.form['uid']) action = str(request.form['action']) host = lDB.getHostByUid(uid) if host == None: return make_response(jsonify({"answer":"KO", "reason":"host not found"}), 404) # global conf r = { "answer" : "OK" } # ------------------------- if action == "restart": if request.form.__contains__('module') == False: return make_response(jsonify({"answer":"KO", "reason":"missing module"}), 400) module = str(request.form['module']) r['action'] = "restart" r['target_uid'] = uid sAction = { "name" : "restart" } if module == "all": sAction['args'] = { "module" : module } lDB.updateHost(host, {"action" : sAction }) if module == "job": if request.form.__contains__('job') == False: return make_response(jsonify({"answer":"KO", "reason":"missing job"}), 400) job = str(request.form['job']) sAction['args'] = { "module" : module, "job" : job } lDB.updateHost(host, {"action" : sAction }) return make_response(jsonify(r), 200) # ------------------------- if action == "upgrade": sAction = { "name" : "upgrade" } sAction['args'] = { "when" : "now" } lDB.updateHost(host, {"action" : sAction }) return make_response(jsonify(r), 200) # exception r['answer'] = "KO" r['reason'] = "action not found" return make_response(jsonify(r), 400)
def ws_discover(): """ discover web service checks host in the configuration and returns its unique id """ logging.debug("/discover") # global conf # global lDB if request.method == 'POST': if not (request.form.__contains__('hostId') and request.form.__contains__('ipv4') and request.form.__contains__('ipv6') and request.form.__contains__('version')): logging.error("probe passing bad args") return make_response(jsonify({"answer": "missing argument"}), 400) _sHostId = request.form['hostId'] _sIpv4 = request.form['ipv4'] _sIpv6 = request.form['ipv6'] _sVersion = request.form['version'] # if the probe is in the configuration db # update the probe db # if conf.checkHost(_sHostId): _id = lDB.getUniqueId(_sHostId) _hostConf = conf.getConfigForHost(_sHostId) lDB.updateHost( _sHostId, { 'uid': _id, 'discoverTime': time.time(), 'last': time.time(), 'ipv4': _sIpv4, 'ipv6': _sIpv6, 'version': _sVersion, 'name': _hostConf['probename'] }) # push to outputer data = {} d = {} d['timestamp'] = datetime.datetime.utcfromtimestamp( time.time()).isoformat() d['probe_ipv4'] = str(_sIpv4) d['probe_ipv6'] = str(_sIpv6) d['probe_version'] = str(_sVersion) d['probe_hostid'] = str(_sHostId) data['date'] = time.time() data['probename'] = _hostConf['probename'] data['probeuid'] = _id data['name'] = str('DISCOVER') data['data'] = d for o in outputer: o.send(data) return make_response(jsonify({"answer": "OK", "uid": _id}), 200) else: logging.warning("probe not found {} {}".format(_sIpv4, _sIpv6)) # push to outputer data = {} d = {} d['timestamp'] = datetime.datetime.utcfromtimestamp( time.time()).isoformat() d['probe_ipv4'] = str(_sIpv4) d['probe_ipv6'] = str(_sIpv6) d['probe_version'] = str(_sVersion) d['probe_hostid'] = str(_sHostId) data['date'] = time.time() data['probename'] = str('unknown') data['probeuid'] = int(0) data['name'] = str('DISCOVER') data['data'] = d for o in outputer: o.send(data) # inform probe return make_response( jsonify({ "answer": "KO", "reason": "not found" }), 404)