Skip to content

Commit

Permalink
Refactor python tests code and make it flake8 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
fmhoeger committed Jan 7, 2024
1 parent 8ca0dae commit 86bf227
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 98 deletions.
89 changes: 89 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,95 @@
import logging
import os
from pathlib import Path

import pytest
from pyln.testing.fixtures import ( # noqa: F401
db_provider,
directory,
env,
executor,
jsonschemas,
network_daemons,
node_cls,
node_factory,
teardown_checks,
test_base_dir,
test_name,
)
from pyln.testing.utils import BITCOIND_CONFIG, write_config

SMAUG_PLUGIN = Path.cwd().joinpath("../target/debug/smaug")


# copied from https://github.com/elementsproject/lightning/blob/37ad798a02336a82460b865fd4e6a29d8880856c/contrib/pyln-testing/pyln/testing/fixtures.py#L127-L164
# hacked to include blockfilterindex
@pytest.fixture
def bitcoind(directory, teardown_checks): # noqa: F811
chaind = network_daemons[env("TEST_NETWORK", "regtest")]
bitcoind = chaind(bitcoin_dir=directory)

BITCOIND_REGTEST = {"rpcport": BITCOIND_CONFIG["rpcport"]}

BITCOIND_CONFIG["blockfilterindex"] = 1
BITCOIND_REGTEST["blockfilterindex"] = 1

conf_file = os.path.join(directory, "bitcoin.conf")
write_config(conf_file, BITCOIND_CONFIG, BITCOIND_REGTEST)

try:
bitcoind.start()
except Exception:
bitcoind.stop()
raise

info = bitcoind.rpc.getnetworkinfo()

# FIXME: include liquid-regtest in this check after elementsd has been
# updated
if info["version"] < 200100 and env("TEST_NETWORK") != "liquid-regtest":
bitcoind.rpc.stop()
raise ValueError(
"bitcoind is too old. At least version 20100 (v0.20.1)"
" is needed, current version is {}".format(info["version"])
)
elif info["version"] < 160000:
bitcoind.rpc.stop()
raise ValueError(
"elementsd is too old. At least version 160000 (v0.16.0)"
" is needed, current version is {}".format(info["version"])
)

info = bitcoind.rpc.getblockchaininfo()
# Make sure we have some spendable funds
if info["blocks"] < 101:
bitcoind.generate_block(101 - info["blocks"])
elif bitcoind.rpc.getwalletinfo()["balance"] < 1:
logging.debug("Insufficient balance, generating 1 block")
bitcoind.generate_block(1)

yield bitcoind

try:
bitcoind.stop()
except Exception:
bitcoind.proc.kill()
bitcoind.proc.wait()


@pytest.fixture
def ln_node(node_factory): # noqa: F811
yield node_factory.get_nodes(
1,
opts={
"allow_broken_log": True,
"plugin": SMAUG_PLUGIN,
"smaug_brpc_user": BITCOIND_CONFIG["rpcuser"],
"smaug_brpc_pass": BITCOIND_CONFIG["rpcpassword"],
"smaug_brpc_port": BITCOIND_CONFIG["rpcport"],
},
)[0]


# SMAUG_PLUGIN = Path("~/.cargo/bin/smaug").expanduser()


Expand Down
90 changes: 0 additions & 90 deletions tests/fixtures.py

This file was deleted.

11 changes: 4 additions & 7 deletions tests/test_rpc.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import os
import re

from conftest import SMAUG_PLUGIN
from fixtures import *
from pyln.client import Millisatoshi
from pyln.testing.utils import BITCOIND_CONFIG, only_one, wait_for
from utils import get_bkpr_smaug_balance, get_only_one_descriptor


def test_rpc_add(bitcoind, ln_node):
"""
Test RPC add.
Test RPC add
"""

# Get external/internal only_one descriptors
Expand Down Expand Up @@ -54,7 +51,7 @@ def test_rpc_add(bitcoind, ln_node):

def test_rpc_list(bitcoind, ln_node):
"""
Test RPC list.
Test RPC list
"""

# Get external/internal only_one descriptors
Expand Down Expand Up @@ -122,7 +119,7 @@ def test_rpc_list(bitcoind, ln_node):

def test_rpc_remove(bitcoind, ln_node):
"""
Test RPC remove.
Test RPC remove
"""

# Get external/internal only_one descriptors
Expand Down
6 changes: 5 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def switch_wallet(bitcoind, wallet_name):
if w != wallet_name:
bitcoind.rpc.unloadwallet(w)


def send_from_wallet(bitcoind, wallet_name, address, amount):
current_wallet_name = only_one(bitcoind.rpc.listwallets())
if current_wallet_name != wallet_name:
Expand All @@ -58,7 +59,10 @@ def send_from_wallet(bitcoind, wallet_name, address, amount):
if current_wallet_name != wallet_name:
switch_wallet(bitcoind, current_wallet_name)

def generate_to_mining_wallet(bitcoind, mining_wallet_name, transacting_wallet_name, num_blocks=1):

def generate_to_mining_wallet(
bitcoind, mining_wallet_name, transacting_wallet_name, num_blocks=1
):
switch_wallet(bitcoind, mining_wallet_name)
bitcoind.generate_block(num_blocks)
switch_wallet(bitcoind, transacting_wallet_name)

0 comments on commit 86bf227

Please sign in to comment.