From 2befe0babfc74500de06b0aab0d48204206a4514 Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Mon, 10 Jun 2024 23:24:13 +0200 Subject: [PATCH] refactor tests --- .gitmodules | 9 +++ README.md | 10 +++ docker-compose.yml | 4 +- docker/lndk/wait-for-lnd.sh | 10 ++- scripts/init.sh | 6 ++ scripts/setup.sh | 4 + test/bats | 1 + test/functions.sh | 15 ++++ test/test.bats | 120 ----------------------------- test/test_helper/bats-assert | 1 + test/test_helper/bats-support | 1 + test/test_helper/common-setup.bash | 14 ++++ test/test_payments_cln.bats | 43 +++++++++++ test/test_payments_eclair.bats | 43 +++++++++++ 14 files changed, 157 insertions(+), 124 deletions(-) create mode 100644 .gitmodules create mode 100755 scripts/setup.sh create mode 160000 test/bats create mode 100755 test/functions.sh delete mode 100755 test/test.bats create mode 160000 test/test_helper/bats-assert create mode 160000 test/test_helper/bats-support create mode 100644 test/test_helper/common-setup.bash create mode 100755 test/test_payments_cln.bats create mode 100755 test/test_payments_eclair.bats diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b7efcb4 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,9 @@ +[submodule "test/bats"] + path = test/bats + url = https://github.com/bats-core/bats-core.git +[submodule "test/test_helper/bats-support"] + path = test/test_helper/bats-support + url = https://github.com/bats-core/bats-support.git +[submodule "test/test_helper/bats-assert"] + path = test/test_helper/bats-assert + url = https://github.com/bats-core/bats-assert.git diff --git a/README.md b/README.md index ac91e79..3649e98 100644 --- a/README.md +++ b/README.md @@ -6,14 +6,24 @@ You can use this to get familiar with [Bolt 12](https://bolt12.org/). ## Setup +**Clone the repository:** + +```sh +git clone --recursive https://github.com/your/repo.git +``` + **Start nodes:** +Start the docker stack to start the nodes: + ```sh docker compose up ``` **Initialise the nodes:** +In a separate terminal, run the following command to initialise the nodes: + ```sh ./scripts/init.sh ``` diff --git a/docker-compose.yml b/docker-compose.yml index 1feac87..7cf19b5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -28,7 +28,7 @@ services: restart: unless-stopped depends_on: - lnd1 - entrypoint: ["/wait-for-lnd.sh", "lnd1"] + entrypoint: ["/wait-for-lnd.sh", "lnd1", "5"] command: --address=https://lnd1:10009 --cert-path=/root/.lnd/tls.cert --macaroon-path=/root/.lnd/data/chain/bitcoin/regtest/admin.macaroon --log-level=trace --grpc-host=0.0.0.0 environment: - RUST_BACKTRACE=1 @@ -89,7 +89,7 @@ services: restart: unless-stopped depends_on: - lnd2 - entrypoint: ["/wait-for-lnd.sh", "lnd2"] + entrypoint: ["/wait-for-lnd.sh", "lnd2", "5"] command: --address=https://lnd2:10009 --cert-path=/root/.lnd/tls.cert --macaroon-path=/root/.lnd/data/chain/bitcoin/regtest/admin.macaroon --log-level=trace --grpc-host=0.0.0.0 environment: - RUST_BACKTRACE=1 diff --git a/docker/lndk/wait-for-lnd.sh b/docker/lndk/wait-for-lnd.sh index 4f727a6..8a7dc93 100755 --- a/docker/lndk/wait-for-lnd.sh +++ b/docker/lndk/wait-for-lnd.sh @@ -5,6 +5,7 @@ # # Arguments: # host The host of the lnd's gRPC service. +# delay The delay (in seconds) for the startup of lndk nodes after lnd's gRPC port is ready. # lndk-args The arguments to be passed to the lndk command. is_lnd_ready() { @@ -23,7 +24,6 @@ is_lnd_ready() { return 1 fi } - # Wait for lnd to be ready # The until loop will keep looping as long as the is_lnd_ready function returns a non-zero value (i.e., the port is not open). until is_lnd_ready $1; do @@ -31,7 +31,13 @@ until is_lnd_ready $1; do sleep 2 done +# Delay startup of lndk nodes +# The sleep command is used to pause the script for a specified number of seconds before starting lndk. +# The number of seconds is specified by the second parameter to the script. +echo "Waiting for another $2 seconds before starting lndk..." +sleep "$2" + # Start lndk # The exec command is used to replace the current shell process with the lndk command. # The "${@:3}" part is used to pass all arguments starting from the third one to the lndk command. -exec lndk "${@:2}" \ No newline at end of file +exec lndk "${@:3}" \ No newline at end of file diff --git a/scripts/init.sh b/scripts/init.sh index 77bc5cb..fce9d83 100755 --- a/scripts/init.sh +++ b/scripts/init.sh @@ -2,6 +2,11 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +setup () { + $DIR/setup.sh +} + bitcoind() { $DIR/../bin/bitcoin-cli $@ } @@ -261,6 +266,7 @@ waitForNodes() { } main() { + setup waitBitcoind createBitcoindWallet generateBitcoinAddress diff --git a/scripts/setup.sh b/scripts/setup.sh new file mode 100755 index 0000000..e75f9ae --- /dev/null +++ b/scripts/setup.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +# Initialize and update submodules +git submodule update --init --recursive \ No newline at end of file diff --git a/test/bats b/test/bats new file mode 160000 index 0000000..5642f5b --- /dev/null +++ b/test/bats @@ -0,0 +1 @@ +Subproject commit 5642f5b0b0151a691dda0865e34d7bf9473e09e9 diff --git a/test/functions.sh b/test/functions.sh new file mode 100755 index 0000000..d90d0a5 --- /dev/null +++ b/test/functions.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +generate_offer_eclair() { + local generate_node=$1 + + run $PROJECT_ROOT/bin/eclair-cli $generate_node tipjarshowoffer + echo $output +} + +generate_offer_cln() { + local generate_node=$1 + + run $PROJECT_ROOT/bin/lightning-cli $generate_node offer 1000 "test offer from $generate_node" + echo "$output" | awk -F'"bolt12": "' '{print $2}' | awk -F'"' '{print $1}' +} \ No newline at end of file diff --git a/test/test.bats b/test/test.bats deleted file mode 100755 index 26373dc..0000000 --- a/test/test.bats +++ /dev/null @@ -1,120 +0,0 @@ -#!/usr/bin/env bats - -# set -eo pipefail -# set -x - -setup() { - error_occurred=0 -} - -teardown() { - if [ $error_occurred -eq 1 ]; then - fail "An error occurred during the test" - fi -} - -# Single hop payments -@test "Generate bolt12 offer on eclair1 and pay from lndk1 (lnd1 -> eclair1)" { - run generate_offer_eclair 'eclair1' - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } - offer=$output - run pay_offer 'lndk1' $offer 1000 - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } -} - -@test "Generate bolt12 offer on cln1 and pay from lndk1 (lnd1 -> cln1)" { - run generate_offer_cln 'cln1' - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } - offer=$output - run pay_offer 'lndk1' $offer 1000 - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } -} - -# Multi-hop payments (eclair) -@test "Generate bolt12 offer on eclair2 and pay from lndk1 (lnd1 -> lnd2 -> eclair2)" { - run generate_offer_eclair 'eclair2' - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } - offer=$output - run pay_offer 'lndk1' $offer 1000 - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } -} - -@test "Generate bolt12 offer on eclair3 and pay from lndk2 (lnd2 -> eclair2 -> eclair3)" { - run generate_offer_eclair 'eclair3' - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } - offer=$output - run pay_offer 'lndk2' $offer 1000 - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } -} - -@test "Generate bolt12 offer on eclair3 and pay from lndk1 (lnd1 -> lnd2 -> eclair2 -> eclair3)" { - run generate_offer_eclair 'eclair3' - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } - offer=$output - run pay_offer 'lndk1' $offer 1000 - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } -} - -# Multi-hop payments (cln) -@test "Generate bolt12 offer on cln2 and pay from lndk1 (lnd1 -> lnd2 -> cln2)" { - run generate_offer_eclair 'cln2' - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } - offer=$output - run pay_offer 'lndk1' $offer 1000 - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } -} - -@test "Generate bolt12 offer on cln3 and pay from lndk2 (lnd2 -> cln2 -> cln3)" { - run generate_offer_eclair 'cln3' - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } - offer=$output - run pay_offer 'lndk2' $offer 1000 - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } -} - -@test "Generate bolt12 offer on cln3 and pay from lndk1 (lnd1 -> lnd2 -> cln2 -> cln3)" { - run generate_offer_eclair 'cln3' - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } - offer=$output - run pay_offer 'lndk1' $offer 1000 - [ "$status" -eq 0 ] || { echo "Error: $output"; false; } -} - - -generate_offer_eclair() { - local generate_node=$1 - local offer=$(./bin/eclair-cli $generate_node tipjarshowoffer) - if [ $? -ne 0 ]; then - echo "Failed to generate bolt12 offer on $generate_node" - return 1 - fi - echo $offer -} - -generate_offer_cln() { - local generate_node=$1 - local output=$(./bin/lightning-cli $generate_node offer 1000 "test offer from $generate_node") - if [ $? -ne 0 ]; then - echo "Failed to generate bolt12 offer on $generate_node" - return 1 - fi - local offer=$(echo $output | awk -F'"bolt12": "' '{print $2}' | awk -F'"' '{print $1}') - echo $offer -} - -pay_offer() { - local pay_node=$1 - local offer=$2 - local amount=$3 - tmpfile=$(mktemp) - ./bin/lndk-cli $pay_node pay-offer $offer $amount > $tmpfile 2>&1 - status=$? - output=$(< $tmpfile) - rm $tmpfile - if [ $status -ne 0 ]; then - echo "Failed to pay bolt12 offer from $pay_node: $output" - echo Commad ran was "./bin/lndk-cli $pay_node pay-offer $offer $amount" - return 1 - fi - echo "Successfully paid bolt12 offer from $pay_node" -} diff --git a/test/test_helper/bats-assert b/test/test_helper/bats-assert new file mode 160000 index 0000000..e2d855b --- /dev/null +++ b/test/test_helper/bats-assert @@ -0,0 +1 @@ +Subproject commit e2d855bc78619ee15b0c702b5c30fb074101159f diff --git a/test/test_helper/bats-support b/test/test_helper/bats-support new file mode 160000 index 0000000..9bf10e8 --- /dev/null +++ b/test/test_helper/bats-support @@ -0,0 +1 @@ +Subproject commit 9bf10e876dd6b624fe44423f0b35e064225f7556 diff --git a/test/test_helper/common-setup.bash b/test/test_helper/common-setup.bash new file mode 100644 index 0000000..2a7fbed --- /dev/null +++ b/test/test_helper/common-setup.bash @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +_common_setup() { + load 'test_helper/bats-support/load' + load 'test_helper/bats-assert/load' + + # get the containing directory of this file + # use $BATS_TEST_FILENAME instead of ${BASH_SOURCE[0]} or $0, + # as those will point to the bats executable's location or the preprocessed file respectively + PROJECT_ROOT="$( cd "$( dirname "$BATS_TEST_FILENAME" )/.." >/dev/null 2>&1 && pwd )" + + # Ensure environment is setup. + $PROJECT_ROOT/scripts/setup.sh +} \ No newline at end of file diff --git a/test/test_payments_cln.bats b/test/test_payments_cln.bats new file mode 100755 index 0000000..61737dd --- /dev/null +++ b/test/test_payments_cln.bats @@ -0,0 +1,43 @@ +#!/usr/bin/env bats + +# set -eo pipefail +# set -x + +setup() { + load 'test_helper/common-setup' + _common_setup + + source "$PROJECT_ROOT/test/functions.sh" +} + +@test "Generate bolt12 offer on cln1 and pay from lndk1 (lnd1 -> cln1)" { + run generate_offer_cln 'cln1' + assert_line --partial 'lno' + + run $PROJECT_ROOT/bin/lndk-cli lndk1 pay-offer $output 1000 + assert_line --partial 'Successfully paid for offer!' +} + +@test "Generate bolt12 offer on cln2 and pay from lndk1 (lnd1 -> lnd2 -> cln2)" { + run generate_offer_cln 'cln2' + assert_line --partial 'lno' + + run $PROJECT_ROOT/bin/lndk-cli lndk1 pay-offer $output 1000 + assert_line --partial 'Successfully paid for offer!' +} + +@test "Generate bolt12 offer on cln3 and pay from lndk2 (lnd2 -> cln2 -> cln3)" { + run generate_offer_cln 'cln3' + assert_line --partial 'lno' + + run $PROJECT_ROOT/bin/lndk-cli lndk2 pay-offer $output 1000 + assert_line --partial 'Successfully paid for offer!' +} + +@test "Generate bolt12 offer on cln3 and pay from lndk1 (lnd1 -> lnd2 -> cln2 -> cln3)" { + run generate_offer_cln 'cln3' + assert_line --partial 'lno' + + run $PROJECT_ROOT/bin/lndk-cli lndk1 pay-offer $output 1000 + assert_line --partial 'Successfully paid for offer!' +} \ No newline at end of file diff --git a/test/test_payments_eclair.bats b/test/test_payments_eclair.bats new file mode 100755 index 0000000..d7405bd --- /dev/null +++ b/test/test_payments_eclair.bats @@ -0,0 +1,43 @@ +#!/usr/bin/env bats + +# set -eo pipefail +# set -x + +setup() { + load 'test_helper/common-setup' + _common_setup + + source "$PROJECT_ROOT/test/functions.sh" +} + +@test "Generate bolt12 offer on eclair1 and pay from lndk1 (lnd1 -> eclair1)" { + run generate_offer_eclair 'eclair1' + assert_line --partial 'lno' + + run $PROJECT_ROOT/bin/lndk-cli lndk1 pay-offer $output 1000 + assert_line --partial 'Successfully paid for offer!' +} + +@test "Generate bolt12 offer on eclair2 and pay from lndk1 (lnd1 -> lnd2 -> eclair2)" { + run generate_offer_eclair 'eclair2' + assert_line --partial 'lno' + + run $PROJECT_ROOT/bin/lndk-cli lndk1 pay-offer $output 1000 + assert_line --partial 'Successfully paid for offer!' +} + +@test "Generate bolt12 offer on eclair3 and pay from lndk2 (lnd2 -> eclair2 -> eclair3)" { + run generate_offer_eclair 'eclair3' + assert_line --partial 'lno' + + run $PROJECT_ROOT/bin/lndk-cli lndk1 pay-offer $output 1000 + assert_line --partial 'Successfully paid for offer!' +} + +@test "Generate bolt12 offer on eclair3 and pay from lndk1 (lnd1 -> lnd2 -> eclair2 -> eclair2)" { + run generate_offer_eclair 'eclair2' + assert_line --partial 'lno' + + run $PROJECT_ROOT/bin/lndk-cli lndk1 pay-offer $output 1000 + assert_line --partial 'Successfully paid for offer!' +} \ No newline at end of file