Sari la conținut

Agnostic OS Installer - Testing Documentation

Overview

The Agnostic OS Installer includes a comprehensive test suite covering unit tests, integration tests, and functional tests. Testing focuses on cross-platform compatibility, configuration validation, and end-to-end installation scenarios.

Test Structure

Test Files Organization

tests/
├── __init__.py           # Test package initialization
├── test_engine.py        # Core engine unit tests
├── test_integration.py   # Integration tests
└── test_functional.py    # End-to-end functional tests

Test Categories

1. Unit Tests (test_engine.py)

Purpose: Test individual components and modules in isolation

Platform Detection Tests

  • test_platform_detection_linux() - Linux platform detection
  • test_platform_detection_windows() - Windows platform detection
  • test_platform_detection_macos() - macOS platform detection
  • test_platform_detection_unsupported() - Unsupported platform handling

Configuration Tests

  • test_load_config_valid() - Valid YAML configuration loading
  • test_load_config_missing_file() - Missing configuration file handling
  • test_load_config_invalid_format() - Invalid configuration format validation
  • test_load_config_missing_package() - Missing required package section
  • test_load_config_missing_package_name() - Package name validation
  • test_validate_config_valid() - Configuration schema validation
  • test_validate_config_invalid_dependencies() - Dependency format validation

Installation Tests

  • test_simulate_installation() - Dry-run installation simulation
  • test_install_files_dry_run() - File installation (dry-run mode)
  • test_install_services_dry_run() - Service installation (dry-run mode)
  • test_install_package_dry_run() - Full package installation (dry-run)
  • test_install_package_invalid_config() - Invalid configuration handling
  • test_uninstall_package_basic() - Basic package uninstallation
  • test_list_packages() - Package listing functionality
  • test_show_status() - Status display functionality
  • test_shortcuts_windows_only() - Windows-only shortcut creation
  • test_registry_windows_only() - Windows-only registry operations
  • test_environment_variables_dry_run() - Environment variable handling

CLI Tests

  • test_parse_install_command() - Install command argument parsing
  • test_parse_validate_command() - Validate command parsing
  • test_parse_uninstall_command() - Uninstall command parsing
  • test_parse_list_command() - List command parsing
  • test_parse_help() - Help message display

2. Integration Tests (test_integration.py)

Purpose: Test component interactions and system-level operations

File Operations Tests

  • File copying with permission preservation
  • Cross-platform permission handling
  • Template processing validation
  • File existence and content verification

Service Management Tests

  • Service configuration parsing
  • Platform-specific service templates
  • Service dependency resolution
  • Service uninstallation procedures

Registry Operations Tests

  • Package registry data loading
  • Platform-specific command resolution
  • Registry-based dependency installation
  • Package availability checking

CLI Integration Tests

  • Command execution with subprocess calls
  • Configuration file processing
  • Error handling in command execution
  • Cross-platform command compatibility

3. Functional Tests (test_functional.py)

Purpose: End-to-end testing of complete installation scenarios

Complex Installation Test

  • test_complex_install_uninstall() - Full install/uninstall cycle
  • Files: Copy system files (/bin/bash, /etc/group) with permissions
  • Services: Create systemd services with configuration
  • Environment: Environment variable management
  • Validation: Verify all components installed correctly
  • Cleanup: Complete uninstallation testing

Package Building Tests

  • test_package_kit_generation() - Cross-platform package creation
  • Formats: Generate .deb (Linux), .exe (Windows), .dmg (macOS)
  • Metadata: Include package information in all formats
  • Validation: Verify package file creation and content
  • Platform Support: Multi-platform package generation

Running Tests

Prerequisites

# Install test dependencies
pip install -r requirements.txt

# Optional: Install test-specific packages
pip install pytest coverage

Execute All Tests

# Run all tests
python3 tests/test_functional.py

# Or use pytest (if installed)
pytest tests/

# With coverage reporting
coverage run -m pytest tests/
coverage report

Run Specific Test Categories

# Unit tests only
python3 -m unittest tests.test_engine

# Integration tests only
python3 -m unittest tests.test_integration

# Functional tests only
python3 tests/test_functional.py

# Single test method
python3 -m unittest tests.test_engine.TestAgnosticInstaller.test_platform_detection_linux

Continuous Integration

Tests can be integrated into CI/CD pipelines:

# .github/workflows/test.yml
name: Tests
on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: '3.9'
      - run: pip install -r requirements.txt
      - run: python3 tests/test_functional.py

Test Configuration

Test Data Setup

Tests use temporary files and directories for safe testing:

def setUp(self):
    self.temp_dir = tempfile.TemporaryDirectory()
    self.config_file = Path(self.temp_dir.name) / "test_config.yml"

