fake_itests.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/env bash
  2. # Script to run fake mint tests with proper handling of race conditions
  3. # This script ensures the .env file is properly created and available
  4. # before running tests
  5. # Function to perform cleanup
  6. cleanup() {
  7. echo "Cleaning up..."
  8. if [ -n "$FAKE_MINT_PID" ]; then
  9. echo "Killing the fake mint process"
  10. kill -2 $FAKE_MINT_PID 2>/dev/null || true
  11. wait $FAKE_MINT_PID 2>/dev/null || true
  12. fi
  13. if [ -n "$CDK_SIGNATORY_PID" ]; then
  14. echo "Killing the signatory process"
  15. kill -9 $CDK_SIGNATORY_PID 2>/dev/null || true
  16. wait $CDK_SIGNATORY_PID 2>/dev/null || true
  17. fi
  18. echo "Mint binary terminated"
  19. # Remove the temporary directory
  20. if [ -n "$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_TEST_MINT_URL
  31. unset FAKE_MINT_PID
  32. unset CDK_SIGNATORY_PID
  33. }
  34. # Set up trap to call cleanup on script exit
  35. trap cleanup EXIT INT TERM
  36. # Create a temporary directory
  37. export CDK_ITESTS_DIR=$(mktemp -d)
  38. # Check if the temporary directory was created successfully
  39. if [[ ! -d "$CDK_ITESTS_DIR" ]]; then
  40. echo "Failed to create temp directory"
  41. exit 1
  42. fi
  43. echo "Temp directory created: $CDK_ITESTS_DIR"
  44. # Check if a database type was provided as first argument, default to sqlite
  45. export CDK_MINTD_DATABASE="${1:-sqlite}"
  46. cargo build --bin start_fake_mint
  47. # Start the fake mint binary with the new Rust-based approach
  48. echo "Starting fake mint using Rust binary..."
  49. if [ "${CDK_MINTD_DATABASE}" = "POSTGRES" ]; then
  50. echo "Starting PostgreSQL via nix..."
  51. start-postgres
  52. echo "PostgreSQL is ready"
  53. fi
  54. if [ "$2" = "external_signatory" ]; then
  55. echo "Starting with external signatory support"
  56. bash -x `dirname $0`/../crates/cdk-signatory/generate_certs.sh $CDK_ITESTS_DIR
  57. cargo build --bin signatory
  58. cargo run --bin signatory -- -w $CDK_ITESTS_DIR -u "sat" -u "usd" &
  59. export CDK_SIGNATORY_PID=$!
  60. sleep 5
  61. cargo run --bin start_fake_mint -- --enable-logging --external-signatory "$CDK_MINTD_DATABASE" "$CDK_ITESTS_DIR" &
  62. else
  63. cargo run --bin start_fake_mint -- --enable-logging "$CDK_MINTD_DATABASE" "$CDK_ITESTS_DIR" &
  64. fi
  65. export FAKE_MINT_PID=$!
  66. # Give the mint a moment to start
  67. sleep 3
  68. # Look for the .env file in the temp directory
  69. ENV_FILE_PATH="$CDK_ITESTS_DIR/.env"
  70. # Wait for the .env file to be created (with longer timeout)
  71. max_wait=200
  72. wait_count=0
  73. while [ $wait_count -lt $max_wait ]; do
  74. if [ -f "$ENV_FILE_PATH" ]; then
  75. echo ".env file found at: $ENV_FILE_PATH"
  76. break
  77. fi
  78. echo "Waiting for .env file to be created... ($wait_count/$max_wait)"
  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 after $max_wait seconds"
  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_ITESTS_DIR=$CDK_ITESTS_DIR"
  93. # Validate that we sourced the variables
  94. if [ -z "$CDK_TEST_MINT_URL" ] || [ -z "$CDK_ITESTS_DIR" ]; then
  95. echo "ERROR: Failed to source environment variables from the .env file"
  96. exit 1
  97. fi
  98. # Export all variables so they're available to the tests
  99. export CDK_TEST_MINT_URL
  100. URL="$CDK_TEST_MINT_URL/v1/info"
  101. TIMEOUT=120
  102. START_TIME=$(date +%s)
  103. # Loop until the endpoint returns a 200 OK status or timeout is reached
  104. while true; do
  105. # Get the current time
  106. CURRENT_TIME=$(date +%s)
  107. # Calculate the elapsed time
  108. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  109. # Check if the elapsed time exceeds the timeout
  110. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  111. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  112. exit 1
  113. fi
  114. # Make a request to the endpoint and capture the HTTP status code
  115. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)
  116. # Check if the HTTP status is 200 OK
  117. if [ "$HTTP_STATUS" -eq 200 ]; then
  118. echo "Received 200 OK from $URL"
  119. break
  120. else
  121. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  122. sleep 2 # Wait for 2 seconds before retrying
  123. fi
  124. done
  125. # Run first test
  126. echo "Running fake_wallet test"
  127. cargo test -p cdk-integration-tests --test fake_wallet -- --nocapture
  128. status1=$?
  129. # Exit immediately if the first test failed
  130. if [ $status1 -ne 0 ]; then
  131. echo "First test failed with status $status1, exiting"
  132. exit $status1
  133. fi
  134. # Run second test only if the first one succeeded
  135. echo "Running happy_path_mint_wallet test"
  136. cargo test -p cdk-integration-tests --test happy_path_mint_wallet -- --nocapture
  137. status2=$?
  138. # Exit if the second test failed
  139. if [ $status2 -ne 0 ]; then
  140. echo "Second test failed with status $status2, exiting"
  141. exit $status2
  142. fi
  143. # Run third test (async_melt) only if previous tests succeeded
  144. echo "Running async_melt test"
  145. cargo test -p cdk-integration-tests --test async_melt
  146. status3=$?
  147. # Exit with the status of the third test
  148. if [ $status3 -ne 0 ]; then
  149. echo "Third test (async_melt) failed with status $status3, exiting"
  150. exit $status3
  151. fi
  152. # All tests passed
  153. echo "All tests passed successfully"
  154. exit 0