itests.sh 8.5 KB

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