def test_zstack_non_utf8(tdir, looper, tconf):
    """
    ZStack gets a non utf-8 message and does not hand it over to the
    processing method
    :return:
    """
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)
    (alpha, beta), (alphaP, betaP) = create_and_prep_stacks(names, tdir,
                                                            looper, tconf)

    # Send a utf-8 message and see its received
    for uid in alpha.remotes:
        alpha.transmit(b'{"k1": "v1"}', uid, serialized=True)
    looper.run(eventually(chkPrinted, betaP, {"k1": "v1"}))

    # Send a non utf-8 message and see its not received (by the receiver method)
    for uid in alpha.remotes:
        alpha.transmit(b'{"k2": "v2\x9c"}', uid, serialized=True)
    with pytest.raises(AssertionError):
        looper.run(eventually(chkPrinted, betaP, {"k2": "v2\x9c"}))
    # TODO: A better test where the output of the parsing method is checked
        # requires spyable methods

    # Again send a utf-8 message and see its received (checks if stack is
    # functional after receiving a bad message)
    for uid in alpha.remotes:
        alpha.transmit(b'{"k3": "v3"}', uid, serialized=True)
    looper.run(eventually(chkPrinted, betaP, {"k3": "v3"}))
def connected_nodestacks(registry, tdir, looper, connection_timeout, tconf):
    genKeys(tdir, registry.keys())
    stacks = []
    for name, ha in registry.items():
        printer = Printer(name)
        stackParams = dict(name=name,
                           ha=ha,
                           basedirpath=tdir,
                           auth_mode=AuthMode.RESTRICTED.value)
        reg = copy(registry)
        reg.pop(name)
        stack = KITZStack(stackParams, printer.print, reg)
        stack.listenerQuota = tconf.NODE_TO_NODE_STACK_QUOTA
        stack.listenerSize = tconf.NODE_TO_NODE_STACK_SIZE
        stacks.append(stack)

    motors = prepStacks(looper, *stacks, connect=False, useKeys=True)

    looper.run(
        eventually(checkStacksConnected,
                   stacks,
                   retryWait=1,
                   timeout=connection_timeout))

    return stacks, motors
Beispiel #3
0
def testZStackSendRecvHugeDataUnderLimit(set_info_log_level, tdir, looper, tconf):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)

    # we use json serializer now, so final form will be {'k':'vvv...vvv'}
    # so here we try to prepare exactly tconf.MSG_LEN_LIMIT bytes after serialization
    msg = {'k': 'v' * (tconf.MSG_LEN_LIMIT - len("{'k':''}"))}

    betaHandler = [False]

    def recvHandlerAlpha(wrpMsg):
        pass

    def recvHandlerBeta(wrpMsg):
        rmsg, frm = wrpMsg
        betaHandler[0] = True
        assert frm == 'Alpha'
        assert rmsg == msg

    alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerAlpha, restricted=True,
                   config=adict(**tconf.__dict__), msgRejectHandler=None)

    beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerBeta, restricted=True,
                  config=adict(**tconf.__dict__), msgRejectHandler=None)

    assert len(alpha.serializeMsg(msg)) == tconf.MSG_LEN_LIMIT

    prepStacks(looper, *(alpha, beta), connect=True, useKeys=True)

    stat = alpha.send(msg, beta.name)
    assert stat[0] is True

    looper.runFor(5)

    assert betaHandler[0] is True
def testManyMessages(set_info_log_level, tdir, looper, tconf):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)
    alphaP = Printer(names[0])
    betaMsgHandler = CounterMsgsHandler()

    alpha = ZStack(names[0],
                   ha=genHa(),
                   basedirpath=tdir,
                   msgHandler=alphaP.print,
                   restricted=True)
    beta = ZStack(names[1],
                  ha=genHa(),
                  basedirpath=tdir,
                  msgHandler=betaMsgHandler.handler,
                  restricted=True)
    prepStacks(looper, alpha, beta, connect=True, useKeys=True)

    looper.runFor(1)

    msgNum = BIG_NUM_OF_MSGS
    msgSender = MessageSender(msgNum, alpha, beta.name)
    looper.add(msgSender)

    def checkAllReceived():
        assert msgSender.sentMsgCount == msgNum
        assert betaMsgHandler.receivedMsgCount == msgNum

    looper.run(eventually(checkAllReceived,
                          retryWait=1,
                          timeout=60))
