fake_itests.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 PG_DB_URL="host=localhost user=${DB_USER} password=${DB_PASS} dbname=${DB_NAME} port=${DB_PORT}"
  62. echo "Starting fresh PostgreSQL container..."
  63. fi
  64. if [ "$2" = "external_signatory" ]; then
  65. echo "Starting with external signatory support"
  66. bash -x `dirname $0`/../crates/cdk-signatory/generate_certs.sh $CDK_ITESTS_DIR
  67. cargo build --bin signatory
  68. cargo run --bin signatory -- -w $CDK_ITESTS_DIR -u "sat" -u "usd" &
  69. export CDK_SIGNATORY_PID=$!
  70. sleep 5
  71. cargo run --bin start_fake_mint -- --enable-logging --external-signatory "$CDK_MINTD_DATABASE" "$CDK_ITESTS_DIR" &
  72. else
  73. cargo run --bin start_fake_mint -- --enable-logging "$CDK_MINTD_DATABASE" "$CDK_ITESTS_DIR" &
  74. fi
  75. export FAKE_MINT_PID=$!
  76. # Give the mint a moment to start
  77. sleep 3
  78. # Look for the .env file in the temp directory
  79. ENV_FILE_PATH="$CDK_ITESTS_DIR/.env"
  80. # Wait for the .env file to be created (with longer timeout)
  81. max_wait=200
  82. wait_count=0
  83. while [ $wait_count -lt $max_wait ]; do
  84. if [ -f "$ENV_FILE_PATH" ]; then
  85. echo ".env file found at: $ENV_FILE_PATH"
  86. break
  87. fi
  88. echo "Waiting for .env file to be created... ($wait_count/$max_wait)"
  89. wait_count=$((wait_count + 1))
  90. sleep 1
  91. done
  92. # Check if we found the .env file
  93. if [ ! -f "$ENV_FILE_PATH" ]; then
  94. echo "ERROR: Could not find .env file at $ENV_FILE_PATH after $max_wait seconds"
  95. exit 1
  96. fi
  97. # Source the environment variables from the .env file
  98. echo "Sourcing environment variables from $ENV_FILE_PATH"
  99. source "$ENV_FILE_PATH"
  100. echo "Sourced environment variables:"
  101. echo "CDK_TEST_MINT_URL=$CDK_TEST_MINT_URL"
  102. echo "CDK_ITESTS_DIR=$CDK_ITESTS_DIR"
  103. # Validate that we sourced the variables
  104. if [ -z "$CDK_TEST_MINT_URL" ] || [ -z "$CDK_ITESTS_DIR" ]; then
  105. echo "ERROR: Failed to source environment variables from the .env file"
  106. exit 1
  107. fi
  108. # Export all variables so they're available to the tests
  109. export CDK_TEST_MINT_URL
  110. URL="$CDK_TEST_MINT_URL/v1/info"
  111. TIMEOUT=120
  112. START_TIME=$(date +%s)
  113. # Loop until the endpoint returns a 200 OK status or timeout is reached
  114. while true; do
  115. # Get the current time
  116. CURRENT_TIME=$(date +%s)
  117. # Calculate the elapsed time
  118. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  119. # Check if the elapsed time exceeds the timeout
  120. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  121. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  122. exit 1
  123. fi
  124. # Make a request to the endpoint and capture the HTTP status code
  125. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)
  126. # Check if the HTTP status is 200 OK
  127. if [ "$HTTP_STATUS" -eq 200 ]; then
  128. echo "Received 200 OK from $URL"
  129. break
  130. else
  131. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  132. sleep 2 # Wait for 2 seconds before retrying
  133. fi
  134. done
  135. # Run first test
  136. echo "Running fake_wallet test"
  137. cargo test -p cdk-integration-tests --test fake_wallet
  138. status1=$?
  139. # Exit immediately if the first test failed
  140. if [ $status1 -ne 0 ]; then
  141. echo "First test failed with status $status1, exiting"
  142. exit $status1
  143. fi
  144. # Run second test only if the first one succeeded
  145. echo "Running happy_path_mint_wallet test"
  146. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  147. status2=$?
  148. # Exit with the status of the second test
  149. if [ $status2 -ne 0 ]; then
  150. echo "Second test failed with status $status2, exiting"
  151. exit $status2
  152. fi
  153. # Both tests passed
  154. echo "All tests passed successfully"
  155. exit 0