Skip to content

Commit e309bb2

Browse files
committed
chore(test): rework TestEnv to allow custom node configuration
Also adds a `corepc_node::Client` to the `TestEnv` for future use.
1 parent f5fce39 commit e309bb2

2 files changed

Lines changed: 96 additions & 59 deletions

File tree

tests/test_rpc_client.rs

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use testenv::TestEnv;
1212

1313
#[test]
1414
fn test_invalid_credentials() {
15-
let env = TestEnv::setup().unwrap();
15+
let env = TestEnv::new();
16+
1617
let client = Client::with_auth(
1718
&env.node.rpc_url(),
1819
Auth::UserPass("wrong".to_string(), "credentials".to_string()),
@@ -28,7 +29,7 @@ fn test_invalid_credentials() {
2829
fn test_client_with_custom_transport() {
2930
use jsonrpc::http::bitreq_http::Builder;
3031

31-
let env = TestEnv::setup().unwrap();
32+
let env = TestEnv::new();
3233

3334
let rpc_url = env.node.rpc_url();
3435
let cookie = env
@@ -54,7 +55,7 @@ fn test_client_with_custom_transport() {
5455

5556
#[test]
5657
fn test_get_block_count() {
57-
let env = TestEnv::setup().unwrap();
58+
let env = TestEnv::new();
5859

5960
let block_count = env
6061
.client
@@ -66,7 +67,7 @@ fn test_get_block_count() {
6667

6768
#[test]
6869
fn test_get_block_hash() {
69-
let env = TestEnv::setup().unwrap();
70+
let env = TestEnv::new();
7071

7172
let _genesis_hash = env
7273
.client
@@ -76,21 +77,22 @@ fn test_get_block_hash() {
7677

7778
#[test]
7879
fn test_get_block_hash_for_current_height() {
79-
let TestEnv {
80-
client,
81-
node: _node,
82-
} = TestEnv::setup().unwrap();
80+
let env = TestEnv::new();
8381

84-
let block_count = client.get_block_count().expect("failed to get block count");
82+
let block_count = env
83+
.client
84+
.get_block_count()
85+
.expect("failed to get block count");
8586

86-
let _block_hash = client
87+
let _block_hash = env
88+
.client
8789
.get_block_hash(block_count)
8890
.expect("failed to get block hash");
8991
}
9092

9193
#[test]
9294
fn test_get_block_hash_invalid_height() {
93-
let env = TestEnv::setup().unwrap();
95+
let env = TestEnv::new();
9496

9597
let result = env.client.get_block_hash(999_999_999);
9698

@@ -99,17 +101,19 @@ fn test_get_block_hash_invalid_height() {
99101

100102
#[test]
101103
fn test_get_best_block_hash() {
102-
let TestEnv {
103-
client,
104-
node: _node,
105-
} = TestEnv::setup().unwrap();
104+
let env = TestEnv::new();
106105

107-
let best_block_hash = client
106+
let best_block_hash = env
107+
.client
108108
.get_best_block_hash()
109109
.expect("failed to get best block hash");
110110

111-
let block_count = client.get_block_count().expect("failed to get block count");
112-
let block_hash = client
111+
let block_count = env
112+
.client
113+
.get_block_count()
114+
.expect("failed to get block count");
115+
let block_hash = env
116+
.client
113117
.get_block_hash(block_count)
114118
.expect("failed to get block hash");
115119

@@ -118,16 +122,15 @@ fn test_get_best_block_hash() {
118122

119123
#[test]
120124
fn test_get_block() {
121-
let TestEnv {
122-
client,
123-
node: _node,
124-
} = TestEnv::setup().unwrap();
125+
let env = TestEnv::new();
125126

126-
let genesis_hash = client
127+
let genesis_hash = env
128+
.client
127129
.get_block_hash(0)
128130
.expect("failed to get genesis hash");
129131

130-
let block = client
132+
let block = env
133+
.client
131134
.get_block(&genesis_hash)
132135
.expect("failed to get block");
133136

@@ -137,7 +140,7 @@ fn test_get_block() {
137140

138141
#[test]
139142
fn test_get_block_after_mining() {
140-
let env = TestEnv::setup().unwrap();
143+
let env = TestEnv::new();
141144

142145
let hashes = env.mine_blocks(1, None).expect("failed to mine block");
143146
let block_hash = hashes[0];
@@ -153,7 +156,7 @@ fn test_get_block_after_mining() {
153156

154157
#[test]
155158
fn test_get_block_verbose() {
156-
let env = TestEnv::setup().unwrap();
159+
let env = TestEnv::new();
157160

158161
let hashes = env.mine_blocks(1, None).expect("failed to mine block");
159162
let block_hash = hashes[0];
@@ -169,7 +172,7 @@ fn test_get_block_verbose() {
169172

170173
#[test]
171174
fn test_get_block_invalid_hash() {
172-
let env = TestEnv::setup().unwrap();
175+
let env = TestEnv::new();
173176

174177
let invalid_hash =
175178
BlockHash::from_str("0000000000000000000000000000000000000000000000000000000000000000")
@@ -182,16 +185,15 @@ fn test_get_block_invalid_hash() {
182185

183186
#[test]
184187
fn test_get_block_header() {
185-
let TestEnv {
186-
client,
187-
node: _node,
188-
} = TestEnv::setup().unwrap();
188+
let env = TestEnv::new();
189189

190-
let genesis_hash = client
190+
let genesis_hash = env
191+
.client
191192
.get_block_hash(0)
192193
.expect("failed to get genesis hash");
193194

194-
let header = client
195+
let header = env
196+
.client
195197
.get_block_header(&genesis_hash)
196198
.expect("failed to get block header");
197199

@@ -200,16 +202,15 @@ fn test_get_block_header() {
200202

201203
#[test]
202204
fn test_get_block_header_verbose() {
203-
let TestEnv {
204-
client,
205-
node: _node,
206-
} = TestEnv::setup().unwrap();
205+
let env = TestEnv::new();
207206

208-
let genesis_hash = client
207+
let genesis_hash = env
208+
.client
209209
.get_block_hash(0)
210210
.expect("failed to get genesis hash");
211211

212-
let header = client
212+
let header = env
213+
.client
213214
.get_block_header_verbose(&genesis_hash)
214215
.expect("failed to get block header verbose");
215216

@@ -218,7 +219,7 @@ fn test_get_block_header_verbose() {
218219

219220
#[test]
220221
fn test_get_raw_mempool_empty() {
221-
let env = TestEnv::setup().unwrap();
222+
let env = TestEnv::new();
222223

223224
let _hashes = env.mine_blocks(1, None).expect("failed to mine block");
224225

@@ -231,7 +232,7 @@ fn test_get_raw_mempool_empty() {
231232

232233
#[test]
233234
fn test_get_raw_mempool_with_transaction() {
234-
let env = TestEnv::setup().unwrap();
235+
let env = TestEnv::new();
235236

236237
let _hashes = env.mine_blocks(101, None).expect("failed to mine block");
237238

@@ -251,7 +252,7 @@ fn test_get_raw_mempool_with_transaction() {
251252

252253
#[test]
253254
fn test_get_raw_transaction() {
254-
let env = TestEnv::setup().unwrap();
255+
let env = TestEnv::new();
255256

256257
let _hashes = env.mine_blocks(1, None).expect("failed to mine block");
257258

@@ -279,7 +280,7 @@ fn test_get_raw_transaction() {
279280

280281
#[test]
281282
fn test_get_raw_transaction_invalid_txid() {
282-
let env = TestEnv::setup().unwrap();
283+
let env = TestEnv::new();
283284

284285
let fake_txid =
285286
Txid::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap();
@@ -291,16 +292,15 @@ fn test_get_raw_transaction_invalid_txid() {
291292

292293
#[test]
293294
fn test_get_block_filter() {
294-
let TestEnv {
295-
client,
296-
node: _node,
297-
} = TestEnv::setup().unwrap();
295+
let env = TestEnv::new();
298296

299-
let genesis_hash = client
297+
let genesis_hash = env
298+
.client
300299
.get_block_hash(0)
301300
.expect("failed to get genesis hash");
302301

303-
let result = client
302+
let result = env
303+
.client
304304
.get_block_filter(&genesis_hash)
305305
.expect("failed to get block filter");
306306

tests/testenv.rs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use bdk_bitcoind_client::{Auth, Client};
22
use bitcoin::{Address, BlockHash};
3+
use corepc_node::client::client_sync::Auth as CorepcAuth;
34
use corepc_node::{exe_path, Conf, Node};
45
use corepc_types::bitcoin;
56

6-
/// Test environment for running integration tests.
7+
/// Test Environment for running integration tests.
78
///
89
/// [`TestEnv`] exposes the [`Client`] API defined by this crate to be tested against
910
/// a running [`corepc_node::Node`] instance.
@@ -13,32 +14,68 @@ pub struct TestEnv {
1314
pub client: Client,
1415
/// [`corepc_node::Node`]
1516
pub node: Node,
17+
#[allow(unused)]
18+
/// [`corepc_node::Client]
19+
pub corepc_client: corepc_node::Client,
20+
}
21+
22+
impl Default for TestEnv {
23+
fn default() -> Self {
24+
Self::new()
25+
}
1626
}
1727

1828
impl TestEnv {
19-
/// Create new [`TestEnv`].
29+
/// Create new [`TestEnv`] with a default [configuration](Config).
2030
///
2131
/// This will first look for the path of the `bitcoind` executable using [`corepc_node::exe_path`]
2232
/// before returning a new [`TestEnv`] with [`Client`] connected to it.
2333
///
2434
/// Note that [`Node`] also exposes its own RPC [`client`](Node::client) which may help with
2535
/// creating different test cases, but be aware that this is different from the client we're
2636
/// actually testing.
27-
pub fn setup() -> anyhow::Result<Self> {
28-
let exe = exe_path()?;
37+
pub fn new() -> Self {
38+
// Enable `txindex` and `blockfilterindex` by default.
39+
let mut bitcoind_config = Conf::default();
40+
bitcoind_config.args.push("-txindex=1");
41+
bitcoind_config.args.push("-blockfilterindex=1");
2942

30-
let mut conf = Conf::default();
31-
conf.args.push("-blockfilterindex=1");
32-
conf.args.push("-txindex=1");
43+
TestEnv::new_with_config(&bitcoind_config)
44+
}
45+
46+
/// Create new [`TestEnv`] with a custom [configuration](Config) for the [node](Node).
47+
///
48+
/// This will first look for the path of the `bitcoind` executable using [`corepc_node::exe_path`]
49+
/// before returning a new [`TestEnv`] with [`Client`] connected to it.
50+
///
51+
/// Note that [`Node`] also exposes its own RPC [`client`](Node::client) which may help with
52+
/// creating different test cases, but be aware that this is different from the client we're
53+
/// actually testing.
54+
pub fn new_with_config(config: &Conf) -> Self {
55+
// Try to get `BITCOIN_EXE` from the environment.
56+
let bitcoind_exe = exe_path().unwrap();
3357

34-
let node = Node::with_conf(exe, &conf)?;
58+
// Spawn a `corepc::Node` with the default configuration.
59+
let node = Node::with_conf(bitcoind_exe, config).unwrap();
3560

61+
// Get the URL for the RPC server.
3662
let rpc_url = node.rpc_url();
63+
// Get the location for the cookie file.
3764
let cookie_file = &node.params.cookie_file;
65+
66+
// Setup authentication and create the `bdk_client`.
3867
let auth = Auth::CookieFile(cookie_file.clone());
39-
let client = Client::with_auth(&rpc_url, auth)?;
68+
let client = Client::with_auth(&rpc_url, auth).unwrap();
69+
70+
// Setup authentication and create the `corepc_client`.
71+
let corepc_auth = CorepcAuth::CookieFile(cookie_file.clone());
72+
let corepc_client = corepc_node::Client::new_with_auth(&rpc_url, corepc_auth).unwrap();
4073

41-
Ok(Self { client, node })
74+
Self {
75+
client,
76+
node,
77+
corepc_client,
78+
}
4279
}
4380

4481
/// Mines `nblocks` blocks to the given `address`, or an address controlled

0 commit comments

Comments
 (0)