| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 | #!/usr/bin/env bash# Function to perform cleanupcleanup() {    echo "Cleaning up..."    echo "Killing the cdk regtest and mints"    if [ ! -z "$CDK_REGTEST_PID" ]; then        # First try graceful shutdown with SIGTERM        kill -15 $CDK_REGTEST_PID 2>/dev/null        sleep 2                # Check if process is still running, if so force kill with SIGKILL        if ps -p $CDK_REGTEST_PID > /dev/null 2>&1; then            echo "Process still running, force killing..."            kill -9 $CDK_REGTEST_PID 2>/dev/null        fi                # Wait for process to terminate        wait $CDK_REGTEST_PID 2>/dev/null || true    fi    echo "Mint binary terminated"    # # Remove the temporary directory    # if [ ! -z "$CDK_ITESTS_DIR" ] && [ -d "$CDK_ITESTS_DIR" ]; then    #     rm -rf "$CDK_ITESTS_DIR"    #     echo "Temp directory removed: $CDK_ITESTS_DIR"    # fi        # Unset all environment variables    unset CDK_ITESTS_DIR    unset CDK_ITESTS_MINT_ADDR    unset CDK_ITESTS_MINT_PORT_0    unset CDK_ITESTS_MINT_PORT_1    unset CDK_MINTD_DATABASE    unset CDK_TEST_MINT_URL    unset CDK_TEST_MINT_URL_2    unset CDK_REGTEST_PID    unset RUST_BACKTRACE    unset CDK_TEST_REGTEST    unset CDK_TEST_LIGHTNING_CLIENT}# Set up trap to call cleanup on script exittrap cleanup EXITexport CDK_TEST_REGTEST=1# Create a temporary directoryexport CDK_ITESTS_DIR=$(mktemp -d)export CDK_ITESTS_MINT_ADDR="127.0.0.1"export CDK_ITESTS_MINT_PORT_0=8085export CDK_ITESTS_MINT_PORT_1=8087# Check if the temporary directory was created successfullyif [[ ! -d "$CDK_ITESTS_DIR" ]]; then    echo "Failed to create temp directory"    exit 1fiecho "Temp directory created: $CDK_ITESTS_DIR"export CDK_MINTD_DATABASE="$1"cargo build --bin start_regtest_mints echo "Starting regtest and mints"# Run the binary in backgroundcargo r --bin start_regtest_mints -- --enable-logging "$CDK_MINTD_DATABASE" "$CDK_ITESTS_DIR" "$CDK_ITESTS_MINT_ADDR" "$CDK_ITESTS_MINT_PORT_0" "$CDK_ITESTS_MINT_PORT_1" &export CDK_REGTEST_PID=$!# Give it a moment to start - reduced from 5 to 2 seconds since we have better waiting mechanisms nowsleep 2# Look for the .env file in the current directoryENV_FILE_PATH="$CDK_ITESTS_DIR/.env"# Wait for the .env file to be created in the current directorymax_wait=120wait_count=0while [ $wait_count -lt $max_wait ]; do    if [ -f "$ENV_FILE_PATH" ]; then        echo ".env file found at: $ENV_FILE_PATH"        break    fi    wait_count=$((wait_count + 1))    sleep 1done# Check if we found the .env fileif [ ! -f "$ENV_FILE_PATH" ]; then    echo "ERROR: Could not find .env file at $ENV_FILE_PATH"    exit 1fi# Source the environment variables from the .env fileecho "Sourcing environment variables from $ENV_FILE_PATH"source "$ENV_FILE_PATH"echo "Sourced environment variables:"echo "CDK_TEST_MINT_URL=$CDK_TEST_MINT_URL"echo "CDK_TEST_MINT_URL_2=$CDK_TEST_MINT_URL_2"echo "CDK_ITESTS_DIR=$CDK_ITESTS_DIR"# Validate that we sourced the variablesif [ -z "$CDK_TEST_MINT_URL" ] || [ -z "$CDK_TEST_MINT_URL_2" ] || [ -z "$CDK_ITESTS_DIR" ]; then    echo "ERROR: Failed to source environment variables from the .env file"    exit 1fi# Export all variables so they're available to the testsexport CDK_TEST_MINT_URLexport CDK_TEST_MINT_URL_2URL="$CDK_TEST_MINT_URL/v1/info"TIMEOUT=500START_TIME=$(date +%s)# Loop until the endpoint returns a 200 OK status or timeout is reachedwhile true; do    # Get the current time    CURRENT_TIME=$(date +%s)        # Calculate the elapsed time    ELAPSED_TIME=$((CURRENT_TIME - START_TIME))    # Check if the elapsed time exceeds the timeout    if [ $ELAPSED_TIME -ge $TIMEOUT ]; then        echo "Timeout of $TIMEOUT seconds reached. Exiting..."        exit 1    fi    # Make a request to the endpoint and capture the HTTP status code    HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)    # Check if the HTTP status is 200 OK    if [ "$HTTP_STATUS" -eq 200 ]; then        echo "Received 200 OK from $URL"        break    else        echo "Waiting for 200 OK response, current status: $HTTP_STATUS"        sleep 2  # Wait for 2 seconds before retrying    fidoneURL="$CDK_TEST_MINT_URL_2/v1/info"TIMEOUT=100START_TIME=$(date +%s)# Loop until the endpoint returns a 200 OK status or timeout is reachedwhile true; do    # Get the current time    CURRENT_TIME=$(date +%s)        # Calculate the elapsed time    ELAPSED_TIME=$((CURRENT_TIME - START_TIME))    # Check if the elapsed time exceeds the timeout    if [ $ELAPSED_TIME -ge $TIMEOUT ]; then        echo "Timeout of $TIMEOUT seconds reached. Exiting..."        exit 1    fi    # Make a request to the endpoint and capture the HTTP status code    HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $URL)    # Check if the HTTP status is 200 OK    if [ "$HTTP_STATUS" -eq 200 ]; then        echo "Received 200 OK from $URL"        break    else        echo "Waiting for 200 OK response, current status: $HTTP_STATUS"        sleep 2  # Wait for 2 seconds before retrying    fidone# Run cargo testecho "Running regtest test with CLN mint and CLN client"export CDK_TEST_LIGHTNING_CLIENT="lnd"cargo test -p cdk-integration-tests --test regtestif [ $? -ne 0 ]; then    echo "regtest test with cln mint failed, exiting"    exit 1fiecho "Running happy_path_mint_wallet test with CLN mint and CLN client"cargo test -p cdk-integration-tests --test happy_path_mint_walletif [ $? -ne 0 ]; then    echo "happy_path_mint_wallet with cln mint test failed, exiting"    exit 1fi# Run cargo test with the http_subscription featureecho "Running regtest test with http_subscription feature (CLN client)"cargo test -p cdk-integration-tests --test regtest --features http_subscriptionif [ $? -ne 0 ]; then    echo "regtest test with http_subscription failed, exiting"    exit 1fiecho "Running regtest test with cln mint for bolt12 (CLN client)"cargo test -p cdk-integration-tests --test bolt12if [ $? -ne 0 ]; then    echo "regtest test failed, exiting"    exit 1fi# Switch Mints: Run tests with LND mintecho "Switching to LND mint for tests"echo "Running regtest test with LND mint and LND client"CDK_TEST_MINT_URL_SWITCHED=$CDK_TEST_MINT_URL_2CDK_TEST_MINT_URL_2_SWITCHED=$CDK_TEST_MINT_URLexport CDK_TEST_MINT_URL=$CDK_TEST_MINT_URL_SWITCHEDexport CDK_TEST_MINT_URL_2=$CDK_TEST_MINT_URL_2_SWITCHED cargo test -p cdk-integration-tests --test regtest if [ $? -ne 0 ]; then     echo "regtest test with LND mint failed, exiting"     exit 1 fi echo "Running happy_path_mint_wallet test with LND mint and LND client" cargo test -p cdk-integration-tests --test happy_path_mint_wallet if [ $? -ne 0 ]; then     echo "happy_path_mint_wallet test with LND mint failed, exiting"     exit 1 fiexport CDK_TEST_MINT_URL="http://127.0.0.1:8089" TIMEOUT=100START_TIME=$(date +%s)# Loop until the endpoint returns a 200 OK status or timeout is reachedwhile true; do    # Get the current time    CURRENT_TIME=$(date +%s)        # Calculate the elapsed time    ELAPSED_TIME=$((CURRENT_TIME - START_TIME))    # Check if the elapsed time exceeds the timeout    if [ $ELAPSED_TIME -ge $TIMEOUT ]; then        echo "Timeout of $TIMEOUT seconds reached. Exiting..."        exit 1    fi    # Make a request to the endpoint and capture the HTTP status code    HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" $CDK_TEST_MINT_URL/v1/info)    # Check if the HTTP status is 200 OK    if [ "$HTTP_STATUS" -eq 200 ]; then        echo "Received 200 OK from $CDK_TEST_MINT_URL"        break    else        echo "Waiting for 200 OK response, current status: $HTTP_STATUS"        sleep 2  # Wait for 2 seconds before retrying    fidoneecho "Running happy_path_mint_wallet test with LDK mint and CLN client"export CDK_TEST_LIGHTNING_CLIENT="cln"  # Use CLN client for LDK testscargo test -p cdk-integration-tests --test happy_path_mint_walletif [ $? -ne 0 ]; then    echo "happy_path_mint_wallet test with LDK mint failed, exiting"    exit 1fiecho "Running regtest test with LDK mint and CLN client"cargo test -p cdk-integration-tests --test regtestif [ $? -ne 0 ]; then    echo "regtest test LDK mint failed, exiting"    exit 1fiecho "All tests passed successfully"exit 0
 |