itests.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #!/usr/bin/env bash
  2. # Function to perform cleanup
  3. cleanup() {
  4. echo "Cleaning up..."
  5. echo "Killing the cdk regtest and mints"
  6. if [ ! -z "$CDK_REGTEST_PID" ]; then
  7. # First try graceful shutdown with SIGTERM
  8. kill -15 $CDK_REGTEST_PID 2>/dev/null
  9. sleep 2
  10. # Check if process is still running, if so force kill with SIGKILL
  11. if ps -p $CDK_REGTEST_PID > /dev/null 2>&1; then
  12. echo "Process still running, force killing..."
  13. kill -9 $CDK_REGTEST_PID 2>/dev/null
  14. fi
  15. # Wait for process to terminate
  16. wait $CDK_REGTEST_PID 2>/dev/null || true
  17. fi
  18. echo "Mint binary terminated"
  19. # # Remove the temporary directory
  20. # if [ ! -z "$CDK_ITESTS_DIR" ] && [ -d "$CDK_ITESTS_DIR" ]; then
  21. # rm -rf "$CDK_ITESTS_DIR"
  22. # echo "Temp directory removed: $CDK_ITESTS_DIR"
  23. # fi
  24. # Unset all environment variables
  25. unset CDK_ITESTS_DIR
  26. unset CDK_ITESTS_MINT_ADDR
  27. unset CDK_ITESTS_MINT_PORT_0
  28. unset CDK_ITESTS_MINT_PORT_1
  29. unset CDK_MINTD_DATABASE
  30. unset CDK_TEST_MINT_URL
  31. unset CDK_TEST_MINT_URL_2
  32. unset CDK_REGTEST_PID
  33. unset RUST_BACKTRACE
  34. unset CDK_TEST_REGTEST
  35. unset CDK_TEST_LIGHTNING_CLIENT
  36. }
  37. # Set up trap to call cleanup on script exit
  38. trap cleanup EXIT
  39. export CDK_TEST_REGTEST=1
  40. # Create a temporary directory
  41. export CDK_ITESTS_DIR=$(mktemp -d)
  42. export CDK_ITESTS_MINT_ADDR="127.0.0.1"
  43. export CDK_ITESTS_MINT_PORT_0=8085
  44. export CDK_ITESTS_MINT_PORT_1=8087
  45. # Check if the temporary directory was created successfully
  46. if [[ ! -d "$CDK_ITESTS_DIR" ]]; then
  47. echo "Failed to create temp directory"
  48. exit 1
  49. fi
  50. echo "Temp directory created: $CDK_ITESTS_DIR"
  51. export CDK_MINTD_DATABASE="$1"
  52. cargo build --bin start_regtest_mints
  53. echo "Starting regtest and mints"
  54. # Run the binary in background
  55. cargo r --bin start_regtest_mints -- --enable-logging "$CDK_MINTD_DATABASE" "$CDK_ITESTS_DIR" "$CDK_ITESTS_MINT_ADDR" "$CDK_ITESTS_MINT_PORT_0" "$CDK_ITESTS_MINT_PORT_1" &
  56. export CDK_REGTEST_PID=$!
  57. # Give it a moment to start - reduced from 5 to 2 seconds since we have better waiting mechanisms now
  58. sleep 2
  59. # Look for the .env file in the current directory
  60. ENV_FILE_PATH="$CDK_ITESTS_DIR/.env"
  61. # Wait for the .env file to be created in the current directory
  62. max_wait=120
  63. wait_count=0
  64. while [ $wait_count -lt $max_wait ]; do
  65. if [ -f "$ENV_FILE_PATH" ]; then
  66. echo ".env file found at: $ENV_FILE_PATH"
  67. break
  68. fi
  69. wait_count=$((wait_count + 1))
  70. sleep 1
  71. done
  72. # Check if we found the .env file
  73. if [ ! -f "$ENV_FILE_PATH" ]; then
  74. echo "ERROR: Could not find .env file at $ENV_FILE_PATH"
  75. exit 1
  76. fi
  77. # Source the environment variables from the .env file
  78. echo "Sourcing environment variables from $ENV_FILE_PATH"
  79. source "$ENV_FILE_PATH"
  80. echo "Sourced environment variables:"
  81. echo "CDK_TEST_MINT_URL=$CDK_TEST_MINT_URL"
  82. echo "CDK_TEST_MINT_URL_2=$CDK_TEST_MINT_URL_2"
  83. echo "CDK_ITESTS_DIR=$CDK_ITESTS_DIR"
  84. # Validate that we sourced the variables
  85. if [ -z "$CDK_TEST_MINT_URL" ] || [ -z "$CDK_TEST_MINT_URL_2" ] || [ -z "$CDK_ITESTS_DIR" ]; then
  86. echo "ERROR: Failed to source environment variables from the .env file"
  87. exit 1
  88. fi
  89. # Export all variables so they're available to the tests
  90. export CDK_TEST_MINT_URL
  91. export CDK_TEST_MINT_URL_2
  92. URL="$CDK_TEST_MINT_URL/v1/info"
  93. TIMEOUT=500
  94. START_TIME=$(date +%s)
  95. # Loop until the endpoint returns a 200 OK status or timeout is reached
  96. while true; do
  97. # Get the current time
  98. CURRENT_TIME=$(date +%s)
  99. # Calculate the elapsed time
  100. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  101. # Check if the elapsed time exceeds the timeout
  102. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  103. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  104. exit 1
  105. fi
  106. # Make a request to the endpoint and capture the HTTP status code
  107. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)
  108. # Check if the HTTP status is 200 OK
  109. if [ "$HTTP_STATUS" -eq 200 ]; then
  110. echo "Received 200 OK from $URL"
  111. break
  112. else
  113. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  114. sleep 2 # Wait for 2 seconds before retrying
  115. fi
  116. done
  117. URL="$CDK_TEST_MINT_URL_2/v1/info"
  118. TIMEOUT=100
  119. START_TIME=$(date +%s)
  120. # Loop until the endpoint returns a 200 OK status or timeout is reached
  121. while true; do
  122. # Get the current time
  123. CURRENT_TIME=$(date +%s)
  124. # Calculate the elapsed time
  125. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  126. # Check if the elapsed time exceeds the timeout
  127. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  128. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  129. exit 1
  130. fi
  131. # Make a request to the endpoint and capture the HTTP status code
  132. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)
  133. # Check if the HTTP status is 200 OK
  134. if [ "$HTTP_STATUS" -eq 200 ]; then
  135. echo "Received 200 OK from $URL"
  136. break
  137. else
  138. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  139. sleep 2 # Wait for 2 seconds before retrying
  140. fi
  141. done
  142. # Run cargo test
  143. echo "Running regtest test with CLN mint and CLN client"
  144. export CDK_TEST_LIGHTNING_CLIENT="lnd"
  145. cargo test -p cdk-integration-tests --test regtest
  146. if [ $? -ne 0 ]; then
  147. echo "regtest test with cln mint failed, exiting"
  148. exit 1
  149. fi
  150. echo "Running happy_path_mint_wallet test with CLN mint and CLN client"
  151. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  152. if [ $? -ne 0 ]; then
  153. echo "happy_path_mint_wallet with cln mint test failed, exiting"
  154. exit 1
  155. fi
  156. # Run cargo test with the http_subscription feature
  157. echo "Running regtest test with http_subscription feature (CLN client)"
  158. cargo test -p cdk-integration-tests --test regtest --features http_subscription
  159. if [ $? -ne 0 ]; then
  160. echo "regtest test with http_subscription failed, exiting"
  161. exit 1
  162. fi
  163. echo "Running regtest test with cln mint for bolt12 (CLN client)"
  164. cargo test -p cdk-integration-tests --test bolt12
  165. if [ $? -ne 0 ]; then
  166. echo "regtest test failed, exiting"
  167. exit 1
  168. fi
  169. # Switch Mints: Run tests with LND mint
  170. echo "Switching to LND mint for tests"
  171. echo "Running regtest test with LND mint and LND client"
  172. CDK_TEST_MINT_URL_SWITCHED=$CDK_TEST_MINT_URL_2
  173. CDK_TEST_MINT_URL_2_SWITCHED=$CDK_TEST_MINT_URL
  174. export CDK_TEST_MINT_URL=$CDK_TEST_MINT_URL_SWITCHED
  175. export CDK_TEST_MINT_URL_2=$CDK_TEST_MINT_URL_2_SWITCHED
  176. cargo test -p cdk-integration-tests --test regtest
  177. if [ $? -ne 0 ]; then
  178. echo "regtest test with LND mint failed, exiting"
  179. exit 1
  180. fi
  181. echo "Running happy_path_mint_wallet test with LND mint and LND client"
  182. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  183. if [ $? -ne 0 ]; then
  184. echo "happy_path_mint_wallet test with LND mint failed, exiting"
  185. exit 1
  186. fi
  187. export CDK_TEST_MINT_URL="http://127.0.0.1:8089"
  188. TIMEOUT=100
  189. START_TIME=$(date +%s)
  190. # Loop until the endpoint returns a 200 OK status or timeout is reached
  191. while true; do
  192. # Get the current time
  193. CURRENT_TIME=$(date +%s)
  194. # Calculate the elapsed time
  195. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  196. # Check if the elapsed time exceeds the timeout
  197. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  198. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  199. exit 1
  200. fi
  201. # Make a request to the endpoint and capture the HTTP status code
  202. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $CDK_TEST_MINT_URL/v1/info)
  203. # Check if the HTTP status is 200 OK
  204. if [ "$HTTP_STATUS" -eq 200 ]; then
  205. echo "Received 200 OK from $CDK_TEST_MINT_URL"
  206. break
  207. else
  208. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  209. sleep 2 # Wait for 2 seconds before retrying
  210. fi
  211. done
  212. echo "Running happy_path_mint_wallet test with LDK mint and CLN client"
  213. export CDK_TEST_LIGHTNING_CLIENT="cln" # Use CLN client for LDK tests
  214. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  215. if [ $? -ne 0 ]; then
  216. echo "happy_path_mint_wallet test with LDK mint failed, exiting"
  217. exit 1
  218. fi
  219. echo "Running regtest test with LDK mint and CLN client"
  220. cargo test -p cdk-integration-tests --test regtest
  221. if [ $? -ne 0 ]; then
  222. echo "regtest test LDK mint failed, exiting"
  223. exit 1
  224. fi
  225. echo "All tests passed successfully"
  226. exit 0