]> Git Repo - VerusCoin.git/blob - qa/rpc-tests/test_framework/test_framework.py
Incorporate all Zcash updates through 2.0.7-3 in addition to PBaaS, reserves, etc.
[VerusCoin.git] / qa / rpc-tests / test_framework / test_framework.py
1 #!/usr/bin/env python2
2 # Copyright (c) 2014 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or https://www.opensource.org/licenses/mit-license.php .
5
6 # Base class for RPC testing
7
8 # Add python-bitcoinrpc to module search path:
9 import os
10 import sys
11
12 import shutil
13 import tempfile
14 import traceback
15
16 from authproxy import JSONRPCException
17 from util import assert_equal, check_json_precision, \
18     initialize_chain, initialize_chain_clean, \
19     start_nodes, connect_nodes_bi, stop_nodes, \
20     sync_blocks, sync_mempools, wait_bitcoinds
21
22
23 class BitcoinTestFramework(object):
24
25     # These may be over-ridden by subclasses:
26     def run_test(self):
27         for node in self.nodes:
28             assert_equal(node.getblockcount(), 200)
29             assert_equal(node.getbalance(), 25*10)
30
31     def add_options(self, parser):
32         pass
33
34     def setup_chain(self):
35         print("Initializing test directory "+self.options.tmpdir)
36         initialize_chain(self.options.tmpdir)
37
38     def setup_nodes(self):
39         return start_nodes(4, self.options.tmpdir)
40
41     def setup_network(self, split = False):
42         self.nodes = self.setup_nodes()
43
44         # Connect the nodes as a "chain".  This allows us
45         # to split the network between nodes 1 and 2 to get
46         # two halves that can work on competing chains.
47
48         # If we joined network halves, connect the nodes from the joint
49         # on outward.  This ensures that chains are properly reorganised.
50         if not split:
51             connect_nodes_bi(self.nodes, 1, 2)
52             sync_blocks(self.nodes[1:3])
53             sync_mempools(self.nodes[1:3])
54
55         connect_nodes_bi(self.nodes, 0, 1)
56         connect_nodes_bi(self.nodes, 2, 3)
57         self.is_network_split = split
58         self.sync_all()
59
60     def split_network(self):
61         """
62         Split the network of four nodes into nodes 0/1 and 2/3.
63         """
64         assert not self.is_network_split
65         stop_nodes(self.nodes)
66         wait_bitcoinds()
67         self.setup_network(True)
68
69     def sync_all(self):
70         if self.is_network_split:
71             sync_blocks(self.nodes[:2])
72             sync_blocks(self.nodes[2:])
73             sync_mempools(self.nodes[:2])
74             sync_mempools(self.nodes[2:])
75         else:
76             sync_blocks(self.nodes)
77             sync_mempools(self.nodes)
78
79     def join_network(self):
80         """
81         Join the (previously split) network halves together.
82         """
83         assert self.is_network_split
84         stop_nodes(self.nodes)
85         wait_bitcoinds()
86         self.setup_network(False)
87
88     def main(self):
89         import optparse
90
91         parser = optparse.OptionParser(usage="%prog [options]")
92         parser.add_option("--nocleanup", dest="nocleanup", default=False, action="store_true",
93                           help="Leave komodods and test.* datadir on exit or error")
94         parser.add_option("--noshutdown", dest="noshutdown", default=False, action="store_true",
95                           help="Don't stop komodods after the test execution")
96         parser.add_option("--srcdir", dest="srcdir", default="../../src",
97                           help="Source directory containing komodod/komodo-cli (default: %default)")
98         parser.add_option("--tmpdir", dest="tmpdir", default=tempfile.mkdtemp(prefix="test"),
99                           help="Root directory for datadirs")
100         parser.add_option("--tracerpc", dest="trace_rpc", default=False, action="store_true",
101                           help="Print out all RPC calls as they are made")
102         self.add_options(parser)
103         (self.options, self.args) = parser.parse_args()
104
105         if self.options.trace_rpc:
106             import logging
107             logging.basicConfig(level=logging.DEBUG)
108
109         os.environ['PATH'] = self.options.srcdir+":"+os.environ['PATH']
110
111         check_json_precision()
112
113         success = False
114         try:
115             if not os.path.isdir(self.options.tmpdir):
116                 os.makedirs(self.options.tmpdir)
117             self.setup_chain()
118
119             self.setup_network()
120
121             self.run_test()
122
123             success = True
124
125         except JSONRPCException as e:
126             print("JSONRPC error: "+e.error['message'])
127             traceback.print_tb(sys.exc_info()[2])
128         except AssertionError as e:
129             print("Assertion failed: "+e.message)
130             traceback.print_tb(sys.exc_info()[2])
131         except Exception as e:
132             print("Unexpected exception caught during testing: "+str(e))
133             traceback.print_tb(sys.exc_info()[2])
134
135         if not self.options.noshutdown:
136             print("Stopping nodes")
137             stop_nodes(self.nodes)
138             wait_bitcoinds()
139         else:
140             print("Note: komodods were not stopped and may still be running")
141
142         if not self.options.nocleanup and not self.options.noshutdown:
143             print("Cleaning up")
144             shutil.rmtree(self.options.tmpdir)
145
146         if success:
147             print("Tests successful")
148             sys.exit(0)
149         else:
150             print("Failed")
151             sys.exit(1)
152
153
154 # Test framework for doing p2p comparison testing, which sets up some komodod
155 # binaries:
156 # 1 binary: test binary
157 # 2 binaries: 1 test binary, 1 ref binary
158 # n>2 binaries: 1 test binary, n-1 ref binaries
159
160 class ComparisonTestFramework(BitcoinTestFramework):
161
162     # Can override the num_nodes variable to indicate how many nodes to run.
163     def __init__(self):
164         self.num_nodes = 2
165
166     def add_options(self, parser):
167         parser.add_option("--testbinary", dest="testbinary",
168                           default=os.getenv("BITCOIND", "komodod"),
169                           help="bitcoind binary to test")
170         parser.add_option("--refbinary", dest="refbinary",
171                           default=os.getenv("BITCOIND", "komodod"),
172                           help="bitcoind binary to use for reference nodes (if any)")
173
174     def setup_chain(self):
175         print "Initializing test directory "+self.options.tmpdir
176         initialize_chain_clean(self.options.tmpdir, self.num_nodes)
177
178     def setup_network(self):
179         self.nodes = start_nodes(self.num_nodes, self.options.tmpdir,
180                                     extra_args=[['-debug', '-whitelist=127.0.0.1']] * self.num_nodes,
181                                     binary=[self.options.testbinary] +
182                                            [self.options.refbinary]*(self.num_nodes-1))
This page took 0.035637 seconds and 4 git commands to generate.