#!/usr/bin/python3 """ Test script for the apt stage in Debian Forge This script tests the apt stage with apt-cacher-ng integration. """ import os import sys import tempfile import shutil import subprocess from pathlib import Path def test_apt_proxy_config(): """Test apt proxy configuration""" print("Testing apt proxy configuration...") with tempfile.TemporaryDirectory() as temp_dir: # Create minimal structure apt_conf_dir = os.path.join(temp_dir, "etc/apt/apt.conf.d") os.makedirs(apt_conf_dir, exist_ok=True) # Test proxy configuration proxy_config = """Acquire::http::Proxy "192.168.1.101:3142"; Acquire::https::Proxy "192.168.1.101:3142"; """ proxy_file = os.path.join(apt_conf_dir, "99proxy") with open(proxy_file, "w") as f: f.write(proxy_config) # Verify proxy configuration if os.path.exists(proxy_file): with open(proxy_file, "r") as f: content = f.read() if "192.168.1.101:3142" in content: print("✅ Apt proxy configuration test passed") return True else: print("❌ Proxy configuration content mismatch") return False else: print("❌ Proxy configuration file not created") return False def test_debootstrap_availability(): """Test if debootstrap is available or can be installed""" print("Testing debootstrap availability...") try: # Check if debootstrap is available result = subprocess.run(["which", "debootstrap"], capture_output=True, text=True) if result.returncode == 0: print("✅ debootstrap is available") # Check debootstrap version version_result = subprocess.run(["debootstrap", "--version"], capture_output=True, text=True) if version_result.returncode == 0: print(f"✅ debootstrap version: {version_result.stdout.strip()}") return True else: print("❌ Failed to get debootstrap version") return False else: print("⚠️ debootstrap not found in PATH") print(" This is expected on non-Debian systems") print(" debootstrap will be installed during build environment setup") return True # Not a failure, just not available yet except Exception as e: print(f"❌ debootstrap test failed: {e}") return False def test_ostree_integration(): """Test OSTree integration""" print("Testing OSTree integration...") try: # Check if ostree is available result = subprocess.run(["which", "ostree"], capture_output=True, text=True) if result.returncode == 0: print("✅ ostree is available") # Check ostree version version_result = subprocess.run(["ostree", "--version"], capture_output=True, text=True) if version_result.returncode == 0: print(f"✅ ostree version: {version_result.stdout.strip()}") return True else: print("❌ Failed to get ostree version") return False else: print("❌ ostree not found in PATH") return False except Exception as e: print(f"❌ ostree test failed: {e}") return False def test_apt_cacher_ng_connectivity(): """Test connectivity to apt-cacher-ng""" print("Testing apt-cacher-ng connectivity...") try: # Test if we can connect to the apt-cacher-ng server import socket host, port = "192.168.1.101", 3142 # Create a socket and try to connect sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(5) # 5 second timeout result = sock.connect_ex((host, port)) sock.close() if result == 0: print(f"✅ Successfully connected to apt-cacher-ng at {host}:{port}") return True else: print(f"⚠️ Cannot connect to apt-cacher-ng at {host}:{port}") print(" This is expected if apt-cacher-ng is not running") print(" Use setup-apt-cacher.sh to start the service") return True # Not a failure, just not running except Exception as e: print(f"❌ apt-cacher-ng connectivity test failed: {e}") return False def test_stage_file_structure(): """Test that stage files have correct structure""" print("Testing stage file structure...") stage_files = [ "stages/org.osbuild.apt.py", "stages/org.osbuild.debootstrap.py", "stages/org.osbuild.ostree.commit.py", "stages/org.osbuild.ostree.deploy.py", "stages/org.osbuild.sbuild.py", "stages/org.osbuild.debian.source.py" ] all_exist = True for stage_file in stage_files: if os.path.exists(stage_file): print(f"✅ {stage_file} exists") else: print(f"❌ {stage_file} missing") all_exist = False if all_exist: print("✅ All Debian stage files are present") return True else: print("❌ Some Debian stage files are missing") return False def test_metadata_files(): """Test that metadata files exist for stages""" print("Testing metadata files...") metadata_files = [ "stages/org.osbuild.apt.meta.json", "stages/org.osbuild.debootstrap.meta.json", "stages/org.osbuild.ostree.commit.meta.json", "stages/org.osbuild.ostree.deploy.meta.json", "stages/org.osbuild.sbuild.meta.json", "stages/org.osbuild.debian.source.meta.json" ] all_exist = True for meta_file in metadata_files: if os.path.exists(meta_file): print(f"✅ {meta_file} exists") else: print(f"❌ {meta_file} missing") all_exist = False if all_exist: print("✅ All metadata files are present") return True else: print("❌ Some metadata files are missing") return False def main(): """Run all tests""" print("Debian Forge Apt Stage Tests") print("=" * 40) tests = [ test_apt_proxy_config, test_debootstrap_availability, test_ostree_integration, test_apt_cacher_ng_connectivity, test_stage_file_structure, test_metadata_files ] passed = 0 total = len(tests) for test in tests: try: print(f"\nRunning {test.__name__}...") if test(): passed += 1 else: print(f"❌ {test.__name__} failed") except Exception as e: print(f"❌ {test.__name__} failed with exception: {e}") print() print("=" * 40) print(f"Test Results: {passed}/{total} passed") if passed == total: print("🎉 All apt stage tests passed!") return 0 else: print("⚠️ Some tests failed") return 1 if __name__ == "__main__": sys.exit(main())