def tearDown(self):
    self.temp_dir.cleanup()

Mock Objects

Tests use Python's unittest.mock for external dependencies:

@patch('platform.system', return_value='Linux')
def test_platform_detection(self, mock_platform):
    installer = AgnosticInstaller()
    self.assertEqual(installer.platform, 'linux')

Configuration Templates

Tests create realistic YAML configurations:

config_data = {
    'package': {
        'name': 'test-package',
        'version': '1.0.0',
        'description': 'Test package description'
    },
    'files': [
        {
            'src': 'test.txt',
            'dest': '/tmp/test.txt',
            'permissions': '644'
        }
    ]
}

Test Coverage Areas

Platform Compatibility

  • ✅ Linux (Ubuntu/Debian)
  • ✅ Windows (cross-platform testing via mocks)
  • ✅ macOS (future implementation)

Feature Coverage

  • ✅ Configuration parsing and validation
  • ✅ File operations and permissions
  • ✅ Service installation (systemd)
  • ✅ Dependency resolution
  • ✅ Environment variable management
  • ⚠️ Windows registry operations (mocked)
  • ⚠️ Desktop shortcuts (mocked)
  • ⚠️ macOS launchd services (future)

Error Scenarios

  • Invalid configuration files
  • Missing source files
  • Permission denied during installation
  • Service installation failures
  • Platform incompatibility

Test Maintenance

Adding New Tests

  1. Identify test category: unit, integration, or functional
  2. Create test method: Use descriptive naming
  3. Setup test data: Use temporary files/directories
  4. Write assertions: Check expected vs actual behavior
  5. Mock external dependencies: Use unittest.mock for isolation
def test_new_feature(self):
    """Test description of new feature."""
    # Setup
    config = create_test_config()

    # Execute
    result = installer.new_feature(config)

    # Assert
    self.assertTrue(result.success)
    self.assertEqual(result.output, "expected")

Updating Test Configurations

When adding new features, update test configurations:

# New configuration options
files:
  - src: "new-file"
    dest: "/opt/app/new-file"
    permissions: "755"
    ownership: "appuser:appgroup"  # New feature

services:
  - name: "new-service"
    exec: "/opt/app/new-file"
    restart_on_failure: true  # New feature

Cross-Platform Testing

For Windows/macOS features, use mock objects:

@patch('core.services_manage.ServicesManager._install_windows_service')
def test_windows_service_install(self, mock_install):
    installer = AgnosticInstaller()
    installer.platform = 'windows'
    result = installer.install_service(service_config)
    mock_install.assert_called_once()

Troubleshooting Tests

Common Issues

Permission Errors

# Run tests with elevated privileges (careful!)
sudo python3 tests/test_functional.py

# Or mock permission-related operations

Platform-Specific Failures

# Check platform detection
python3 -c "from core.platform_detect import PlatformDetect; pd = PlatformDetect(); print(pd.detect_platform())"

# Mock platform for testing

Configuration Loading Issues

# Validate test configuration files
python3 -c "
import yaml
with open('tests/config.yml') as f:
    config = yaml.safe_load(f)
    print('Config loaded successfully')
"

Debug Test Execution

# Enable verbose logging in tests
installer = AgnosticInstaller(verbose=True, log_file='/tmp/test.log')

# Check log output
tail -f /tmp/test.log

Performance Testing

For performance-critical code:

import time

def test_installation_performance(self):
    start_time = time.time()
    installer.install_package(config_path)
    end_time = time.time()
    self.assertLess(end_time - start_time, 30.0)  # Must complete within 30 seconds

Future Test Enhancements

Areas for Expansion

  1. GUI Testing: Test graphical installer interface (future)
  2. Network Testing: Test remote package repositories
  3. Load Testing: Test with large configuration files
  4. Stress Testing: Test system resource limits
  5. Docker Integration: Test within containerized environments
  6. macOS Testing: Native macOS testing environment
  7. Windows Testing: Native Windows testing environment

Automated Testing Pipeline

# Enhanced CI/CD with multiple platforms
jobs:
  test-linux:
    runs-on: ubuntu-latest
  test-windows:
    runs-on: windows-latest
  test-macos:
    runs-on: macos-latest

Test Results Interpretation

Exit Codes

  • 0: All tests passed
  • 1: One or more tests failed
  • 2: Test execution error

Coverage Report

# Generate HTML coverage report
coverage html

# Open in browser
xdg-open htmlcov/index.html

Performance Metrics

  • Test Execution Time: Should complete within 2 minutes
  • Code Coverage: Target >85% coverage
  • Platform Compatibility: Must pass on all supported platforms

This comprehensive testing approach ensures reliability, cross-platform compatibility, and maintainability of the Agnostic OS Installer.