nutshell_wallet_itest.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env bash
  2. set -e
  3. # Configuration
  4. MINT_PORT=8085
  5. WALLET_PORT=4448
  6. MINT_CONTAINER_NAME="nutshell-mint"
  7. WALLET_CONTAINER_NAME="nutshell-wallet"
  8. # Use host.docker.internal for the mint URL so containers can access it
  9. MINT_URL="http://0.0.0.0:${MINT_PORT}"
  10. WALLET_URL="http://localhost:${WALLET_PORT}"
  11. CDK_MINTD_PID=""
  12. # Function to clean up resources
  13. cleanup() {
  14. echo "Cleaning up resources..."
  15. if docker ps -a | grep -q ${WALLET_CONTAINER_NAME}; then
  16. echo "Stopping and removing Docker container '${WALLET_CONTAINER_NAME}'..."
  17. docker stop ${WALLET_CONTAINER_NAME} >/dev/null 2>&1
  18. docker rm ${WALLET_CONTAINER_NAME} >/dev/null 2>&1
  19. fi
  20. if [ -n "$CDK_MINTD_PID" ]; then
  21. echo "Stopping mintd process (PID: $CDK_MINTD_PID)..."
  22. kill -TERM $CDK_MINTD_PID >/dev/null 2>&1 || true
  23. fi
  24. # Unset variables
  25. unset MINT_URL WALLET_URL MINT_PORT WALLET_PORT MINT_CONTAINER_NAME WALLET_CONTAINER_NAME
  26. unset CDK_MINTD_PID CDK_MINTD_URL CDK_MINTD_WORK_DIR CDK_MINTD_LISTEN_HOST CDK_MINTD_LISTEN_PORT
  27. unset CDK_MINTD_LN_BACKEND CDK_MINTD_FAKE_WALLET_SUPPORTED_UNITS CDK_MINTD_MNEMONIC
  28. unset CDK_MINTD_FAKE_WALLET_FEE_PERCENT CDK_MINTD_FAKE_WALLET_RESERVE_FEE_MIN CDK_MINTD_DATABASE
  29. unset TEST_STATUS
  30. unset CDK_MINTD_INPUT_FEE_PPK
  31. echo "Cleanup complete."
  32. }
  33. # Set up trap to call cleanup function on script exit
  34. trap cleanup EXIT INT TERM
  35. # Create a temporary directory for mintd
  36. CDK_ITESTS=$(mktemp -d)
  37. echo "Created temporary directory for mintd: $CDK_ITESTS"
  38. export CDK_MINTD_URL="$MINT_URL"
  39. export CDK_MINTD_WORK_DIR="$CDK_ITESTS"
  40. export CDK_MINTD_LISTEN_HOST="127.0.0.1"
  41. export CDK_MINTD_LISTEN_PORT="8085"
  42. export CDK_MINTD_LN_BACKEND="fakewallet"
  43. export CDK_MINTD_FAKE_WALLET_SUPPORTED_UNITS="sat,usd"
  44. export CDK_MINTD_MNEMONIC="eye survey guilt napkin crystal cup whisper salt luggage manage unveil loyal"
  45. export CDK_MINTD_FAKE_WALLET_FEE_PERCENT="0"
  46. export CDK_MINTD_FAKE_WALLET_RESERVE_FEE_MIN="1"
  47. export CDK_MINTD_DATABASE="redb"
  48. export CDK_MINTD_INPUT_FEE_PPK="100"
  49. echo "Starting fake mintd"
  50. cargo run --bin cdk-mintd --features "redb" &
  51. CDK_MINTD_PID=$!
  52. # Wait for the mint to be ready
  53. echo "Waiting for mintd to start..."
  54. TIMEOUT=300
  55. START_TIME=$(date +%s)
  56. # Try different URLs since the endpoint might vary
  57. URLS=("http://localhost:${MINT_PORT}/v1/info" "http://127.0.0.1:${MINT_PORT}/v1/info" "http://0.0.0.0:${MINT_PORT}/v1/info")
  58. # Loop until one of the endpoints 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. # Try each URL
  70. for URL in "${URLS[@]}"; do
  71. # Make a request to the endpoint and capture the HTTP status code
  72. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" "$URL" || echo "000")
  73. # Check if the HTTP status is 200 OK
  74. if [ "$HTTP_STATUS" -eq 200 ]; then
  75. echo "Received 200 OK from $URL"
  76. MINT_URL=$(echo "$URL" | sed 's|/v1/info||')
  77. echo "Setting MINT_URL to $MINT_URL"
  78. export MINT_URL
  79. break 2 # Break out of both loops
  80. else
  81. echo "Waiting for 200 OK response from $URL, current status: $HTTP_STATUS"
  82. fi
  83. done
  84. # Wait before retrying
  85. sleep 5
  86. done
  87. # Check if Docker is available and accessible
  88. if docker info > /dev/null 2>&1; then
  89. echo "Docker is available, starting Nutshell wallet container"
  90. # Use the MINT_URL which is already set to host.docker.internal
  91. docker run -d --name ${WALLET_CONTAINER_NAME} \
  92. --network=host \
  93. -p ${WALLET_PORT}:${WALLET_PORT} \
  94. -e MINT_URL=${MINT_URL} \
  95. cashubtc/nutshell:latest \
  96. poetry run cashu -d
  97. else
  98. echo "Docker is not accessible, skipping Nutshell wallet container setup"
  99. # Set a flag to indicate we're not using Docker
  100. export NO_DOCKER=true
  101. fi
  102. # Wait for the mint to be ready
  103. echo "Waiting for Nutshell Mint to start..."
  104. sleep 5
  105. # Check if the Mint API is responding (use localhost for local curl check)
  106. echo "Checking if Nutshell Mint API is available..."
  107. if curl -s "http://localhost:${MINT_PORT}/v1/info" > /dev/null; then
  108. echo "Nutshell Mint is running and accessible at ${MINT_URL}"
  109. else
  110. echo "Warning: Nutshell Mint API is not responding. It might not be ready yet."
  111. fi
  112. # Only check wallet if Docker is available
  113. if [ -z "$NO_DOCKER" ]; then
  114. # Check if the Wallet API is responding
  115. echo "Checking if Nutshell Wallet API is available..."
  116. if curl -s "${WALLET_URL}/info" > /dev/null; then
  117. echo "Nutshell Wallet is running in container '${WALLET_CONTAINER_NAME}'"
  118. echo "You can access it at ${WALLET_URL}"
  119. else
  120. echo "Warning: Nutshell Wallet API is not responding. The container might not be ready yet."
  121. fi
  122. fi
  123. # Export URLs as environment variables
  124. export MINT_URL=${MINT_URL}
  125. export WALLET_URL=${WALLET_URL}
  126. export CDK_TEST_MINT_URL=${MINT_URL}
  127. # Run the integration test
  128. echo "Running integration test..."
  129. cargo test -p cdk-integration-tests --test nutshell_wallet
  130. cargo test -p cdk-integration-tests --test test_fees
  131. TEST_STATUS=$?
  132. # Exit with the test status
  133. echo "Integration test completed with status: $TEST_STATUS"
  134. exit $TEST_STATUS