nutshell_wallet_itest.sh 5.6 KB

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