itests.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. # Stop PostgreSQL if it was started
  25. if [ -d "$PWD/.pg_data" ]; then
  26. stop-postgres 2>/dev/null || true
  27. fi
  28. # Unset all environment variables
  29. unset CDK_ITESTS_DIR
  30. unset CDK_ITESTS_MINT_ADDR
  31. unset CDK_ITESTS_MINT_PORT_0
  32. unset CDK_ITESTS_MINT_PORT_1
  33. unset CDK_MINTD_DATABASE
  34. unset CDK_TEST_MINT_URL
  35. unset CDK_TEST_MINT_URL_2
  36. unset CDK_REGTEST_PID
  37. unset RUST_BACKTRACE
  38. unset CDK_TEST_REGTEST
  39. unset CDK_TEST_LIGHTNING_CLIENT
  40. }
  41. # Set up trap to call cleanup on script exit
  42. trap cleanup EXIT
  43. export CDK_TEST_REGTEST=1
  44. export RUST_BACKTRACE=full
  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. # Start PostgreSQL if needed
  58. if [ "${CDK_MINTD_DATABASE}" = "POSTGRES" ]; then
  59. echo "Starting PostgreSQL via nix..."
  60. start-postgres
  61. echo "PostgreSQL is ready"
  62. fi
  63. cargo build --bin start_regtest_mints
  64. echo "Starting regtest and mints"
  65. # Run the binary in background
  66. 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" &
  67. export CDK_REGTEST_PID=$!
  68. # Give it a moment to start - reduced from 5 to 2 seconds since we have better waiting mechanisms now
  69. sleep 2
  70. # Look for the .env file in the current directory
  71. ENV_FILE_PATH="$CDK_ITESTS_DIR/.env"
  72. # Wait for the .env file to be created in the current directory
  73. max_wait=120
  74. wait_count=0
  75. while [ $wait_count -lt $max_wait ]; do
  76. if [ -f "$ENV_FILE_PATH" ]; then
  77. echo ".env file found at: $ENV_FILE_PATH"
  78. break
  79. fi
  80. wait_count=$((wait_count + 1))
  81. sleep 1
  82. done
  83. # Check if we found the .env file
  84. if [ ! -f "$ENV_FILE_PATH" ]; then
  85. echo "ERROR: Could not find .env file at $ENV_FILE_PATH"
  86. exit 1
  87. fi
  88. # Source the environment variables from the .env file
  89. echo "Sourcing environment variables from $ENV_FILE_PATH"
  90. source "$ENV_FILE_PATH"
  91. echo "Sourced environment variables:"
  92. echo "CDK_TEST_MINT_URL=$CDK_TEST_MINT_URL"
  93. echo "CDK_TEST_MINT_URL_2=$CDK_TEST_MINT_URL_2"
  94. echo "CDK_ITESTS_DIR=$CDK_ITESTS_DIR"
  95. # Validate that we sourced the variables
  96. if [ -z "$CDK_TEST_MINT_URL" ] || [ -z "$CDK_TEST_MINT_URL_2" ] || [ -z "$CDK_ITESTS_DIR" ]; then
  97. echo "ERROR: Failed to source environment variables from the .env file"
  98. exit 1
  99. fi
  100. # Export all variables so they're available to the tests
  101. export CDK_TEST_MINT_URL
  102. export CDK_TEST_MINT_URL_2
  103. URL="$CDK_TEST_MINT_URL/v1/info"
  104. TIMEOUT=500
  105. START_TIME=$(date +%s)
  106. # Loop until the endpoint returns a 200 OK status or timeout is reached
  107. while true; do
  108. # Get the current time
  109. CURRENT_TIME=$(date +%s)
  110. # Calculate the elapsed time
  111. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  112. # Check if the elapsed time exceeds the timeout
  113. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  114. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  115. exit 1
  116. fi
  117. # Make a request to the endpoint and capture the HTTP status code
  118. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)
  119. # Check if the HTTP status is 200 OK
  120. if [ "$HTTP_STATUS" -eq 200 ]; then
  121. echo "Received 200 OK from $URL"
  122. break
  123. else
  124. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  125. sleep 2 # Wait for 2 seconds before retrying
  126. fi
  127. done
  128. URL="$CDK_TEST_MINT_URL_2/v1/info"
  129. TIMEOUT=100
  130. START_TIME=$(date +%s)
  131. # Loop until the endpoint returns a 200 OK status or timeout is reached
  132. while true; do
  133. # Get the current time
  134. CURRENT_TIME=$(date +%s)
  135. # Calculate the elapsed time
  136. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  137. # Check if the elapsed time exceeds the timeout
  138. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  139. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  140. exit 1
  141. fi
  142. # Make a request to the endpoint and capture the HTTP status code
  143. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)
  144. # Check if the HTTP status is 200 OK
  145. if [ "$HTTP_STATUS" -eq 200 ]; then
  146. echo "Received 200 OK from $URL"
  147. break
  148. else
  149. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  150. sleep 2 # Wait for 2 seconds before retrying
  151. fi
  152. done
  153. # Run cargo test
  154. echo "Running regtest test with CLN mint and CLN client"
  155. export CDK_TEST_LIGHTNING_CLIENT="lnd"
  156. cargo test -p cdk-integration-tests --test regtest
  157. if [ $? -ne 0 ]; then
  158. echo "regtest test with cln mint failed, exiting"
  159. exit 1
  160. fi
  161. echo "Running happy_path_mint_wallet test with CLN mint and CLN client"
  162. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  163. if [ $? -ne 0 ]; then
  164. echo "happy_path_mint_wallet with cln mint test failed, exiting"
  165. exit 1
  166. fi
  167. # Run cargo test with the http_subscription feature
  168. echo "Running regtest test with http_subscription feature (CLN client)"
  169. cargo test -p cdk-integration-tests --test regtest --features http_subscription
  170. if [ $? -ne 0 ]; then
  171. echo "regtest test with http_subscription failed, exiting"
  172. exit 1
  173. fi
  174. echo "Running regtest test with cln mint for bolt12 (CLN client)"
  175. cargo test -p cdk-integration-tests --test bolt12
  176. if [ $? -ne 0 ]; then
  177. echo "regtest test failed, exiting"
  178. exit 1
  179. fi
  180. # Switch Mints: Run tests with LND mint
  181. echo "Switching to LND mint for tests"
  182. echo "Running regtest test with LND mint and LND client"
  183. CDK_TEST_MINT_URL_SWITCHED=$CDK_TEST_MINT_URL_2
  184. CDK_TEST_MINT_URL_2_SWITCHED=$CDK_TEST_MINT_URL
  185. export CDK_TEST_MINT_URL=$CDK_TEST_MINT_URL_SWITCHED
  186. export CDK_TEST_MINT_URL_2=$CDK_TEST_MINT_URL_2_SWITCHED
  187. cargo test -p cdk-integration-tests --test regtest
  188. if [ $? -ne 0 ]; then
  189. echo "regtest test with LND mint failed, exiting"
  190. exit 1
  191. fi
  192. echo "Running happy_path_mint_wallet test with LND mint and LND client"
  193. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  194. if [ $? -ne 0 ]; then
  195. echo "happy_path_mint_wallet test with LND mint failed, exiting"
  196. exit 1
  197. fi
  198. export CDK_TEST_MINT_URL="http://127.0.0.1:8089"
  199. TIMEOUT=100
  200. START_TIME=$(date +%s)
  201. # Loop until the endpoint returns a 200 OK status or timeout is reached
  202. while true; do
  203. # Get the current time
  204. CURRENT_TIME=$(date +%s)
  205. # Calculate the elapsed time
  206. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  207. # Check if the elapsed time exceeds the timeout
  208. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  209. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  210. exit 1
  211. fi
  212. # Make a request to the endpoint and capture the HTTP status code
  213. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $CDK_TEST_MINT_URL/v1/info)
  214. # Check if the HTTP status is 200 OK
  215. if [ "$HTTP_STATUS" -eq 200 ]; then
  216. echo "Received 200 OK from $CDK_TEST_MINT_URL"
  217. break
  218. else
  219. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  220. sleep 2 # Wait for 2 seconds before retrying
  221. fi
  222. done
  223. echo "Running happy_path_mint_wallet test with LDK mint and CLN client"
  224. export CDK_TEST_LIGHTNING_CLIENT="cln" # Use CLN client for LDK tests
  225. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  226. if [ $? -ne 0 ]; then
  227. echo "happy_path_mint_wallet test with LDK mint failed, exiting"
  228. exit 1
  229. fi
  230. echo "Running regtest test with LDK mint and CLN client"
  231. cargo test -p cdk-integration-tests --test regtest
  232. if [ $? -ne 0 ]; then
  233. echo "regtest test LDK mint failed, exiting"
  234. exit 1
  235. fi
  236. echo "All tests passed successfully"
  237. exit 0