Beispiel #5
0
def testRestricted2ZStackCommunication(tdir, looper):
    """
    Create 2 ZStack and make them send and receive messages.
    Both stacks allow communication only when keys are shared
    :return:
    """
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)
    alphaP = Printer(names[0])
    betaP = Printer(names[1])

    alpha = ZStack(names[0],
                   ha=genHa(),
                   basedirpath=tdir,
                   msgHandler=alphaP.print,
                   restricted=True)
    beta = ZStack(names[1],
                  ha=genHa(),
                  basedirpath=tdir,
                  msgHandler=betaP.print,
                  restricted=True)

    prepStacks(looper, alpha, beta, connect=True, useKeys=True)
    alpha.send({'greetings': 'hi'}, beta.name)
    beta.send({'greetings': 'hello'}, alpha.name)

    looper.run(eventually(chkPrinted, alphaP, {'greetings': 'hello'}))
    looper.run(eventually(chkPrinted, betaP, {'greetings': 'hi'}))
Beispiel #6
0
def test_zstack_non_utf8(tdir, looper, tconf):
    """
    ZStack gets a non utf-8 message and does not hand it over to the
    processing method
    :return:
    """
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)
    (alpha, beta), (alphaP, betaP) = create_and_prep_stacks(names, tdir,
                                                            looper, tconf)

    # Send a utf-8 message and see its received
    for uid in alpha.remotes:
        alpha.transmit(b'{"k1": "v1"}', uid, serialized=True)
    looper.run(eventually(chkPrinted, betaP, {"k1": "v1"}))

    # Send a non utf-8 message and see its not received (by the receiver method)
    for uid in alpha.remotes:
        alpha.transmit(b'{"k2": "v2\x9c"}', uid, serialized=True)
    with pytest.raises(AssertionError):
        looper.run(eventually(chkPrinted, betaP, {"k2": "v2\x9c"}))
    # TODO: A better test where the output of the parsing method is checked
        # requires spyable methods

    # Again send a utf-8 message and see its received (checks if stack is
    # functional after receiving a bad message)
    for uid in alpha.remotes:
        alpha.transmit(b'{"k3": "v3"}', uid, serialized=True)
    looper.run(eventually(chkPrinted, betaP, {"k3": "v3"}))
Beispiel #7
0
def testManyMessages(tdir, looper, set_info_log_level):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)
    alphaP = Printer(names[0])
    betaMsgHandler = CounterMsgsHandler()

    alpha = ZStack(names[0],
                   ha=genHa(),
                   basedirpath=tdir,
                   msgHandler=alphaP.print,
                   restricted=True)
    beta = ZStack(names[1],
                  ha=genHa(),
                  basedirpath=tdir,
                  msgHandler=betaMsgHandler.handler,
                  restricted=True)
    prepStacks(looper, alpha, beta, connect=True, useKeys=True)

    looper.runFor(1)

    msgNum = 100000
    msgSender = MessageSender(msgNum, alpha, beta.name)
    looper.add(msgSender)

    def checkAllReceived():
        assert msgSender.sentMsgCount == msgNum
        assert betaMsgHandler.receivedMsgCount == msgNum

    looper.run(eventually(checkAllReceived, retryWait=1, timeout=60))
Beispiel #8
0
def testMessageQuota(tdir, looper):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)
    alphaP = Printer(names[0])
    betaMsgHandler = CollectingMsgsHandler()

    alpha = ZStack(names[0],
                   ha=genHa(),
                   basedirpath=tdir,
                   msgHandler=alphaP.print,
                   restricted=True)
    beta = ZStack(names[1],
                  ha=genHa(),
                  basedirpath=tdir,
                  msgHandler=betaMsgHandler.handler,
                  restricted=True,
                  onlyListener=True)

    prepStacks(looper, alpha, beta, connect=True, useKeys=True)

    messages = []
    numMessages = 150 * beta.listenerQuota
    for i in range(numMessages):
        msg = json.dumps({'random': randomSeed().decode()}).encode()
        if alpha.send(msg, beta.name):
            messages.append(json.loads(msg.decode()))

    def checkAllReceived():
        assert len(messages) == len(betaMsgHandler.receivedMessages)
        assert messages == betaMsgHandler.receivedMessages

    looper.run(eventually(checkAllReceived, retryWait=0.5, timeout=5))
