fake_itests.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. # Unset all environment variables
  25. unset CDK_ITESTS_DIR
  26. unset CDK_TEST_MINT_URL
  27. unset FAKE_MINT_PID
  28. unset CDK_SIGNATORY_PID
  29. }
  30. # Set up trap to call cleanup on script exit
  31. trap cleanup EXIT INT TERM
  32. # Create a temporary directory
  33. export CDK_ITESTS_DIR=$(mktemp -d)
  34. # Check if the temporary directory was created successfully
  35. if [[ ! -d "$CDK_ITESTS_DIR" ]]; then
  36. echo "Failed to create temp directory"
  37. exit 1
  38. fi
  39. echo "Temp directory created: $CDK_ITESTS_DIR"
  40. # Check if a database type was provided as first argument, default to sqlite
  41. export CDK_MINTD_DATABASE="${1:-sqlite}"
  42. cargo build -p cdk-integration-tests
  43. # Start the fake mint binary with the new Rust-based approach
  44. echo "Starting fake mint using Rust binary..."
  45. if [ "$2" = "external_signatory" ]; then
  46. echo "Starting with external signatory support"
  47. bash -x `dirname $0`/../crates/cdk-signatory/generate_certs.sh $CDK_ITESTS_DIR
  48. cargo build --bin signatory
  49. cargo run --bin signatory -- -w $CDK_ITESTS_DIR -u "sat" -u "usd" &
  50. export CDK_SIGNATORY_PID=$!
  51. sleep 5
  52. cargo run --bin start_fake_mint -- --enable-logging --external-signatory "$CDK_MINTD_DATABASE" "$CDK_ITESTS_DIR" &
  53. else
  54. cargo run --bin start_fake_mint -- --enable-logging "$CDK_MINTD_DATABASE" "$CDK_ITESTS_DIR" &
  55. fi
  56. export FAKE_MINT_PID=$!
  57. # Give the mint a moment to start
  58. sleep 3
  59. # Look for the .env file in the temp directory
  60. ENV_FILE_PATH="$CDK_ITESTS_DIR/.env"
  61. # Wait for the .env file to be created (with longer timeout)
  62. max_wait=200
  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. echo "Waiting for .env file to be created... ($wait_count/$max_wait)"
  70. wait_count=$((wait_count + 1))
  71. sleep 1
  72. done
  73. # Check if we found the .env file
  74. if [ ! -f "$ENV_FILE_PATH" ]; then
  75. echo "ERROR: Could not find .env file at $ENV_FILE_PATH after $max_wait seconds"
  76. exit 1
  77. fi
  78. # Source the environment variables from the .env file
  79. echo "Sourcing environment variables from $ENV_FILE_PATH"
  80. source "$ENV_FILE_PATH"
  81. echo "Sourced environment variables:"
  82. echo "CDK_TEST_MINT_URL=$CDK_TEST_MINT_URL"
  83. echo "CDK_ITESTS_DIR=$CDK_ITESTS_DIR"
  84. # Validate that we sourced the variables
  85. if [ -z "$CDK_TEST_MINT_URL" ] || [ -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. URL="$CDK_TEST_MINT_URL/v1/info"
  92. TIMEOUT=120
  93. START_TIME=$(date +%s)
  94. # Loop until the endpoint returns a 200 OK status or timeout is reached
  95. while true; do
  96. # Get the current time
  97. CURRENT_TIME=$(date +%s)
  98. # Calculate the elapsed time
  99. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  100. # Check if the elapsed time exceeds the timeout
  101. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  102. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  103. exit 1
  104. fi
  105. # Make a request to the endpoint and capture the HTTP status code
  106. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)
  107. # Check if the HTTP status is 200 OK
  108. if [ "$HTTP_STATUS" -eq 200 ]; then
  109. echo "Received 200 OK from $URL"
  110. break
  111. else
  112. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  113. sleep 2 # Wait for 2 seconds before retrying
  114. fi
  115. done
  116. # Run first test
  117. echo "Running fake_wallet test"
  118. cargo test -p cdk-integration-tests --test fake_wallet
  119. status1=$?
  120. # Exit immediately if the first test failed
  121. if [ $status1 -ne 0 ]; then
  122. echo "First test failed with status $status1, exiting"
  123. exit $status1
  124. fi
  125. # Run second test only if the first one succeeded
  126. echo "Running happy_path_mint_wallet test"
  127. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  128. status2=$?
  129. # Exit with the status of the second test
  130. if [ $status2 -ne 0 ]; then
  131. echo "Second test failed with status $status2, exiting"
  132. exit $status2
  133. fi
  134. # Both tests passed
  135. echo "All tests passed successfully"
  136. exit 0