示例#1
0
def test_duplicate():
    config = skelton.Config()
    config.check_health = lambda: 3
    config.become_active = lambda: 0
    z = zha.ZHA(config)
    with pytest.raises(zha.AlreadyExistException) as exc:
        z2 = zha.ZHA(config)
    assert "Same name zha seems to exist already, Exit." in str(exc.value)
    trigger_zha(z)
    time.sleep(10)
示例#2
0
def test_reentrant():
    obj = type('', (), {})
    obj.act = []
    obj.sby = []
    health_seq = itertools.cycle([3, 3, 3, 3, 1, 1, 1, 1])

    def _ch():
        return health_seq.next()

    def _oa():
        obj.act.append(1)
        return 0

    def _os():
        obj.sby.append(1)
        return 0

    config = skelton.Config()
    config.check_health = _ch
    config.become_active = _oa
    config.become_standby_from_active = _os
    z = zha.ZHA(config)
    trigger_zha(z, 300)
    assert obj.act.__len__() > 2 and obj.sby.__len__() > 2
    time.sleep(10)
示例#3
0
def test_leave_abc_behind():
    obj = type('', (), {})
    obj.flg = False
    config = skelton.Config()
    config.check_health = lambda: 3
    config.become_active = lambda: 0
    config.become_standby_from_active = lambda: -1
    z = zha.ZHA(config)
    trigger_zha(z)
    zk = KazooClient(hosts="127.0.0.1:2181")
    zk.start()
    assert zk.exists("/zha-abc")
    zk.stop()
    time.sleep(10)
示例#4
0
def test_unexpected_exception():
    def _ch():
        raise Exception("")

    def _oa():
        return 0

    config = skelton.Config()
    config.check_health = _ch
    config.become_active = _oa
    z = zha.ZHA(config)
    ret = z.mainloop()
    assert ret == 1
    time.sleep(10)
示例#5
0
def test_become_active():
    obj = type('', (), {})
    obj.flg = False

    def _oa():
        obj.flg = True
        return 0

    config = skelton.Config()
    config.check_health = lambda: 3
    config.become_active = _oa
    z = zha.ZHA(config)
    trigger_zha(z)
    assert obj.flg
    time.sleep(10)
示例#6
0
def test_fail_activate():
    obj = type('', (), {})
    obj.act = []

    def _oa():
        obj.act.append(1)
        return 1

    config = skelton.Config()
    config.check_health = lambda: 3
    config.become_active = _oa
    z = zha.ZHA(config)
    trigger_zha(z, 120)
    assert obj.act.__len__() > 10
    time.sleep(10)
示例#7
0
def test_handle_abc():
    subprocess.call("zookeeper-client create /zha-abc dummy", shell=True)
    obj = type('', (), {})
    obj.flg = False

    def _f():
        obj.flg = True
        return 0

    config = skelton.Config()
    config.check_health = lambda: 3
    config.become_active = lambda: 0
    config.trigger_fence = _f
    z = zha.ZHA(config)
    trigger_zha(z)
    assert obj.flg
    time.sleep(10)
示例#8
0
def test_cluster_reentrant():
    class DummyState(threading.Thread):
        def __init__(self, duration):
            self.duration = duration
            threading.Thread.__init__(self)

        def run(self):
            subprocess.call(
                "zookeeper-client create /zha-state/dummy SBY:HEALTHY",
                shell=True)
            state = itertools.cycle(["SBY:HEALTHY", "SBY:UNHEALTHY"])
            while self.duration > 0:
                time.sleep(20)
                subprocess.call("zookeeper-client set /zha-state/dummy %s" %
                                (state.next(), ),
                                shell=True)
                self.duration -= 20
            subprocess.call("zookeeper-client delete /zha-state/dummy",
                            shell=True)

    th = DummyState(300)
    th.start()
    obj = type('', (), {})
    obj.c, obj.d = [], []

    def _oc():
        obj.c.append(1)
        return 0

    def _od():
        obj.d.append(1)
        return 0

    config = skelton.Config()
    config.check_health = lambda: 3
    config.become_active = lambda: 0
    config.become_clustered = _oc
    config.become_declustered = _od
    z = zha.ZHA(config)
    trigger_zha(z, 300)
    th.join()
    assert obj.c.__len__() > 3 and obj.d.__len__() > 3
    time.sleep(10)
示例#9
0
def test_lock_timeout():
    zk = KazooClient(hosts="127.0.0.1:2181")
    zk.start()
    lock = zk.Lock("/zha-lock", "test")
    lock.acquire()

    obj = type('', (), {})
    obj.flg = False

    def _oa():
        obj.flg = True
        return 0

    config = skelton.Config()
    config.check_health = lambda: 3
    config.become_active = _oa
    z = zha.ZHA(config)
    trigger_zha(z)
    assert obj.flg is False
    lock.release()
    zk.stop()
    time.sleep(10)
示例#10
0
def test_zkconnlost():
    obj = type('', (), {})
    obj.sc = []

    def _oa():
        obj.sc.append(1)
        return 0

    def _os():
        obj.sc.append(-1)
        return 0

    config = skelton.Config()
    config.check_health = lambda: 3
    config.become_active = _oa
    config.become_standby_from_active = _os
    z = zha.ZHA(config)

    class Runner(threading.Thread):
        def __init__(self, z):
            self.z = z
            threading.Thread.__init__(self)

        def run(self):
            self.z.mainloop()

    r = Runner(z)
    r.start()
    time.sleep(30)
    subprocess.call("sudo service zookeeper-server stop", shell=True)
    time.sleep(30)
    subprocess.call("sudo service zookeeper-server start", shell=True)
    time.sleep(60)
    z.stop()
    r.join()
    assert obj.sc[:3] == [1, -1, 1]
    time.sleep(10)
示例#11
0
def test_occasional_healtherror():
    health_seq = itertools.cycle([3, 3, 3, 3, 0])
    obj = type('', (), {})
    obj.flg, obj.flg2 = False, False

    def _ch():
        return health_seq.next()

    def _oa():
        obj.flg = True
        return 0

    def _os():
        obj.flg2 = True
        return 0

    config = skelton.Config()
    config.check_health = _ch
    config.become_active = _oa
    config.become_standby_from_active = _os
    z = zha.ZHA(config)
    trigger_zha(z, 120)
    assert obj.flg and (obj.flg2 is False)
    time.sleep(10)
示例#12
0
    @returns_minusone_on_Exception
    def become_declustered(self):
        return self._trigger_script_with_timeout(10,
                                                 "./impl/on_declustered.sh")

    @returns_minusone_on_Exception
    def become_standby_from_active(self):
        return self._trigger_script_with_timeout(10, "./impl/on_standby.sh")

    @returns_minusone_on_Exception
    def trigger_fence(self):
        return self._trigger_script_with_timeout(10, "./impl/on_fence.sh")

    def _trigger_script_with_timeout(self, timeout, fname, *args):
        popen_args = ["/bin/bash", fname]
        popen_args.extend(args)
        popen = subprocess.Popen(popen_args)
        t = 0
        while t < timeout:
            popen.poll()
            if popen.returncode is not None:
                return popen.returncode
            time.sleep(0.2)
            t += 0.2
        raise Exception("Script timeout")


if __name__ == '__main__':
    obj = zha.ZHA(Config())
    obj.mainloop()  #Ctrl+C or SIGINT to stop this program