Skip to content

Commit

Permalink
Merge pull request #23 from LN-Zap/test-improvements
Browse files Browse the repository at this point in the history
refactor tests
  • Loading branch information
mrfelton authored Jun 10, 2024
2 parents c4ea642 + 2befe0b commit 1497508
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 124 deletions.
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions docker/lndk/wait-for-lnd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -23,15 +24,20 @@ 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
echo "Waiting for lnd to be ready..."
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}"
exec lndk "${@:3}"
6 changes: 6 additions & 0 deletions scripts/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"


setup () {
$DIR/setup.sh
}

bitcoind() {
$DIR/../bin/bitcoin-cli $@
}
Expand Down Expand Up @@ -261,6 +266,7 @@ waitForNodes() {
}

main() {
setup
waitBitcoind
createBitcoindWallet
generateBitcoinAddress
Expand Down
4 changes: 4 additions & 0 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# Initialize and update submodules
git submodule update --init --recursive
1 change: 1 addition & 0 deletions test/bats
Submodule bats added at 5642f5
15 changes: 15 additions & 0 deletions test/functions.sh
Original file line number Diff line number Diff line change
@@ -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}'
}
120 changes: 0 additions & 120 deletions test/test.bats

This file was deleted.

1 change: 1 addition & 0 deletions test/test_helper/bats-assert
Submodule bats-assert added at e2d855
1 change: 1 addition & 0 deletions test/test_helper/bats-support
Submodule bats-support added at 9bf10e
14 changes: 14 additions & 0 deletions test/test_helper/common-setup.bash
Original file line number Diff line number Diff line change
@@ -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
}
43 changes: 43 additions & 0 deletions test/test_payments_cln.bats
Original file line number Diff line number Diff line change
@@ -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!'
}
43 changes: 43 additions & 0 deletions test/test_payments_eclair.bats
Original file line number Diff line number Diff line change
@@ -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!'
}

0 comments on commit 1497508

Please sign in to comment.