#!/usr/bin/env python3 """ Test Debian Atomic Blueprint Generator for Debian Forge This script tests the enhanced blueprint generation system for Debian atomic images. """ import json import os import sys import tempfile from pathlib import Path # Add current directory to Python path sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) def test_blueprint_generator_import(): """Test importing the blueprint generator""" print("Testing blueprint generator import...") try: from debian_atomic_blueprint_generator import DebianAtomicBlueprintGenerator, AtomicBlueprintConfig print(" ✅ Blueprint generator imported successfully") return True except ImportError as e: print(f" ❌ Failed to import blueprint generator: {e}") return False def test_atomic_blueprint_config(): """Test AtomicBlueprintConfig dataclass""" print("\nTesting AtomicBlueprintConfig dataclass...") try: from debian_atomic_blueprint_generator import AtomicBlueprintConfig config = AtomicBlueprintConfig( name="test-config", description="Test configuration", version="1.0.0", base_packages=["systemd", "ostree"] ) if config.name != "test-config": print(" ❌ Config name not set correctly") return False if len(config.base_packages) != 2: print(" ❌ Base packages not set correctly") return False print(" ✅ AtomicBlueprintConfig works correctly") return True except Exception as e: print(f" ❌ AtomicBlueprintConfig test failed: {e}") return False def test_blueprint_generator_initialization(): """Test blueprint generator initialization""" print("\nTesting blueprint generator initialization...") try: from debian_atomic_blueprint_generator import DebianAtomicBlueprintGenerator generator = DebianAtomicBlueprintGenerator() if not hasattr(generator, 'base_packages'): print(" ❌ Base packages not initialized") return False if len(generator.base_packages) == 0: print(" ❌ No base packages defined") return False print(" ✅ Blueprint generator initialization works correctly") return True except Exception as e: print(f" ❌ Blueprint generator initialization test failed: {e}") return False def test_base_blueprint_generation(): """Test base blueprint generation""" print("\nTesting base blueprint generation...") try: from debian_atomic_blueprint_generator import DebianAtomicBlueprintGenerator generator = DebianAtomicBlueprintGenerator() blueprint = generator.generate_base_blueprint() # Check required fields required_fields = ["name", "description", "version", "packages"] for field in required_fields: if field not in blueprint: print(f" ❌ Missing required field: {field}") return False # Check packages if not blueprint["packages"]: print(" ❌ No packages in blueprint") return False # Check customizations if "customizations" not in blueprint: print(" ❌ No customizations in blueprint") return False print(" ✅ Base blueprint generation works correctly") return True except Exception as e: print(f" ❌ Base blueprint generation test failed: {e}") return False def test_specialized_blueprint_generation(): """Test specialized blueprint generation""" print("\nTesting specialized blueprint generation...") try: from debian_atomic_blueprint_generator import DebianAtomicBlueprintGenerator generator = DebianAtomicBlueprintGenerator() # Test workstation blueprint workstation = generator.generate_workstation_blueprint() if workstation["name"] != "debian-atomic-workstation": print(" ❌ Workstation blueprint name incorrect") return False # Test server blueprint server = generator.generate_server_blueprint() if server["name"] != "debian-atomic-server": print(" ❌ Server blueprint name incorrect") return False # Test container blueprint container = generator.generate_container_blueprint() if container["name"] != "debian-atomic-container": print(" ❌ Container blueprint name incorrect") return False # Test minimal blueprint minimal = generator.generate_minimal_blueprint() if minimal["name"] != "debian-atomic-minimal": print(" ❌ Minimal blueprint name incorrect") return False print(" ✅ Specialized blueprint generation works correctly") return True except Exception as e: print(f" ❌ Specialized blueprint generation test failed: {e}") return False def test_osbuild_manifest_generation(): """Test OSBuild manifest generation""" print("\nTesting OSBuild manifest generation...") try: from debian_atomic_blueprint_generator import DebianAtomicBlueprintGenerator generator = DebianAtomicBlueprintGenerator() blueprint = generator.generate_base_blueprint() manifest = generator.generate_osbuild_manifest(blueprint) # Check manifest structure if "version" not in manifest: print(" ❌ Manifest missing version") return False if "pipelines" not in manifest: print(" ❌ Manifest missing pipelines") return False if len(manifest["pipelines"]) == 0: print(" ❌ No pipelines in manifest") return False # Check stages build_pipeline = manifest["pipelines"][0] if "stages" not in build_pipeline: print(" ❌ Build pipeline missing stages") return False stage_types = [stage["type"] for stage in build_pipeline["stages"]] expected_stages = ["org.osbuild.debootstrap", "org.osbuild.apt", "org.osbuild.ostree.commit"] for expected in expected_stages: if expected not in stage_types: print(f" ❌ Missing expected stage: {expected}") return False print(" ✅ OSBuild manifest generation works correctly") return True except Exception as e: print(f" ❌ OSBuild manifest generation test failed: {e}") return False def test_blueprint_validation(): """Test blueprint validation""" print("\nTesting blueprint validation...") try: from debian_atomic_blueprint_generator import DebianAtomicBlueprintGenerator generator = DebianAtomicBlueprintGenerator() # Test valid blueprint valid_blueprint = generator.generate_base_blueprint() validation = generator.validate_blueprint(valid_blueprint) if not validation["valid"]: print(f" ❌ Valid blueprint marked as invalid: {validation['errors']}") return False # Test invalid blueprint (missing required fields) invalid_blueprint = {"name": "test"} invalid_validation = generator.validate_blueprint(invalid_blueprint) if invalid_validation["valid"]: print(" ❌ Invalid blueprint marked as valid") return False print(" ✅ Blueprint validation works correctly") return True except Exception as e: print(f" ❌ Blueprint validation test failed: {e}") return False def test_blueprint_save_load(): """Test blueprint save and load""" print("\nTesting blueprint save and load...") try: from debian_atomic_blueprint_generator import DebianAtomicBlueprintGenerator generator = DebianAtomicBlueprintGenerator() blueprint = generator.generate_base_blueprint() with tempfile.TemporaryDirectory() as temp_dir: # Test save saved_path = generator.save_blueprint(blueprint, temp_dir) if not os.path.exists(saved_path): print(" ❌ Blueprint file not saved") return False # Test load with open(saved_path, 'r') as f: loaded_blueprint = json.load(f) if loaded_blueprint["name"] != blueprint["name"]: print(" ❌ Loaded blueprint name doesn't match") return False if len(loaded_blueprint["packages"]) != len(blueprint["packages"]): print(" ❌ Loaded blueprint packages don't match") return False print(" ✅ Blueprint save and load works correctly") return True except Exception as e: print(f" ❌ Blueprint save and load test failed: {e}") return False def test_all_blueprints_generation(): """Test generation of all blueprint types""" print("\nTesting all blueprints generation...") try: from debian_atomic_blueprint_generator import DebianAtomicBlueprintGenerator generator = DebianAtomicBlueprintGenerator() with tempfile.TemporaryDirectory() as temp_dir: saved_files = generator.generate_all_blueprints(temp_dir) if len(saved_files) == 0: print(" ❌ No blueprints generated") return False # Check if all files exist for file_path in saved_files: if not os.path.exists(file_path): print(f" ❌ Blueprint file not found: {file_path}") return False # Check expected blueprint types expected_types = ["base", "workstation", "server", "container", "minimal"] found_types = [] for file_path in saved_files: filename = Path(file_path).stem for bp_type in expected_types: if bp_type in filename: found_types.append(bp_type) break if len(found_types) != len(expected_types): print(f" ❌ Expected {len(expected_types)} blueprint types, found {len(found_types)}") return False print(" ✅ All blueprints generation works correctly") return True except Exception as e: print(f" ❌ All blueprints generation test failed: {e}") return False def main(): """Main test function""" print("Debian Atomic Blueprint Generator Test for Debian Forge") print("=" * 60) tests = [ ("Blueprint Generator Import", test_blueprint_generator_import), ("AtomicBlueprintConfig", test_atomic_blueprint_config), ("Blueprint Generator Initialization", test_blueprint_generator_initialization), ("Base Blueprint Generation", test_base_blueprint_generation), ("Specialized Blueprint Generation", test_specialized_blueprint_generation), ("OSBuild Manifest Generation", test_osbuild_manifest_generation), ("Blueprint Validation", test_blueprint_validation), ("Blueprint Save and Load", test_blueprint_save_load), ("All Blueprints Generation", test_all_blueprints_generation) ] results = [] for test_name, test_func in tests: try: result = test_func() results.append((test_name, result)) except Exception as e: print(f" ❌ {test_name} test failed with exception: {e}") results.append((test_name, False)) # Summary print("\n" + "=" * 60) print("TEST SUMMARY") print("=" * 60) passed = 0 total = len(results) for test_name, result in results: status = "✅ PASS" if result else "❌ FAIL" print(f"{test_name}: {status}") if result: passed += 1 print(f"\nOverall: {passed}/{total} tests passed") if passed == total: print("🎉 All tests passed! Debian atomic blueprint generator is ready.") return 0 else: print("⚠️ Some tests failed. Please review the issues above.") return 1 if __name__ == '__main__': sys.exit(main())