TestHelper.py 8.4 KB
Newer Older
1
from testUtils import Utils
2 3
from Cluster import Cluster
from WalletMgr import WalletMgr
4 5
from datetime import datetime
import platform
6 7 8

import argparse

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
class AppArgs:
    def __init__(self):
        self.args=[]

    class AppArg:
        def __init__(self, flag, type, help, default, choices=None):
            self.flag=flag
            self.type=type
            self.help=help
            self.default=default
            self.choices=choices

    def add(self, flag, type, help, default, choices=None):
        arg=self.AppArg(flag, type, help, default, choices)
        self.args.append(arg)

25 26 27 28
# pylint: disable=too-many-instance-attributes
class TestHelper(object):
    LOCAL_HOST="localhost"
    DEFAULT_PORT=8888
29
    DEFAULT_WALLET_PORT=9899
30 31 32 33

    @staticmethod
    # pylint: disable=too-many-branches
    # pylint: disable=too-many-statements
34
    def parse_args(includeArgs, applicationSpecificArgs=AppArgs()):
35 36 37
        """Accepts set of arguments, builds argument parser and returns parse_args() output."""
        assert(includeArgs)
        assert(isinstance(includeArgs, set))
38
        assert(isinstance(applicationSpecificArgs, AppArgs))
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

        parser = argparse.ArgumentParser(add_help=False)
        parser.add_argument('-?', action='help', default=argparse.SUPPRESS,
                                 help=argparse._('show this help message and exit'))

        if "-p" in includeArgs:
            parser.add_argument("-p", type=int, help="producing nodes count", default=1)
        if "-n" in includeArgs:
            parser.add_argument("-n", type=int, help="total nodes", default=0)
        if "-d" in includeArgs:
            parser.add_argument("-d", type=int, help="delay between nodes startup", default=1)
        if "--nodes-file" in includeArgs:
            parser.add_argument("--nodes-file", type=str, help="File containing nodes info in JSON format.")
        if "-s" in includeArgs:
            parser.add_argument("-s", type=str, help="topology", choices=["mesh"], default="mesh")
        if "-c" in includeArgs:
            parser.add_argument("-c", type=str, help="chain strategy",
                    choices=[Utils.SyncResyncTag, Utils.SyncNoneTag, Utils.SyncHardReplayTag],
                    default=Utils.SyncResyncTag)
        if "--kill-sig" in includeArgs:
            parser.add_argument("--kill-sig", type=str, choices=[Utils.SigKillTag, Utils.SigTermTag], help="kill signal.",
                    default=Utils.SigKillTag)
        if "--kill-count" in includeArgs:
            parser.add_argument("--kill-count", type=int, help="nodeos instances to kill", default=-1)
        if "--p2p-plugin" in includeArgs:
            parser.add_argument("--p2p-plugin", choices=["net", "bnet"], help="select a p2p plugin to use. Defaults to net.", default="net")
        if "--seed" in includeArgs:
            parser.add_argument("--seed", type=int, help="random seed", default=1)

        if "--host" in includeArgs:
            parser.add_argument("-h", "--host", type=str, help="%s host name" % (Utils.EosServerName),
                                     default=TestHelper.LOCAL_HOST)
        if "--port" in includeArgs:
            parser.add_argument("-p", "--port", type=int, help="%s host port" % Utils.EosServerName,
                                     default=TestHelper.DEFAULT_PORT)
