nutshell_wallet_itest.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_INPUT_FEE_PPK="100"
  48. echo "Starting fake mintd"
  49. cargo run --bin cdk-mintd &
  50. CDK_MINTD_PID=$!
  51. # Wait for the mint to be ready
  52. echo "Waiting for mintd to start..."
  53. TIMEOUT=300
  54. START_TIME=$(date +%s)
  55. # Try different URLs since the endpoint might vary
  56. 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")
  57. # Loop until one of the endpoints returns a 200 OK status or timeout is reached
  58. while true; do
  59. # Get the current time
  60. CURRENT_TIME=$(date +%s)
  61. # Calculate the elapsed time
  62. ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
  63. # Check if the elapsed time exceeds the timeout
  64. if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
  65. echo "Timeout of $TIMEOUT seconds reached. Exiting..."
  66. exit 1
  67. fi
  68. # Try each URL
  69. for URL in "${URLS[@]}"; do
  70. # Make a request to the endpoint and capture the HTTP status code
  71. HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" "$URL" || echo "000")
  72. # Check if the HTTP status is 200 OK
  73. if [ "$HTTP_STATUS" -eq 200 ]; then
  74. echo "Received 200 OK from $URL"
  75. MINT_URL=$(echo "$URL" | sed 's|/v1/info||')
  76. echo "Setting MINT_URL to $MINT_URL"
  77. export MINT_URL
  78. break 2 # Break out of both loops
  79. else
  80. echo "Waiting for 200 OK response from $URL, current status: $HTTP_STATUS"
  81. fi
  82. done
  83. # Wait before retrying
  84. sleep 5
  85. done
  86. # Check if Docker is available and accessible
  87. if docker info > /dev/null 2>&1; then
  88. echo "Docker is available, starting Nutshell wallet container"
  89. # Use the MINT_URL which is already set to host.docker.internal
  90. docker run -d --name ${WALLET_CONTAINER_NAME} \
  91. --network=host \
  92. -p ${WALLET_PORT}:${WALLET_PORT} \
  93. -e MINT_URL=${MINT_URL} \
  94. cashubtc/nutshell:0.16.5 \
  95. poetry run cashu -d
  96. else
  97. echo "Docker is not accessible, skipping Nutshell wallet container setup"
  98. # Set a flag to indicate we're not using Docker
  99. export NO_DOCKER=true
  100. fi
  101. # Wait for the mint to be ready
  102. echo "Waiting for Nutshell Mint to start..."
  103. sleep 5
  104. # Check if the Mint API is responding (use localhost for local curl check)
  105. echo "Checking if Nutshell Mint API is available..."
  106. if curl -s "http://localhost:${MINT_PORT}/v1/info" > /dev/null; then
  107. echo "Nutshell Mint is running and accessible at ${MINT_URL}"
  108. else
  109. echo "Warning: Nutshell Mint API is not responding. It might not be ready yet."
  110. fi
  111. # Only check wallet if Docker is available
  112. if [ -z "$NO_DOCKER" ]; then
  113. # Check if the Wallet API is responding
  114. echo "Checking if Nutshell Wallet API is available..."
  115. if curl -s "${WALLET_URL}/info" > /dev/null; then
  116. echo "Nutshell Wallet is running in container '${WALLET_CONTAINER_NAME}'"
  117. echo "You can access it at ${WALLET_URL}"
  118. else
  119. echo "Warning: Nutshell Wallet API is not responding. The container might not be ready yet."
  120. fi
  121. fi
  122. # Export URLs as environment variables
  123. export MINT_URL=${MINT_URL}
  124. export WALLET_URL=${WALLET_URL}
  125. export CDK_TEST_MINT_URL=${MINT_URL}
  126. # Run the integration test
  127. echo "Running integration test..."
  128. cargo test -p cdk-integration-tests --test nutshell_wallet -- --test-threads 1
  129. cargo test -p cdk-integration-tests --test test_fees -- --test-threads 1
  130. TEST_STATUS=$?
  131. # Exit with the test status
  132. echo "Integration test completed with status: $TEST_STATUS"
  133. exit $TEST_STATUS