Beispiel #9
0
def testMessageQuota(tdir, looper):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)
    alphaP = Printer(names[0])
    receivedMessages = []
    betaMshHandler = makeHandler(receivedMessages)

    alpha = ZStack(names[0],
                   ha=genHa(),
                   basedirpath=tdir,
                   msgHandler=alphaP.print,
                   restricted=True)
    beta = ZStack(names[1],
                  ha=genHa(),
                  basedirpath=tdir,
                  msgHandler=betaMshHandler,
                  restricted=True,
                  onlyListener=True)

    prepStacks(looper, alpha, beta, connect=True, useKeys=True)

    messages = []
    numMessages = 100 * beta.listenerQuota
    for i in range(numMessages):
        msg = json.dumps({'random': randomSeed().decode()}).encode()
        messages.append(json.loads(msg.decode()))
        alpha.send(msg, beta.name)

    looper.runFor(2)
    assert messages == receivedMessages
Beispiel #10
0
def testZStackSendRecvHugeDataUnderLimit(set_info_log_level, tdir, looper, tconf):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)

    # we use json serializer now, so final form will be {'k':'vvv...vvv'}
    # so here we try to prepare exactly tconf.MSG_LEN_LIMIT bytes after serialization
    msg = {'k': 'v' * (tconf.MSG_LEN_LIMIT - len("{'k':''}"))}

    betaHandler = [False]

    def recvHandlerAlpha(wrpMsg):
        pass

    def recvHandlerBeta(wrpMsg):
        rmsg, frm = wrpMsg
        betaHandler[0] = True
        assert frm == 'Alpha'
        assert rmsg == msg

    alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerAlpha, restricted=True,
                   config=adict(**tconf.__dict__), msgRejectHandler=None)

    beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerBeta, restricted=True,
                  config=adict(**tconf.__dict__), msgRejectHandler=None)

    assert len(alpha.serializeMsg(msg)) == tconf.MSG_LEN_LIMIT

    prepStacks(looper, *(alpha, beta), connect=True, useKeys=True)

    stat = alpha.send(msg, beta.name)
    assert stat[0] is True

    looper.runFor(5)

    assert betaHandler[0] is True
def create_stacks(tdir, looper):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)

    alpha = create_alpha(tdir, names[0])
    beta, beta_msg_handler = create_beta(tdir, names[1])
    return alpha, beta, beta_msg_handler
Beispiel #12
0
def clientstack(tdir, looper, alpha_handler):
    names = ['ClientA', 'Alpha']
    genKeys(tdir, names)
    aseed = randomSeed()
    cseed = randomSeed()

    stackParams = {
        "name": names[0],
        "ha": genHa(),
        "auto": 2,
        "basedirpath": tdir
    }
    timer = MockTimer(0)
    client = ClientZStack(stackParams, alpha_handler.handle, cseed, False,
                          timer=timer)

    stackParams = {
        "name": names[1],
        "ha": genHa(),
        "auto": 2,
        "basedirpath": tdir
    }
    timer = MockTimer(0)
    alpha = SimpleZStack(stackParams, alpha_handler, aseed, True,
                         timer=timer)

    amotor = SMotor(alpha)
    looper.add(amotor)

    cmotor = SMotor(client)
    looper.add(cmotor)
    return alpha, client
Beispiel #13
0
def create_and_prep_stacks(names, tdir, looper):
    genKeys(tdir, names)
    printers = [Printer(n) for n in names]
    stacks = [ZStack(n, ha=genHa(), basedirpath=tdir, msgHandler=printers[i].print,
                     restricted=True) for i, n in enumerate(names)]
    prepStacks(looper, *stacks, connect=True, useKeys=True)
    return stacks, printers
Beispiel #14
0
def testMessageQuota(set_info_log_level, tdir, looper):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)
    alphaP = Printer(names[0])
    betaMsgHandler = CollectingMsgsHandler()

    alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print,
                   restricted=True)
    beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=betaMsgHandler.handler,
                  restricted=True, onlyListener=True)

    prepStacks(looper, alpha, beta, connect=True, useKeys=True)

    messages = []
    numMessages = 150 * beta.listenerQuota
    for i in range(numMessages):
        msg = json.dumps({'random': randomSeed().decode()}).encode()
        if alpha.send(msg, beta.name):
            messages.append(json.loads(msg.decode()))

    def checkAllReceived():
        assert len(messages) == len(betaMsgHandler.receivedMessages)
        assert messages == betaMsgHandler.receivedMessages

    looper.run(eventually(checkAllReceived, retryWait=0.5,
                          timeout=5))
