Skip to content

Commit 92340a2

Browse files
committed
integration_test: Remove constructors
Turns out it is easier to support various Bitcoin Core args if we just have a single constructor and pass a slice of arg strings into it.
1 parent 828b63d commit 92340a2

File tree

7 files changed

+67
-86
lines changed

7 files changed

+67
-86
lines changed

integration_test/src/lib.rs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ use rand::Rng;
88
#[rustfmt::skip] // Keep public re-exports separate.
99
pub use node::Node; // Re-export this to make test imports more terse.
1010

11-
/// Number of blocks to generate when funding wallet.
12-
const NBLOCKS: usize = 101;
13-
1411
/// Initialize a logger (configure with `RUST_LOG=trace cargo test`).
1512
#[allow(dead_code)] // Not all tests use this function.
1613
pub fn init_logger() { let _ = env_logger::try_init(); }
1714

1815
/// Controls the loaded wallet.
16+
#[derive(Debug, PartialEq, Eq)]
1917
pub enum Wallet {
2018
/// Load the default wallet.
2119
Default,
@@ -27,27 +25,9 @@ pub enum Wallet {
2725

2826
pub trait NodeExt {
2927
/// Returns a handle to a `bitcoind` instance after leading wallet if present.
30-
fn _new(wallet: Wallet, txindex: bool) -> Node;
31-
32-
/// Returns a handle to a `bitcoind` instance with "default" wallet loaded.
33-
fn new_with_default_wallet() -> Node { Self::_new(Wallet::Default, false) }
34-
35-
/// Returns a handle to a `bitcoind` instance with "default" wallet loaded and `-txindex` enabled.
36-
fn new_with_default_wallet_txindex() -> Node { Self::_new(Wallet::Default, true) }
37-
38-
/// Returns a handle to a `bitcoind` instance with `wallet` loaded.
39-
fn new_with_wallet(wallet: String) -> Node { Self::_new(Wallet::Load(wallet), false) }
40-
41-
/// Returns a handle to a `bitcoind` instance with `wallet` loaded and `-txindex` enabled.
42-
fn new_with_wallet_txindex(wallet: String) -> Node { Self::_new(Wallet::Load(wallet), true) }
43-
44-
/// Returns a handle to a `bitcoind` instance without any wallet loaded.
45-
fn new_no_wallet() -> Node { Self::_new(Wallet::None, false) }
46-
47-
/// Returns a handle to a `bitcoind` instance without any wallet loaded and `-txindex` enabled.
48-
fn new_no_wallet_txindex() -> Node { Self::_new(Wallet::None, true) }
28+
fn with_wallet(wallet: Wallet, args: &[&str]) -> Node;
4929

50-
/// Generates [`NBLOCKS`] to an address controlled by the loaded wallet.
30+
/// Generates 101 blocks to an address controlled by the loaded wallet.
5131
fn fund_wallet(&self);
5232

5333
/// Mines a block.
@@ -71,26 +51,26 @@ pub trait NodeExt {
7151
}
7252

7353
impl NodeExt for Node {
74-
fn _new(wallet: Wallet, txindex: bool) -> Node {
54+
fn with_wallet(wallet: Wallet, args: &[&str]) -> Node {
7555
let exe = node::exe_path().expect("failed to get bitcoind executable");
7656

7757
let mut conf = node::Conf::default();
7858
match wallet {
7959
Wallet::Default => {}, // conf.wallet = Some("default")
80-
Wallet::Load(w) => conf.wallet = Some(w),
60+
Wallet::Load(w) => conf.wallet = Some(w.to_owned()),
8161
Wallet::None => conf.wallet = None,
8262
}
8363

84-
if txindex {
85-
conf.args.push("-txindex");
64+
for arg in args {
65+
conf.args.push(arg);
8666
}
8767

8868
Node::with_conf(exe, &conf).expect("failed to create node")
8969
}
9070

9171
fn fund_wallet(&self) {
9272
let address = self.client.new_address().expect("failed to get new address");
93-
self.client.generate_to_address(NBLOCKS, &address).expect("failed to generate to address");
73+
self.client.generate_to_address(101, &address).expect("failed to generate to address");
9474
}
9575

9676
fn mine_a_block(&self) {

integration_test/tests/blockchain.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22

33
//! Tests for methods found under the `== Blockchain ==` section of the API docs.
44
5-
use integration_test::{Node, NodeExt as _};
5+
use integration_test::{Node, NodeExt as _, Wallet};
66

77
#[test]
88
fn get_blockchain_info() {
9-
let node = Node::new_no_wallet();
9+
let node = Node::with_wallet(Wallet::None, &[]);
1010
let json = node.client.get_blockchain_info().expect("getblockchaininfo");
1111
assert!(json.into_model().is_ok());
1212
}
1313

1414
#[test]
1515
fn get_best_block_hash() {
16-
let node = Node::new_no_wallet();
16+
let node = Node::with_wallet(Wallet::None, &[]);
1717
let json = node.client.get_best_block_hash().expect("getbestblockhash");
1818
assert!(json.into_model().is_ok());
1919
}
2020

2121
#[test]
2222
fn get_block() {
23-
let node = Node::new_no_wallet();
23+
let node = Node::with_wallet(Wallet::None, &[]);
2424
let block_hash = node.client.best_block_hash().expect("best_block_hash failed");
2525

2626
let json = node.client.get_block_verbose_zero(block_hash).expect("getblock verbose=0");
@@ -36,29 +36,29 @@ fn get_block() {
3636

3737
#[test]
3838
fn get_block_count() {
39-
let node = Node::new_no_wallet();
39+
let node = Node::with_wallet(Wallet::None, &[]);
4040
let json = node.client.get_block_count().expect("getblockcount");
4141
let _ = json.into_model();
4242
}
4343

4444
#[test]
4545
fn get_block_hash() {
46-
let node = Node::new_no_wallet();
46+
let node = Node::with_wallet(Wallet::None, &[]);
4747
let json = node.client.get_block_hash(0).expect("getblockhash");
4848
assert!(json.into_model().is_ok());
4949
}
5050

5151
#[test]
5252
fn get_block_header() { // verbose = false
53-
let node = Node::new_no_wallet();
53+
let node = Node::with_wallet(Wallet::None, &[]);
5454
let block_hash = node.client.best_block_hash().expect("best_block_hash failed");
5555
let json = node.client.get_block_header(&block_hash).expect("getblockheader");
5656
assert!(json.into_model().is_ok());
5757
}
5858

5959
#[test]
6060
fn get_block_header_verbose() { // verbose = true
61-
let node = Node::new_no_wallet();
61+
let node = Node::with_wallet(Wallet::None, &[]);
6262
let block_hash = node.client.best_block_hash().expect("best_block_hash failed");
6363
let json = node.client.get_block_header_verbose(&block_hash).expect("getblockheader");
6464
assert!(json.into_model().is_ok());
@@ -75,7 +75,7 @@ fn get_block_stats() {
7575
}
7676

7777
fn getblockstats() {
78-
let node = Node::new_with_default_wallet();
78+
let node = Node::with_wallet(Wallet::Default, &[]);
7979
node.fund_wallet();
8080

8181
let json = node.client.get_block_stats_by_height(1).expect("getblockstats");
@@ -87,7 +87,7 @@ fn getblockstats() {
8787
}
8888

8989
fn getblockstats_txindex() {
90-
let node = Node::new_with_default_wallet_txindex();
90+
let node = Node::with_wallet(Wallet::Default, &["-txindex"]);
9191
node.fund_wallet();
9292

9393
let json = node.client.get_block_stats_by_height(101).expect("getblockstats");
@@ -100,21 +100,21 @@ fn getblockstats_txindex() {
100100

101101
#[test]
102102
fn get_chain_tips() {
103-
let node = Node::new_no_wallet();
103+
let node = Node::with_wallet(Wallet::None, &[]);
104104
let json = node.client.get_chain_tips().expect("getchaintips");
105105
assert!(json.into_model().is_ok());
106106
}
107107

108108
#[test]
109109
fn get_chain_tx_stats() {
110-
let node = Node::new_no_wallet();
110+
let node = Node::with_wallet(Wallet::None, &[]);
111111
let json = node.client.get_chain_tx_stats().expect("getchaintxstats");
112112
assert!(json.into_model().is_ok());
113113
}
114114

115115
#[test]
116116
fn get_difficulty() {
117-
let node = Node::new_no_wallet();
117+
let node = Node::with_wallet(Wallet::None, &[]);
118118
let json = node.client.get_difficulty().expect("getdifficulty");
119119
let _ = json.into_model();
120120
}
@@ -135,7 +135,7 @@ fn get_mempool_descendants() {
135135

136136
#[test]
137137
fn get_mempool_entry() {
138-
let node = Node::new_with_default_wallet();
138+
let node = Node::with_wallet(Wallet::Default, &[]);
139139
node.fund_wallet();
140140
let (_address, txid) = node.create_mempool_transaction();
141141

@@ -145,7 +145,7 @@ fn get_mempool_entry() {
145145

146146
#[test]
147147
fn get_mempool_info() {
148-
let node = Node::new_with_default_wallet();
148+
let node = Node::with_wallet(Wallet::Default, &[]);
149149
node.fund_wallet();
150150
let (_address, _txid) = node.create_mempool_transaction();
151151

@@ -158,7 +158,7 @@ fn get_mempool_info() {
158158

159159
#[test]
160160
fn get_raw_mempool() {
161-
let node = Node::new_with_default_wallet();
161+
let node = Node::with_wallet(Wallet::Default, &[]);
162162
node.fund_wallet();
163163
let (_address, _txid) = node.create_mempool_transaction();
164164

@@ -173,7 +173,7 @@ fn get_raw_mempool() {
173173
// FIXME: Fails with getrawmempool verbose: JsonRpc(Json(Error("invalid type: map, expected a sequence", line: 1, column: 0)))
174174
#[cfg(feature = "TODO")]
175175
fn get_raw_mempool_verbose() {
176-
let node = Node::new_with_default_wallet();
176+
let node = Node::with_wallet(Wallet::Default, &[]);
177177
node.fund_wallet();
178178
let (_address, _txid) = node.create_mempool_transaction();
179179

@@ -186,7 +186,7 @@ fn get_raw_mempool_verbose() {
186186

187187
#[test]
188188
fn get_tx_out() {
189-
let node = Node::new_with_default_wallet();
189+
let node = Node::with_wallet(Wallet::Default, &[]);
190190
node.fund_wallet();
191191
let (_address, tx) = node.create_mined_transaction();
192192
let txid = tx.compute_txid();
@@ -198,7 +198,7 @@ fn get_tx_out() {
198198

199199
#[test]
200200
fn get_tx_out_proof() {
201-
let node = Node::new_with_default_wallet();
201+
let node = Node::with_wallet(Wallet::Default, &[]);
202202
node.fund_wallet();
203203
let (_address, tx) = node.create_mined_transaction();
204204
let txid = tx.compute_txid();
@@ -208,19 +208,18 @@ fn get_tx_out_proof() {
208208

209209
#[test]
210210
fn get_tx_out_set_info() {
211-
let node = Node::new_with_default_wallet();
211+
let node = Node::with_wallet(Wallet::Default, &[]);
212212
node.fund_wallet();
213213
let (_address, _tx) = node.create_mined_transaction();
214214

215215
// Test the type and into model conversion code.
216216
let json = node.client.get_tx_out_set_info().expect("gettxoutsetinfo");
217217
let _ = json.into_model().expect("into_model");
218-
219218
}
220219

221220
#[test]
222221
fn precious_block() {
223-
let node = Node::new_with_default_wallet();
222+
let node = Node::with_wallet(Wallet::Default, &[]);
224223
node.mine_a_block();
225224
let hash = node.client.best_block_hash().expect("best_block_hash failed");
226225
node.mine_a_block();
@@ -231,7 +230,7 @@ fn precious_block() {
231230
// Implicitly tests the omitted method `gettxoutproof` as well.
232231
#[test]
233232
fn verify_tx_out_proof() {
234-
let node = Node::new_with_default_wallet();
233+
let node = Node::with_wallet(Wallet::Default, &[]);
235234
node.fund_wallet();
236235
let (_address, tx) = node.create_mined_transaction();
237236
let txid = tx.compute_txid();

integration_test/tests/control.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,43 @@
22

33
//! Tests for methods found under the `== Control ==` section of the API docs.
44
5-
use integration_test::{Node, NodeExt as _};
5+
use integration_test::{Node, NodeExt as _, Wallet};
66

77
#[test]
88
fn get_memory_info() {
9-
let node = Node::new_no_wallet();
9+
let node = Node::with_wallet(Wallet::None, &[]);
1010
let _ = node.client.get_memory_info().expect("getmemoryinfo");
1111
}
1212

1313
#[cfg(not(feature = "v17"))]
1414
#[test]
1515
fn get_rpc_info() {
16-
let node = Node::new_no_wallet();
16+
let node = Node::with_wallet(Wallet::None, &[]);
1717
let _ = node.client.get_rpc_info().expect("getrpcinfo");
1818
}
1919

2020
#[test]
2121
fn help() {
22-
let node = Node::new_no_wallet();
22+
let node = Node::with_wallet(Wallet::None, &[]);
2323
// There is no json object for `stop`, we just return a string.
2424
let _ = node.client.help().expect("help");
2525
}
2626

2727
#[test]
2828
fn logging() {
29-
let node = Node::new_no_wallet();
29+
let node = Node::with_wallet(Wallet::None, &[]);
3030
let _ = node.client.logging().expect("logging");
3131
}
3232

3333
#[test]
3434
fn stop() {
35-
let node = Node::new_no_wallet();
35+
let node = Node::with_wallet(Wallet::None, &[]);
3636
// There is no json object for `stop`, we just return a string.
3737
let _ = node.client.stop().expect("stop");
3838
}
3939

4040
#[test]
4141
fn uptime() {
42-
let node = Node::new_no_wallet();
42+
let node = Node::with_wallet(Wallet::None, &[]);
4343
let _ = node.client.uptime().expect("uptime");
4444
}

integration_test/tests/generating.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
//! Tests for methods found under the `== Generating ==` section of the API docs.
44
5-
use integration_test::{Node, NodeExt as _};
5+
use integration_test::{Node, NodeExt as _, Wallet};
66

77
#[test]
88
fn generate_to_address() {
99
const NBLOCKS: usize = 1;
1010

11-
let node = Node::new_with_default_wallet();
11+
let node = Node::with_wallet(Wallet::Default, &[]);
12+
1213
let address = node.client.new_address().expect("failed to get new address");
1314
let json = node.client.generate_to_address(NBLOCKS, &address).expect("generatetoaddress");
1415
json.into_model().unwrap();
@@ -18,7 +19,8 @@ fn generate_to_address() {
1819
fn invalidate_block() {
1920
const NBLOCKS: usize = 1;
2021

21-
let node = Node::new_with_default_wallet();
22+
let node = Node::with_wallet(Wallet::Default, &[]);
23+
2224
let address = node.client.new_address().expect("failed to get new address");
2325
let old_best_block = node.client.get_best_block_hash().expect("getbestblockhash").into_model().unwrap().0;
2426
node.client.generate_to_address(NBLOCKS, &address).expect("generatetoaddress").into_model().unwrap();
@@ -36,7 +38,7 @@ fn invalidate_block() {
3638
// fn generate() {
3739
// const NBLOCKS: usize = 100;
3840

39-
// let node = Node::new_with_default_wallet();
41+
// let node = Node::with_wallet_with_default_wallet();
4042
// let json = node.client.generate(NBLOCKS).expect("generate");
4143
// let model = json.into_model().unwrap();
4244
// assert_eq!(model.len(), NBLOCKS);

integration_test/tests/network.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44
55
#![cfg(any(feature = "0_17_1", feature = "0_18_1"))]
66

7-
use integration_test::{Node, NodeExt as _};
7+
use integration_test::{Node, NodeExt as _, Wallet};
88

99
#[test]
1010
fn get_added_node_info() {
11-
let node = Node::new_no_wallet();
11+
let node = Node::with_wallet(Wallet::None, &[]);
1212
let _ = node.client.get_added_node_info().expect("getaddednodeinfo");
1313
}
1414

1515
#[test]
1616
fn get_net_totals() {
17-
let node = Node::new_no_wallet();
17+
let node = Node::with_wallet(Wallet::None, &[]);
1818
let _ = node.client.get_net_totals().expect("getnettotals");
1919
}
2020

2121
#[test]
2222
fn get_network_info() {
23-
let node = Node::new_no_wallet();
23+
let node = Node::with_wallet(Wallet::None, &[]);
2424
let json = node.client.get_network_info().expect("getnetworkinfo");
2525
assert!(json.into_model().is_ok());
2626

@@ -30,6 +30,6 @@ fn get_network_info() {
3030

3131
#[test]
3232
fn get_peer_info() {
33-
let node = Node::new_no_wallet();
33+
let node = Node::with_wallet(Wallet::None, &[]);
3434
let _ = node.client.get_peer_info().expect("getpeerinfo");
3535
}

0 commit comments

Comments
 (0)