#!/usr/bin/env python3 """ Simple test of deb-mock API without requiring sbuild """ import sys import os from unittest.mock import Mock, patch # Add deb-mock to path sys.path.insert(0, os.path.join(os.path.dirname(__file__))) def test_api_imports(): """Test that all API components can be imported""" print("Testing API imports...") try: from deb_mock.api import MockAPIClient, MockEnvironment, MockConfigBuilder, create_client from deb_mock.environment_manager import EnvironmentManager, EnvironmentInfo, BuildResult from deb_mock.config import Config print("✅ All API imports successful") return True except ImportError as e: print(f"❌ Import error: {e}") return False def test_config_builder(): """Test the configuration builder""" print("\nTesting configuration builder...") try: from deb_mock.api import MockConfigBuilder config = (MockConfigBuilder() .environment("test-env") .architecture("amd64") .suite("trixie") .packages(["build-essential"]) .cache_enabled(True) .verbose(True) .build()) assert config.chroot_name == "test-env" assert config.architecture == "amd64" assert config.suite == "trixie" assert config.chroot_additional_packages == ["build-essential"] assert config.use_root_cache == True assert config.verbose == True print("✅ Configuration builder works correctly") return True except Exception as e: print(f"❌ Configuration builder error: {e}") return False def test_api_without_sbuild(): """Test API components without sbuild dependency""" print("\nTesting API without sbuild...") try: from deb_mock.api import MockConfigBuilder, create_client from deb_mock.config import Config # Create a minimal config that doesn't require sbuild config = Config( chroot_name="test-env", architecture="amd64", suite="trixie", output_dir="/tmp/test-output", chroot_dir="/tmp/test-chroots", chroot_config_dir="/tmp/test-config" ) # Mock the DebMock class to avoid sbuild dependency with patch('deb_mock.api.DebMock') as mock_deb_mock: mock_instance = Mock() mock_deb_mock.return_value = mock_instance # Mock chroot manager mock_chroot_manager = Mock() mock_instance.chroot_manager = mock_chroot_manager mock_chroot_manager.chroot_exists.return_value = False mock_chroot_manager.get_chroot_info.return_value = { 'name': 'test-env', 'status': 'active', 'size': 1024 } # Mock other methods mock_instance.init_chroot = Mock() mock_instance.install_packages = Mock(return_value={'success': True}) mock_instance.clean_chroot = Mock() mock_instance.list_chroots = Mock(return_value=[]) mock_instance.build = Mock(return_value={'success': True, 'artifacts': []}) mock_instance.get_cache_stats = Mock(return_value={}) # Create client client = create_client(config) # Test basic operations assert client.config.chroot_name == "test-env" # Test environment creation (mocked) env = client.create_environment("test-env", "amd64", "trixie", ["build-essential"]) assert env.name == "test-env" # Test environment listing environments = client.list_environments() assert isinstance(environments, list) print("✅ API works correctly with mocked dependencies") return True except Exception as e: print(f"❌ API test error: {e}") import traceback traceback.print_exc() return False def test_plugin_system(): """Test plugin system components""" print("\nTesting plugin system...") try: from deb_mock.plugin import BasePlugin, HookStages, PluginManager from deb_mock.plugins.registry import PluginRegistry, PluginInfo # Test plugin registry registry = PluginRegistry() assert isinstance(registry, PluginRegistry) # Test hook stages assert hasattr(HookStages, 'PREBUILD') assert hasattr(HookStages, 'POSTBUILD') assert hasattr(HookStages, 'ON_ERROR') # Test plugin info plugin_info = PluginInfo( name="test_plugin", version="1.0.0", description="Test plugin", author="Test Author", requires_api_version="1.0", plugin_class=BasePlugin, init_function=lambda x, y, z: None, file_path="/tmp/test.py", loaded_at=None ) assert plugin_info.name == "test_plugin" assert plugin_info.version == "1.0.0" print("✅ Plugin system components work correctly") return True except Exception as e: print(f"❌ Plugin system error: {e}") return False def test_environment_manager(): """Test environment manager components""" print("\nTesting environment manager...") try: from deb_mock.environment_manager import EnvironmentManager, EnvironmentInfo, BuildResult from deb_mock.config import Config # Test data classes env_info = EnvironmentInfo( name="test-env", architecture="amd64", suite="trixie", status="active" ) assert env_info.name == "test-env" assert env_info.architecture == "amd64" build_result = BuildResult( success=True, artifacts=["test.deb"], output_dir="/tmp", log_file="/tmp/build.log", metadata={"package": "test"} ) assert build_result.success == True assert len(build_result.artifacts) == 1 print("✅ Environment manager components work correctly") return True except Exception as e: print(f"❌ Environment manager error: {e}") return False def main(): """Run all tests""" print("deb-mock API Simple Test") print("=" * 50) tests = [ test_api_imports, test_config_builder, test_api_without_sbuild, test_plugin_system, test_environment_manager ] passed = 0 total = len(tests) for test in tests: if test(): passed += 1 print(f"\n{'=' * 50}") print(f"Test Results: {passed}/{total} tests passed") if passed == total: print("🎉 All tests passed! The deb-mock API is working correctly.") return 0 else: print("❌ Some tests failed. Please check the errors above.") return 1 if __name__ == "__main__": sys.exit(main())