fake_itests.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. if [ -n "$CONTAINER_NAME" ]; then
  25. docker rm "${CONTAINER_NAME}" -f
  26. fi
  27. # Unset all environment variables
  28. unset CDK_ITESTS_DIR
  29. unset CDK_TEST_MINT_URL
  30. unset FAKE_MINT_PID
  31. unset CDK_SIGNATORY_PID
  32. }
  33. # Set up trap to call cleanup on script exit
  34. trap cleanup EXIT INT TERM
  35. # Create a temporary directory
  36. export CDK_ITESTS_DIR=$(mktemp -d)
  37. # Check if the temporary directory was created successfully
  38. if [[ ! -d "$CDK_ITESTS_DIR" ]]; then
  39. echo "Failed to create temp directory"
  40. exit 1
  41. fi
  42. echo "Temp directory created: $CDK_ITESTS_DIR"
  43. # Check if a database type was provided as first argument, default to sqlite
  44. export CDK_MINTD_DATABASE="${1:-sqlite}"
  45. cargo build -p cdk-integration-tests
  46. # Start the fake mint binary with the new Rust-based approach
  47. echo "Starting fake mint using Rust binary..."
  48. if [ "${CDK_MINTD_DATABASE}" = "POSTGRES" ]; then
  49. export CONTAINER_NAME="rust-fake-test-pg"
  50. DB_USER="test"
  51. DB_PASS="test"
  52. DB_NAME="testdb"
  53. DB_PORT="15433"
  54. docker run -d --rm \
  55. --name "${CONTAINER_NAME}" \
  56. -e POSTGRES_USER="${DB_USER}" \
  57. -e POSTGRES_PASSWORD="${DB_PASS}" \
  58. -e POSTGRES_DB="${DB_NAME}" \
  59. -p ${DB_PORT}:5432 \
  60. postgres:16
  61. export CDK_MINTD_DATABASE_URL="postgresql://${DB_USER}:${DB_PASS}@localhost:${DB_PORT}/${DB_NAME}"
  62. echo "Waiting for PostgreSQL to be ready and database '${DB_NAME}' to exist..."
  63. until docker exec -e PGPASSWORD="${DB_PASS}" "${CONTAINER_NAME}" \
  64. psql -U "${DB_USER}" -d "${DB_NAME}" -c "SELECT 1;" >/dev/null 2>&1; do
  65. sleep 0.5
  66. done
  67. echo "PostgreSQL container is ready"
  68. fi
  69. if [ "$2" = "external_signatory" ]; then
  70. echo "Starting with external signatory support"
  71. bash -x `dirname $0`/../crates/cdk-signatory/generate_certs.sh $CDK_ITESTS_DIR
  72. cargo build --bin signatory
  73. cargo run --bin signatory -- -w $CDK_ITESTS_DIR -u "sat" -u "usd" &
  74. export CDK_SIGNATORY_PID=$!
  75. sleep 5
  76. cargo run --bin start_fake_mint -- --enable-logging --external-signatory "$CDK_MINTD_DATABASE" "$CDK_ITESTS_DIR" &
  77. else
  78. cargo run --bin start_fake_mint -- --enable-logging "$CDK_MINTD_DATABASE" "$CDK_ITESTS_DIR" &
  79. fi
  80. export FAKE_MINT_PID=$!
  81. # Give the mint a moment to start
  82. sleep 3
  83. # Look for the .env file in the temp directory
  84. ENV_FILE_PATH="$CDK_ITESTS_DIR/.env"
  85. # Wait for the .env file to be created (with longer timeout)
  86. max_wait=200
  87. wait_count=0
  88. while [ $wait_count -lt $max_wait ]; do
  89. if [ -f "$ENV_FILE_PATH" ]; then
  90. echo ".env file found at: $ENV_FILE_PATH"
  91. break
  92. fi
  93. echo "Waiting for .env file to be created... ($wait_count/$max_wait)"
  94. wait_count=$((wait_count + 1))
  95. sleep 1
  96. done
  97. # Check if we found the .env file
  98. if [ ! -f "$ENV_FILE_PATH" ]; then
  99. echo "ERROR: Could not find .env file at $ENV_FILE_PATH after $max_wait seconds"
  100. exit 1
  101. fi
  102. # Source the environment variables from the .env file
  103. echo "Sourcing environment variables from $ENV_FILE_PATH"
  104. source "$ENV_FILE_PATH"
  105. echo "Sourced environment variables:"
  106. echo "CDK_TEST_MINT_URL=$CDK_TEST_MINT_URL"
  107. echo "CDK_ITESTS_DIR=$CDK_ITESTS_DIR"
  108. # Validate that we sourced the variables
  109. if [ -z "$CDK_TEST_MINT_URL" ] || [ -z "$CDK_ITESTS_DIR" ]; then
  110. echo "ERROR: Failed to source environment variables from the .env file"
  111. exit 1
  112. fi
  113. # Export all variables so they're available to the tests
  114. export CDK_TEST_MINT_URL
  115. URL="$CDK_TEST_MINT_URL/v1/info"
  116. TIMEOUT=120
  117. START_TIME=$(date +%s)
  118. # Loop until the endpoint returns a 200 OK status or timeout is reached
  119. while true; do
  120. # Get the current time
  121. CURRENT_TIME=$(date +%s)
  122. # Calculate the elapsed time
  123. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  124. # Check if the elapsed time exceeds the timeout
  125. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  126. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  127. exit 1
  128. fi
  129. # Make a request to the endpoint and capture the HTTP status code
  130. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)
  131. # Check if the HTTP status is 200 OK
  132. if [ "$HTTP_STATUS" -eq 200 ]; then
  133. echo "Received 200 OK from $URL"
  134. break
  135. else
  136. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  137. sleep 2 # Wait for 2 seconds before retrying
  138. fi
  139. done
  140. # Run first test
  141. echo "Running fake_wallet test"
  142. cargo test -p cdk-integration-tests --test fake_wallet
  143. status1=$?
  144. # Exit immediately if the first test failed
  145. if [ $status1 -ne 0 ]; then
  146. echo "First test failed with status $status1, exiting"
  147. exit $status1
  148. fi
  149. # Run second test only if the first one succeeded
  150. echo "Running happy_path_mint_wallet test"
  151. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  152. status2=$?
  153. # Exit with the status of the second test
  154. if [ $status2 -ne 0 ]; then
  155. echo "Second test failed with status $status2, exiting"
  156. exit $status2
  157. fi
  158. # Both tests passed
  159. echo "All tests passed successfully"
  160. exit 0