okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
64a91dcdc
main
1"""Offline policy tests (checklist U08). 2 3The checklist asks for two claims that are easy to conflate, and the whole point of these tests is 4that they are separate: 5 6* **runtime-only no-download** — the server fetches nothing while serving; 7* **strict offline** — nothing in the process tree reaches the network, including children, tools and 8the agent runtime. 9 10A passing runtime-only test says nothing about the second: the first is about the server's own code 11paths, and the second is about every process it can start. So they are tested separately, and the 12strict tests check the actual environment a child receives rather than the flag that was passed. 13 14The limits are tested too. A policy that claims more than it enforces is worse than one that states 15its boundary, so `limits()` is asserted to be non-empty and specific. 16""" 17 18from __future__import annotations 19 20import os 21import sys 22import unittest 23from pathlib import Path 24 25sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 26 27from strata_control import offline as O # noqa: E402 28 29 30class TestUrlDecisions (unittest .TestCase ): 31"""One predicate decides every fetch, so "offline" is not a scattered set of ifs.""" 32 33def test_a_remote_url_is_refused_when_offline (self ): 34decision = O .check_url ("https://huggingface.co/model.bin" ,offline = True ) 35self .assertFalse (decision ) 36self .assertIn ("huggingface.co" ,decision .reason ) 37 38def test_a_remote_url_is_allowed_when_not_offline (self ): 39self .assertTrue (O .check_url ("https://huggingface.co/model.bin" ,offline = False )) 40 41def test_a_local_scheme_is_allowed_even_when_offline (self ): 42"""Talking to this machine is not a download, and forbidding it would break the local API.""" 43for url in ("file:///tmp/image.png" ,"data:image/png;base64,AAAA" ): 44decision = O .check_url (url ,offline = True ) 45self .assertTrue (decision ,f" { url } must be allowed offline" ) 46 47def test_loopback_over_http_is_allowed_when_offline (self ): 48for url in ("http://127.0.0.1:8095/v1/models" ,"http://localhost:8095/health" , 49"http://[::1]:8095/health" ): 50self .assertTrue (O .check_url (url ,offline = True ),f" { url } is this machine" ) 51 52def test_a_bare_path_is_allowed_when_offline (self ): 53"""A profile usually names a local image as a path, not as a URL.""" 54self .assertTrue (O .check_url ("/models/photo.png" ,offline = True )) 55self .assertTrue (O .check_url ("photo.png" ,offline = True )) 56 57def test_a_non_http_scheme_is_refused_when_offline (self ): 58decision = O .check_url ("ftp://example.com/x" ,offline = True ) 59self .assertFalse (decision ) 60self .assertIn ("scheme" ,decision .reason ) 61 62def test_require_url_raises_with_the_reason (self ): 63with self .assertRaises (O .OfflineViolation )as ctx : 64O .require_url ("https://example.com/x" ,offline = True ) 65self .assertIn ("would leave this machine" ,str (ctx .exception )) 66self .assertEqual (ctx .exception .url ,"https://example.com/x" ) 67 68def test_require_url_passes_a_local_url (self ): 69O .require_url ("http://127.0.0.1:1/x" ,offline = True )# must not raise 70 71 72class TestChildEnvironment (unittest .TestCase ): 73"""Strict offline must reach code we do not own, which means the environment.""" 74 75def test_the_offline_markers_are_set_for_children (self ): 76env = O .child_environment (offline = True ,base = {}) 77self .assertEqual (env ["HF_HUB_OFFLINE" ],"1" ) 78self .assertEqual (env ["TRANSFORMERS_OFFLINE" ],"1" ) 79self .assertEqual (env ["PIP_NO_INDEX" ],"1" ) 80self .assertEqual (env ["STRATA_OFFLINE" ],"1" ) 81 82def test_proxies_are_emptied_so_an_accidental_call_fails_fast (self ): 83env = O .child_environment (offline = True , 84base = {"HTTPS_PROXY" :"http://corp-proxy:8080" }) 85self .assertEqual (env ["HTTPS_PROXY" ],"" ) 86self .assertEqual (env ["NO_PROXY" ],"*" ) 87 88def test_the_parent_environment_is_not_mutated (self ): 89base = {"HTTPS_PROXY" :"http://corp-proxy:8080" ,"PATH" :"/usr/bin" } 90O .child_environment (offline = True ,base = base ) 91self .assertEqual (base ["HTTPS_PROXY" ],"http://corp-proxy:8080" , 92"the caller's environment must be untouched" ) 93 94def test_a_marker_from_a_previous_run_is_removed_when_not_offline (self ): 95"""Otherwise a later server mysteriously refuses to fetch anything.""" 96env = O .child_environment (offline = False ,base = {"HF_HUB_OFFLINE" :"1" ,"PATH" :"/usr/bin" }) 97self .assertNotIn ("HF_HUB_OFFLINE" ,env ) 98self .assertEqual (env ["PATH" ],"/usr/bin" ) 99 100def test_the_real_environment_is_used_by_default (self ): 101env = O .child_environment (offline = True ) 102self .assertEqual (env ["STRATA_OFFLINE" ],"1" ) 103self .assertIn ("PATH" ,env ,"the child still needs a PATH" ) 104 105 106class TestProfilePreflight (unittest .TestCase ): 107"""Refusing before the server starts beats failing on the first request.""" 108 109def test_a_plain_profile_is_strictly_offline_capable (self ): 110self .assertEqual (O .ensure_strictly_offline ({"model" :"x" ,"args" : []}), []) 111 112def test_mcp_servers_are_named_as_a_reason (self ): 113reasons = O .ensure_strictly_offline ({"mcp_servers" : {"a" : {},"b" : {}}}) 114self .assertTrue (reasons ) 115self .assertIn ("2 MCP server" ,reasons [0 ]) 116 117def test_a_before_load_command_is_named_as_a_reason (self ): 118reasons = O .ensure_strictly_offline ({"before_load" :"curl https://example.com/x" }) 119self .assertTrue (reasons ) 120self .assertIn ("before_load" ,reasons [0 ]) 121 122def test_remote_image_sources_are_named_as_a_reason (self ): 123reasons = O .ensure_strictly_offline ({"images" : {"allowed_hosts" : ["cdn.example.com" ]}}) 124self .assertTrue (reasons ) 125self .assertIn ("images.allowed_hosts" ,reasons [0 ]) 126 127def test_a_local_image_section_is_not_a_reason (self ): 128self .assertEqual (O .ensure_strictly_offline ({"images" : {"size" :512 }}), []) 129 130 131class TestTheLimitIsStated (unittest .TestCase ): 132"""A policy that claims more than it enforces is worse than one that names its boundary.""" 133 134def test_the_limits_are_specific_and_non_empty (self ): 135limits = O .limits () 136self .assertTrue (limits ) 137joined = " " .join (limits ).lower () 138self .assertIn ("socket" ,joined ,"the socket case is the one that matters" ) 139self .assertIn ("sandbox" ,joined ) 140 141def test_the_report_separates_the_two_claims (self ): 142report = O .report (offline = True ,cfg = {"model" :"x" }) 143self .assertTrue (report ["strict_offline" ]) 144self .assertTrue (report ["runtime_only_no_download" ], 145"runtime-only is true independently of strict offline" ) 146self .assertTrue (report ["enforced" ]) 147self .assertTrue (report ["not_enforced" ],"the limits travel with the claim" ) 148 149def test_the_report_of_a_non_offline_run_does_not_claim_enforcement (self ): 150report = O .report (offline = False ,cfg = {}) 151self .assertFalse (report ["strict_offline" ]) 152self .assertEqual (report ["not_enforced" ], []) 153self .assertTrue (report ["runtime_only_no_download" ]) 154 155def test_the_report_says_which_profile_parts_block_strict_offline (self ): 156report = O .report (offline = True ,cfg = {"mcp_servers" : {"a" : {}}}) 157self .assertTrue (report ["profile_needs_network" ]) 158 159 160class TestTheServerUsesThePolicy (unittest .TestCase ): 161"""A guard that is not wired in is decoration, so the wiring itself is asserted.""" 162 163def setUp (self ): 164self .source = (Path (__file__ ).resolve ().parents [2 ]/ "serve" / "server.py" ).read_text ( 165encoding = "utf-8" ) 166 167def test_the_image_path_consults_the_policy_or_the_flag (self ): 168# The reviewed server already refuses a remote image URL when offline; what matters here is 169# that the refusal exists on both image paths rather than one. 170self .assertGreaterEqual (self .source .count ("offline mode cannot download an image URL" ),2 , 171"both image paths must refuse a remote fetch" ) 172 173def test_child_env_stamps_the_policy (self ): 174self .assertIn ("offline_policy.OFFLINE_ENV" ,self .source , 175"children must be given the offline environment" ) 176 177def test_every_child_environment_call_passes_the_policy (self ): 178"""A single call site that forgot the flag would leak network access to that child.""" 179offenders = [] 180for index ,line in enumerate (self .source .splitlines (),start = 1 ): 181if "child_env(" in line and "def child_env" not in line : 182if "offline=" not in line : 183offenders .append (f"line { index } : { line . strip () } " ) 184self .assertEqual (offenders , [], 185"every child_env call must pass the server's offline state: " 186+ "; " .join (offenders )) 187 188def test_the_strict_offline_preflight_runs_before_serving (self ): 189self .assertIn ("ensure_strictly_offline" ,self .source ) 190 191 192if __name__ == "__main__" : 193unittest .main ()