Skip to content

Commit

Permalink
Merge #14024: qa: Add TestNode::assert_debug_log
Browse files Browse the repository at this point in the history
Summary:
fa3e9f7627 qa: Add TestNode::assert_debug_log (MarcoFalke)

Pull request description:

  Closes #13006

Tree-SHA512: 8e2d2331039d70525a425aad65a4eaf9b83fb1f85a4260d69453176f04dbede6dd9b7bb4d5f089b46cf8f8c98571aa6ba7fac8fa6847bb3bdf6a6ad21a27b1a7

Backport of Core PR14024
https://github.com/bitcoin/bitcoin/pull/14024/files

Test Plan: `test_runner.py p2p_invalid_tx`

Reviewers: #bitcoin_abc, deadalnix, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D3860
  • Loading branch information
MarcoFalke authored and jasonbcox committed Aug 13, 2019
1 parent b30d4ad commit 65afda2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 7 additions & 2 deletions test/functional/p2p_invalid_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,13 @@ def run_test(self):
'Test a transaction that is rejected, with BIP61 disabled')
self.restart_node(0, ['-enablebip61=0', '-persistmempool=0'])
self.reconnect_p2p(num_connections=1)
node.p2p.send_txs_and_test(
[tx1], node, success=False, expect_disconnect=True)
with node.assert_debug_log(expected_msgs=[
"{} from peer=0 was not accepted: mandatory-script-verify-flag-failed (Invalid OP_IF construction) (code 16)".format(
tx1.hash),
"disconnecting peer=0",
]):
node.p2p.send_txs_and_test(
[tx1], node, success=False, expect_disconnect=True)
# send_txs_and_test will have waited for disconnect, so we can safely check that no reject has been received
assert_equal(node.p2p.reject_code_received, None)

Expand Down
19 changes: 19 additions & 0 deletions test/functional/test_framework/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Class for bitcoind node under test"""

import contextlib
import decimal
import errno
import http.client
Expand Down Expand Up @@ -243,6 +244,24 @@ def is_node_stopped(self):
def wait_until_stopped(self, timeout=BITCOIND_PROC_WAIT_TIMEOUT):
wait_until(self.is_node_stopped, timeout=timeout)

@contextlib.contextmanager
def assert_debug_log(self, expected_msgs):
debug_log = os.path.join(self.datadir, 'regtest', 'debug.log')
with open(debug_log, encoding='utf-8') as dl:
dl.seek(0, 2)
prev_size = dl.tell()
try:
yield
finally:
with open(debug_log, encoding='utf-8') as dl:
dl.seek(prev_size)
log = dl.read()
print_log = " - " + "\n - ".join(log.splitlines())
for expected_msg in expected_msgs:
if re.search(re.escape(expected_msg), log, flags=re.MULTILINE) is None:
self._raise_assertion_error(
'Expected message "{}" does not partially match log:\n\n{}\n\n'.format(expected_msg, print_log))

def assert_start_raises_init_error(self, extra_args=None, expected_msg=None, partial_match=False, *args, **kwargs):
"""Attempt to start the node and expect it to raise an error.
Expand Down

0 comments on commit 65afda2

Please sign in to comment.