Beispiel #15
0
def testNodeConnection(allPluginsPath, tdirAndLooper, nodeReg, conf):
    console = getConsole()
    console.reinit(flushy=True, verbosity=console.Wordage.verbose)
    tdir, looper = tdirAndLooper
    names = ["Alpha", "Beta"]
    if conf.UseZStack:
        genKeys(tdir, names + [_ + CLIENT_STACK_SUFFIX for _ in names])

    logger.debug(names)
    nrg = {n: nodeReg[n] for n in names}
    A, B = [
        TestNode(name, nrg, basedirpath=tdir, pluginPaths=allPluginsPath)
        for name in names
    ]
    looper.add(A)
    looper.runFor(4)
    logger.debug("wait done")
    looper.add(B)
    looper.runFor(4)
    looper.run(checkNodesConnected([A, B]))
    looper.stopall()
    A.start(looper.loop)
    looper.runFor(4)
    B.start(looper.loop)
    looper.run(checkNodesConnected([A, B]))
    stopNodes([A, B], looper)
Beispiel #16
0
def testNodesConnectsWhenOneNodeIsLate(allPluginsPath, tdirAndLooper, nodeReg,
                                       conf):
    tdir, looper = tdirAndLooper
    nodes = []
    names = list(nodeReg.keys())
    logger.debug("Node names: {}".format(names))

    def create(name):
        node = TestNode(name,
                        nodeReg,
                        basedirpath=tdir,
                        pluginPaths=allPluginsPath)
        looper.add(node)
        nodes.append(node)

    # TODO: This will be moved to a fixture
    if conf.UseZStack:
        genKeys(tdir, names + [_ + CLIENT_STACK_SUFFIX for _ in names])

    for name in names[:3]:
        create(name)

    looper.run(checkNodesConnected(nodes))

    # wait for the election to complete with the first three nodes
    looper.runFor(10)

    # create the fourth and see that it learns who the primaries are
    # from the other nodes
    create(names[3])
    checkProtocolInstanceSetup(looper, nodes, timeout=10)
    stopNodes(nodes, looper)
Beispiel #17
0
def create_stacks(tdir, looper, reconnect_strategy):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)

    alpha = create_alpha(tdir, looper, names[0])
    beta, beta_msg_handler = create_beta(tdir, looper, names[1],
                                         start_stack=reconnect_strategy != 'reconnect')
    return alpha, beta, beta_msg_handler
def testSimpleZStacksMsgs(tdir, looper):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)
    names = ['Alpha', 'Beta']
    aseed = randomSeed()
    bseed = randomSeed()

    size = 100000
    msg = json.dumps({'random': randomSeed(size).decode()}).encode()

    def aHandler(m):
        str_m = "{}".format(m)
        print('{} printing... {}'.format(names[0], str_m[:100]))
        d, _ = m
        print('Message size is {}'.format(len(d['random'])))
        assert len(d['random']) == size

    def bHandler(m):
        print(beta.msgHandler)
        a = m[1]
        try:
            beta.listener.send_multipart([a, msg], flags=zmq.NOBLOCK)
        except zmq.Again:
            return False
        str_m = "{}".format(m)
        print('{} printing... {}'.format(names[1], str_m[:100]))

    stackParams = {
        "name": names[0],
        "ha": genHa(),
        "auto": 2,
        "basedirpath": tdir
    }
    alpha = SimpleZStack(stackParams, aHandler, aseed, False)

    stackParams = {
        "name": names[1],
        "ha": genHa(),
        "auto": 2,
        "basedirpath": tdir
    }
    beta = SimpleZStack(stackParams, bHandler, bseed, True)

    amotor = SMotor(alpha)
    looper.add(amotor)

    bmotor = SMotor(beta)
    looper.add(bmotor)

    alpha.connect(name=beta.name,
                  ha=beta.ha,
                  verKeyRaw=beta.verKeyRaw,
                  publicKeyRaw=beta.publicKeyRaw)

    looper.runFor(0.25)
    alpha.send({'greetings': 'hi'}, beta.name)
    looper.runFor(1)
