Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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()
Exemplo n.º 4
0
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()
Exemplo n.º 5
0
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()
Exemplo n.º 6
0
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"
Exemplo n.º 7
0
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"
Exemplo n.º 8
0
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()
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
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()
Exemplo n.º 11
0
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()
Exemplo n.º 12
0
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()
Exemplo n.º 13
0
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()
Exemplo n.º 14
0
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"
Exemplo n.º 15
0
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()
Exemplo n.º 16
0
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()
Exemplo n.º 17
0
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()
Exemplo n.º 18
0
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)
Exemplo n.º 19
0
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()
Exemplo n.º 20
0
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()
Exemplo n.º 21
0
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"
Exemplo n.º 22
0
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()
Exemplo n.º 23
0
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)
Exemplo n.º 24
0
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)
Exemplo n.º 25
0
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)
Exemplo n.º 26
0
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)