Esempio n. 1
0
def newKeyPair(cli: TestCli, alias: str = None):
    cmd = "new key {}".format(alias) if alias else "new key"
    idrs = set()
    if cli.activeWallet:
        idrs = set(cli.activeWallet.idsToSigners.keys())
    checkCmdValid(cli, cmd)
    assert len(cli.activeWallet.idsToSigners.keys()) == len(idrs) + 1
    pubKey = set(cli.activeWallet.idsToSigners.keys()).difference(idrs).pop()
    expected = ["Key created in keyring Default"]
    if alias:
        expected.append("Identifier for key is {}".format(cli.activeWallet.aliasesToIds.get(alias)))
        expected.append("Alias for identifier is {}".format(alias))
    else:
        expected.append("Identifier for key is {}".format(pubKey))
    expected.append("Current identifier set to {}".format(alias or pubKey))

    # TODO: Reconsider this
    # Using `in` rather than `=` so as to take care of the fact that this might
    # be the first time wallet is accessed so wallet would be created and some
    # output corresponding to that would be printed.
    assert "\n".join(expected) in cli.lastCmdOutput

    # the public key and alias are listed
    cli.enterCmd("list ids")
    needle = alias if alias else pubKey
    # assert cli.lastMsg().split("\n")[0] == alias if alias else pubKey
    assert needle in cli.lastCmdOutput
    return pubKey
Esempio n. 2
0
def newKeyPair(cli: TestCli, alias: str=None):
    cmd = "new key {}".format(alias) if alias else "new key"
    idrs = set()
    if cli.activeWallet:
        idrs = set(cli.activeWallet.idsToSigners.keys())
    checkCmdValid(cli, cmd)
    assert len(cli.activeWallet.idsToSigners.keys()) == len(idrs) + 1
    pubKey = set(cli.activeWallet.idsToSigners.keys()).difference(idrs).pop()
    expected = ['Key created in keyring Default']
    if alias:
        expected.append('Identifier for key is {}'.
                        format(cli.activeWallet.aliasesToIds.get(alias)))
        expected.append('Alias for identifier is {}'.format(alias))
    else:
        expected.append('Identifier for key is {}'.format(pubKey))
    expected.append('Current identifier set to {}'.format(alias or pubKey))

    # TODO: Reconsider this
    # Using `in` rather than `=` so as to take care of the fact that this might
    # be the first time wallet is accessed so wallet would be created and some
    # output corresponding to that would be printed.
    assert "\n".join(expected) in cli.lastCmdOutput

    # the public key and alias are listed
    cli.enterCmd("list ids")
    needle = alias if alias else pubKey
    # assert cli.lastMsg().split("\n")[0] == alias if alias else pubKey
    assert needle in cli.lastCmdOutput
    return pubKey
Esempio n. 3
0
def _newStewardsAddedByValue(cli):
    cli.enterCmd(
        "add genesis transaction {nym} for 59d9225473451efffe6b36dbcaefdbf7b1895de62084509a7f5b58bf01d06420 role={role}"
        .format(nym=NYM, role=Roles.STEWARD.name))
    cli.enterCmd(
        'add genesis transaction {nym} for 59d9225473451efffe6b36dbcaefdbf7b1895de62084509a7f5b58bf01d06421 '
        'with data {{"alias": "Ty"}} role={role}'.format(
            nym=NYM, role=Roles.STEWARD.name))
Esempio n. 4
0
def createClientAndConnect(cli, nodeNames, clientName):
    cli.enterCmd("new client {}".format(clientName))
    createNewKeyring(clientName, cli)
    cli.enterCmd("new key clientName{}".format("key"))
    cli.looper.run(
        eventually(checkClientConnected,
                   cli,
                   nodeNames,
                   clientName,
                   retryWait=1,
                   timeout=3))
Esempio n. 5
0
def _newNodesAddedByValue(cli):
    cli.enterCmd(
        'add genesis transaction {node} for 59d9225473451efffe6b36dbcaefdbf7b1895de62084509a7f5b58bf01d06420 by 59d9225473451efffe6b36dbcaefdbf7b1895de62084509a7f5b58bf01d06420 with data '
        '{{"node_ip": "localhost", "node_port": "9701", "client_ip": "localhost", '
        '"client_port": "9702", '
        '"alias": "PhilNode"}}'.format(node=NODE))
    cli.enterCmd(
        'add genesis transaction {node} for 59d9225473451efffe6b36dbcaefdbf7b1895de62084509a7f5b58bf01d06421 by 59d9225473451efffe6b36dbcaefdbf7b1895de62084509a7f5b58bf01d06420 with data '
        '{{"node_ip": "localhost", "node_port": "9701", "client_ip": "localhost", '
        '"client_port": "9702", '
        '"alias": "PhilNode"}}'.format(node=NODE))
