Subversion Repositories havirt

Rev

Blame | Last modification | View Log | Download | RSS feed

# havirt Test Suite

This directory (tests/) contains comprehensive test scripts for exercising the havirt system.

All test scripts are located in the `tests/` directory and are designed to run from there.

## Test Scripts

### 1. test_havirt_safe.sh (Bash Safe Tests - Recommended)

A safe bash script that tests havirt in read-only mode without requiring SSH connections.

**Usage:**
```bash
./test_havirt_safe.sh [--verbose|-v]
```

**Features:**
- Tests all read-only havirt commands
- No SSH connections required
- Safe to run in any environment
- Tests command syntax and help functions
- Validates configuration files and modules
- Colored output with pass/fail indicators

**Example:**
```bash
# Run safe tests
./test_havirt_safe.sh

# Run with verbose output
./test_havirt_safe.sh --verbose
```

### 2. test_havirt.sh (Bash Full Tests - Requires Live Environment)

A comprehensive bash script that tests havirt commands including operations that require SSH connections.

**Usage:**
```bash
./test_havirt.sh [--verbose|-v]
```

**Features:**
- Tests all major havirt commands (domain, node, cluster)
- Runs in dry-run mode by default (safe to run)
- Tests command shortcuts (start, shutdown, migrate, destroy)
- Tests various flag combinations
- Validates configuration files and directory structure
- Error handling tests
- Colored output with pass/fail indicators

**Test Sections:**
1. Basic Commands (version, help)
2. Domain Module (list, update, start, shutdown)
3. Node Module (list, update, scan)
4. Cluster Module (status, balance, iscsi)
5. Flag Combinations (verbose, debug, quiet, testing)
6. Configuration (config files, directories)
7. Error Handling (invalid commands)

**Example:**
```bash
# Run all tests with minimal output
./test_havirt.sh

# Run with verbose output
./test_havirt.sh --verbose
```

**WARNING:** This script performs actual operations including SSH connections to configured nodes. Use with caution in production environments.

### 3. test_integration.pl (Perl Integration Tests)

A Perl script that tests havirt modules and functions directly at the code level.

**Usage:**
```bash
./test_integration.pl [--verbose|-v] [--dryrun|-n]
```

**Features:**
- Tests havirt.pm core functions directly
- Tests domain.pm, node.pm, and cluster.pm modules
- Validates data structures in status database
- Tests utility functions (MAC generation, VNC port finding)
- Checks for data integrity issues (orphaned domains)
- Runs in dry-run mode by default for safety
- Colored output with detailed results

**Test Sections:**
1. Configuration Tests
2. havirt.pm Core Functions
3. node.pm Module Tests
4. domain.pm Module Tests
5. cluster.pm Module Tests
6. Data Structure Validation

**Example:**
```bash
# Run integration tests in dry-run mode
./test_integration.pl

# Run with verbose output
./test_integration.pl -v

# Run with extra verbose output
./test_integration.pl -vv

# Run in live mode (actually executes operations)
./test_integration.pl --nodryrun  # Use with caution!
```

## Prerequisites

Both test scripts require:
- havirt to be properly installed and configured
- Required Perl modules (see [INSTALL.md](INSTALL.md))
- Bash shell (for test_havirt.sh)
- Perl 5.x (for test_integration.pl)

## Running the Tests

All commands should be run from the `tests/` directory:

```bash
cd tests/
```

### Quick Test (Safe)
```bash
# Run safe read-only tests (recommended for first test)
./test_havirt_safe.sh
```

### Full Test Suite
```bash
# Run all test suites
./test_havirt_safe.sh --verbose
./test_integration.pl --verbose
# Only run test_havirt.sh if you have a live cluster configured
./test_havirt.sh --verbose
```

### Using the Test Runner
```bash
# Interactive test runner
./run_tests.sh
```

### Continuous Integration
```bash
# Exit with non-zero status if any tests fail
cd tests/
./test_havirt_safe.sh && ./test_integration.pl
```

## Test Output

Both scripts provide:
- **Colored output**: Green for pass, red for fail, yellow for test names
- **Test counters**: Total tests run, passed, and failed
- **Summary report**: Final results with counts
- **Verbose mode**: Detailed output for debugging

### Example Output
```
==========================================
  havirt Test Suite
==========================================

[TEST] Version flag
[PASS] Version flag
[TEST] Help flag
[PASS] Help flag
...

==========================================
  Test Summary
==========================================

Total tests run:    45
Tests passed:       43
Tests failed:       2
```

## Safety Features

- **Dry-run by default**: Both scripts default to dry-run mode to prevent accidental changes
- **No destructive operations**: Tests don't create, delete, or modify actual VMs
- **Configuration validation**: Tests verify configuration before running
- **Error handling**: Graceful failure with informative error messages

## Adding New Tests

### Adding to test_havirt.sh

Add new test sections following the pattern:

```bash
echo "=========================================="
echo "Section N: Your Test Section"
echo "=========================================="
echo ""

run_test "Test description" "$HAVIRT command --dryrun"
run_test_with_output "Test that needs output" "$HAVIRT other-command --dryrun"
```

### Adding to test_integration.pl

Add new tests using the test framework:

```perl
run_test("Test description", sub {
    my $result = your_function();
    return $result ? "Success message" : undef;
});

test_returns_defined("Test that must return value", sub {
    return your_function_that_must_return_something();
});
```

## Troubleshooting

### Tests Fail to Run
- Ensure havirt executable exists and is executable
- Check that config.yaml exists (will be auto-generated if missing)
- Verify Perl modules are installed: `perl -c havirt`

### All Tests Fail
- Check that you're in the correct directory
- Verify file permissions: `ls -l test_*.sh test_*.pl`
- Run with --verbose to see detailed error messages

### Specific Tests Fail
- Review the error output
- Check if the required nodes/domains exist
- Verify configuration in config.yaml
- Some tests may fail if there are no domains or nodes configured

## Notes

- Tests are designed to be **idempotent** - safe to run multiple times
- Tests use the existing configuration and status files
- No external dependencies beyond standard Perl modules and bash
- Compatible with havirt version 1.2.2 and later

## License

Copyright 2026 Daily Data, Inc.

See main havirt license for details.