Browse Source

pytest: always use the fake-bitcoin-cli, name it more appropriately.

We're going to use it to override specific commands.  It's non-valgrinded
already since we use '--trace-children-skip=*bitcoin-cli*' so the overhead
should be minimal.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 6 years ago
committed by Christian Decker
parent
commit
7a77b81dff
  1. 6
      tests/test_connection.py
  2. 6
      tests/test_misc.py
  3. 31
      tests/utils.py

6
tests/test_connection.py

@ -1083,7 +1083,7 @@ def test_fundee_forget_funding_tx_unconfirmed(node_factory, bitcoind):
# blocks.
blocks = 200
# funder
l1 = node_factory.get_node(fake_bitcoin_cli=True)
l1 = node_factory.get_node()
# fundee
l2 = node_factory.get_node(options={"dev-max-funding-unconfirmed-blocks": blocks})
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
@ -1094,13 +1094,13 @@ def test_fundee_forget_funding_tx_unconfirmed(node_factory, bitcoind):
time.sleep(1)
# Prevent funder from broadcasting funding tx.
l1.fake_bitcoind_fail('exit 1')
l1.bitcoind_cmd_override('exit 1')
# Fund the channel.
# The process will complete, but funder will be unable
# to broadcast and confirm funding tx.
l1.rpc.fundchannel(l2.info['id'], 10**6)
# Prevent l1 from timing out bitcoin-cli.
l1.fake_bitcoind_unfail()
l1.bitcoind_cmd_remove_override()
# Generate blocks until unconfirmed.
bitcoind.generate_block(blocks)

6
tests/test_misc.py

@ -83,12 +83,12 @@ def test_db_upgrade(node_factory):
def test_bitcoin_failure(node_factory, bitcoind):
l1 = node_factory.get_node(fake_bitcoin_cli=True)
l1 = node_factory.get_node()
# Make sure we're not failing it between getblockhash and getblock.
sync_blockheight(bitcoind, [l1])
l1.fake_bitcoind_fail('exit 1')
l1.bitcoind_cmd_override('exit 1')
# This should cause both estimatefee and getblockhash fail
l1.daemon.wait_for_logs(['estimatesmartfee .* exited with status 1',
@ -99,7 +99,7 @@ def test_bitcoin_failure(node_factory, bitcoind):
'getblockhash .* exited with status 1'])
# Restore, then it should recover and get blockheight.
l1.fake_bitcoind_unfail()
l1.bitcoind_cmd_remove_override()
bitcoind.generate_block(5)
sync_blockheight(bitcoind, [l1])

31
tests/utils.py

@ -601,18 +601,19 @@ class LightningNode(object):
# wait for sendpay to comply
self.rpc.waitsendpay(rhash)
def fake_bitcoind_fail(self, failscript='exit 1', cmd=None):
def bitcoind_cmd_override(self, failscript='exit 1', cmd=None):
# Create and rename, for atomicity.
f = os.path.join(self.daemon.lightning_dir, "bitcoin-cli-fail.tmp")
with open(f, "w") as text_file:
text_file.write(failscript)
os.chmod(f, os.stat(f).st_mode | stat.S_IEXEC)
if cmd:
failfile = "bitcoin-cli-fail-{}".format(cmd)
else:
failfile = "bitcoin-cli-fail"
os.rename(f, os.path.join(self.daemon.lightning_dir, failfile))
def fake_bitcoind_unfail(self, cmd=None):
def bitcoind_cmd_remove_override(self, cmd=None):
if cmd:
failfile = "bitcoin-cli-fail-{}".format(cmd)
else:
@ -644,7 +645,6 @@ class NodeFactory(object):
'may_fail',
'may_reconnect',
'random_hsm',
'fake_bitcoin_cli'
]
node_opts = {k: v for k, v in opts.items() if k in node_opt_keys}
cli_opts = {k: v for k, v in opts.items() if k not in node_opt_keys}
@ -673,8 +673,7 @@ class NodeFactory(object):
return [j.result() for j in jobs]
def get_node(self, disconnect=None, options=None, may_fail=False, may_reconnect=False, random_hsm=False,
fake_bitcoin_cli=False):
def get_node(self, disconnect=None, options=None, may_fail=False, may_reconnect=False, random_hsm=False):
with self.lock:
node_id = self.next_id
self.next_id += 1
@ -705,18 +704,16 @@ class NodeFactory(object):
if not may_reconnect:
daemon.opts["dev-no-reconnect"] = None
if fake_bitcoin_cli:
cli = os.path.join(lightning_dir, "fake-bitcoin-cli")
with open(cli, "w") as text_file:
text_file.write('#! /bin/sh\n'
'! [ -f bitcoin-cli-fail ] || . {ldir}/bitcoin-cli-fail\n'
'for a in "$@"; do\n'
'\t! [ -f bitcoin-cli-fail-"$a" ] || . {ldir}/bitcoin-cli-fail-"$a"\n'
'done\n'
'exec bitcoin-cli "$@"\n'
.format(ldir=lightning_dir))
os.chmod(cli, os.stat(cli).st_mode | stat.S_IEXEC)
daemon.opts['bitcoin-cli'] = cli
cli = os.path.join(lightning_dir, "fake-bitcoin-cli")
with open(cli, "w") as text_file:
text_file.write('#! /bin/sh\n'
'! [ -x bitcoin-cli-fail ] || exec ./bitcoin-cli-fail "$@"\n'
'for a in "$@"; do\n'
'\t! [ -x bitcoin-cli-fail-"$a" ] || exec ./bitcoin-cli-fail-"$a" "$@"\n'
'done\n'
'exec bitcoin-cli "$@"\n')
os.chmod(cli, os.stat(cli).st_mode | stat.S_IEXEC)
daemon.opts['bitcoin-cli'] = cli
if options is not None:
daemon.opts.update(options)

Loading…
Cancel
Save