Esempio n. 6
0
def checkRequest(cli, operation):
    cName = "Joe"
    cli.enterCmd("new client {}".format(cName))
    # Let client connect to the nodes
    cli.looper.run(eventually(checkClientConnected, cli, list(cli.nodes.keys()), cName, retryWait=1, timeout=5))
    # Send request to all nodes

    createNewKeyring(cName, cli)

    cli.enterCmd("new key {}".format("testkey1"))
    assert "Key created in keyring {}".format(cName) in cli.lastCmdOutput

    cli.enterCmd("client {} send {}".format(cName, operation))
    client = cli.clients[cName]
    wallet = cli.wallets[cName]  # type: Wallet
    f = getMaxFailures(len(cli.nodes))
    # Ensure client gets back the replies
    lastReqId = wallet._getIdData().lastReqId
    cli.looper.run(eventually(checkSufficientRepliesRecvd, client.inBox, lastReqId, f, retryWait=2, timeout=10))

    txn, status = client.getReply(wallet.defaultId, lastReqId)

    # Ensure the cli shows appropriate output
    cli.enterCmd("client {} show {}".format(cName, lastReqId))
    printeds = cli.printeds
    printedReply = printeds[1]
    printedStatus = printeds[0]
    # txnTimePattern = "'txnTime', \d+\.*\d*"
    # txnIdPattern = "'txnId', '" + txn['txnId'] + "'"
    txnTimePattern = "'txnTime': \d+\.*\d*"
    txnIdPattern = "'txnId': '" + txn["txnId"] + "'"
    assert re.search(txnIdPattern, printedReply["msg"])
    assert re.search(txnTimePattern, printedReply["msg"])
    assert printedStatus["msg"] == "Status: {}".format(status)
    return client, wallet
Esempio n. 7
0
def checkRequest(cli, looper, operation):
    cName = "Joe"
    cli.enterCmd("new client {}".format(cName))
    # Let client connect to the nodes
    looper.runFor(3)
    # Send request to all nodes
    cli.enterCmd('client {} send {}'.format(cName, operation))
    client = cli.clients[cName]
    f = getMaxFailures(len(cli.nodes))
    # Ensure client gets back the replies
    looper.run(eventually(
            checkSufficientRepliesRecvd,
            client.inBox,
            client.lastReqId,
            f,
            retryWait=2,
            timeout=30))

    txn, status = client.getReply(client.lastReqId)

    # Ensure the cli shows appropriate output
    cli.enterCmd('client {} show {}'.format(cName, client.lastReqId))
    printeds = cli.printeds
    printedReply = printeds[1]
    printedStatus = printeds[0]
    txnTimePattern = "\'txnTime\': \d+\.*\d*"
    txnIdPattern = "\'txnId\': '" + txn['txnId'] + "'"
    # txnPattern1 = "Reply for the request: \{" + timePattern + ", " + txnIdPattern + "\}"
    # txnPattern2 = "Reply for the request: \{" + txnIdPattern + ", " + timePattern + "\}"
    # assert re.match(txnPattern1, printedReply['msg']) or \
    #        re.match(txnPattern2, printedReply['msg'])
    assert re.search(txnIdPattern, printedReply['msg'])
    assert re.search(txnTimePattern, printedReply['msg'])
    assert printedStatus['msg'] == "Status: {}".format(status)
Esempio n. 8
0
def checkRequest(cli, looper, operation):
    cName = "Joe"
    cli.enterCmd("new client {}".format(cName))
    # Let client connect to the nodes
    looper.runFor(3)
    # Send request to all nodes
    cli.enterCmd('client {} send {}'.format(cName, operation))
    client = cli.clients[cName]
    f = getMaxFailures(len(cli.nodes))
    # Ensure client gets back the replies
    looper.run(eventually(
            checkSufficientRepliesRecvd,
            client.inBox,
            client.lastReqId,
            f,
            retryWait=2,
            timeout=30))

    txn, status = client.getReply(client.lastReqId)

    # Ensure the cli shows appropriate output
    cli.enterCmd('client {} show {}'.format(cName, client.lastReqId))
    printeds = cli.printeds
    printedReply = printeds[1]
    printedStatus = printeds[0]
    assert printedReply['msg'] == "Reply for the request: {{'txnId': '{}" \
                                  "'}}".format(txn['txnId'])
    assert printedStatus['msg'] == "Status: {}".format(status)