def testSimpleZStacksMsgs(tdir, looper):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)
    names = ['Alpha', 'Beta']
    aseed = randomSeed()
    bseed = randomSeed()

    size = 100000
    msg = json.dumps({'random': randomSeed(size).decode()}).encode()

    def aHandler(m):
        str_m = "{}".format(m)
        print('{} printing... {}'.format(names[0], str_m[:100]))
        d, _ = m
        print('Message size is {}'.format(len(d['random'])))
        assert len(d['random']) == size

    def bHandler(m):
        print(beta.msgHandler)
        a = m[1]
        try:
            beta.listener.send_multipart([a, msg],
                                         flags=zmq.NOBLOCK)
        except zmq.Again:
            return False
        str_m = "{}".format(m)
        print('{} printing... {}'.format(names[1], str_m[:100]))

    stackParams = {
        "name": names[0],
        "ha": genHa(),
        "auto": 2,
        "basedirpath": tdir
    }
    alpha = SimpleZStack(stackParams, aHandler, aseed, False)

    stackParams = {
        "name": names[1],
        "ha": genHa(),
        "auto": 2,
        "basedirpath": tdir
    }
    beta = SimpleZStack(stackParams, bHandler, bseed, True)

    amotor = SMotor(alpha)
    looper.add(amotor)

    bmotor = SMotor(beta)
    looper.add(bmotor)

    alpha.connect(name=beta.name, ha=beta.ha,
                  verKeyRaw=beta.verKeyRaw, publicKeyRaw=beta.publicKeyRaw)

    looper.runFor(0.25)
    alpha.send({'greetings': 'hi'}, beta.name)
    looper.runFor(1)
Beispiel #20
0
def testZStackRecvHugeDataOverLimit(set_info_log_level, tdir, looper, tconf):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)

    # we use json serializer now, so final form will be {'k':'vvv...vvv'}
    # so here we try to prepare exactly tconf.MSG_LEN_LIMIT + 1 bytes after serialization
    msg = {'k': 'v' * (tconf.MSG_LEN_LIMIT - len("{'k':''}") + 1)}

    betaHandlers = [False, False]

    def recvHandlerAlpha(wrpMsg):
        pass

    def recvHandlerBeta(wrpMsg):
        rmsg, frm = wrpMsg
        betaHandlers[0] = True
        assert frm is not None
        assert rmsg is not None

    def rejectHandlerBeta(reason, frm):
        betaHandlers[1] = True
        assert 'exceeded allowed limit of {}'.format(
            tconf.MSG_LEN_LIMIT) in reason
        assert frm == 'Alpha'

    alpha = ZStack(names[0],
                   ha=genHa(),
                   basedirpath=tdir,
                   msgHandler=recvHandlerAlpha,
                   restricted=True,
                   config=adict(**tconf.__dict__),
                   msgRejectHandler=None)

    beta = ZStack(names[1],
                  ha=genHa(),
                  basedirpath=tdir,
                  msgHandler=recvHandlerBeta,
                  restricted=True,
                  config=adict(**tconf.__dict__),
                  msgRejectHandler=rejectHandlerBeta)

    bytemsg = alpha.serializeMsg(msg)
    assert len(bytemsg) == (tconf.MSG_LEN_LIMIT + 1)

    prepStacks(looper, *(alpha, beta), connect=True, useKeys=True)

    stat = alpha._remotes['Beta'].socket.send(bytemsg, copy=False, track=True)
    assert stat

    looper.runFor(5)

    assert betaHandlers[0] is False
    assert betaHandlers[1] is True
def test_stashing_unknown_remote_msgs(looper, tdir, tconf):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)

    alpha = ZStack(names[0],
                   ha=genHa(),
                   basedirpath=tdir,
                   msgHandler=None,
                   restricted=False)

    beta_msg_handler = CounterMsgsHandler()
    beta = ZStack(names[1],
                  ha=genHa(),
                  basedirpath=tdir,
                  msgHandler=beta_msg_handler.handler,
                  restricted=False)

    prepStacks(looper, alpha, beta, connect=False)

    assert not alpha.hasRemote(beta.name)
    assert not alpha.isConnectedTo(beta.name)
    assert not beta.hasRemote(alpha.name)
    assert not beta.isConnectedTo(alpha.name)

    alpha.connect(name=beta.name,
                  ha=beta.ha,
                  verKeyRaw=beta.verKeyRaw,
                  publicKeyRaw=beta.publicKeyRaw)
    alpha.getRemote(beta.name, beta.ha).setConnected()

    assert alpha.hasRemote(beta.name)
    assert alpha.isConnectedTo(beta.name)
    assert not beta.hasRemote(alpha.name)
    assert not beta.isConnectedTo(alpha.name)

    def check_unknown_remote_msg():
        assert len(alpha._stashed_to_disconnected) == 0
        assert len(beta._stashed_unknown_remote_msgs) == len(sent_msgs)
        for index, item in enumerate(sent_msgs):
            assert item == beta._stashed_unknown_remote_msgs[index][0]
            assert alpha.remotes[
                'Beta'].socket.IDENTITY == beta._stashed_unknown_remote_msgs[
                    index][1]

    sent_msgs = deque(maxlen=tconf.ZMQ_STASH_UNKNOWN_REMOTE_MSGS_QUEUE_SIZE)
    msg = 'message num: {}'
    for i in range(5):
        _msg = msg.format(i)
        alpha.send(_msg)
        sent_msgs.append(_msg)
        looper.run(
            eventually(check_unknown_remote_msg, retryWait=1, timeout=60))
