itests.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #!/usr/bin/env bash
  2. # Function to perform cleanup
  3. cleanup() {
  4. echo "Cleaning up..."
  5. echo "Killing the cdk mintd"
  6. kill -2 $CDK_MINTD_PID
  7. wait $CDK_MINTD_PID
  8. echo "Killing the cdk lnd mintd"
  9. kill -2 $CDK_MINTD_LND_PID
  10. wait $CDK_MINTD_LND_PID
  11. echo "Killing the cdk regtest"
  12. kill -2 $CDK_REGTEST_PID
  13. wait $CDK_REGTEST_PID
  14. echo "Mint binary terminated"
  15. # Remove the temporary directory
  16. rm -rf "$CDK_ITESTS_DIR"
  17. echo "Temp directory removed: $CDK_ITESTS_DIR"
  18. # Unset all environment variables
  19. unset CDK_ITESTS_DIR
  20. unset CDK_ITESTS_MINT_ADDR
  21. unset CDK_ITESTS_MINT_PORT_0
  22. unset CDK_ITESTS_MINT_PORT_1
  23. unset CDK_MINTD_DATABASE
  24. unset CDK_TEST_MINT_URL
  25. unset CDK_TEST_MINT_URL_2
  26. unset CDK_MINTD_URL
  27. unset CDK_MINTD_WORK_DIR
  28. unset CDK_MINTD_LISTEN_HOST
  29. unset CDK_MINTD_LISTEN_PORT
  30. unset CDK_MINTD_LN_BACKEND
  31. unset CDK_MINTD_MNEMONIC
  32. unset CDK_MINTD_CLN_RPC_PATH
  33. unset CDK_MINTD_LND_ADDRESS
  34. unset CDK_MINTD_LND_CERT_FILE
  35. unset CDK_MINTD_LND_MACAROON_FILE
  36. unset CDK_MINTD_PID
  37. unset CDK_MINTD_LND_PID
  38. unset CDK_REGTEST_PID
  39. unset RUST_BACKTRACE
  40. unset CDK_TEST_REGTEST
  41. }
  42. # Set up trap to call cleanup on script exit
  43. trap cleanup EXIT
  44. export CDK_TEST_REGTEST=1
  45. # Create a temporary directory
  46. export CDK_ITESTS_DIR=$(mktemp -d)
  47. export CDK_ITESTS_MINT_ADDR="127.0.0.1"
  48. export CDK_ITESTS_MINT_PORT_0=8085
  49. export CDK_ITESTS_MINT_PORT_1=8087
  50. # Check if the temporary directory was created successfully
  51. if [[ ! -d "$CDK_ITESTS_DIR" ]]; then
  52. echo "Failed to create temp directory"
  53. exit 1
  54. fi
  55. echo "Temp directory created: $CDK_ITESTS_DIR"
  56. export CDK_MINTD_DATABASE="$1"
  57. cargo build -p cdk-integration-tests
  58. cargo run --bin start_regtest &
  59. export CDK_REGTEST_PID=$!
  60. mkfifo "$CDK_ITESTS_DIR/progress_pipe"
  61. rm -f "$CDK_ITESTS_DIR/signal_received" # Ensure clean state
  62. # Start reading from pipe in background
  63. (while read line; do
  64. case "$line" in
  65. "checkpoint1")
  66. echo "Reached first checkpoint"
  67. touch "$CDK_ITESTS_DIR/signal_received"
  68. exit 0
  69. ;;
  70. esac
  71. done < "$CDK_ITESTS_DIR/progress_pipe") &
  72. # Wait for up to 120 seconds
  73. for ((i=0; i<120; i++)); do
  74. if [ -f "$CDK_ITESTS_DIR/signal_received" ]; then
  75. echo "break signal received"
  76. break
  77. fi
  78. sleep 1
  79. done
  80. echo "Regtest set up continuing"
  81. echo "Starting regtest mint"
  82. # cargo run --bin regtest_mint &
  83. export CDK_MINTD_CLN_RPC_PATH="$CDK_ITESTS_DIR/cln/one/regtest/lightning-rpc"
  84. export CDK_MINTD_URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_0"
  85. export CDK_MINTD_WORK_DIR="$CDK_ITESTS_DIR"
  86. export CDK_MINTD_LISTEN_HOST=$CDK_ITESTS_MINT_ADDR
  87. export CDK_MINTD_LISTEN_PORT=$CDK_ITESTS_MINT_PORT_0
  88. export CDK_MINTD_LN_BACKEND="cln"
  89. export CDK_MINTD_MNEMONIC="eye survey guilt napkin crystal cup whisper salt luggage manage unveil loyal"
  90. export RUST_BACKTRACE=1
  91. echo "Starting cln mintd"
  92. cargo run --bin cdk-mintd --features "redb" &
  93. export CDK_MINTD_PID=$!
  94. echo $CDK_ITESTS_DIR
  95. URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_0/v1/info"
  96. TIMEOUT=100
  97. START_TIME=$(date +%s)
  98. # Loop until the endpoint returns a 200 OK status or timeout is reached
  99. while true; do
  100. # Get the current time
  101. CURRENT_TIME=$(date +%s)
  102. # Calculate the elapsed time
  103. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  104. # Check if the elapsed time exceeds the timeout
  105. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  106. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  107. exit 1
  108. fi
  109. # Make a request to the endpoint and capture the HTTP status code
  110. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)
  111. # Check if the HTTP status is 200 OK
  112. if [ "$HTTP_STATUS" -eq 200 ]; then
  113. echo "Received 200 OK from $URL"
  114. break
  115. else
  116. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  117. sleep 2 # Wait for 2 seconds before retrying
  118. fi
  119. done
  120. export CDK_MINTD_LND_ADDRESS="https://localhost:10010"
  121. export CDK_MINTD_LND_CERT_FILE="$CDK_ITESTS_DIR/lnd/two/tls.cert"
  122. export CDK_MINTD_LND_MACAROON_FILE="$CDK_ITESTS_DIR/lnd/two/data/chain/bitcoin/regtest/admin.macaroon"
  123. export CDK_MINTD_URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_1"
  124. mkdir -p "$CDK_ITESTS_DIR/lnd_mint"
  125. export CDK_MINTD_WORK_DIR="$CDK_ITESTS_DIR/lnd_mint"
  126. export CDK_MINTD_LISTEN_HOST=$CDK_ITESTS_MINT_ADDR
  127. export CDK_MINTD_LISTEN_PORT=$CDK_ITESTS_MINT_PORT_1
  128. export CDK_MINTD_LN_BACKEND="lnd"
  129. export CDK_MINTD_MNEMONIC="cattle gold bind busy sound reduce tone addict baby spend february strategy"
  130. echo "Starting lnd mintd"
  131. cargo run --bin cdk-mintd --features "redb" &
  132. export CDK_MINTD_LND_PID=$!
  133. URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_1/v1/info"
  134. TIMEOUT=100
  135. START_TIME=$(date +%s)
  136. # Loop until the endpoint returns a 200 OK status or timeout is reached
  137. while true; do
  138. # Get the current time
  139. CURRENT_TIME=$(date +%s)
  140. # Calculate the elapsed time
  141. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  142. # Check if the elapsed time exceeds the timeout
  143. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  144. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  145. exit 1
  146. fi
  147. # Make a request to the endpoint and capture the HTTP status code
  148. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)
  149. # Check if the HTTP status is 200 OK
  150. if [ "$HTTP_STATUS" -eq 200 ]; then
  151. echo "Received 200 OK from $URL"
  152. break
  153. else
  154. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  155. sleep 2 # Wait for 2 seconds before retrying
  156. fi
  157. done
  158. export CDK_TEST_MINT_URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_0"
  159. export CDK_TEST_MINT_URL_2="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_1"
  160. # Run tests and exit immediately on failure
  161. # Run cargo test
  162. echo "Running regtest test with CLN mint"
  163. cargo test -p cdk-integration-tests --test regtest
  164. if [ $? -ne 0 ]; then
  165. echo "regtest test failed, exiting"
  166. exit 1
  167. fi
  168. echo "Running happy_path_mint_wallet test with CLN mint"
  169. cargo test -p cdk-integration-tests --test happy_path_mint_wallet test_happy_mint_melt_round_trip
  170. if [ $? -ne 0 ]; then
  171. echo "happy_path_mint_wallet test failed, exiting"
  172. exit 1
  173. fi
  174. # # Run cargo test with the http_subscription feature
  175. echo "Running regtest test with http_subscription feature"
  176. cargo test -p cdk-integration-tests --test regtest --features http_subscription
  177. if [ $? -ne 0 ]; then
  178. echo "regtest test with http_subscription failed, exiting"
  179. exit 1
  180. fi
  181. # Switch Mints: Run tests with LND mint
  182. echo "Switching to LND mint for tests"
  183. export CDK_ITESTS_MINT_PORT_0=8087
  184. export CDK_ITESTS_MINT_PORT_1=8085
  185. export CDK_TEST_MINT_URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_0"
  186. export CDK_TEST_MINT_URL_2="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_1"
  187. echo "Running regtest test with LND mint"
  188. cargo test -p cdk-integration-tests --test regtest
  189. if [ $? -ne 0 ]; then
  190. echo "regtest test with LND mint failed, exiting"
  191. exit 1
  192. fi
  193. echo "Running happy_path_mint_wallet test with LND mint"
  194. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  195. if [ $? -ne 0 ]; then
  196. echo "happy_path_mint_wallet test with LND mint failed, exiting"
  197. exit 1
  198. fi
  199. echo "All tests passed successfully"
  200. exit 0