nutshell_wallet_itest.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. echo "Cleanup complete."
  31. }
  32. # Set up trap to call cleanup function on script exit
  33. trap cleanup EXIT INT TERM
  34. # Create a temporary directory for mintd
  35. CDK_ITESTS=$(mktemp -d)
  36. echo "Created temporary directory for mintd: $CDK_ITESTS"
  37. export CDK_MINTD_URL="$MINT_URL"
  38. export CDK_MINTD_WORK_DIR="$CDK_ITESTS"
  39. export CDK_MINTD_LISTEN_HOST="127.0.0.1"
  40. export CDK_MINTD_LISTEN_PORT="8085"
  41. export CDK_MINTD_LN_BACKEND="fakewallet"
  42. export CDK_MINTD_FAKE_WALLET_SUPPORTED_UNITS="sat,usd"
  43. export CDK_MINTD_MNEMONIC="eye survey guilt napkin crystal cup whisper salt luggage manage unveil loyal"
  44. export CDK_MINTD_FAKE_WALLET_FEE_PERCENT="0"
  45. export CDK_MINTD_FAKE_WALLET_RESERVE_FEE_MIN="1"
  46. export CDK_MINTD_DATABASE="redb"
  47. echo "Starting fake mintd"
  48. cargo run --bin cdk-mintd --features "redb" &
  49. CDK_MINTD_PID=$!
  50. # Wait for the mint to be ready
  51. echo "Waiting for mintd to start..."
  52. TIMEOUT=300
  53. START_TIME=$(date +%s)
  54. # Try different URLs since the endpoint might vary
  55. 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")
  56. # Loop until one of the endpoints returns a 200 OK status or timeout is reached
  57. while true; do
  58. # Get the current time
  59. CURRENT_TIME=$(date +%s)
  60. # Calculate the elapsed time
  61. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  62. # Check if the elapsed time exceeds the timeout
  63. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  64. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  65. exit 1
  66. fi
  67. # Try each URL
  68. for URL in "${URLS[@]}"; do
  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" || echo "000")
  71. # Check if the HTTP status is 200 OK
  72. if [ "$HTTP_STATUS" -eq 200 ]; then
  73. echo "Received 200 OK from $URL"
  74. MINT_URL=$(echo "$URL" | sed 's|/v1/info||')
  75. echo "Setting MINT_URL to $MINT_URL"
  76. export MINT_URL
  77. break 2 # Break out of both loops
  78. else
  79. echo "Waiting for 200 OK response from $URL, current status: $HTTP_STATUS"
  80. fi
  81. done
  82. # Wait before retrying
  83. sleep 5
  84. done
  85. # Check if Docker is available and accessible
  86. if docker info > /dev/null 2>&1; then
  87. echo "Docker is available, starting Nutshell wallet container"
  88. # Use the MINT_URL which is already set to host.docker.internal
  89. docker run -d --name ${WALLET_CONTAINER_NAME} \
  90. --network=host \
  91. -p ${WALLET_PORT}:${WALLET_PORT} \
  92. -e MINT_URL=${MINT_URL} \
  93. cashubtc/nutshell:latest \
  94. poetry run cashu -d
  95. else
  96. echo "Docker is not accessible, skipping Nutshell wallet container setup"
  97. # Set a flag to indicate we're not using Docker
  98. export NO_DOCKER=true
  99. fi
  100. # Wait for the mint to be ready
  101. echo "Waiting for Nutshell Mint to start..."
  102. sleep 5
  103. # Check if the Mint API is responding (use localhost for local curl check)
  104. echo "Checking if Nutshell Mint API is available..."
  105. if curl -s "http://localhost:${MINT_PORT}/v1/info" > /dev/null; then
  106. echo "Nutshell Mint is running and accessible at ${MINT_URL}"
  107. else
  108. echo "Warning: Nutshell Mint API is not responding. It might not be ready yet."
  109. fi
  110. # Only check wallet if Docker is available
  111. if [ -z "$NO_DOCKER" ]; then
  112. # Check if the Wallet API is responding
  113. echo "Checking if Nutshell Wallet API is available..."
  114. if curl -s "${WALLET_URL}/info" > /dev/null; then
  115. echo "Nutshell Wallet is running in container '${WALLET_CONTAINER_NAME}'"
  116. echo "You can access it at ${WALLET_URL}"
  117. else
  118. echo "Warning: Nutshell Wallet API is not responding. The container might not be ready yet."
  119. fi
  120. fi
  121. # Export URLs as environment variables
  122. export MINT_URL=${MINT_URL}
  123. export WALLET_URL=${WALLET_URL}
  124. # Run the integration test
  125. echo "Running integration test..."
  126. cargo test -p cdk-integration-tests --tests nutshell_wallet
  127. TEST_STATUS=$?
  128. # Exit with the test status
  129. echo "Integration test completed with status: $TEST_STATUS"
  130. exit $TEST_STATUS