Skip to content

Commit 6fc287f

Browse files
committed
Merge pull request #7027
4ea1790 [qa] keypool: DRY: Use test framework (MarcoFalke) c6973ca [qa] keypool: Fix white space to prepare transition to test framework (MarcoFalke)
2 parents 9ebedc1 + 4ea1790 commit 6fc287f

File tree

1 file changed

+57
-106
lines changed

1 file changed

+57
-106
lines changed

qa/rpc-tests/keypool.py

Lines changed: 57 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@
66
# Exercise the wallet keypool, and interaction with wallet encryption/locking
77

88
# Add python-bitcoinrpc to module search path:
9-
import os
10-
import sys
11-
12-
import json
13-
import shutil
14-
import subprocess
15-
import tempfile
16-
import traceback
179

10+
from test_framework.test_framework import BitcoinTestFramework
1811
from test_framework.util import *
1912

2013

@@ -39,107 +32,65 @@ def check_array_result(object_array, to_match, expected):
3932
if num_matched == 0:
4033
raise AssertionError("No objects matched %s"%(str(to_match)))
4134

42-
def run_test(nodes, tmpdir):
43-
# Encrypt wallet and wait to terminate
44-
nodes[0].encryptwallet('test')
45-
bitcoind_processes[0].wait()
46-
# Restart node 0
47-
nodes[0] = start_node(0, tmpdir)
48-
# Keep creating keys
49-
addr = nodes[0].getnewaddress()
50-
try:
51-
addr = nodes[0].getnewaddress()
52-
raise AssertionError('Keypool should be exhausted after one address')
53-
except JSONRPCException,e:
54-
assert(e.error['code']==-12)
55-
56-
# put three new keys in the keypool
57-
nodes[0].walletpassphrase('test', 12000)
58-
nodes[0].keypoolrefill(3)
59-
nodes[0].walletlock()
60-
61-
# drain the keys
62-
addr = set()
63-
addr.add(nodes[0].getrawchangeaddress())
64-
addr.add(nodes[0].getrawchangeaddress())
65-
addr.add(nodes[0].getrawchangeaddress())
66-
addr.add(nodes[0].getrawchangeaddress())
67-
# assert that four unique addresses were returned
68-
assert(len(addr) == 4)
69-
# the next one should fail
70-
try:
71-
addr = nodes[0].getrawchangeaddress()
72-
raise AssertionError('Keypool should be exhausted after three addresses')
73-
except JSONRPCException,e:
74-
assert(e.error['code']==-12)
75-
76-
# refill keypool with three new addresses
77-
nodes[0].walletpassphrase('test', 12000)
78-
nodes[0].keypoolrefill(3)
79-
nodes[0].walletlock()
35+
class KeyPoolTest(BitcoinTestFramework):
8036

81-
# drain them by mining
82-
nodes[0].generate(1)
83-
nodes[0].generate(1)
84-
nodes[0].generate(1)
85-
nodes[0].generate(1)
86-
try:
37+
def run_test(self):
38+
nodes = self.nodes
39+
# Encrypt wallet and wait to terminate
40+
nodes[0].encryptwallet('test')
41+
bitcoind_processes[0].wait()
42+
# Restart node 0
43+
nodes[0] = start_node(0, self.options.tmpdir)
44+
# Keep creating keys
45+
addr = nodes[0].getnewaddress()
46+
try:
47+
addr = nodes[0].getnewaddress()
48+
raise AssertionError('Keypool should be exhausted after one address')
49+
except JSONRPCException,e:
50+
assert(e.error['code']==-12)
51+
52+
# put three new keys in the keypool
53+
nodes[0].walletpassphrase('test', 12000)
54+
nodes[0].keypoolrefill(3)
55+
nodes[0].walletlock()
56+
57+
# drain the keys
58+
addr = set()
59+
addr.add(nodes[0].getrawchangeaddress())
60+
addr.add(nodes[0].getrawchangeaddress())
61+
addr.add(nodes[0].getrawchangeaddress())
62+
addr.add(nodes[0].getrawchangeaddress())
63+
# assert that four unique addresses were returned
64+
assert(len(addr) == 4)
65+
# the next one should fail
66+
try:
67+
addr = nodes[0].getrawchangeaddress()
68+
raise AssertionError('Keypool should be exhausted after three addresses')
69+
except JSONRPCException,e:
70+
assert(e.error['code']==-12)
71+
72+
# refill keypool with three new addresses
73+
nodes[0].walletpassphrase('test', 12000)
74+
nodes[0].keypoolrefill(3)
75+
nodes[0].walletlock()
76+
77+
# drain them by mining
8778
nodes[0].generate(1)
88-
raise AssertionError('Keypool should be exhausted after three addesses')
89-
except JSONRPCException,e:
90-
assert(e.error['code']==-12)
91-
92-
def main():
93-
import optparse
94-
95-
parser = optparse.OptionParser(usage="%prog [options]")
96-
parser.add_option("--nocleanup", dest="nocleanup", default=False, action="store_true",
97-
help="Leave bitcoinds and test.* datadir on exit or error")
98-
parser.add_option("--srcdir", dest="srcdir", default="../../src",
99-
help="Source directory containing bitcoind/bitcoin-cli (default: %default%)")
100-
parser.add_option("--tmpdir", dest="tmpdir", default=tempfile.mkdtemp(prefix="test"),
101-
help="Root directory for datadirs")
102-
(options, args) = parser.parse_args()
103-
104-
os.environ['PATH'] = options.srcdir+":"+os.environ['PATH']
105-
106-
check_json_precision()
107-
108-
success = False
109-
nodes = []
110-
try:
111-
print("Initializing test directory "+options.tmpdir)
112-
if not os.path.isdir(options.tmpdir):
113-
os.makedirs(options.tmpdir)
114-
initialize_chain(options.tmpdir)
115-
116-
nodes = start_nodes(1, options.tmpdir)
117-
118-
run_test(nodes, options.tmpdir)
119-
120-
success = True
121-
122-
except AssertionError as e:
123-
print("Assertion failed: "+e.message)
124-
except JSONRPCException as e:
125-
print("JSONRPC error: "+e.error['message'])
126-
traceback.print_tb(sys.exc_info()[2])
127-
except Exception as e:
128-
print("Unexpected exception caught during testing: "+str(sys.exc_info()[0]))
129-
traceback.print_tb(sys.exc_info()[2])
79+
nodes[0].generate(1)
80+
nodes[0].generate(1)
81+
nodes[0].generate(1)
82+
try:
83+
nodes[0].generate(1)
84+
raise AssertionError('Keypool should be exhausted after three addesses')
85+
except JSONRPCException,e:
86+
assert(e.error['code']==-12)
13087

131-
if not options.nocleanup:
132-
print("Cleaning up")
133-
stop_nodes(nodes)
134-
wait_bitcoinds()
135-
shutil.rmtree(options.tmpdir)
88+
def setup_chain(self):
89+
print("Initializing test directory "+self.options.tmpdir)
90+
initialize_chain(self.options.tmpdir)
13691

137-
if success:
138-
print("Tests successful")
139-
sys.exit(0)
140-
else:
141-
print("Failed")
142-
sys.exit(1)
92+
def setup_network(self):
93+
self.nodes = start_nodes(1, self.options.tmpdir)
14394

14495
if __name__ == '__main__':
145-
main()
96+
KeyPoolTest().main()

0 commit comments

Comments
 (0)