|
| 1 | +"""Tests for stable filesystem quota validation and forwarding.""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from hyperlight_sandbox import CodeExecutionTool, Sandbox, SandboxEnvironment |
| 7 | + |
| 8 | + |
| 9 | +class _FakeNativeSandbox: |
| 10 | + calls: list[dict] = [] |
| 11 | + |
| 12 | + def __init__(self, **kwargs): |
| 13 | + self.calls.append(kwargs) |
| 14 | + |
| 15 | + def register_tool(self, *args): |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +class TestFilesystemLimitForwarding(unittest.TestCase): |
| 20 | + def setUp(self): |
| 21 | + _FakeNativeSandbox.calls.clear() |
| 22 | + |
| 23 | + def test_omitted_limits_leave_backend_defaults_unchanged(self): |
| 24 | + with patch( |
| 25 | + "hyperlight_sandbox._load_backend", |
| 26 | + return_value=("hyperlight-js", _FakeNativeSandbox), |
| 27 | + ): |
| 28 | + Sandbox(backend="hyperlight-js") |
| 29 | + |
| 30 | + kwargs = _FakeNativeSandbox.calls[-1] |
| 31 | + self.assertNotIn("filesystem_limits", kwargs) |
| 32 | + self.assertNotIn("max_file_size", kwargs) |
| 33 | + self.assertNotIn("max_total_size", kwargs) |
| 34 | + self.assertNotIn("max_file_count", kwargs) |
| 35 | + |
| 36 | + def test_custom_limits_and_zero_are_forwarded(self): |
| 37 | + with patch( |
| 38 | + "hyperlight_sandbox._load_backend", |
| 39 | + return_value=("hyperlight-js", _FakeNativeSandbox), |
| 40 | + ): |
| 41 | + Sandbox( |
| 42 | + backend="hyperlight-js", |
| 43 | + max_file_size="8Mi", |
| 44 | + max_total_size="20Mi", |
| 45 | + max_file_count=0, |
| 46 | + ) |
| 47 | + |
| 48 | + self.assertEqual(_FakeNativeSandbox.calls[-1]["max_file_size"], "8Mi") |
| 49 | + self.assertEqual(_FakeNativeSandbox.calls[-1]["max_total_size"], "20Mi") |
| 50 | + self.assertEqual(_FakeNativeSandbox.calls[-1]["max_file_count"], 0) |
| 51 | + |
| 52 | + def test_unlimited_is_forwarded(self): |
| 53 | + with patch( |
| 54 | + "hyperlight_sandbox._load_backend", |
| 55 | + return_value=("hyperlight-js", _FakeNativeSandbox), |
| 56 | + ): |
| 57 | + Sandbox(backend="hyperlight-js", filesystem_limits="unlimited") |
| 58 | + |
| 59 | + self.assertEqual(_FakeNativeSandbox.calls[-1]["filesystem_limits"], "unlimited") |
| 60 | + |
| 61 | + def test_wasm_backend_forwards_limits(self): |
| 62 | + with ( |
| 63 | + patch( |
| 64 | + "hyperlight_sandbox._load_backend", |
| 65 | + return_value=("wasm", _FakeNativeSandbox), |
| 66 | + ), |
| 67 | + patch("hyperlight_sandbox.resolve_module_path", return_value="/guest.wasm"), |
| 68 | + ): |
| 69 | + Sandbox(backend="wasm", max_total_size="4Mi") |
| 70 | + |
| 71 | + kwargs = _FakeNativeSandbox.calls[-1] |
| 72 | + self.assertEqual(kwargs["module_path"], "/guest.wasm") |
| 73 | + self.assertEqual(kwargs["max_total_size"], "4Mi") |
| 74 | + |
| 75 | + def test_environment_forwards_limits(self): |
| 76 | + environment = SandboxEnvironment(max_file_size="1Ki", max_file_count=2) |
| 77 | + tool = CodeExecutionTool(environment=environment) |
| 78 | + with patch( |
| 79 | + "hyperlight_sandbox._load_backend", |
| 80 | + return_value=("hyperlight-js", _FakeNativeSandbox), |
| 81 | + ): |
| 82 | + tool._get_sandbox() |
| 83 | + |
| 84 | + kwargs = _FakeNativeSandbox.calls[-1] |
| 85 | + self.assertEqual(kwargs["max_file_size"], "1Ki") |
| 86 | + self.assertEqual(kwargs["max_file_count"], 2) |
| 87 | + |
| 88 | + |
| 89 | +class TestFilesystemLimitValidation(unittest.TestCase): |
| 90 | + def test_unlimited_rejects_numeric_overrides(self): |
| 91 | + with self.assertRaisesRegex(ValueError, "cannot be combined"): |
| 92 | + Sandbox(filesystem_limits="unlimited", max_file_count=0) |
| 93 | + |
| 94 | + def test_unknown_mode_is_rejected(self): |
| 95 | + with self.assertRaisesRegex(ValueError, "must be 'unlimited'"): |
| 96 | + Sandbox(filesystem_limits="default") |
| 97 | + |
| 98 | + def test_negative_file_count_is_rejected(self): |
| 99 | + with self.assertRaisesRegex(ValueError, "non-negative"): |
| 100 | + Sandbox(max_file_count=-1) |
| 101 | + |
| 102 | + def test_file_count_rejects_bool(self): |
| 103 | + with self.assertRaisesRegex(TypeError, "must be an integer"): |
| 104 | + Sandbox(max_file_count=True) |
| 105 | + |
| 106 | + def test_size_override_must_be_a_string(self): |
| 107 | + with self.assertRaisesRegex(TypeError, "must be a size string"): |
| 108 | + Sandbox(max_file_size=1024) # type: ignore[arg-type] |
| 109 | + |
| 110 | + def test_new_kwargs_are_not_silently_dropped_for_old_backends(self): |
| 111 | + class _OldBackend: |
| 112 | + def __init__(self, *, heap_size, stack_size): |
| 113 | + pass |
| 114 | + |
| 115 | + with ( |
| 116 | + patch( |
| 117 | + "hyperlight_sandbox._load_backend", |
| 118 | + return_value=("hyperlight-js", _OldBackend), |
| 119 | + ), |
| 120 | + self.assertRaisesRegex(TypeError, "max_file_count"), |
| 121 | + ): |
| 122 | + Sandbox(backend="hyperlight-js", max_file_count=1) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + unittest.main() |
0 commit comments