Blame | Last modification | View Log | Download | RSS feed
#!/bin/bash
# test_havirt.sh
# Comprehensive test script to exercise havirt functionality
# This script tests various havirt commands in dry-run mode to ensure they work properly
#
# Usage: ./test_havirt.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++))
}
# 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 Test Suite"
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" "$HAVIRT --version"
run_test "Help flag" "$HAVIRT --help"
run_test "Help command" "$HAVIRT help"
echo ""
# Section 2: Domain Module Tests
echo "=========================================="
echo "Section 2: Domain Module"
echo "=========================================="
echo ""
run_test "Domain help" "$HAVIRT domain help"
run_test_with_output "Domain list (screen format)" "$HAVIRT domain list --dryrun"
run_test_with_output "Domain list (TSV format)" "$HAVIRT domain list --format tsv --dryrun"
run_test "Domain update (dry-run)" "$HAVIRT domain update --dryrun"
run_test "Domain update with verbose" "$HAVIRT domain update --dryrun --verbose"
# Test shortcut commands (these should work even without existing domains in dry-run mode)
if run_test_with_output "Get domain list" "$HAVIRT domain list --dryrun"; then
# Extract a domain name if one exists
DOMAIN_NAME=$(eval "$HAVIRT domain list --dryrun" 2>/dev/null | awk 'NR>1 && NF>0 {print $1; exit}')
if [ -n "$DOMAIN_NAME" ]; then
log "Found test domain: $DOMAIN_NAME"
run_test "Domain start (shortcut)" "$HAVIRT start $DOMAIN_NAME --dryrun"
run_test "Domain shutdown (shortcut)" "$HAVIRT shutdown $DOMAIN_NAME --dryrun"
run_test "Domain start (explicit)" "$HAVIRT domain start $DOMAIN_NAME --dryrun"
run_test "Domain shutdown (explicit)" "$HAVIRT domain shutdown $DOMAIN_NAME --dryrun"
else
log "No domains found for shortcut tests (skipping)"
fi
fi
echo ""
# Section 3: Node Module Tests
echo "=========================================="
echo "Section 3: Node Module"
echo "=========================================="
echo ""
run_test "Node help" "$HAVIRT node help"
run_test_with_output "Node list (screen format)" "$HAVIRT node list --dryrun"
run_test_with_output "Node list (TSV format)" "$HAVIRT node list --format tsv --dryrun"
run_test "Node update" "$HAVIRT node update --dryrun"
run_test "Node scan (dry-run)" "$HAVIRT node scan --dryrun"
run_test "Node scan with force" "$HAVIRT node scan --force --dryrun"
run_test "Node scan with verbose" "$HAVIRT node scan --verbose --dryrun"
# Get a node name for targeted tests
if run_test_with_output "Get node list" "$HAVIRT node list --dryrun"; then
NODE_NAME=$(eval "$HAVIRT node list --dryrun" 2>/dev/null | awk 'NR>1 && NF>0 {print $1; exit}')
if [ -n "$NODE_NAME" ]; then
log "Found test node: $NODE_NAME"
run_test "Node scan specific node" "$HAVIRT node scan --target $NODE_NAME --dryrun"
else
log "No nodes found for targeted tests (skipping)"
fi
fi
echo ""
# Section 4: Cluster Module Tests
echo "=========================================="
echo "Section 4: Cluster Module"
echo "=========================================="
echo ""
run_test "Cluster help" "$HAVIRT cluster help"
run_test_with_output "Cluster status" "$HAVIRT cluster status --dryrun"
run_test_with_output "Cluster status (TSV)" "$HAVIRT cluster status --format tsv --dryrun"
run_test "Cluster balance (dry-run)" "$HAVIRT cluster balance --dryrun"
run_test "Cluster balance with variance" "$HAVIRT cluster balance --variance 0.15 --dryrun"
run_test "Cluster iscsi" "$HAVIRT cluster iscsi --dryrun"
echo ""
# Section 5: Flag Combinations
echo "=========================================="
echo "Section 5: Flag Combinations"
echo "=========================================="
echo ""
run_test "Multiple verbose flags" "$HAVIRT node list -vv --dryrun"
run_test "Debug flag" "$HAVIRT domain list --debug --dryrun"
run_test "Quiet flag" "$HAVIRT node list --quiet --dryrun"
run_test "Testing flag" "$HAVIRT domain list --testing"
run_test "Force flag" "$HAVIRT node scan --force --dryrun"
run_test "Bundled flags" "$HAVIRT node list -vvn"
echo ""
# Section 6: Config File Tests
echo "=========================================="
echo "Section 6: Configuration"
echo "=========================================="
echo ""
if [ -f "$PARENT_DIR/config.yaml" ]; then
log_success "Config file exists"
if [ $VERBOSE -eq 1 ]; then
echo " Config file location: $PARENT_DIR/config.yaml"
fi
else
log "Config file not found (will be auto-generated on first run)"
fi
if [ -d "$PARENT_DIR/conf" ]; then
CONF_COUNT=$(find "$PARENT_DIR/conf" -name "*.xml" -type f | wc -l)
log_success "Domain config directory exists with $CONF_COUNT XML files"
else
log "Domain config directory not found"
fi
if [ -f "$PARENT_DIR/var/status.yaml" ]; then
log_success "Status database exists"
else
log "Status database not found (will be created on first scan)"
fi
echo ""
# Section 7: Error Handling Tests
echo "=========================================="
echo "Section 7: Error Handling"
echo "=========================================="
echo ""
# These should fail gracefully
log_test "Invalid command"
((TESTS_RUN++))
if output=$("$HAVIRT" invalidcommand 2>&1); then
log_failure "Invalid command (should have failed)"
else
log_success "Invalid command (failed as expected)"
fi
log_test "Invalid action"
((TESTS_RUN++))
if output=$("$HAVIRT" domain invalidaction --dryrun 2>&1); then
log_failure "Invalid action (should have failed)"
else
log_success "Invalid action (failed as expected)"
fi
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}"
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