Esempio n. 9
0
def checkRequest(cli, looper, operation):
    cName = "Joe"
    cli.enterCmd("new client {}".format(cName))
    # Let client connect to the nodes
    looper.runFor(3)
    # Send request to all nodes
    cli.enterCmd('client {} send {}'.format(cName, operation))
    client = cli.clients[cName]
    f = getMaxFailures(len(cli.nodes))
    # Ensure client gets back the replies
    looper.run(
        eventually(checkSufficientRepliesRecvd,
                   client.inBox,
                   client.lastReqId,
                   f,
                   retryWait=2,
                   timeout=30))

    txn, status = client.getReply(client.lastReqId)

    # Ensure the cli shows appropriate output
    cli.enterCmd('client {} show {}'.format(cName, client.lastReqId))
    printeds = cli.printeds
    printedReply = printeds[1]
    printedStatus = printeds[0]
    txnTimePattern = "\'txnTime\': \d+\.*\d*"
    txnIdPattern = "\'txnId\': '" + txn['txnId'] + "'"
    # txnPattern1 = "Reply for the request: \{" + timePattern + ", " + txnIdPattern + "\}"
    # txnPattern2 = "Reply for the request: \{" + txnIdPattern + ", " + timePattern + "\}"
    # assert re.match(txnPattern1, printedReply['msg']) or \
    #        re.match(txnPattern2, printedReply['msg'])
    assert re.search(txnIdPattern, printedReply['msg'])
    assert re.search(txnTimePattern, printedReply['msg'])
    assert printedStatus['msg'] == "Status: {}".format(status)
Esempio n. 10
0
def checkRequest(cli, operation):
    cName = "Joe"
    cli.enterCmd("new client {}".format(cName))
    # Let client connect to the nodes
    cli.looper.run(
        eventually(checkClientConnected,
                   cli,
                   list(cli.nodes.keys()),
                   cName,
                   retryWait=1,
                   timeout=5))
    # Send request to all nodes

    createNewKeyring(cName, cli)

    cli.enterCmd("new key {}".format("testkey1"))
    assert 'Key created in keyring {}'.format(cName) in cli.lastCmdOutput

    cli.enterCmd('client {} send {}'.format(cName, operation))
    client = cli.clients[cName]
    wallet = cli.wallets[cName]  # type: Wallet
    f = getMaxFailures(len(cli.nodes))
    # Ensure client gets back the replies
    lastReqId = wallet._getIdData().lastReqId
    cli.looper.run(
        eventually(checkSufficientRepliesRecvd,
                   client.inBox,
                   lastReqId,
                   f,
                   retryWait=2,
                   timeout=10))

    txn, status = client.getReply(wallet.defaultId, lastReqId)

    # Ensure the cli shows appropriate output
    cli.enterCmd('client {} show {}'.format(cName, lastReqId))
    printeds = cli.printeds
    printedReply = printeds[1]
    printedStatus = printeds[0]
    # txnTimePattern = "'txnTime', \d+\.*\d*"
    # txnIdPattern = "'txnId', '" + txn['txnId'] + "'"
    txnTimePattern = "\'txnTime\': \d+\.*\d*"
    txnIdPattern = "\'txnId\': '" + txn['txnId'] + "'"
    assert re.search(txnIdPattern, printedReply['msg'])
    assert re.search(txnTimePattern, printedReply['msg'])
    assert printedStatus['msg'] == "Status: {}".format(status)
    return client, wallet
Esempio n. 11
0
def checkRequest(cli, operation):
    cName = "Joe"
    cli.enterCmd("new client {}".format(cName))
    # Let client connect to the nodes

    nodeNames = list(cli.nodes.keys())
    waitClientConnected(cli, nodeNames, cName)

    # Send request to all nodes

    createNewKeyring(cName, cli)

    cli.enterCmd("new key {}".format("testkey1"))
    assert 'Key created in wallet {}'.format(cName) in cli.lastCmdOutput

    cli.enterCmd('client {} send {}'.format(cName, operation))
    client = cli.clients[cName]
    wallet = cli.wallets[cName]  # type: Wallet
    # Ensure client gets back the replies
    lastReqId = wallet._getIdData().lastReqId

    request = Request(identifier=wallet.defaultId,
                      reqId=lastReqId,
                      protocolVersion=CURRENT_PROTOCOL_VERSION)

    waitForSufficientRepliesForRequests(cli.looper, client,
                                        requests=[request])

    txn, status = client.getReply(wallet.defaultId, lastReqId)

    # Ensure the cli shows appropriate output
    cli.enterCmd('client {} show {}'.format(cName, lastReqId))
    printeds = cli.printeds
    printedReply = printeds[1]
    printedStatus = printeds[0]
    # txnTimePattern = "'txnTime', \d+\.*\d*"
    # txnIdPattern = "'txnId', '" + txn['txnId'] + "'"
    txnTimePattern = "\'txnTime\': \d+\.*\d*"
    # DEPR
    # txnIdPattern = "\'txnId\': '" + txn['txnId'] + "'"
    # assert re.search(txnIdPattern, printedReply['msg'])
    assert re.search(txnTimePattern, printedReply['msg'])
    assert printedStatus['msg'] == "Status: {}".format(status)
    return client, wallet
