box-exec.exp•19.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 box exec command...${NC}"
# First, create a test box for our exec tests
puts "\n${YELLOW}Creating a test box for exec tests...${NC}"
set test_box_id ""
if {[catch {
spawn $gbox_binary box create linux
expect {
-re "Linux box created with ID: (\[a-f0-9-]+)" {
set test_box_id $expect_out(1,string)
puts "${GREEN}✓ Test box created with ID: $test_box_id${NC}"
exp_continue
}
-re "Linux box created successfully" {
puts "${GREEN}✓ Test box created successfully (no ID returned)${NC}"
# If no ID is returned, we'll use a placeholder
set test_box_id "test-box-placeholder"
exp_continue
}
-re "failed to create Linux box" {
puts "${YELLOW}⚠ Failed to create test box (API not available), using placeholder ID${NC}"
set test_box_id "test-box-placeholder"
exp_continue
}
timeout {
puts "${RED}✗ Timeout waiting for box creation${NC}"
set test_box_id "test-box-placeholder"
exp_continue
}
eof {
puts "${GREEN}✓ Box create command completed${NC}"
}
}
catch {close}
} result]} {
puts "${RED}✗ Box creation failed: $result${NC}"
set test_box_id "test-box-placeholder"
}
if {$test_box_id == ""} {
set test_box_id "test-box-placeholder"
}
# Test 1: Basic box exec command without arguments (should show error)
puts "\n${YELLOW}Testing basic box exec command without arguments...${NC}"
puts "${BLUE}Running Test 1: Basic box exec command without arguments${NC}"
if {[catch {
spawn $gbox_binary box exec
expect {
-re "command must be specified after" {
puts "${GREEN}✓ Found expected error message for missing command${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for error message${NC}"
exit 1
}
eof {
puts "${RED}✗ Command exited unexpectedly${NC}"
exit 1
}
}
expect {
eof {
puts "${GREEN}✓ Error message displayed correctly${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for command completion${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Test failed: $result${NC}"
exit 1
}
# Test 2: Box exec command without command (should show error)
puts "\n${YELLOW}Testing box exec command without command...${NC}"
puts "${BLUE}Running Test 2: Box exec command without command${NC}"
if {[catch {
spawn $gbox_binary box exec test-box-id
expect {
-re "command must be specified after" {
puts "${GREEN}✓ Found expected error message for missing command${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for error message${NC}"
exit 1
}
eof {
puts "${RED}✗ Command exited unexpectedly${NC}"
exit 1
}
}
expect {
eof {
puts "${GREEN}✓ Error message displayed correctly${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for command completion${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Test failed: $result${NC}"
exit 1
}
# Test 3: Help command for box exec
puts "\n${YELLOW}Testing help command for box exec...${NC}"
puts "${BLUE}Running Test 3: Help command for box exec${NC}"
if {[catch {
spawn $gbox_binary box exec --help
expect {
-re "Execute a command in a box" {
puts "${GREEN}✓ Found help description${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for help description${NC}"
exit 1
}
eof {
puts "${RED}✗ Help command exited unexpectedly${NC}"
exit 1
}
}
expect {
-re "Flags:" {
puts "${GREEN}✓ Found Flags section${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for Flags section${NC}"
exit 1
}
}
expect {
-re "-i, --interactive.*Enable interactive mode" {
puts "${GREEN}✓ Found interactive flag description${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for interactive flag description${NC}"
exit 1
}
}
expect {
-re "-t, --tty.*Force TTY allocation" {
puts "${GREEN}✓ Found tty flag description${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for tty flag description${NC}"
exit 1
}
}
expect {
-re "-w, --workdir.*Working directory" {
puts "${GREEN}✓ Found workdir flag description${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for workdir flag description${NC}"
exit 1
}
}
expect {
eof {
puts "${GREEN}✓ Help command completed successfully${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for help completion${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Help test failed: $result${NC}"
exit 1
}
# Test 4: Box exec with invalid box ID
puts "\n${YELLOW}Testing box exec with invalid box ID...${NC}"
puts "${BLUE}Running Test 4: Box exec with invalid box ID${NC}"
if {[catch {
spawn $gbox_binary box exec invalid-box-id -- ls
expect {
-re "failed to resolve box ID" {
puts "${GREEN}✓ Found expected error for invalid box ID${NC}"
}
-re "Error:" {
puts "${GREEN}✓ Found error message for invalid box ID${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for invalid box ID error${NC}"
exit 1
}
eof {
puts "${RED}✗ Invalid box ID command exited unexpectedly${NC}"
exit 1
}
}
expect {
eof {
puts "${GREEN}✓ Invalid box ID error displayed correctly${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for invalid box ID completion${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Invalid box ID test failed: $result${NC}"
exit 1
}
# Test 5: Box exec with valid box ID (if we have a real box)
puts "\n${YELLOW}Testing box exec with valid box ID...${NC}"
puts "${BLUE}Running Test 5: Box exec with valid box ID${NC}"
if {[catch {
spawn $gbox_binary box exec $test_box_id -- ls
expect {
-re "failed to connect websocket" {
puts "${GREEN}✓ Found expected websocket connection error (API not available)${NC}"
}
-re "failed to resolve box ID" {
puts "${GREEN}✓ Found expected box ID resolution error${NC}"
}
-re "total" {
puts "${GREEN}✓ Found ls command output (box exec working)${NC}"
}
-re "bin.*dev.*etc" {
puts "${GREEN}✓ Found directory listing (box exec working)${NC}"
}
-re "Error:" {
puts "${GREEN}✓ Found error message${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for exec command${NC}"
exit 1
}
eof {
puts "${RED}✗ Exec command exited unexpectedly${NC}"
exit 1
}
}
expect {
eof {
puts "${GREEN}✓ Exec command completed${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for exec completion${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Valid box exec test failed: $result${NC}"
exit 1
}
# Test 6: Box exec with interactive flag
puts "\n${YELLOW}Testing box exec with interactive flag...${NC}"
puts "${BLUE}Running Test 6: Box exec with interactive flag${NC}"
if {[catch {
spawn $gbox_binary box exec -i $test_box_id -- cat
expect {
-re "failed to connect websocket" {
puts "${GREEN}✓ Found expected websocket connection error with interactive flag${NC}"
}
-re "failed to resolve box ID" {
puts "${GREEN}✓ Found expected box ID resolution error with interactive flag${NC}"
}
-re "Error:" {
puts "${GREEN}✓ Found error message for non-existent box with interactive flag${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box error with interactive flag${NC}"
exit 1
}
eof {
puts "${RED}✗ Non-existent box with interactive flag command exited unexpectedly${NC}"
exit 1
}
}
expect {
eof {
puts "${GREEN}✓ Non-existent box with interactive flag error displayed correctly${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box with interactive flag completion${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Non-existent box with interactive flag test failed: $result${NC}"
exit 1
}
# Test 7: Box exec with tty flag
puts "\n${YELLOW}Testing box exec with tty flag...${NC}"
puts "${BLUE}Running Test 7: Box exec with tty flag${NC}"
if {[catch {
spawn $gbox_binary box exec -t $test_box_id -- bash
expect {
-re "failed to connect websocket" {
puts "${GREEN}✓ Found expected websocket connection error with tty flag${NC}"
}
-re "failed to resolve box ID" {
puts "${GREEN}✓ Found expected box ID resolution error with tty flag${NC}"
}
-re "Error:" {
puts "${GREEN}✓ Found error message for non-existent box with tty flag${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box error with tty flag${NC}"
exit 1
}
eof {
puts "${RED}✗ Non-existent box with tty flag command exited unexpectedly${NC}"
exit 1
}
}
expect {
eof {
puts "${GREEN}✓ Non-existent box with tty flag error displayed correctly${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box with tty flag completion${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Non-existent box with tty flag test failed: $result${NC}"
exit 1
}
# Test 8: Box exec with workdir flag
puts "\n${YELLOW}Testing box exec with workdir flag...${NC}"
puts "${BLUE}Running Test 8: Box exec with workdir flag${NC}"
if {[catch {
spawn $gbox_binary box exec -w /tmp $test_box_id -- pwd
expect {
-re "failed to connect websocket" {
puts "${GREEN}✓ Found expected websocket connection error with workdir flag${NC}"
}
-re "failed to resolve box ID" {
puts "${GREEN}✓ Found expected box ID resolution error with workdir flag${NC}"
}
-re "Error:" {
puts "${GREEN}✓ Found error message for non-existent box with workdir flag${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box error with workdir flag${NC}"
exit 1
}
eof {
puts "${RED}✗ Non-existent box with workdir flag command exited unexpectedly${NC}"
exit 1
}
}
expect {
eof {
puts "${GREEN}✓ Non-existent box with workdir flag error displayed correctly${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box with workdir flag completion${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Non-existent box with workdir flag test failed: $result${NC}"
exit 1
}
# Test 9: Box exec with multiple flags
puts "\n${YELLOW}Testing box exec with multiple flags...${NC}"
puts "${BLUE}Running Test 9: Box exec with multiple flags${NC}"
if {[catch {
spawn $gbox_binary box exec -i -t -w /home $test_box_id -- bash
expect {
-re "failed to connect websocket" {
puts "${GREEN}✓ Found expected websocket connection error with multiple flags${NC}"
}
-re "failed to resolve box ID" {
puts "${GREEN}✓ Found expected box ID resolution error with multiple flags${NC}"
}
-re "Error:" {
puts "${GREEN}✓ Found error message for non-existent box with multiple flags${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box error with multiple flags${NC}"
exit 1
}
eof {
puts "${RED}✗ Non-existent box with multiple flags command exited unexpectedly${NC}"
exit 1
}
}
expect {
eof {
puts "${GREEN}✓ Non-existent box with multiple flags error displayed correctly${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box with multiple flags completion${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Non-existent box with multiple flags test failed: $result${NC}"
exit 1
}
# Test 10: Box exec with long flag names
puts "\n${YELLOW}Testing box exec with long flag names...${NC}"
puts "${BLUE}Running Test 10: Box exec with long flag names${NC}"
if {[catch {
spawn $gbox_binary box exec --interactive --tty --workdir /var $test_box_id -- ls -la
expect {
-re "failed to connect websocket" {
puts "${GREEN}✓ Found expected websocket connection error with long flags${NC}"
}
-re "failed to resolve box ID" {
puts "${GREEN}✓ Found expected box ID resolution error with long flags${NC}"
}
-re "Error:" {
puts "${GREEN}✓ Found error message for non-existent box with long flags${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box error with long flags${NC}"
exit 1
}
eof {
puts "${RED}✗ Non-existent box with long flags command exited unexpectedly${NC}"
exit 1
}
}
expect {
eof {
puts "${GREEN}✓ Non-existent box with long flags error displayed correctly${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box with long flags completion${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Non-existent box with long flags test failed: $result${NC}"
exit 1
}
# Test 11: Box exec with complex command
puts "\n${YELLOW}Testing box exec with complex command...${NC}"
puts "${BLUE}Running Test 11: Box exec with complex command${NC}"
if {[catch {
spawn $gbox_binary box exec $test_box_id -- sh -c "echo 'Hello World' && ls -la /tmp"
expect {
-re "failed to connect websocket" {
puts "${GREEN}✓ Found expected websocket connection error with complex command${NC}"
}
-re "failed to resolve box ID" {
puts "${GREEN}✓ Found expected box ID resolution error with complex command${NC}"
}
-re "Error:" {
puts "${GREEN}✓ Found error message for non-existent box with complex command${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box error with complex command${NC}"
exit 1
}
eof {
puts "${RED}✗ Non-existent box with complex command exited unexpectedly${NC}"
exit 1
}
}
expect {
eof {
puts "${GREEN}✓ Non-existent box with complex command error displayed correctly${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box with complex command completion${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Non-existent box with complex command test failed: $result${NC}"
exit 1
}
# Test 12: Box exec with command that has spaces and special characters
puts "\n${YELLOW}Testing box exec with command that has spaces and special characters...${NC}"
puts "${BLUE}Running Test 12: Box exec with command that has spaces and special characters${NC}"
if {[catch {
spawn $gbox_binary box exec $test_box_id -- echo "Hello World with spaces and special chars: !@#$%^&*()"
expect {
-re "failed to connect websocket" {
puts "${GREEN}✓ Found expected websocket connection error with special characters${NC}"
}
-re "failed to resolve box ID" {
puts "${GREEN}✓ Found expected box ID resolution error with special characters${NC}"
}
-re "Error:" {
puts "${GREEN}✓ Found error message for non-existent box with special characters${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box error with special characters${NC}"
exit 1
}
eof {
puts "${RED}✗ Non-existent box with special characters command exited unexpectedly${NC}"
exit 1
}
}
expect {
eof {
puts "${GREEN}✓ Non-existent box with special characters error displayed correctly${NC}"
}
timeout {
puts "${RED}✗ Timeout waiting for non-existent box with special characters completion${NC}"
exit 1
}
}
catch {close}
} result]} {
puts "${RED}✗ Non-existent box with special characters test failed: $result${NC}"
exit 1
}
# Clean up: terminate the test box if it was created
if {$test_box_id != "test-box-placeholder" && $test_box_id != ""} {
puts "\n${YELLOW}Cleaning up test box: $test_box_id${NC}"
if {[catch {
spawn $gbox_binary box terminate $test_box_id
expect {
-re "Box terminated successfully" {
puts "${GREEN}✓ Test box terminated successfully${NC}"
}
-re "failed to terminate box" {
puts "${YELLOW}⚠ Failed to terminate test box (may not exist)${NC}"
}
timeout {
puts "${YELLOW}⚠ Timeout waiting for box termination${NC}"
}
eof {
puts "${GREEN}✓ Box terminate command completed${NC}"
}
}
expect {
eof {
puts "${GREEN}✓ Box terminate command finished${NC}"
}
timeout {
puts "${YELLOW}⚠ Timeout waiting for terminate completion${NC}"
}
}
catch {close}
} result]} {
puts "${YELLOW}⚠ Box cleanup failed: $result${NC}"
}
} else {
puts "\n${YELLOW}No test box to clean up (used placeholder)${NC}"
}
puts "\n${GREEN}All box exec tests passed successfully!${NC}"
exit 0