From 3e67d946c68dafa54de8e00a06695b6b4d73f775 Mon Sep 17 00:00:00 2001 From: lucannez64 <58551020+lucannez64@users.noreply.github.com> Date: Sun, 18 Oct 2026 13:54:18 +0200 Subject: [PATCH] test(nix): cover region routing, stable readiness and lost terminals --- tests/test_installer_regions.py | 33 ++++++++++++++++++++++++++++++ tests/test_nix_lifecycle.py | 21 ++++++++++++++++++- tests/test_nix_runtime_identity.py | 33 ++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tests/test_installer_regions.py diff --git a/tests/test_installer_regions.py b/tests/test_installer_regions.py new file mode 100644 index 0000000..5d06082 --- /dev/null +++ b/tests/test_installer_regions.py @@ -0,0 +1,33 @@ +"""Stop at a stub fetcher: verify installer routing without network or runtime use.""" +import os +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] + + +class InstallerRegions(unittest.TestCase): + def test_aliases_and_platform_codes_reach_the_expected_cdn_region(self): + with tempfile.TemporaryDirectory() as folder: + root = Path(folder) + (root / 'scripts').mkdir() + (root / 'build/runtime/tools').mkdir(parents=True) + shutil.copy(ROOT / 'scripts/installer.sh', root / 'scripts') + (root / 'scripts/configure.py').write_text('') + (root / 'build/runtime/tools/fetch-installer.sh').write_text( + 'printf "%s" "$1" > "$TEST_REGION_LOG"\nexit 23\n') + log = root / 'region' + env = dict(os.environ, TEST_REGION_LOG=str(log)) + env.pop('DARLING_WORKSPACE', None) + for region, expected in [('lan', 'la1'), ('las', 'la2'), ('oce', 'oc1'), + ('la1', 'la1'), ('la2', 'la2'), ('oc1', 'oc1'), + ('euw', 'euw')]: + with self.subTest(region=region): + log.unlink(missing_ok=True) + result = subprocess.run(['sh', str(root / 'scripts/installer.sh'), region], + env=env, capture_output=True, text=True, timeout=10) + self.assertEqual(result.returncode, 23, result.stderr) + self.assertEqual(log.read_text(), expected) diff --git a/tests/test_nix_lifecycle.py b/tests/test_nix_lifecycle.py index 72ebd3e..f339e07 100644 --- a/tests/test_nix_lifecycle.py +++ b/tests/test_nix_lifecycle.py @@ -6,7 +6,7 @@ import shutil import subprocess import tempfile import unittest -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch ROOT = Path(__file__).resolve().parents[1] @@ -88,3 +88,22 @@ exit "${CLIENT_RC:-0}" self.assertIn('23', result.stderr) self.assertEqual(self.log.read_text().splitlines().count('shutdown:shutdown'), 2) self.assertIn('shutdown:shutdown', self.log.read_text()) + + def test_shutdown_retries_when_terminal_diagnostics_fail(self): + spec = importlib.util.spec_from_file_location('lifecycle_fixture', self.root / 'scripts/nix-lifecycle.py') + lifecycle = importlib.util.module_from_spec(spec) + spec.loader.exec_module(lifecycle) + child = MagicMock() + child.wait.return_value = 17 + stderr = MagicMock() + stderr.write.side_effect = OSError('terminal disconnected') + with patch.dict(os.environ, self.env), \ + patch.object(lifecycle.signal, 'signal'), \ + patch.object(lifecycle.subprocess, 'Popen', return_value=child), \ + patch.object(lifecycle.subprocess, 'run', side_effect=[ + subprocess.CompletedProcess([], 23), subprocess.CompletedProcess([], 0)]) as shutdown, \ + patch.object(lifecycle.time, 'sleep'), \ + patch.object(lifecycle.sys, 'stderr', stderr): + self.assertEqual(lifecycle.main(), 17) + self.assertEqual(shutdown.call_count, 2) + stderr.write.assert_called() diff --git a/tests/test_nix_runtime_identity.py b/tests/test_nix_runtime_identity.py index e292085..e58448c 100644 --- a/tests/test_nix_runtime_identity.py +++ b/tests/test_nix_runtime_identity.py @@ -106,6 +106,39 @@ class RuntimeIdentityTests(unittest.TestCase): env.pop('DPREFIX') self.assertEqual(self.state('check', env).returncode, 0) + def test_inventory_ignores_changes_inside_backup_directories(self): + spec = importlib.util.spec_from_file_location('inventory_fixture', self.root / 'scripts/nix-state.py') + state = importlib.util.module_from_spec(spec) + spec.loader.exec_module(state) + runtime = self.root / 'usr-local/libexec/darling' + for suffix in ('.before-test', '.rt-test', '.llvm-test'): + backup = runtime / ('backup' + suffix) / 'nested' + backup.mkdir(parents=True) + saved = backup / 'saved.dylib' + saved.write_text('old backup') + before = state.inventory(runtime) + saved.write_text('changed backup') + self.assertEqual(state.inventory(runtime), before) + + def test_inventory_is_independent_of_filesystem_directory_order(self): + from unittest.mock import patch + spec = importlib.util.spec_from_file_location('inventory_fixture', self.root / 'scripts/nix-state.py') + state = importlib.util.module_from_spec(spec) + spec.loader.exec_module(state) + runtime = self.root / 'usr-local/libexec/darling' + for name in ('a', 'b'): + folder = runtime / name + folder.mkdir() + (folder / 'file').write_text(name) + original_walk = os.walk + def reversed_walk(*args, **kwargs): + for directory, dirs, files in original_walk(*args, **kwargs): + dirs.reverse() + yield directory, dirs, files + before = state.inventory(runtime) + with patch.object(state.os, 'walk', reversed_walk): + self.assertEqual(state.inventory(runtime), before) + if __name__ == '__main__': unittest.main()