adb-expose-list.exp•11.7 kB
#!/usr/bin/env expect
# Exit codes:
# 0 - Test passed
# 1 - Test failed
# Set timeout for command execution
set timeout 30
# Colors for output
set GREEN "\033\[0;32m"
set RED "\033\[0;31m"
set YELLOW "\033\[1;33m"
set BLUE "\033\[0;34m"
set NC "\033\[0m"
# Get the gbox binary path
# Default to using the built binary in the parent directory
set gbox_binary "../gbox"
# If the built binary doesn't exist, fall back to system gbox
if {![file exists $gbox_binary]} {
set gbox_binary "gbox"
}
puts "${BLUE}Testing gbox adb-expose list command...${NC}"
# Test 1: Help command for adb-expose list
puts "\n${YELLOW}Testing help command for adb-expose list...${NC}"
puts "${BLUE}Running Test 1: Help command for adb-expose list${NC}"
if {[catch {
spawn $gbox_binary adb-expose list --help
expect {
-re "List all running adb-expose processes" {
puts "${GREEN}✓ Found list help description${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for list help description${NC}"
exit 1
}
eof {
puts "${RED}✗ List help command exited unexpectedly${NC}"
exit 1
}
}
expect {
-re "--output.*Output format" {
puts "${GREEN}✓ Found output flag description${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for output flag description${NC}"
exit 1
}
}
expect {
-re "table.*json" {
puts "${GREEN}✓ Found output format options${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for output format options${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Test failed: $result${NC}"
exit 1
}
# Test 2: Basic adb-expose list command (default table format)
puts "\n${YELLOW}Testing basic adb-expose list command...${NC}"
puts "${BLUE}Running Test 2: Basic adb-expose list command${NC}"
if {[catch {
spawn $gbox_binary adb-expose list
expect {
-re "No ADB port exposures found" {
puts "${GREEN}✓ Found expected message for no exposures${NC}"
exp_continue
}
-re "PID.*BoxID.*Port.*Status.*StartedAt" {
puts "${GREEN}✓ Found table header for existing exposures${NC}"
exp_continue
}
-re "\\|.*\\|.*\\|.*\\|.*\\|" {
puts "${GREEN}✓ Found table row with data${NC}"
exp_continue
}
timeout {
puts "${RED}✗ Timeout waiting for list output${NC}"
exit 1
}
eof {
puts "${GREEN}✓ List command completed successfully${NC}"
}
}
catch {close}
} result]} {
puts "${RED}✗ Test failed: $result${NC}"
exit 1
}
# Test 3: adb-expose list with table output format (explicit)
puts "\n${YELLOW}Testing adb-expose list with table output format...${NC}"
puts "${BLUE}Running Test 3: adb-expose list with table output format${NC}"
if {[catch {
spawn $gbox_binary adb-expose list --output table
expect {
-re "No ADB port exposures found" {
puts "${GREEN}✓ Found expected message for no exposures${NC}"
exp_continue
}
-re "PID.*BoxID.*Port.*Status.*StartedAt" {
puts "${GREEN}✓ Found table header for existing exposures${NC}"
exp_continue
}
-re "\\|.*\\|.*\\|.*\\|.*\\|" {
puts "${GREEN}✓ Found table row with data${NC}"
exp_continue
}
timeout {
puts "${RED}✗ Timeout waiting for table output${NC}"
exit 1
}
eof {
puts "${GREEN}✓ Table list command completed successfully${NC}"
}
}
catch {close}
} result]} {
puts "${RED}✗ Test failed: $result${NC}"
exit 1
}
# Test 4: adb-expose list with JSON output format
puts "\n${YELLOW}Testing adb-expose list with JSON output format...${NC}"
puts "${BLUE}Running Test 4: adb-expose list with JSON output format${NC}"
if {[catch {
spawn $gbox_binary adb-expose list --output json
expect {
-re "\\\[\\\]" {
puts "${GREEN}✓ Found empty JSON array for no exposures${NC}"
exp_continue
}
-re "\\\[.*\\\]" {
puts "${GREEN}✓ Found JSON array for existing exposures${NC}"
exp_continue
}
-re "\"pid\":" {
puts "${GREEN}✓ Found pid field in JSON${NC}"
exp_continue
}
-re "\"boxId\":" {
puts "${GREEN}✓ Found boxId field in JSON${NC}"
exp_continue
}
-re "\"localPorts\":" {
puts "${GREEN}✓ Found localPorts field in JSON${NC}"
exp_continue
}
-re "\"status\":" {
puts "${GREEN}✓ Found status field in JSON${NC}"
exp_continue
}
-re "\"startedAt\":" {
puts "${GREEN}✓ Found startedAt field in JSON${NC}"
exp_continue
}
timeout {
puts "${RED}✗ Timeout waiting for JSON output${NC}"
exit 1
}
eof {
puts "${GREEN}✓ JSON list command completed successfully${NC}"
}
}
catch {close}
} result]} {
puts "${RED}✗ Test failed: $result${NC}"
exit 1
}
# Test 5: adb-expose list with invalid output format
puts "\n${YELLOW}Testing adb-expose list with invalid output format...${NC}"
puts "${BLUE}Running Test 5: adb-expose list with invalid output format${NC}"
if {[catch {
spawn $gbox_binary adb-expose list --output invalid-format
expect {
-re "Error:.*invalid value.*invalid-format" {
puts "${GREEN}✓ Found expected error message for invalid output format${NC}"
exp_continue
}
-re "No ADB port exposures found" {
puts "${GREEN}✓ Command succeeded with default format (invalid format ignored)${NC}"
exp_continue
}
timeout {
puts "${RED}✗ Timeout waiting for output${NC}"
exit 1
}
eof {
puts "${GREEN}✓ Command completed${NC}"
}
}
catch {close}
} result]} {
puts "${RED}✗ Test failed: $result${NC}"
exit 1
}
# Test 6: adb-expose list with extra arguments (should show error)
puts "\n${YELLOW}Testing adb-expose list with extra arguments...${NC}"
puts "${BLUE}Running Test 6: adb-expose list with extra arguments${NC}"
if {[catch {
spawn $gbox_binary adb-expose list extra-arg
expect {
-re "Error:.*unknown command.*extra-arg" {
puts "${GREEN}✓ Found expected error message for extra arguments${NC}"
exp_continue
}
-re "Error:.*accepts 0 argument" {
puts "${GREEN}✓ Found expected error message for too many arguments${NC}"
exp_continue
}
timeout {
puts "${RED}✗ Timeout waiting for error message${NC}"
exit 1
}
eof {
puts "${GREEN}✓ Error message displayed correctly${NC}"
}
}
catch {close}
} result]} {
puts "${RED}✗ Test failed: $result${NC}"
exit 1
}
# Test 7: adb-expose list with multiple output format flags (should use last one)
puts "\n${YELLOW}Testing adb-expose list with multiple output format flags...${NC}"
puts "${BLUE}Running Test 7: adb-expose list with multiple output format flags${NC}"
if {[catch {
spawn $gbox_binary adb-expose list --output table --output json
expect {
-re "\\\[\\\]" {
puts "${GREEN}✓ Found JSON output (last format flag used)${NC}"
exp_continue
}
-re "\\\[.*\\\]" {
puts "${GREEN}✓ Found JSON array for existing exposures${NC}"
exp_continue
}
timeout {
puts "${RED}✗ Timeout waiting for JSON output${NC}"
exit 1
}
eof {
puts "${GREEN}✓ Multiple output format flags test completed successfully${NC}"
}
}
catch {close}
} result]} {
puts "${RED}✗ Test failed: $result${NC}"
exit 1
}
# Test 8: adb-expose list with case variations of output format
puts "\n${YELLOW}Testing adb-expose list with case variations of output format...${NC}"
puts "${BLUE}Running Test 8: adb-expose list with case variations${NC}"
if {[catch {
spawn $gbox_binary adb-expose list --output TABLE
expect {
-re "No ADB port exposures found" {
puts "${GREEN}✓ Found expected message for no exposures (uppercase TABLE)${NC}"
exp_continue
}
-re "PID.*BoxID.*Port.*Status.*StartedAt" {
puts "${GREEN}✓ Found table header for existing exposures (uppercase TABLE)${NC}"
exp_continue
}
timeout {
puts "${RED}✗ Timeout waiting for table output${NC}"
exit 1
}
eof {
puts "${GREEN}✓ Uppercase TABLE format test completed successfully${NC}"
}
}
catch {close}
} result]} {
puts "${RED}✗ Test failed: $result${NC}"
exit 1
}
# Test 9: adb-expose list with JSON format (uppercase)
puts "\n${YELLOW}Testing adb-expose list with JSON format (uppercase)...${NC}"
puts "${BLUE}Running Test 9: adb-expose list with JSON format (uppercase)${NC}"
if {[catch {
spawn $gbox_binary adb-expose list --output JSON
expect {
-re "\\\[\\\]" {
puts "${GREEN}✓ Found empty JSON array for no exposures (uppercase JSON)${NC}"
exp_continue
}
-re "\\\[.*\\\]" {
puts "${GREEN}✓ Found JSON array for existing exposures (uppercase JSON)${NC}"
exp_continue
}
timeout {
puts "${RED}✗ Timeout waiting for JSON output${NC}"
exit 1
}
eof {
puts "${GREEN}✓ Uppercase JSON format test completed successfully${NC}"
}
}
catch {close}
} result]} {
puts "${RED}✗ Test failed: $result${NC}"
exit 1
}
# Test 10: Test cleanup messages (if any stale processes exist)
puts "\n${YELLOW}Testing cleanup messages in adb-expose list...${NC}"
puts "${BLUE}Running Test 10: Cleanup messages test${NC}"
if {[catch {
spawn $gbox_binary adb-expose list
expect {
-re "\\\[WARN\\\].*Found running adb-expose process.*not in registry" {
puts "${GREEN}✓ Found warning message for unregistered process${NC}"
exp_continue
}
-re "\\\[CLEANUP\\\].*Removing stale pid file" {
puts "${GREEN}✓ Found cleanup message for stale pid file${NC}"
exp_continue
}
-re "\\\[CLEANUP\\\].*Box.*is not running.*killing adb-expose process" {
puts "${GREEN}✓ Found cleanup message for dead box${NC}"
exp_continue
}
-re "No ADB port exposures found" {
puts "${GREEN}✓ Found expected message for no exposures${NC}"
exp_continue
}
-re "PID.*BoxID.*Port.*Status.*StartedAt" {
puts "${GREEN}✓ Found table header for existing exposures${NC}"
exp_continue
}
timeout {
puts "${RED}✗ Timeout waiting for list output${NC}"
exit 1
}
eof {
puts "${GREEN}✓ Cleanup messages test completed successfully${NC}"
}
}
catch {close}
} result]} {
puts "${RED}✗ Test failed: $result${NC}"
exit 1
}
puts "\n${GREEN}All adb-expose list tests passed!${NC}"
exit 0