itests.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. }
  36. # Set up trap to call cleanup on script exit
  37. trap cleanup EXIT
  38. export CDK_TEST_REGTEST=1
  39. # Create a temporary directory
  40. export CDK_ITESTS_DIR=$(mktemp -d)
  41. export CDK_ITESTS_MINT_ADDR="127.0.0.1"
  42. export CDK_ITESTS_MINT_PORT_0=8085
  43. export CDK_ITESTS_MINT_PORT_1=8087
  44. # Check if the temporary directory was created successfully
  45. if [[ ! -d "$CDK_ITESTS_DIR" ]]; then
  46. echo "Failed to create temp directory"
  47. exit 1
  48. fi
  49. echo "Temp directory created: $CDK_ITESTS_DIR"
  50. export CDK_MINTD_DATABASE="$1"
  51. cargo build -p cdk-integration-tests
  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=100
  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"
  144. cargo test -p cdk-integration-tests --test regtest
  145. if [ $? -ne 0 ]; then
  146. echo "regtest test failed, exiting"
  147. exit 1
  148. fi
  149. echo "Running happy_path_mint_wallet test with CLN mint"
  150. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  151. if [ $? -ne 0 ]; then
  152. echo "happy_path_mint_wallet test failed, exiting"
  153. exit 1
  154. fi
  155. # Run cargo test with the http_subscription feature
  156. echo "Running regtest test with http_subscription feature"
  157. cargo test -p cdk-integration-tests --test regtest --features http_subscription
  158. if [ $? -ne 0 ]; then
  159. echo "regtest test with http_subscription failed, exiting"
  160. exit 1
  161. fi
  162. echo "Running regtest test with cln mint for bolt12"
  163. cargo test -p cdk-integration-tests --test bolt12
  164. if [ $? -ne 0 ]; then
  165. echo "regtest test failed, exiting"
  166. exit 1
  167. fi
  168. # Switch Mints: Run tests with LND mint
  169. echo "Switching to LND mint for tests"
  170. echo "Running regtest test with LND mint"
  171. CDK_TEST_MINT_URL_SWITCHED=$CDK_TEST_MINT_URL_2
  172. CDK_TEST_MINT_URL_2_SWITCHED=$CDK_TEST_MINT_URL
  173. export CDK_TEST_MINT_URL=$CDK_TEST_MINT_URL_SWITCHED
  174. export CDK_TEST_MINT_URL_2=$CDK_TEST_MINT_URL_2_SWITCHED
  175. cargo test -p cdk-integration-tests --test regtest
  176. if [ $? -ne 0 ]; then
  177. echo "regtest test with LND mint failed, exiting"
  178. exit 1
  179. fi
  180. echo "Running happy_path_mint_wallet test with LND mint"
  181. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  182. if [ $? -ne 0 ]; then
  183. echo "happy_path_mint_wallet test with LND mint failed, exiting"
  184. exit 1
  185. fi
  186. echo "All tests passed successfully"
  187. exit 0