Skip to content

Commit 5a58d49

Browse files
committed
Merge bitcoin/bitcoin#33546: test: add functional test for TestShell (matching doc example)
57f7c68 test: add functional test for `TestShell` (matching doc example) (Sebastian Falbesoner) 53874f7 doc: test: update TestShell example instructions/options (Sebastian Falbesoner) Pull request description: This PR adds a functional framework test for the `TestShell` class. The primary motivation for this is to avoid that the example instructions for the interactive Python shell in `test-shell.md` get outdated or broken without noticing, a problem we had already several times in the past (see #26520, #27906, #31415). Having a copy is still not perfect, as docs and functional test have to be kept in sync, but I don't expect this to be a problem in practice, assuming the hint in the functional test comment is hopefully noticed if changes are made. Alternatively, the example instructions in `test-shell.md` could be removed with a hint to the functional test (I tend to prefer to keep the docs as-is though, showing the full REPL interaction). The first commit contain two small removal fix-ups in `test-shell.md` regarding the result of the `createwallet` RPC call and the mentioning of the `noshutdown` option that was removed earlier (see #31620). Could be backported to v30. ACKs for top commit: brunoerg: ACK 57f7c68 stratospher: ACK 57f7c68. pinheadmz: ACK 57f7c68 Tree-SHA512: 25c35af46b742bbefce7838708437529bbf613fa3d1f0fba590cacef0acdde82b3a78c7a01459c73eaac26ce3f1041e54092d098f0fc94a8c76ac0b4f4970338
2 parents 1abc8fa + 57f7c68 commit 5a58d49

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Tests for the `TestShell` submodule."""
6+
7+
from decimal import Decimal
8+
from pathlib import Path
9+
10+
# Note that we need to import from functional test framework modules
11+
# *after* extending the Python path via sys.path.insert(0, ...) below,
12+
# in order to work with the full symlinked (unresolved) path within the
13+
# build directory (usually ./build/test/functional).
14+
15+
16+
# Test matching the minimal example from the documentation. Should be kept
17+
# in sync with the interactive shell instructions ('>>> ') in test-shell.md.
18+
def run_testshell_doc_example(functional_tests_dir):
19+
import sys
20+
sys.path.insert(0, functional_tests_dir)
21+
from test_framework.test_shell import TestShell
22+
from test_framework.util import assert_equal
23+
24+
test = TestShell().setup(num_nodes=2, setup_clean_chain=True)
25+
try:
26+
assert test is not None
27+
test2 = TestShell().setup()
28+
assert test2 is None # TODO: check for "TestShell is already running!" output to stdout
29+
assert_equal(test.nodes[0].getblockchaininfo()["blocks"], 0)
30+
if test.is_wallet_compiled():
31+
res = test.nodes[0].createwallet('default')
32+
assert_equal(res, {'name': 'default'})
33+
address = test.nodes[0].getnewaddress()
34+
res = test.generatetoaddress(test.nodes[0], 101, address)
35+
assert_equal(len(res), 101)
36+
test.sync_blocks()
37+
assert_equal(test.nodes[1].getblockchaininfo()["blocks"], 101)
38+
assert_equal(test.nodes[0].getbalance(), Decimal('50.0'))
39+
test.nodes[0].log.info("Successfully mined regtest chain!")
40+
finally:
41+
test.shutdown()
42+
test.reset()
43+
assert test.num_nodes is None
44+
45+
46+
if __name__ == "__main__":
47+
run_testshell_doc_example(str(Path(__file__).parent))

test/functional/test-shell.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ rewards to a wallet address owned by the mining node.
9797

9898
```
9999
>>> test.nodes[0].createwallet('default')
100-
{'name': 'default', 'warning': 'Empty string given as passphrase, wallet will not be encrypted.'}
100+
{'name': 'default'}
101101
>>> address = test.nodes[0].getnewaddress()
102102
>>> test.generatetoaddress(test.nodes[0], 101, address)
103103
['2b98dd0044aae6f1cca7f88a0acf366a4bfe053c7f7b00da3c0d115f03d67efb', ...
@@ -179,7 +179,6 @@ can be called after the TestShell is shut down.
179179
| `coveragedir` | `None` | Records bitcoind RPC test coverage into this directory if set. |
180180
| `loglevel` | `INFO` | Logs events at this level and higher. Can be set to `DEBUG`, `INFO`, `WARNING`, `ERROR` or `CRITICAL`. |
181181
| `nocleanup` | `False` | Cleans up temporary test directory if set to `True` during `shutdown`. |
182-
| `noshutdown` | `False` | Does not stop bitcoind instances after `shutdown` if set to `True`. |
183182
| `num_nodes` | `1` | Sets the number of initialized bitcoind processes. |
184183
| `perf` | False | Profiles running nodes with `perf` for the duration of the test if set to `True`. |
185184
| `rpc_timeout` | `60` | Sets the RPC server timeout for the underlying bitcoind processes. |

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@
362362
'rpc_getdescriptorinfo.py',
363363
'rpc_mempool_info.py',
364364
'rpc_help.py',
365+
'feature_framework_testshell.py',
365366
'tool_rpcauth.py',
366367
'p2p_handshake.py',
367368
'p2p_handshake.py --v2transport',

0 commit comments

Comments
 (0)