|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Browser Pool Lifecycle Test |
| 6 | +# |
| 7 | +# This script tests the full lifecycle of browser pools: |
| 8 | +# 1. Create a pool |
| 9 | +# 2. Acquire a browser from it |
| 10 | +# 3. Use the browser (simulated with sleep) |
| 11 | +# 4. Release the browser back to the pool |
| 12 | +# 5. Check pool state |
| 13 | +# 6. Flush idle browsers |
| 14 | +# 7. Delete the pool |
| 15 | + |
| 16 | +# Colors for output |
| 17 | +RED='\033[0;31m' |
| 18 | +GREEN='\033[0;32m' |
| 19 | +YELLOW='\033[1;33m' |
| 20 | +BLUE='\033[0;34m' |
| 21 | +NC='\033[0m' # No Color |
| 22 | + |
| 23 | +# Configuration |
| 24 | +KERNEL="${KERNEL:-kernel}" # Use $KERNEL env var or default to 'kernel' |
| 25 | +POOL_NAME="test-pool-$(date +%s)" |
| 26 | +POOL_SIZE=2 |
| 27 | +SLEEP_TIME=5 |
| 28 | + |
| 29 | +# Helper functions |
| 30 | +log_step() { |
| 31 | + echo -e "${BLUE}==>${NC} $1" |
| 32 | +} |
| 33 | + |
| 34 | +log_success() { |
| 35 | + echo -e "${GREEN}✓${NC} $1" |
| 36 | +} |
| 37 | + |
| 38 | +log_error() { |
| 39 | + echo -e "${RED}✗${NC} $1" |
| 40 | +} |
| 41 | + |
| 42 | +log_info() { |
| 43 | + echo -e "${YELLOW}ℹ${NC} $1" |
| 44 | +} |
| 45 | + |
| 46 | +# Check if kernel CLI is available |
| 47 | +if ! command -v "$KERNEL" &> /dev/null && [ ! -x "$KERNEL" ]; then |
| 48 | + log_error "kernel CLI not found at '$KERNEL'. Please install it or set KERNEL env var." |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +# Cleanup function (only runs if script exits early/unexpectedly) |
| 53 | +cleanup() { |
| 54 | + if [ -n "$POOL_ID" ]; then |
| 55 | + echo "" |
| 56 | + log_step "Script exited early - cleaning up pool $POOL_ID" |
| 57 | + "$KERNEL" browser-pools delete "$POOL_ID" --force --no-color || true |
| 58 | + log_info "Cleanup complete (used --force to ensure deletion)" |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +trap cleanup EXIT |
| 63 | + |
| 64 | +echo "" |
| 65 | +log_step "Starting browser pool integration test" |
| 66 | +echo "" |
| 67 | + |
| 68 | +# Step 1: Create a pool |
| 69 | +log_step "Step 1: Creating browser pool with name '$POOL_NAME' and size $POOL_SIZE" |
| 70 | +"$KERNEL" browser-pools create \ |
| 71 | + --name "$POOL_NAME" \ |
| 72 | + --size "$POOL_SIZE" \ |
| 73 | + --timeout 300 \ |
| 74 | + --no-color |
| 75 | + |
| 76 | +# Extract pool ID using the list command |
| 77 | +POOL_ID=$("$KERNEL" browser-pools list --output json --no-color | jq -r ".[] | select(.name == \"$POOL_NAME\") | .id") |
| 78 | + |
| 79 | +if [ -z "$POOL_ID" ]; then |
| 80 | + log_error "Failed to create pool or extract pool ID" |
| 81 | + exit 1 |
| 82 | +fi |
| 83 | + |
| 84 | +log_success "Created pool: $POOL_ID" |
| 85 | +echo "" |
| 86 | + |
| 87 | +# Step 2: List pools to verify |
| 88 | +log_step "Step 2: Listing all pools" |
| 89 | +"$KERNEL" browser-pools list --no-color |
| 90 | +echo "" |
| 91 | + |
| 92 | +# Step 3: Get pool details |
| 93 | +log_step "Step 3: Getting pool details" |
| 94 | +"$KERNEL" browser-pools get "$POOL_ID" --no-color |
| 95 | +echo "" |
| 96 | + |
| 97 | +# Wait for pool to be ready |
| 98 | +log_info "Waiting for pool to initialize..." |
| 99 | +sleep 3 |
| 100 | + |
| 101 | +# Step 4: Acquire a browser from the pool |
| 102 | +log_step "Step 4: Acquiring a browser from the pool" |
| 103 | +ACQUIRE_OUTPUT=$("$KERNEL" browser-pools acquire "$POOL_ID" --timeout 10 --no-color 2>&1) |
| 104 | + |
| 105 | +if echo "$ACQUIRE_OUTPUT" | grep -q "timed out"; then |
| 106 | + log_error "Failed to acquire browser (timeout or no browsers available)" |
| 107 | + exit 1 |
| 108 | +fi |
| 109 | + |
| 110 | +# Parse the session ID from the table output (format: "Session ID | <id>") |
| 111 | +SESSION_ID=$(echo "$ACQUIRE_OUTPUT" | grep "Session ID" | awk -F'|' '{print $2}' | xargs) |
| 112 | + |
| 113 | +if [ -z "$SESSION_ID" ]; then |
| 114 | + log_error "Failed to extract session ID from acquire response" |
| 115 | + echo "Response: $ACQUIRE_OUTPUT" |
| 116 | + exit 1 |
| 117 | +fi |
| 118 | + |
| 119 | +log_success "Acquired browser with session ID: $SESSION_ID" |
| 120 | +echo "" |
| 121 | + |
| 122 | +# Step 5: Get pool details again to see the acquired browser |
| 123 | +log_step "Step 5: Checking pool state (should show 1 acquired)" |
| 124 | +POOL_DETAILS=$("$KERNEL" browser-pools get "$POOL_ID" --output json --no-color) |
| 125 | +ACQUIRED_COUNT=$(echo "$POOL_DETAILS" | jq -r '.acquired_count // .acquiredCount // 0') |
| 126 | +AVAILABLE_COUNT=$(echo "$POOL_DETAILS" | jq -r '.available_count // .availableCount // 0') |
| 127 | + |
| 128 | +log_info "Acquired: $ACQUIRED_COUNT, Available: $AVAILABLE_COUNT" |
| 129 | +"$KERNEL" browser-pools get "$POOL_ID" --no-color |
| 130 | +echo "" |
| 131 | + |
| 132 | +# Step 6: Sleep to simulate usage |
| 133 | +log_step "Step 6: Simulating browser usage (sleeping for ${SLEEP_TIME}s)" |
| 134 | +sleep "$SLEEP_TIME" |
| 135 | +log_success "Usage simulation complete" |
| 136 | +echo "" |
| 137 | + |
| 138 | +# Step 7: Release the browser back to the pool |
| 139 | +log_step "Step 7: Releasing browser back to pool" |
| 140 | +"$KERNEL" browser-pools release "$POOL_ID" \ |
| 141 | + --session-id "$SESSION_ID" \ |
| 142 | + --reuse \ |
| 143 | + --no-color |
| 144 | + |
| 145 | +log_success "Browser released" |
| 146 | +echo "" |
| 147 | + |
| 148 | +# Step 8: Get pool details again |
| 149 | +log_step "Step 8: Checking pool state after release" |
| 150 | +"$KERNEL" browser-pools get "$POOL_ID" --no-color |
| 151 | +echo "" |
| 152 | + |
| 153 | +# Step 9: Flush the pool |
| 154 | +log_step "Step 9: Flushing idle browsers from pool" |
| 155 | +"$KERNEL" browser-pools flush "$POOL_ID" --no-color |
| 156 | +log_success "Pool flushed" |
| 157 | +echo "" |
| 158 | + |
| 159 | +# Step 10: Delete the pool (should succeed if browsers are properly released) |
| 160 | +log_step "Step 10: Deleting the pool" |
| 161 | +DELETED_POOL_ID="$POOL_ID" |
| 162 | +set +e # Temporarily disable exit-on-error to see the result |
| 163 | +"$KERNEL" browser-pools delete "$POOL_ID" --no-color |
| 164 | +DELETE_EXIT=$? |
| 165 | +set -e # Re-enable exit-on-error |
| 166 | + |
| 167 | +if [ $DELETE_EXIT -eq 0 ]; then |
| 168 | + log_success "Pool deleted successfully" |
| 169 | + POOL_ID="" # Clear so cleanup doesn't try again |
| 170 | + echo "" |
| 171 | +else |
| 172 | + log_error "Failed to delete pool - browsers may still be in acquired state" |
| 173 | + log_info "This suggests the release operation hasn't fully completed" |
| 174 | + log_info "Pool $POOL_ID left for debugging (clean up manually if needed)" |
| 175 | + POOL_ID="" # Clear to prevent cleanup trap from trying |
| 176 | + echo "" |
| 177 | +fi |
| 178 | + |
| 179 | +# Verify deletion |
| 180 | +log_step "Verifying pool deletion" |
| 181 | +if "$KERNEL" browser-pools list --output json --no-color | jq -e ".[] | select(.id == \"$DELETED_POOL_ID\") | .id" > /dev/null 2>&1; then |
| 182 | + log_error "Pool may still exist" |
| 183 | +else |
| 184 | + log_success "Pool successfully deleted and no longer exists" |
| 185 | +fi |
| 186 | + |
| 187 | +echo "" |
| 188 | +log_success "Integration test completed successfully!" |
| 189 | +echo "" |
| 190 | + |
0 commit comments