from __future__ import annotations import os import unittest from pathlib import Path from types import SimpleNamespace from unittest.mock import patch from uvm.config import Settings from uvm.errors import ConfigurationError class ArchitectureSettingsTests(unittest.TestCase): def test_resolves_native_defaults_when_settings_are_created(self) -> None: for machine, architecture in ( ("x86_64", "x86_64"), ("AMD64", "x86_64"), ("aarch64", "aarch64"), ("arm64", "aarch64"), ): for factory in (Settings, Settings.from_environment): with ( self.subTest(machine=machine, factory=factory), patch.dict(os.environ, {}, clear=True), patch("uvm.config.os.uname", return_value=SimpleNamespace(machine=machine)), ): settings = factory() base = ( "https://s3.amazonaws.com/spec.ccfc.min/img/quickstart_guide/" f"{architecture}" ) self.assertEqual(settings.architecture, architecture) self.assertEqual(settings.kernel_url, f"{base}/kernels/vmlinux.bin") self.assertEqual(settings.rootfs_url, f"{base}/rootfs/bionic.rootfs.ext4") def test_preserves_individual_environment_url_overrides_on_arm64(self) -> None: kernel = "https://example.com/custom-arm64-kernel" rootfs = "https://example.com/custom-arm64-rootfs.ext4" base = "https://s3.amazonaws.com/spec.ccfc.min/img/quickstart_guide/aarch64" for overrides in ( {"UVM_KERNEL_URL": kernel}, {"UVM_ROOTFS_URL": rootfs}, {"UVM_KERNEL_URL": kernel, "UVM_ROOTFS_URL": rootfs}, ): with ( self.subTest(overrides=overrides), patch.dict(os.environ, overrides, clear=True), patch("uvm.config.os.uname", return_value=SimpleNamespace(machine="aarch64")), ): settings = Settings.from_environment() self.assertEqual( settings.kernel_url, overrides.get("UVM_KERNEL_URL", f"{base}/kernels/vmlinux.bin"), ) self.assertEqual( settings.rootfs_url, overrides.get("UVM_ROOTFS_URL", f"{base}/rootfs/bionic.rootfs.ext4"), ) def test_rejects_unsupported_hosts_even_with_custom_guest_assets(self) -> None: with ( patch.dict(os.environ, { "UVM_KERNEL_URL": "https://example.com/kernel", "UVM_ROOTFS_URL": "https://example.com/rootfs.ext4", }, clear=True), patch("uvm.config.os.uname", return_value=SimpleNamespace(machine="riscv64")), self.assertRaisesRegex(ConfigurationError, "unsupported host architecture: riscv64"), ): Settings.from_environment() class KnownHostsSettingsTests(unittest.TestCase): def test_known_hosts_files_are_loaded_as_explicit_absolute_paths(self) -> None: with patch.dict(os.environ, { "UVM_KNOWN_HOSTS_FILES": "/root/.ssh/known_hosts:/home/operator/.ssh/known_hosts", }, clear=True): settings = Settings.from_environment() self.assertEqual(settings.known_hosts_files, ( Path("/root/.ssh/known_hosts"), Path("/home/operator/.ssh/known_hosts"), )) with patch.dict(os.environ, {}, clear=True): self.assertIsNone(Settings.from_environment().known_hosts_files) def test_relative_or_empty_path_entries_are_rejected(self) -> None: for value in ("known_hosts", "/tmp/known_hosts:", ":/tmp/known_hosts"): with self.subTest(value=value), patch.dict(os.environ, {"UVM_KNOWN_HOSTS_FILES": value}, clear=True): with self.assertRaisesRegex(ConfigurationError, "UVM_KNOWN_HOSTS_FILES"): Settings.from_environment()