#!/usr/bin/env python3 """ Test script for the APT solver implementation """ import os import sys import tempfile import shutil from pathlib import Path # Add the project root to the Python path sys.path.insert(0, str(Path(__file__).parent.parent)) from osbuild.solver.apt import APT from osbuild.solver import RepoError, NoReposError, DepsolveError def test_apt_solver_basic(): """Test basic APT solver functionality.""" print("๐Ÿงช Testing APT solver basic functionality...") # Create a temporary directory for testing with tempfile.TemporaryDirectory() as temp_dir: # Test configuration request = { "arch": "amd64", "releasever": "trixie", "arguments": { "repos": [ { "name": "debian-main", "baseurl": "http://deb.debian.org/debian", "enabled": True, "gpgcheck": False, # Skip GPG for testing "components": ["main"], "architectures": ["amd64"], } ], "root_dir": temp_dir, } } try: # Initialize APT solver solver = APT(request, temp_dir, temp_dir) print("โœ… APT solver initialized successfully") # Test dump functionality dump_result = solver.dump() print(f"โœ… Dump result: {len(dump_result.get('packages', []))} packages found") # Test search functionality search_result = solver.search({"query": "apt", "match_type": "name", "limit": 5}) print(f"โœ… Search result: {len(search_result)} packages found") # Test dependency resolution (this might fail in test environment) try: depsolve_result = solver.depsolve({"packages": ["apt"]}) print(f"โœ… Depsolve result: {len(depsolve_result)} packages resolved") except DepsolveError as e: print(f"โš ๏ธ Depsolve failed (expected in test environment): {e}") print("โœ… Basic APT solver test completed successfully") return True except Exception as e: print(f"โŒ APT solver test failed: {e}") return False def test_apt_solver_error_handling(): """Test APT solver error handling.""" print("๐Ÿงช Testing APT solver error handling...") # Test with no repositories try: request = { "arch": "amd64", "arguments": {"repos": []} } solver = APT(request, "/tmp", "/tmp") print("โŒ Should have raised NoReposError") return False except NoReposError: print("โœ… Correctly raised NoReposError for no repositories") except Exception as e: print(f"โŒ Unexpected error: {e}") return False # Test with invalid repository try: request = { "arch": "amd64", "arguments": { "repos": [{"name": "invalid", "baseurl": ""}] } } solver = APT(request, "/tmp", "/tmp") print("โŒ Should have raised RepoError") return False except RepoError: print("โœ… Correctly raised RepoError for invalid repository") except Exception as e: print(f"โŒ Unexpected error: {e}") return False print("โœ… Error handling test completed successfully") return True def test_apt_solver_configuration(): """Test APT solver configuration options.""" print("๐Ÿงช Testing APT solver configuration...") with tempfile.TemporaryDirectory() as temp_dir: # Test with proxy configuration request = { "arch": "amd64", "releasever": "trixie", "proxy": "http://proxy.example.com:8080", "arguments": { "repos": [ { "name": "debian-main", "baseurl": "http://deb.debian.org/debian", "enabled": True, "gpgcheck": False, "components": ["main"], "architectures": ["amd64"], } ], "root_dir": temp_dir, } } try: solver = APT(request, temp_dir, temp_dir) # Check if proxy configuration is set if "http://proxy.example.com:8080" in str(solver.apt_config): print("โœ… Proxy configuration applied correctly") else: print("โš ๏ธ Proxy configuration not found in apt_config") # Check architecture configuration if solver.arch == "amd64": print("โœ… Architecture configuration correct") else: print(f"โŒ Architecture configuration incorrect: {solver.arch}") return False # Check releasever configuration if solver.releasever == "trixie": print("โœ… Releasever configuration correct") else: print(f"โŒ Releasever configuration incorrect: {solver.releasever}") return False print("โœ… Configuration test completed successfully") return True except Exception as e: print(f"โŒ Configuration test failed: {e}") return False def main(): """Run all APT solver tests.""" print("๐Ÿš€ Starting APT solver tests...") print("=" * 50) tests = [ test_apt_solver_basic, test_apt_solver_error_handling, test_apt_solver_configuration, ] passed = 0 total = len(tests) for test in tests: try: if test(): passed += 1 except Exception as e: print(f"โŒ Test {test.__name__} failed with exception: {e}") print("-" * 30) print(f"๐Ÿ“Š Test Results: {passed}/{total} tests passed") if passed == total: print("๐ŸŽ‰ All APT solver tests passed!") return 0 else: print("โŒ Some APT solver tests failed!") return 1 if __name__ == "__main__": sys.exit(main())