Beispiel #22
0
def testKitZStacksConnected(registry, tdir, looper):
    genKeys(tdir, registry.keys())
    stacks = []
    for name, ha in registry.items():
        printer = Printer(name)
        stackParams = dict(name=name, ha=ha, basedirpath=tdir, auto=0)
        reg = copy(registry)
        reg.pop(name)
        stack = KITZStack(stackParams, printer.print, reg)
        stacks.append(stack)

    prepStacks(looper, *stacks, connect=False, useKeys=True)
    looper.run(eventually(checkStacksConnected, stacks, retryWait=1,
                          timeout=10))
Beispiel #23
0
def testKitZStacksCommunication(registry, tdir, looper):
    # TODO: Use a packet capture tool
    genKeys(tdir, registry.keys())
    stacks = []
    names = []
    for name, ha in registry.items():
        printer = Printer(name)
        stackParams = dict(name=name, ha=ha, basedirpath=tdir,
                           auth_mode=AuthMode.RESTRICTED.value)
        reg = copy(registry)
        reg.pop(name)
        stack = KITZStack(stackParams, printer.print, reg)
        stacks.append(stack)
        names.append((name, printer))

    prepStacks(looper, *stacks, connect=False, useKeys=True)
    # TODO: the connection may not be established for the first try because
    # some of the stacks may not have had a remote yet (that is they haven't had yet called connect)
    timeout = 4 * KITZStack.RETRY_TIMEOUT_RESTRICTED + 1
    looper.run(eventually(
        checkStacksConnected, stacks, retryWait=1, timeout=timeout))

    def send_recv():
        for i, j in combinations(range(len(stacks)), 2):
            alpha = stacks[i]
            beta = stacks[j]

            alpha.send({'greetings': 'hi'}, beta.name)
            beta.send({'greetings': 'hello'}, alpha.name)

            looper.run(
                eventually(chkPrinted, names[i][1], {'greetings': 'hello'}, timeout=15))
            looper.run(eventually(chkPrinted, names[j][1], {
                       'greetings': 'hi'}, timeout=15))

            names[i][1].reset()
            names[j][1].reset()

    def pr():
        for stack in stacks:
            for r in stack.remotes.values():
                print(r._lastSocketEvents())

    for _ in range(100):
        # send_recv()
        looper.run(eventually(
            checkStacksConnected, stacks, retryWait=1, timeout=timeout))
        # pr()
        looper.runFor(30)
Beispiel #24
0
def testNodeRemoveUnknownRemote(allPluginsPath, tdirAndLooper, nodeReg, conf):
    """
    The nodes Alpha and Beta know about each other so they should connect but
    they should remove remote for C when it tries to connect to them
    """

    tdir, looper = tdirAndLooper
    names = ["Alpha", "Beta"]
    logger.debug(names)

    if conf.UseZStack:
        _names = names + ['Gamma']
        genKeys(tdir, _names + [_ + CLIENT_STACK_SUFFIX for _ in _names])

    nrg = {n: nodeReg[n] for n in names}
    A, B = [
        TestNode(name, nrg, basedirpath=tdir, pluginPaths=allPluginsPath)
        for name in names
    ]
    for node in (A, B):
        looper.add(node)
    looper.run(checkNodesConnected([A, B]))

    C = TestNode("Gamma", {
        **nrg,
        **{
            "Gamma": nodeReg["Gamma"]
        }
    },
                 basedirpath=tdir,
                 pluginPaths=allPluginsPath)
    looper.add(C)

    def chk():
        assert not C.nodestack.isKeySharing

    looper.run(eventually(chk, retryWait=2, timeout=22))
    stopNodes([
        C,
    ], looper)

    def chk():
        assert C.name not in B.nodestack.nameRemotes
        assert C.name not in A.nodestack.nameRemotes

    looper.run(eventually(chk, retryWait=2, timeout=5))
    stopNodes([A, B], looper)
