fake_itests.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env bash
  2. # Function to perform cleanup
  3. cleanup() {
  4. echo "Cleaning up..."
  5. echo "Killing the cdk mintd"
  6. kill -2 $CDK_MINTD_PID
  7. wait $CDK_MINTD_PID
  8. echo "Mint binary terminated"
  9. # Remove the temporary directory
  10. rm -rf "$CDK_ITESTS_DIR"
  11. echo "Temp directory removed: $CDK_ITESTS_DIR"
  12. # Unset all environment variables
  13. unset CDK_ITESTS_DIR
  14. unset CDK_ITESTS_MINT_ADDR
  15. unset CDK_ITESTS_MINT_PORT
  16. unset CDK_MINTD_DATABASE
  17. unset CDK_TEST_MINT_URL
  18. unset CDK_MINTD_URL
  19. unset CDK_MINTD_WORK_DIR
  20. unset CDK_MINTD_LISTEN_HOST
  21. unset CDK_MINTD_LISTEN_PORT
  22. unset CDK_MINTD_LN_BACKEND
  23. unset CDK_MINTD_FAKE_WALLET_SUPPORTED_UNITS
  24. unset CDK_MINTD_MNEMONIC
  25. unset CDK_MINTD_FAKE_WALLET_FEE_PERCENT
  26. unset CDK_MINTD_FAKE_WALLET_RESERVE_FEE_MIN
  27. unset CDK_MINTD_PID
  28. }
  29. # Set up trap to call cleanup on script exit
  30. trap cleanup EXIT
  31. # Create a temporary directory
  32. export CDK_ITESTS_DIR=$(mktemp -d)
  33. export CDK_ITESTS_MINT_ADDR="127.0.0.1"
  34. export CDK_ITESTS_MINT_PORT=8086
  35. # Check if the temporary directory was created successfully
  36. if [[ ! -d "$CDK_ITESTS_DIR" ]]; then
  37. echo "Failed to create temp directory"
  38. exit 1
  39. fi
  40. echo "Temp directory created: $CDK_ITESTS_DIR"
  41. export CDK_MINTD_DATABASE="$1"
  42. cargo build -p cdk-integration-tests
  43. export CDK_MINTD_URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT"
  44. export CDK_MINTD_WORK_DIR="$CDK_ITESTS_DIR"
  45. export CDK_MINTD_LISTEN_HOST=$CDK_ITESTS_MINT_ADDR
  46. export CDK_MINTD_LISTEN_PORT=$CDK_ITESTS_MINT_PORT
  47. export CDK_MINTD_LN_BACKEND="fakewallet"
  48. export CDK_MINTD_FAKE_WALLET_SUPPORTED_UNITS="sat,usd"
  49. export CDK_MINTD_MNEMONIC="eye survey guilt napkin crystal cup whisper salt luggage manage unveil loyal"
  50. export CDK_MINTD_FAKE_WALLET_FEE_PERCENT="0"
  51. export CDK_MINTD_FAKE_WALLET_RESERVE_FEE_MIN="1"
  52. echo "Starting fake mintd"
  53. cargo run --bin cdk-mintd --features "redb" &
  54. export CDK_MINTD_PID=$!
  55. URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT/v1/info"
  56. TIMEOUT=100
  57. START_TIME=$(date +%s)
  58. # Loop until the endpoint returns a 200 OK status or timeout is reached
  59. while true; do
  60. # Get the current time
  61. CURRENT_TIME=$(date +%s)
  62. # Calculate the elapsed time
  63. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  64. # Check if the elapsed time exceeds the timeout
  65. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  66. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  67. exit 1
  68. fi
  69. # Make a request to the endpoint and capture the HTTP status code
  70. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)
  71. # Check if the HTTP status is 200 OK
  72. if [ "$HTTP_STATUS" -eq 200 ]; then
  73. echo "Received 200 OK from $URL"
  74. break
  75. else
  76. echo "Waiting for 200 OK response, current status: $HTTP_STATUS"
  77. sleep 2 # Wait for 2 seconds before retrying
  78. fi
  79. done
  80. export CDK_TEST_MINT_URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT"
  81. # Run first test
  82. cargo test -p cdk-integration-tests --test fake_wallet
  83. status1=$?
  84. # Exit immediately if the first test failed
  85. if [ $status1 -ne 0 ]; then
  86. echo "First test failed with status $status1, exiting"
  87. exit $status1
  88. fi
  89. # Run second test only if the first one succeeded
  90. cargo test -p cdk-integration-tests --test happy_path_mint_wallet
  91. status2=$?
  92. # Exit with the status of the second test
  93. if [ $status2 -ne 0 ]; then
  94. echo "Second test failed with status $status2, exiting"
  95. exit $status2
  96. fi
  97. # Both tests passed
  98. echo "All tests passed successfully"
  99. exit 0