tests: improve test script formatting

Remove annyoing spacing constraints enforced by the previous 'black' linter.
This commit is contained in:
Erik Arvstedt 2021-08-05 00:49:09 +02:00
parent 1be924529d
commit 01804e6dfb
No known key found for this signature in database
GPG Key ID: 33312B944DD97846

View File

@ -7,34 +7,28 @@ def succeed(*cmds):
"""Returns the concatenated output of all cmds""" """Returns the concatenated output of all cmds"""
return machine.succeed(*cmds) return machine.succeed(*cmds)
def assert_matches(cmd, regexp): def assert_matches(cmd, regexp):
out = succeed(cmd) out = succeed(cmd)
if not re.search(regexp, out): if not re.search(regexp, out):
raise Exception(f"Pattern '{regexp}' not found in '{out}'") raise Exception(f"Pattern '{regexp}' not found in '{out}'")
def assert_full_match(cmd, regexp): def assert_full_match(cmd, regexp):
out = succeed(cmd) out = succeed(cmd)
if not re.fullmatch(regexp, out): if not re.fullmatch(regexp, out):
raise Exception(f"Pattern '{regexp}' doesn't match '{out}'") raise Exception(f"Pattern '{regexp}' doesn't match '{out}'")
def log_has_string(unit, str): def log_has_string(unit, str):
return f"journalctl -b --output=cat -u {unit} --grep='{str}'" return f"journalctl -b --output=cat -u {unit} --grep='{str}'"
def assert_no_failure(unit): def assert_no_failure(unit):
"""Unit should not have failed since the system is running""" """Unit should not have failed since the system is running"""
machine.fail(log_has_string(unit, "Failed with result")) machine.fail(log_has_string(unit, "Failed with result"))
def assert_running(unit): def assert_running(unit):
with machine.nested(f"waiting for unit: {unit}"): with machine.nested(f"waiting for unit: {unit}"):
machine.wait_for_unit(unit) machine.wait_for_unit(unit)
assert_no_failure(unit) assert_no_failure(unit)
def wait_for_open_port(address, port): def wait_for_open_port(address, port):
def is_port_open(_): def is_port_open(_):
status, _ = machine.execute(f"nc -z {address} {port}") status, _ = machine.execute(f"nc -z {address} {port}")
@ -48,14 +42,11 @@ def wait_for_open_port(address, port):
tests = OrderedDict() tests = OrderedDict()
def test(name): def test(name):
def x(fn): def x(fn):
tests[name] = fn tests[name] = fn
return x return x
def run_tests(): def run_tests():
enabled = enabled_tests.copy() enabled = enabled_tests.copy()
to_run = [] to_run = []
@ -70,7 +61,6 @@ def run_tests():
with logger.nested(f"test: {test}"): with logger.nested(f"test: {test}"):
tests[test]() tests[test]()
def run_test(test): def run_test(test):
tests[test]() tests[test]()
@ -78,7 +68,6 @@ def run_test(test):
### Tests ### Tests
# All tests are executed in the order they are defined here # All tests are executed in the order they are defined here
@test("security") @test("security")
def _(): def _():
assert_running("setup-secrets") assert_running("setup-secrets")
@ -112,7 +101,6 @@ def _():
log_has_string("bitcoind", "RPC User public not allowed to call method stop") log_has_string("bitcoind", "RPC User public not allowed to call method stop")
) )
@test("electrs") @test("electrs")
def _(): def _():
assert_running("electrs") assert_running("electrs")
@ -120,14 +108,12 @@ def _():
# Check RPC connection to bitcoind # Check RPC connection to bitcoind
machine.wait_until_succeeds(log_has_string("electrs", "NetworkInfo")) machine.wait_until_succeeds(log_has_string("electrs", "NetworkInfo"))
# Impure: Stops electrs # Impure: Stops electrs
# Stop electrs from spamming the test log with 'WARN - wait until IBD is over' messages # Stop electrs from spamming the test log with 'WARN - wait until IBD is over' messages
@test("stop-electrs") @test("stop-electrs")
def _(): def _():
succeed("systemctl stop electrs") succeed("systemctl stop electrs")
@test("liquidd") @test("liquidd")
def _(): def _():
assert_running("liquidd") assert_running("liquidd")
@ -135,7 +121,6 @@ def _():
assert_matches("runuser -u operator -- elements-cli getnetworkinfo | jq", '"version"') assert_matches("runuser -u operator -- elements-cli getnetworkinfo | jq", '"version"')
succeed("runuser -u operator -- liquidswap-cli --help") succeed("runuser -u operator -- liquidswap-cli --help")
@test("clightning") @test("clightning")
def _(): def _():
assert_running("clightning") assert_running("clightning")
@ -155,19 +140,16 @@ def _():
for p in test_data["clightning-plugins"]: for p in test_data["clightning-plugins"]:
logger.log(os.path.basename(p)) logger.log(os.path.basename(p))
@test("lnd") @test("lnd")
def _(): def _():
assert_running("lnd") assert_running("lnd")
assert_matches("runuser -u operator -- lncli getinfo | jq", '"version"') assert_matches("runuser -u operator -- lncli getinfo | jq", '"version"')
assert_no_failure("lnd") assert_no_failure("lnd")
@test("lnd-rest-onion-service") @test("lnd-rest-onion-service")
def _(): def _():
assert_matches("runuser -u operator -- lndconnect-rest-onion -j", ".onion") assert_matches("runuser -u operator -- lndconnect-rest-onion -j", ".onion")
@test("lightning-loop") @test("lightning-loop")
def _(): def _():
assert_running("lightning-loop") assert_running("lightning-loop")
@ -181,7 +163,6 @@ def _():
) )
) )
@test("lightning-pool") @test("lightning-pool")
def _(): def _():
assert_running("lightning-pool") assert_running("lightning-pool")
@ -195,14 +176,12 @@ def _():
) )
) )
@test("charge-lnd") @test("charge-lnd")
def _(): def _():
# charge-lnd is a oneshot service that is started by a timer under regular operation # charge-lnd is a oneshot service that is started by a timer under regular operation
succeed("systemctl start charge-lnd") succeed("systemctl start charge-lnd")
assert_no_failure("charge-lnd") assert_no_failure("charge-lnd")
@test("btcpayserver") @test("btcpayserver")
def _(): def _():
assert_running("nbxplorer") assert_running("nbxplorer")
@ -220,7 +199,6 @@ def _():
'"version"', '"version"',
) )
@test("spark-wallet") @test("spark-wallet")
def _(): def _():
assert_running("spark-wallet") assert_running("spark-wallet")
@ -228,7 +206,6 @@ def _():
spark_auth = re.search("login=(.*)", succeed("cat /secrets/spark-wallet-login"))[1] spark_auth = re.search("login=(.*)", succeed("cat /secrets/spark-wallet-login"))[1]
assert_matches(f"curl -s {spark_auth}@{ip('spark-wallet')}:9737", "Spark") assert_matches(f"curl -s {spark_auth}@{ip('spark-wallet')}:9737", "Spark")
@test("joinmarket") @test("joinmarket")
def _(): def _():
assert_running("joinmarket") assert_running("joinmarket")
@ -236,20 +213,17 @@ def _():
log_has_string("joinmarket", "JMDaemonServerProtocolFactory starting on 27183") log_has_string("joinmarket", "JMDaemonServerProtocolFactory starting on 27183")
) )
@test("joinmarket-yieldgenerator") @test("joinmarket-yieldgenerator")
def _(): def _():
machine.wait_until_succeeds( machine.wait_until_succeeds(
log_has_string("joinmarket-yieldgenerator", "Critical error updating blockheight.") log_has_string("joinmarket-yieldgenerator", "Critical error updating blockheight.")
) )
@test("joinmarket-ob-watcher") @test("joinmarket-ob-watcher")
def _(): def _():
assert_running("joinmarket-ob-watcher") assert_running("joinmarket-ob-watcher")
machine.wait_until_succeeds(log_has_string("joinmarket-ob-watcher", "Starting ob-watcher")) machine.wait_until_succeeds(log_has_string("joinmarket-ob-watcher", "Starting ob-watcher"))
@test("nodeinfo") @test("nodeinfo")
def _(): def _():
status, _ = machine.execute("systemctl is-enabled --quiet onion-addresses 2> /dev/null") status, _ = machine.execute("systemctl is-enabled --quiet onion-addresses 2> /dev/null")
@ -259,12 +233,10 @@ def _():
info = json.loads(json_info) info = json.loads(json_info)
assert info["bitcoind"]["local_address"] assert info["bitcoind"]["local_address"]
@test("secure-node") @test("secure-node")
def _(): def _():
assert_running("onion-addresses") assert_running("onion-addresses")
# Run this test before the following tests that shut down services # Run this test before the following tests that shut down services
# (and their corresponding network namespaces). # (and their corresponding network namespaces).
@test("netns-isolation") @test("netns-isolation")
@ -347,7 +319,6 @@ def _():
assert_file_exists("secrets/lnd-wallet-password") assert_file_exists("secrets/lnd-wallet-password")
# Impure: restarts services # Impure: restarts services
@test("banlist-and-restart") @test("banlist-and-restart")
def _(): def _():
@ -368,7 +339,6 @@ def _():
) )
assert_no_failure("bitcoind-import-banlist") assert_no_failure("bitcoind-import-banlist")
@test("regtest") @test("regtest")
def _(): def _():
def enabled(unit): def enabled(unit):
@ -407,14 +377,9 @@ def _():
) )
succeed("runuser -u operator -- pool orders list") succeed("runuser -u operator -- pool orders list")
if "netns-isolation" in enabled_tests: if "netns-isolation" in enabled_tests:
def ip(name): def ip(name):
return test_data["netns"][name]["address"] return test_data["netns"][name]["address"]
else: else:
def ip(_): def ip(_):
return "127.0.0.1" return "127.0.0.1"