5
5
"""Test various command line arguments and configuration file parameters."""
6
6
7
7
import os
8
- import pathlib
8
+ from pathlib import Path
9
9
import re
10
10
import sys
11
11
import tempfile
@@ -39,8 +39,8 @@ def test_config_file_parser(self):
39
39
extra_args = [f'-conf={ bad_conf_file_path } ' ],
40
40
expected_msg = conf_in_config_file_err ,
41
41
)
42
- inc_conf_file_path = os . path . join ( self .nodes [0 ].datadir , 'include.conf' )
43
- with open (os . path . join ( self .nodes [0 ].datadir , 'bitcoin.conf' ) , 'a' , encoding = 'utf-8' ) as conf :
42
+ inc_conf_file_path = self .nodes [0 ].datadir_path / 'include.conf'
43
+ with open (self .nodes [0 ].datadir_path / 'bitcoin.conf' , 'a' , encoding = 'utf-8' ) as conf :
44
44
conf .write (f'includeconf={ inc_conf_file_path } \n ' )
45
45
with open (inc_conf_file_path , 'w' , encoding = 'utf-8' ) as conf :
46
46
conf .write ('conf=some.conf\n ' )
@@ -97,8 +97,8 @@ def test_config_file_parser(self):
97
97
conf .write ('server=1\n rpcuser=someuser\n [main]\n rpcpassword=some#pass' )
98
98
self .nodes [0 ].assert_start_raises_init_error (expected_msg = 'Error: Error reading configuration file: parse error on line 4, using # in rpcpassword can be ambiguous and should be avoided' )
99
99
100
- inc_conf_file2_path = os . path . join ( self .nodes [0 ].datadir , 'include2.conf' )
101
- with open (os . path . join ( self .nodes [0 ].datadir , 'bitcoin.conf' ) , 'a' , encoding = 'utf-8' ) as conf :
100
+ inc_conf_file2_path = self .nodes [0 ].datadir_path / 'include2.conf'
101
+ with open (self .nodes [0 ].datadir_path / 'bitcoin.conf' , 'a' , encoding = 'utf-8' ) as conf :
102
102
conf .write (f'includeconf={ inc_conf_file2_path } \n ' )
103
103
104
104
with open (inc_conf_file_path , 'w' , encoding = 'utf-8' ) as conf :
@@ -123,15 +123,15 @@ def test_config_file_log(self):
123
123
124
124
# Create a temporary directory that will be treated as the default data
125
125
# directory by bitcoind.
126
- env , default_datadir = util .get_temp_default_datadir (pathlib . Path (self .options .tmpdir , "test_config_file_log" ))
126
+ env , default_datadir = util .get_temp_default_datadir (Path (self .options .tmpdir , "test_config_file_log" ))
127
127
default_datadir .mkdir (parents = True )
128
128
129
129
# Write a bitcoin.conf file in the default data directory containing a
130
130
# datadir= line pointing at the node datadir.
131
131
node = self .nodes [0 ]
132
- conf_text = pathlib . Path ( node .bitcoinconf ) .read_text ()
132
+ conf_text = node .bitcoinconf .read_text ()
133
133
conf_path = default_datadir / "bitcoin.conf"
134
- conf_path .write_text (f"datadir={ node .datadir } \n { conf_text } " )
134
+ conf_path .write_text (f"datadir={ node .datadir_path } \n { conf_text } " )
135
135
136
136
# Drop the node -datadir= argument during this test, because if it is
137
137
# specified it would take precedence over the datadir setting in the
@@ -218,7 +218,8 @@ def test_networkactive(self):
218
218
219
219
def test_seed_peers (self ):
220
220
self .log .info ('Test seed peers' )
221
- default_data_dir = self .nodes [0 ].datadir
221
+ default_data_dir = self .nodes [0 ].datadir_path
222
+ peer_dat = default_data_dir / 'peers.dat'
222
223
# Only regtest has no fixed seeds. To avoid connections to random
223
224
# nodes, regtest is the only network where it is safe to enable
224
225
# -fixedseeds in tests
@@ -229,7 +230,7 @@ def test_seed_peers(self):
229
230
# We expect the node will use DNS Seeds, but Regtest mode does not have
230
231
# any valid DNS seeds. So after 60 seconds, the node should fallback to
231
232
# fixed seeds
232
- assert not os . path . exists (os . path . join ( default_data_dir , "peers.dat" ) )
233
+ assert not peer_dat . exists ()
233
234
start = int (time .time ())
234
235
with self .nodes [0 ].assert_debug_log (
235
236
expected_msgs = [
@@ -248,7 +249,7 @@ def test_seed_peers(self):
248
249
249
250
# No peers.dat exists and -dnsseed=0
250
251
# We expect the node will fallback immediately to fixed seeds
251
- assert not os . path . exists (os . path . join ( default_data_dir , "peers.dat" ) )
252
+ assert not peer_dat . exists ()
252
253
with self .nodes [0 ].assert_debug_log (expected_msgs = [
253
254
"Loaded 0 addresses from peers.dat" ,
254
255
"DNS seeding disabled" ,
@@ -260,7 +261,7 @@ def test_seed_peers(self):
260
261
261
262
# No peers.dat exists and dns seeds are disabled.
262
263
# We expect the node will not add fixed seeds when explicitly disabled.
263
- assert not os . path . exists (os . path . join ( default_data_dir , "peers.dat" ) )
264
+ assert not peer_dat . exists ()
264
265
with self .nodes [0 ].assert_debug_log (expected_msgs = [
265
266
"Loaded 0 addresses from peers.dat" ,
266
267
"DNS seeding disabled" ,
@@ -271,7 +272,7 @@ def test_seed_peers(self):
271
272
272
273
# No peers.dat exists and -dnsseed=0, but a -addnode is provided
273
274
# We expect the node will allow 60 seconds prior to using fixed seeds
274
- assert not os . path . exists (os . path . join ( default_data_dir , "peers.dat" ) )
275
+ assert not peer_dat . exists ()
275
276
start = int (time .time ())
276
277
with self .nodes [0 ].assert_debug_log (
277
278
expected_msgs = [
@@ -323,16 +324,16 @@ def test_ignored_conf(self):
323
324
'because a conflicting -conf file argument is passed.' )
324
325
node = self .nodes [0 ]
325
326
with tempfile .NamedTemporaryFile (dir = self .options .tmpdir , mode = "wt" , delete = False ) as temp_conf :
326
- temp_conf .write (f"datadir={ node .datadir } \n " )
327
+ temp_conf .write (f"datadir={ node .datadir_path } \n " )
327
328
node .assert_start_raises_init_error ([f"-conf={ temp_conf .name } " ], re .escape (
328
- f'Error: Data directory "{ node .datadir } " contains a "bitcoin.conf" file which is ignored, because a '
329
+ f'Error: Data directory "{ node .datadir_path } " contains a "bitcoin.conf" file which is ignored, because a '
329
330
f'different configuration file "{ temp_conf .name } " from command line argument "-conf={ temp_conf .name } " '
330
331
f'is being used instead.' ) + r"[\s\S]*" , match = ErrorMatch .FULL_REGEX )
331
332
332
333
# Test that passing a redundant -conf command line argument pointing to
333
334
# the same bitcoin.conf that would be loaded anyway does not trigger an
334
335
# error.
335
- self .start_node (0 , [f'-conf={ node .datadir } /bitcoin.conf' ])
336
+ self .start_node (0 , [f'-conf={ node .datadir_path } /bitcoin.conf' ])
336
337
self .stop_node (0 )
337
338
338
339
def test_ignored_default_conf (self ):
@@ -346,23 +347,23 @@ def test_ignored_default_conf(self):
346
347
347
348
# Create a temporary directory that will be treated as the default data
348
349
# directory by bitcoind.
349
- env , default_datadir = util .get_temp_default_datadir (pathlib . Path (self .options .tmpdir , "home" ))
350
+ env , default_datadir = util .get_temp_default_datadir (Path (self .options .tmpdir , "home" ))
350
351
default_datadir .mkdir (parents = True )
351
352
352
353
# Write a bitcoin.conf file in the default data directory containing a
353
354
# datadir= line pointing at the node datadir. This will trigger a
354
355
# startup error because the node datadir contains a different
355
356
# bitcoin.conf that would be ignored.
356
357
node = self .nodes [0 ]
357
- (default_datadir / "bitcoin.conf" ).write_text (f"datadir={ node .datadir } \n " )
358
+ (default_datadir / "bitcoin.conf" ).write_text (f"datadir={ node .datadir_path } \n " )
358
359
359
360
# Drop the node -datadir= argument during this test, because if it is
360
361
# specified it would take precedence over the datadir setting in the
361
362
# config file.
362
363
node_args = node .args
363
364
node .args = [arg for arg in node .args if not arg .startswith ("-datadir=" )]
364
365
node .assert_start_raises_init_error ([], re .escape (
365
- f'Error: Data directory "{ node .datadir } " contains a "bitcoin.conf" file which is ignored, because a '
366
+ f'Error: Data directory "{ node .datadir_path } " contains a "bitcoin.conf" file which is ignored, because a '
366
367
f'different configuration file "{ default_datadir } /bitcoin.conf" from data directory "{ default_datadir } " '
367
368
f'is being used instead.' ) + r"[\s\S]*" , env = env , match = ErrorMatch .FULL_REGEX )
368
369
node .args = node_args
@@ -392,16 +393,16 @@ def run_test(self):
392
393
# Remove the -datadir argument so it doesn't override the config file
393
394
self .nodes [0 ].args = [arg for arg in self .nodes [0 ].args if not arg .startswith ("-datadir" )]
394
395
395
- default_data_dir = self .nodes [0 ].datadir
396
- new_data_dir = os . path . join ( default_data_dir , 'newdatadir' )
397
- new_data_dir_2 = os . path . join ( default_data_dir , 'newdatadir2' )
396
+ default_data_dir = self .nodes [0 ].datadir_path
397
+ new_data_dir = default_data_dir / 'newdatadir'
398
+ new_data_dir_2 = default_data_dir / 'newdatadir2'
398
399
399
400
# Check that using -datadir argument on non-existent directory fails
400
- self .nodes [0 ].datadir = new_data_dir
401
+ self .nodes [0 ].datadir_path = new_data_dir
401
402
self .nodes [0 ].assert_start_raises_init_error ([f'-datadir={ new_data_dir } ' ], f'Error: Specified data directory "{ new_data_dir } " does not exist.' )
402
403
403
404
# Check that using non-existent datadir in conf file fails
404
- conf_file = os . path . join ( default_data_dir , "bitcoin.conf" )
405
+ conf_file = default_data_dir / "bitcoin.conf"
405
406
406
407
# datadir needs to be set before [chain] section
407
408
with open (conf_file , encoding = 'utf8' ) as f :
@@ -413,20 +414,20 @@ def run_test(self):
413
414
self .nodes [0 ].assert_start_raises_init_error ([f'-conf={ conf_file } ' ], f'Error: Error reading configuration file: specified data directory "{ new_data_dir } " does not exist.' )
414
415
415
416
# Check that an explicitly specified config file that cannot be opened fails
416
- none_existent_conf_file = os . path . join ( default_data_dir , "none_existent_bitcoin.conf" )
417
- self .nodes [0 ].assert_start_raises_init_error (['-conf=' + none_existent_conf_file ], 'Error: Error reading configuration file: specified config file "' + none_existent_conf_file + '" could not be opened.' )
417
+ none_existent_conf_file = default_data_dir / "none_existent_bitcoin.conf"
418
+ self .nodes [0 ].assert_start_raises_init_error (['-conf=' + f' { none_existent_conf_file } ' ], 'Error: Error reading configuration file: specified config file "' + f' { none_existent_conf_file } ' + '" could not be opened.' )
418
419
419
420
# Create the directory and ensure the config file now works
420
- os .mkdir (new_data_dir )
421
+ new_data_dir .mkdir ()
421
422
self .start_node (0 , [f'-conf={ conf_file } ' ])
422
423
self .stop_node (0 )
423
- assert os . path . exists ( os . path . join ( new_data_dir , self .chain , 'blocks' ))
424
+ assert ( new_data_dir / self .chain / 'blocks' ). exists ( )
424
425
425
426
# Ensure command line argument overrides datadir in conf
426
- os .mkdir (new_data_dir_2 )
427
- self .nodes [0 ].datadir = new_data_dir_2
427
+ new_data_dir_2 .mkdir ()
428
+ self .nodes [0 ].datadir_path = new_data_dir_2
428
429
self .start_node (0 , [f'-datadir={ new_data_dir_2 } ' , f'-conf={ conf_file } ' ])
429
- assert os . path . exists ( os . path . join ( new_data_dir_2 , self .chain , 'blocks' ))
430
+ assert ( new_data_dir_2 / self .chain / 'blocks' ). exists ( )
430
431
431
432
432
433
if __name__ == '__main__' :
0 commit comments