Blame | Last modification | View Log | Download | RSS feed
#!/bin/bash
# test_havirt_safe.sh
# Safe test script that exercises havirt in read-only mode
# This script only tests commands that don't require SSH connections
#
# Usage: ./test_havirt_safe.sh [--verbose]
#
# Copyright 2026 Daily Data, Inc.
# Don't exit on error - we want to catch and report errors
set +e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Get script directory (tests/) and parent directory (havirt/)
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PARENT_DIR="$( cd "$SCRIPT_DIR/.." && pwd )"
HAVIRT="$PARENT_DIR/havirt"
# Test counter
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0
# Verbose flag
VERBOSE=0
if [[ "$1" == "--verbose" || "$1" == "-v" ]]; then
VERBOSE=1
fi
# Log function
log() {
echo -e "${BLUE}[INFO]${NC} $*"
}
log_success() {
echo -e "${GREEN}[PASS]${NC} $*"
((TESTS_PASSED++))
}
log_failure() {
echo -e "${RED}[FAIL]${NC} $*"
((TESTS_FAILED++))
}
log_test() {
echo -e "${YELLOW}[TEST]${NC} $*"
((TESTS_RUN++))
}
log_skip() {
echo -e "${BLUE}[SKIP]${NC} $*"
}
# Function to run a test command
run_test() {
local test_name="$1"
shift
local cmd="$*"
log_test "$test_name"
if [ $VERBOSE -eq 1 ]; then
echo " Command: $cmd"
fi
# Run command from parent directory so havirt can find its modules
if output=$(cd "$PARENT_DIR" && eval "$cmd" 2>&1); then
if [ $VERBOSE -eq 1 ]; then
echo " Output: $output"
fi
log_success "$test_name"
return 0
else
log_failure "$test_name"
echo " Error output: $output"
return 1
fi
}
# Function to test that a command produces output
run_test_with_output() {
local test_name="$1"
shift
local cmd="$*"
log_test "$test_name"
if [ $VERBOSE -eq 1 ]; then
echo " Command: $cmd"
fi
# Run command from parent directory so havirt can find its modules
if output=$(cd "$PARENT_DIR" && eval "$cmd" 2>&1); then
if [ -n "$output" ]; then
if [ $VERBOSE -eq 1 ]; then
echo " Output: $output"
fi
log_success "$test_name"
return 0
else
log_failure "$test_name (no output produced)"
return 1
fi
else
log_failure "$test_name"
echo " Error output: $output"
return 1
fi
}
echo "=========================================="
echo " havirt Safe Test Suite"
echo " (Read-only operations)"
echo "=========================================="
echo ""
# Check if havirt exists
if [ ! -f "$HAVIRT" ]; then
log_failure "havirt executable not found at $HAVIRT"
exit 1
fi
log "Using havirt at: $HAVIRT"
echo ""
# Section 1: Basic Command Tests
echo "=========================================="
echo "Section 1: Basic Commands"
echo "=========================================="
echo ""
run_test "Version flag (--version)" "$HAVIRT --version"
run_test "Version flag (-V)" "$HAVIRT -V"
run_test "Help flag (--help)" "$HAVIRT --help"
run_test "Help flag (-h)" "$HAVIRT -h"
run_test "Help command" "$HAVIRT help"
echo ""
# Section 2: Domain Module Tests (Read-only)
echo "=========================================="
echo "Section 2: Domain Module (Read-only)"
echo "=========================================="
echo ""
run_test "Domain help" "$HAVIRT domain help"
run_test "Domain with no action (should show help)" "$HAVIRT domain"
run_test_with_output "Domain list (screen format)" "$HAVIRT domain list --testing"
run_test_with_output "Domain list (TSV format)" "$HAVIRT domain list --format tsv --testing"
run_test_with_output "Domain list (TSV shorthand)" "$HAVIRT domain list -f tsv --testing"
run_test "Domain list with quiet flag" "$HAVIRT domain list --quiet --testing"
run_test "Domain list with verbose" "$HAVIRT domain list --verbose --testing"
run_test "Domain list with multiple verbose" "$HAVIRT domain list -vv --testing"
echo ""
# Section 3: Node Module Tests (Read-only)
echo "=========================================="
echo "Section 3: Node Module (Read-only)"
echo "=========================================="
echo ""
run_test "Node help" "$HAVIRT node help"
run_test "Node with no action (should show help)" "$HAVIRT node"
run_test_with_output "Node list (screen format)" "$HAVIRT node list --testing"
run_test_with_output "Node list (TSV format)" "$HAVIRT node list --format tsv --testing"
run_test_with_output "Node list (TSV shorthand)" "$HAVIRT node list -f tsv --testing"
run_test "Node list with quiet flag" "$HAVIRT node list --quiet --testing"
run_test "Node list with verbose" "$HAVIRT node list --verbose --testing"
run_test "Node list with bundled flags" "$HAVIRT node list -vv --testing"
run_test "Node scan with testing flag" "$HAVIRT node scan --testing"
echo ""
# Section 4: Cluster Module Tests (Read-only)
echo "=========================================="
echo "Section 4: Cluster Module (Read-only)"
echo "=========================================="
echo ""
run_test "Cluster help" "$HAVIRT cluster help"
run_test "Cluster with no action (should show help)" "$HAVIRT cluster"
run_test_with_output "Cluster status (screen)" "$HAVIRT cluster status --testing"
run_test_with_output "Cluster status (TSV)" "$HAVIRT cluster status --format tsv --testing"
run_test_with_output "Cluster status (TSV shorthand)" "$HAVIRT cluster status -f tsv --testing"
run_test "Cluster status with verbose" "$HAVIRT cluster status --verbose --testing"
echo ""
# Section 5: Flag Tests (various combinations)
echo "=========================================="
echo "Section 5: Flag Combinations"
echo "=========================================="
echo ""
run_test "Bundled short flags (-vvf)" "$HAVIRT domain list -vvf tsv --testing"
run_test "Mixed long and short flags" "$HAVIRT --verbose node list -f tsv --testing"
run_test "Flags before command" "$HAVIRT --format tsv domain list --testing"
run_test "Flags after action" "$HAVIRT domain list --format tsv --testing"
run_test "Testing flag" "$HAVIRT domain list --testing"
run_test "Debug flag level 1" "$HAVIRT domain list --debug --testing"
run_test "Multiple debug flags" "$HAVIRT domain list -dd --testing"
echo ""
# Section 6: Config and File Tests
echo "=========================================="
echo "Section 6: Configuration & Files"
echo "=========================================="
echo ""
if [ -f "$PARENT_DIR/config.yaml" ]; then
log_success "Config file exists: config.yaml"
((TESTS_PASSED++))
((TESTS_RUN++))
if [ $VERBOSE -eq 1 ]; then
echo " Location: $PARENT_DIR/config.yaml"
echo " Size: $(stat -f%z "$PARENT_DIR/config.yaml" 2>/dev/null || stat -c%s "$PARENT_DIR/config.yaml" 2>/dev/null) bytes"
fi
else
log "Config file not found (will be auto-generated)"
fi
if [ -d "$PARENT_DIR/conf" ]; then
CONF_COUNT=$(find "$PARENT_DIR/conf" -name "*.xml" -type f 2>/dev/null | wc -l)
log_success "Domain config directory exists with $CONF_COUNT XML files"
((TESTS_PASSED++))
((TESTS_RUN++))
else
log "Domain config directory not found"
fi
if [ -d "$PARENT_DIR/var" ]; then
log_success "Var directory exists"
((TESTS_PASSED++))
((TESTS_RUN++))
if [ -f "$PARENT_DIR/var/status.yaml" ]; then
log_success "Status database exists"
((TESTS_PASSED++))
((TESTS_RUN++))
if [ $VERBOSE -eq 1 ]; then
echo " Location: $PARENT_DIR/var/status.yaml"
echo " Size: $(stat -f%z "$PARENT_DIR/var/status.yaml" 2>/dev/null || stat -c%s "$PARENT_DIR/var/status.yaml" 2>/dev/null) bytes"
fi
else
log "Status database not found"
fi
else
log "Var directory not found"
fi
echo ""
# Section 7: Module File Tests
echo "=========================================="
echo "Section 7: Module Files"
echo "=========================================="
echo ""
for module in havirt domain node cluster; do
if [ -f "$PARENT_DIR/$module.pm" ]; then
log_success "Module $module.pm exists"
((TESTS_PASSED++))
((TESTS_RUN++))
# Check if module is syntactically valid
if perl -c "$PARENT_DIR/$module.pm" >/dev/null 2>&1; then
log_success "Module $module.pm syntax valid"
((TESTS_PASSED++))
((TESTS_RUN++))
else
log_failure "Module $module.pm has syntax errors"
((TESTS_FAILED++))
((TESTS_RUN++))
fi
else
log_failure "Module $module.pm not found"
((TESTS_FAILED++))
((TESTS_RUN++))
fi
done
echo ""
# Section 8: Error Handling Tests
echo "=========================================="
echo "Section 8: Error Handling"
echo "=========================================="
echo ""
# These should fail gracefully
log_test "Invalid command"
((TESTS_RUN++))
if output=$(cd "$PARENT_DIR" && "$HAVIRT" invalidcommand 2>&1); then
log_failure "Invalid command (should have failed)"
((TESTS_FAILED++))
else
if echo "$output" | grep -q "Error"; then
log_success "Invalid command (failed gracefully with error message)"
((TESTS_PASSED++))
else
log_failure "Invalid command (failed but no error message)"
((TESTS_FAILED++))
fi
fi
log_test "Invalid action"
((TESTS_RUN++))
if output=$(cd "$PARENT_DIR" && "$HAVIRT" domain invalidaction 2>&1); then
log_failure "Invalid action (should have failed)"
((TESTS_FAILED++))
else
if echo "$output" | grep -q "Error"; then
log_success "Invalid action (failed gracefully with error message)"
((TESTS_PASSED++))
else
log_failure "Invalid action (failed but no error message)"
((TESTS_FAILED++))
fi
fi
echo ""
# Section 9: Skipped Tests (require live connections)
echo "=========================================="
echo "Section 9: Skipped Tests"
echo "=========================================="
echo ""
log_skip "Domain start operations (requires SSH connections)"
log_skip "Domain shutdown operations (requires SSH connections)"
log_skip "Domain migrate operations (requires SSH connections)"
log_skip "Node scan operations (requires SSH connections)"
log_skip "Cluster balance operations (requires SSH connections)"
log_skip "Run test_havirt.sh with actual nodes configured for full tests"
echo ""
# Final Summary
echo "=========================================="
echo " Test Summary"
echo "=========================================="
echo ""
echo "Total tests run: $TESTS_RUN"
echo -e "Tests passed: ${GREEN}$TESTS_PASSED${NC}"
echo -e "Tests failed: ${RED}$TESTS_FAILED${NC}"
if [ $TESTS_RUN -gt 0 ]; then
PASS_PERCENT=$((TESTS_PASSED * 100 / TESTS_RUN))
echo "Success rate: ${PASS_PERCENT}%"
fi
echo ""
if [ $TESTS_FAILED -eq 0 ]; then
echo -e "${GREEN}✓ All tests passed!${NC}"
exit 0
else
echo -e "${RED}✗ Some tests failed.${NC}"
exit 1
fi