Beispiel #25
0
def testNodesConnectWhenTheyAllStartAtOnce(allPluginsPath, tdirAndLooper,
                                           nodeReg, conf):
    tdir, looper = tdirAndLooper
    nodes = []
    if conf.UseZStack:
        names = list(nodeReg.keys())
        genKeys(tdir, names + [_ + CLIENT_STACK_SUFFIX for _ in names])

    for name in nodeReg:
        node = TestNode(name,
                        nodeReg,
                        basedirpath=tdir,
                        pluginPaths=allPluginsPath)
        looper.add(node)
        nodes.append(node)
    looper.run(checkNodesConnected(nodes))
    stopNodes(nodes, looper)
Beispiel #26
0
def testKitZStacksConnected(registry, tdir, looper, tconf):
    genKeys(tdir, registry.keys())
    stacks = []
    for name, ha in registry.items():
        printer = Printer(name)
        stackParams = dict(name=name, ha=ha, basedirpath=tdir, auth_mode=AuthMode.RESTRICTED.value)
        reg = copy(registry)
        reg.pop(name)
        stack = KITZStack(stackParams, printer.print, reg)
        stacks.append(stack)

    prepStacks(looper, *stacks, connect=False, useKeys=True)
    # TODO: the connection may not be established for the first try because
    # some of the stacks may not have had a remote yet (that is they haven't had yet called connect)
    timeout = 2*tconf.RETRY_TIMEOUT_RESTRICTED+1
    looper.run(eventually(
        checkStacksConnected, stacks, retryWait=1, timeout=timeout))
Beispiel #27
0
 def create_stack(count):
     genKeys(tdir, registry.keys())
     stacks = []
     for name, ha in registry.items():
         printer = Printer(name)
         stackParams = dict(name=name,
                            ha=ha,
                            basedirpath=tdir,
                            auth_mode=AuthMode.RESTRICTED.value)
         reg = copy(registry)
         reg.pop(name)
         stack = nodeStackClass(stackParams, printer.print, reg)
         stack.start()
         stacks.append(stack)
         if len(stacks) == count:
             break
     return stacks
def testKitZStacksConnected(registry, tdir, looper, tconf):
    genKeys(tdir, registry.keys())
    stacks = []
    for name, ha in registry.items():
        printer = Printer(name)
        stackParams = dict(name=name, ha=ha, basedirpath=tdir,
                           auth_mode=AuthMode.RESTRICTED.value)
        reg = copy(registry)
        reg.pop(name)
        stack = KITZStack(stackParams, printer.print, reg)
        stacks.append(stack)

    prepStacks(looper, *stacks, connect=False, useKeys=True)
    # TODO: the connection may not be established for the first try because
    # some of the stacks may not have had a remote yet (that is they haven't had yet called connect)
    timeout = 2 * tconf.RETRY_TIMEOUT_RESTRICTED + 1
    looper.run(eventually(
        checkStacksConnected, stacks, retryWait=1, timeout=timeout))
def test_no_size_limit_for_outbound_msgs(looper, tdir, msg):

    names = ['Alpha', 'Beta']
    genKeys(tdir, names)

    alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=None, restricted=False)
    alpha.msgLenVal.max_allowed = MSG_LEN_LIMIT

    beta_msg_handler = CounterMsgsHandler()
    beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=beta_msg_handler.handler, restricted=False)

    prepStacks(looper, alpha, beta)

    def check_received(value):
        assert beta_msg_handler.receivedMsgCount == value

    alpha.send(msg)
    looper.run(eventually(check_received, 1, retryWait=1, timeout=10))
def connected_stacks(registry, tdir, looper, connection_timeout):
    genKeys(tdir, registry.keys())
    stacks = []
    for name, ha in registry.items():
        printer = Printer(name)
        stackParams = dict(name=name, ha=ha, basedirpath=tdir,
                           auth_mode=AuthMode.RESTRICTED.value)
        reg = copy(registry)
        reg.pop(name)
        stack = KITZStack(stackParams, printer.print, reg)
        stacks.append(stack)

    motors = prepStacks(looper, *stacks, connect=False, useKeys=True)

    looper.run(eventually(
        checkStacksConnected, stacks, retryWait=1, timeout=connection_timeout))

    return stacks, motors