Esempio n. 12
0
def loadPlugin(cli, pluginPkgName):
    curPath = os.path.dirname(os.path.dirname(__file__))
    fullPath = os.path.join(curPath, 'plugin', pluginPkgName)
    cli.enterCmd("load plugins from {}".format(fullPath))
    m = pluginLoadedPat.search(cli.printeds[0]['msg'])
    assert m
Esempio n. 13
0
def createClientAndConnect(cli, nodeNames, clientName):
    cli.enterCmd("new client {}".format(clientName))
    createNewKeyring(clientName, cli)
    cli.enterCmd("new key clientName{}".format("key"))
    cli.looper.run(eventually(checkClientConnected, cli, nodeNames, clientName, retryWait=1, timeout=3))
Esempio n. 14
0
def checkCmdValid(cli, cmd):
    cli.enterCmd(cmd)
    assert 'Invalid command' not in cli.lastCmdOutput
Esempio n. 15
0
def checkCmdValid(cli, cmd):
    cli.enterCmd(cmd)
    assert "Invalid command" not in cli.lastCmdOutput
Esempio n. 16
0
    def _(attempt, expect=None, within=None, mapper=None, not_expect=None):
        assert expect is not None or within is None, \
            "'within' not applicable without 'expect'"
        cli = ctx['current_cli']

        # This if was not there earlier, but I felt a need to reuse this
        # feature (be, do, expect ...) without attempting anything
        # mostly because there will be something async which will do something,
        # hence I added the below if check

        if attempt:
            attempt = attempt.format(**mapper) if mapper else attempt
            cli.enterCmd(attempt)

        def getAssertErrorMsg(e, cli, exp: bool, actual: bool):
            length = 80
            sepLines = "\n" + "*" * length + "\n" + "-" * length
            commonMsg = "\n{}\n{}".format(cli.lastCmdOutput, sepLines)
            prefix = ""
            if exp and not actual:
                prefix = "NOT found "
            elif not exp and actual:
                prefix = "FOUND "
            return "{}\n{}\n\n{} in\n {}".format(sepLines, e, prefix,
                                                 commonMsg)

        def check():
            nonlocal expect
            nonlocal not_expect

            def chk(obj, parity=True):
                if not obj:
                    return
                if isinstance(obj, str) or callable(obj):
                    obj = [obj]
                for e in obj:
                    if isinstance(e, str):
                        e = e.format(**mapper) if mapper else e
                        try:

                            if parity:
                                assert e in cli.lastCmdOutput, \
                                    getAssertErrorMsg(
                                        e, cli, exp=True, actual=False)
                            else:
                                assert e not in cli.lastCmdOutput, \
                                    getAssertErrorMsg(
                                        e, cli, exp=False, actual=True)
                        except AssertionError as e:
                            extraMsg = ""
                            if not within:
                                extraMsg = "NOTE: 'within' parameter was not " \
                                           "provided, if test should wait for" \
                                           " sometime before considering this" \
                                           " check failed, then provide that" \
                                           " parameter with appropriate value"
                                separator = "-" * len(extraMsg)
                                extraMsg = "\n\n{}\n{}\n{}".format(
                                    separator, extraMsg, separator)
                            raise (AssertionError("{}{}".format(e, extraMsg)))
                    elif callable(e):
                        # callables should raise exceptions to signal an error
                        if parity:
                            e(cli)
                        else:
                            try:
                                e(cli)
                            except BaseException:
                                # Since its a test so not using logger is not
                                # a big deal
                                traceback.print_exc()
                                continue
                            raise RuntimeError("did not expect success")
                    else:
                        raise AttributeError("only str, callable, or "
                                             "collections of str and callable "
                                             "are allowed")

            chk(expect)
            chk(not_expect, False)

        if within:
            cli.looper.run(eventually(check, timeout=within))
        else:
            check()
Esempio n. 17
0
def createClientAndConnect(cli, nodeNames, clientName):
    cli.enterCmd("new client {}".format(clientName))
    createNewKeyring(clientName, cli)
    cli.enterCmd("new key clientName{}".format("key"))

    waitClientConnected(cli, nodeNames, clientName)
Esempio n. 18
0
def loadPlugin(cli, pluginPkgName):
    curPath = os.path.dirname(os.path.dirname(__file__))
    fullPath = os.path.join(curPath, "plugin", pluginPkgName)
    cli.enterCmd("load plugins from {}".format(fullPath))
    m = pluginLoadedPat.search(cli.printeds[0]["msg"])
    assert m