Skip to content

Commit 2495110

Browse files
committed
test: add coverage for -rpcwallet cli option
and add -getinfo coverage with multiple wallets loaded.
1 parent 9ea4d83 commit 2495110

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

test/functional/interface_bitcoin_cli.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test bitcoin-cli"""
6+
from decimal import Decimal
67
from test_framework.test_framework import BitcoinTestFramework
78
from test_framework.util import assert_equal, assert_raises_process_error, get_auth_cookie
89

@@ -72,8 +73,48 @@ def run_test(self):
7273
assert_equal(cli_get_info['paytxfee'], wallet_info['paytxfee'])
7374
assert_equal(cli_get_info['relayfee'], network_info['relayfee'])
7475
assert_equal(self.nodes[0].cli.getwalletinfo(), wallet_info)
76+
77+
# Setup to test -getinfo and -rpcwallet= with multiple wallets.
78+
wallets = ['', 'Encrypted', 'secret']
79+
amounts = [Decimal('59.999928'), Decimal(9), Decimal(31)]
80+
self.nodes[0].createwallet(wallet_name=wallets[1])
81+
self.nodes[0].createwallet(wallet_name=wallets[2])
82+
w1 = self.nodes[0].get_wallet_rpc(wallets[0])
83+
w2 = self.nodes[0].get_wallet_rpc(wallets[1])
84+
w3 = self.nodes[0].get_wallet_rpc(wallets[2])
85+
w1.walletpassphrase(password, self.rpc_timeout)
86+
w1.sendtoaddress(w2.getnewaddress(), amounts[1])
87+
w1.sendtoaddress(w3.getnewaddress(), amounts[2])
88+
89+
# Mine a block to confirm; adds a block reward (50 BTC) to the default wallet.
90+
self.nodes[0].generate(1)
91+
92+
self.log.info("Test -getinfo with multiple wallets loaded returns no balance")
93+
assert_equal(set(self.nodes[0].listwallets()), set(wallets))
94+
assert 'balance' not in self.nodes[0].cli('-getinfo').send_cli().keys()
95+
96+
self.log.info("Test -getinfo with multiple wallets and -rpcwallet returns specified wallet balance")
97+
for i in range(len(wallets)):
98+
cli_get_info = self.nodes[0].cli('-getinfo').send_cli('-rpcwallet={}'.format(wallets[i]))
99+
assert_equal(cli_get_info['balance'], amounts[i])
100+
101+
self.log.info("Test -getinfo with multiple wallets and -rpcwallet=non-existing-wallet returns no balance")
102+
assert 'balance' not in self.nodes[0].cli('-getinfo').send_cli('-rpcwallet=does-not-exist').keys()
103+
104+
self.log.info("Test -getinfo after unloading all wallets except a non-default one returns its balance")
105+
self.nodes[0].unloadwallet(wallets[0])
106+
self.nodes[0].unloadwallet(wallets[2])
107+
assert_equal(self.nodes[0].listwallets(), [wallets[1]])
108+
assert_equal(self.nodes[0].cli('-getinfo').send_cli()['balance'], amounts[1])
109+
110+
self.log.info("Test -getinfo -rpcwallet=remaining-non-default-wallet returns its balance")
111+
assert_equal(self.nodes[0].cli('-getinfo').send_cli('-rpcwallet={}'.format(wallets[1]))['balance'], amounts[1])
112+
113+
self.log.info("Test -getinfo with -rpcwallet=unloaded wallet returns no balance")
114+
assert 'balance' not in self.nodes[0].cli('-getinfo').send_cli('-rpcwallet={}'.format(wallets[2])).keys()
75115
else:
76116
self.log.info("*** Wallet not compiled; cli getwalletinfo and -getinfo wallet tests skipped")
117+
self.nodes[0].generate(1) # maintain block parity with the wallet_compiled conditional branch
77118

78119
self.log.info("Test -version with node stopped")
79120
self.stop_node(0)
@@ -85,7 +126,7 @@ def run_test(self):
85126
self.nodes[0].wait_for_cookie_credentials() # ensure cookie file is available to avoid race condition
86127
blocks = self.nodes[0].cli('-rpcwait').send_cli('getblockcount')
87128
self.nodes[0].wait_for_rpc_connection()
88-
assert_equal(blocks, BLOCKS)
129+
assert_equal(blocks, BLOCKS + 1)
89130

90131

91132
if __name__ == '__main__':

0 commit comments

Comments
 (0)