Beispiel #31
0
def testZStackRecvHugeDataOverLimit(set_info_log_level, tdir, looper, tconf):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)

    # we use json serializer now, so final form will be {'k':'vvv...vvv'}
    # so here we try to prepare exactly tconf.MSG_LEN_LIMIT + 1 bytes after serialization
    msg = {'k': 'v' * (tconf.MSG_LEN_LIMIT - len("{'k':''}") + 1)}

    betaHandlers = [False, False]

    def recvHandlerAlpha(wrpMsg):
        pass

    def recvHandlerBeta(wrpMsg):
        rmsg, frm = wrpMsg
        betaHandlers[0] = True
        assert frm is not None
        assert rmsg is not None

    def rejectHandlerBeta(reason, frm):
        betaHandlers[1] = True
        assert 'exceeded allowed limit of {}'.format(
            tconf.MSG_LEN_LIMIT) in reason
        assert frm == 'Alpha'

    alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerAlpha, restricted=True,
                   config=adict(**tconf.__dict__), msgRejectHandler=None)

    beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerBeta, restricted=True,
                  config=adict(**tconf.__dict__), msgRejectHandler=rejectHandlerBeta)

    bytemsg = alpha.serializeMsg(msg)
    assert len(bytemsg) == (tconf.MSG_LEN_LIMIT + 1)

    prepStacks(looper, *(alpha, beta), connect=True, useKeys=True)

    stat = alpha._remotes['Beta'].socket.send(bytemsg, copy=False, track=True)
    assert stat

    looper.runFor(5)

    assert betaHandlers[0] is False
    assert betaHandlers[1] is True
Beispiel #32
0
def testNodesComingUpAtDifferentTimes(allPluginsPath, tdirAndLooper, nodeReg,
                                      conf):
    console = getConsole()
    console.reinit(flushy=True, verbosity=console.Wordage.verbose)
    tdir, looper = tdirAndLooper

    nodes = []

    names = list(nodeReg.keys())
    if conf.UseZStack:
        genKeys(tdir, names + [_ + CLIENT_STACK_SUFFIX for _ in names])

    shuffle(names)
    waits = [randint(1, 10) for _ in names]
    rwaits = [randint(1, 10) for _ in names]

    for i, name in enumerate(names):
        node = TestNode(name,
                        nodeReg,
                        basedirpath=tdir,
                        pluginPaths=allPluginsPath)
        looper.add(node)
        nodes.append(node)
        looper.runFor(waits[i])
    looper.run(checkNodesConnected(nodes, overrideTimeout=10))
    logger.debug("connects")
    logger.debug("node order: {}".format(names))
    logger.debug("waits: {}".format(waits))

    stopNodes(nodes, looper)

    # # Giving some time for sockets to close, use eventually
    # time.sleep(1)

    for i, n in enumerate(nodes):
        n.start(looper.loop)
        looper.runFor(rwaits[i])
    looper.runFor(3)
    looper.run(checkNodesConnected(nodes, overrideTimeout=10))
    stopNodes(nodes, looper)
    logger.debug("reconnects")
    logger.debug("node order: {}".format(names))
    logger.debug("rwaits: {}".format(rwaits))
def create_stacks(tdir, looper):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)

    alphaP = Printer(names[0])
    alpha = ZStack(names[0],
                   ha=genHa(),
                   basedirpath=tdir,
                   msgHandler=alphaP.print,
                   restricted=True)
    prepStacks(looper, alpha, connect=False)

    beta_msg_handler = CounterMsgsHandler()
    beta = ZStack(names[1],
                  ha=genHa(),
                  basedirpath=tdir,
                  msgHandler=beta_msg_handler.handler,
                  restricted=True)
    prepStacks(looper, beta, connect=False)

    return alpha, beta
def stacks(tdir, looper, alpha_handler):
    names = ['Alpha', 'Beta']
    genKeys(tdir, names)
    aseed = randomSeed()
    bseed = randomSeed()

    def bHandler(m):
        msg, a = m
        beta.send(msg, a)

    stackParams = {
        "name": names[0],
        "ha": genHa(),
        "auto": 2,
        "basedirpath": tdir
    }
    timer = MockTimer(0)
    alpha = SimpleZStack(stackParams,
                         alpha_handler.handle,
                         aseed,
                         False,
                         timer=timer)

    stackParams = {
        "name": names[1],
        "ha": genHa(),
        "auto": 2,
        "basedirpath": tdir
    }
    timer = MockTimer(0)
    beta = SimpleZStack(stackParams, bHandler, bseed, True, timer=timer)

    amotor = SMotor(alpha)
    looper.add(amotor)

    bmotor = SMotor(beta)
    looper.add(bmotor)
    return alpha, beta
def generated_keys(tdir):
    genKeys(tdir, REGISTRY.keys())