74 75 76 77 78 79
        if "--wallet-host" in includeArgs:
            parser.add_argument("--wallet-host", type=str, help="%s host" % Utils.EosWalletName,
                                     default=TestHelper.LOCAL_HOST)
        if "--wallet-port" in includeArgs:
            parser.add_argument("--wallet-port", type=int, help="%s port" % Utils.EosWalletName,
                                     default=TestHelper.DEFAULT_WALLET_PORT)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        if "--prod-count" in includeArgs:
            parser.add_argument("-c", "--prod-count", type=int, help="Per node producer count", default=1)
        if "--defproducera_prvt_key" in includeArgs:
            parser.add_argument("--defproducera_prvt_key", type=str, help="defproducera private key.")
        if "--defproducerb_prvt_key" in includeArgs:
            parser.add_argument("--defproducerb_prvt_key", type=str, help="defproducerb private key.")
        if "--mongodb" in includeArgs:
            parser.add_argument("--mongodb", help="Configure a MongoDb instance", action='store_true')
        if "--dump-error-details" in includeArgs:
            parser.add_argument("--dump-error-details",
                                     help="Upon error print etc/eosio/node_*/config.ini and var/lib/node_*/stderr.log to stdout",
                                     action='store_true')
        if "--dont-launch" in includeArgs:
            parser.add_argument("--dont-launch", help="Don't launch own node. Assume node is already running.",
                                     action='store_true')
        if "--keep-logs" in includeArgs:
            parser.add_argument("--keep-logs", help="Don't delete var/lib/node_* folders upon test completion",
                                     action='store_true')
        if "-v" in includeArgs:
            parser.add_argument("-v", help="verbose logging", action='store_true')
        if "--leave-running" in includeArgs:
            parser.add_argument("--leave-running", help="Leave cluster running after test finishes", action='store_true')
        if "--only-bios" in includeArgs:
            parser.add_argument("--only-bios", help="Limit testing to bios node.", action='store_true')
        if "--clean-run" in includeArgs:
            parser.add_argument("--clean-run", help="Kill all nodeos and kleos instances", action='store_true')
106 107
        if "--sanity-test" in includeArgs:
            parser.add_argument("--sanity-test", help="Validates nodeos and kleos are in path and can be started up.", action='store_true')
108

109 110 111
        for arg in applicationSpecificArgs.args:
            parser.add_argument(arg.flag, type=arg.type, help=arg.help, choices=arg.choices, default=arg.default)

112 113 114
        args = parser.parse_args()
        return args

115 116 117 118 119 120 121 122 123 124 125
    @staticmethod
    def printSystemInfo(prefix):
        """Print system information to stdout. Print prefix first."""
        if prefix:
            Utils.Print(str(prefix))
        clientVersion=Cluster.getClientVersion()
        Utils.Print("UTC time: %s" % str(datetime.utcnow()))
        Utils.Print("EOS Client version: %s" % (clientVersion))
        Utils.Print("Processor: %s" % (platform.processor()))
        Utils.Print("OS name: %s" % (platform.platform()))
    
126 127 128 129 130 131 132 133 134 135 136 137 138 139
    @staticmethod
    # pylint: disable=too-many-arguments
    def shutdown(cluster, walletMgr, testSuccessful=True, killEosInstances=True, killWallet=True, keepLogs=False, cleanRun=True, dumpErrorDetails=False):
        """Cluster and WalletMgr shutdown and cleanup."""
        assert(cluster)
        assert(isinstance(cluster, Cluster))
        if walletMgr:
            assert(isinstance(walletMgr, WalletMgr))
        assert(isinstance(testSuccessful, bool))
        assert(isinstance(killEosInstances, bool))
        assert(isinstance(killWallet, bool))
        assert(isinstance(cleanRun, bool))
        assert(isinstance(dumpErrorDetails, bool))

140 141
        Utils.ShuttingDown=True

142 143 144 145 146
        if testSuccessful:
            Utils.Print("Test succeeded.")
        else:
            Utils.Print("Test failed.")
        if not testSuccessful and dumpErrorDetails:
147
            cluster.reportStatus()
148 149 150 151
            cluster.dumpErrorDetails()
            if walletMgr:
                walletMgr.dumpErrorDetails()
            Utils.Print("== Errors see above ==")
152 153
            if len(Utils.CheckOutputDeque)>0:
                Utils.Print("== cout/cerr pairs from last %d calls to Utils. ==" % len(Utils.CheckOutputDeque))
B
Brian Johnson 已提交
154 155
                for out, err, cmd in reversed(Utils.CheckOutputDeque):
                    Utils.Print("cmd={%s}" % (" ".join(cmd)))
156
                    Utils.Print("cout={%s}" % (out))
B
Brian Johnson 已提交
157 158
                    Utils.Print("cerr={%s}\n" % (err))
                Utils.Print("== cmd/cout/cerr pairs done. ==")
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

        if killEosInstances:
            Utils.Print("Shut down the cluster.")
            cluster.killall(allInstances=cleanRun)
            if testSuccessful and not keepLogs:
                Utils.Print("Cleanup cluster data.")
                cluster.cleanup()

        if walletMgr and killWallet:
            Utils.Print("Shut down the wallet.")
            walletMgr.killall(allInstances=cleanRun)
            if testSuccessful and not keepLogs:
                Utils.Print("Cleanup wallet data.")
                walletMgr.cleanup()