okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
64a91dcdc
main
1"""An owned server attests the bytes and arguments it actually received.""" 2 3import json 4import unittest 5from unittest import mock 6 7from serve .launch_identity import attestation 8from strata_control import readiness 9 10 11class LaunchIdentityTests (unittest .TestCase ): 12def test_attestation_changes_with_source_args_and_key (self ): 13key = bytes (range (32 )) 14cfg = b'{"api_key":"a private setting","model_name":"fake"}' 15args = ["--config" ,"/tmp/run.json" ,"--parallel" ,"4" ] 16value = attestation (key ,cfg ,args ) 17self .assertEqual (len (value ),64 ) 18self .assertEqual (value ,attestation (key ,cfg ,args )) 19self .assertNotEqual (value ,attestation (key ,cfg + b" " ,args )) 20self .assertNotEqual (value ,attestation (key ,cfg , [* args ,"--batch-telemetry" ])) 21self .assertNotEqual (value ,attestation (bytes (reversed (key )),cfg ,args )) 22self .assertNotIn ("private" ,value ) 23 24def test_wrong_or_absent_server_attestation_rejects_readiness_before_generation (self ): 25ready = {"bound_host" :"127.0.0.1" ,"bound_port" :1234 , 26"model_id" :"fake" ,"model_state" :"ready" } 27cfg = json .dumps ({"model_name" :"fake" }).encode () 28for reported in (None ,"0" * 64 ): 29with self .subTest (reported = reported ),mock .patch .object (readiness ,"_request" )as request : 30request .return_value = {"instance" : {"id" :"one" ,"launch_attestation" :reported }, 31"model" : {"id" :"fake" ,"loaded" :True }} 32with self .assertRaisesRegex (readiness .ReadinessError , 33"probe_launch_attestation_mismatch" ): 34readiness .verify (ready ,cfg ,expected_host = "127.0.0.1" , 35expected_instance = "one" ,expected_attestation = "f" * 64 , 36timeout = 1 ) 37self .assertEqual (request .call_count ,1 ) 38 39def test_attested_lazy_model_reports_unloaded_without_generation (self ): 40ready = {"bound_host" :"127.0.0.1" ,"bound_port" :1234 , 41"model_id" :"fake" ,"model_state" :"unloaded" } 42with mock .patch .object (readiness ,"_request" ,return_value = { 43"instance" : {"id" :"one" ,"launch_attestation" :"f" * 64 }, 44"model" : {"id" :"fake" ,"loaded" :False }})as request : 45result = readiness .verify (ready ,b'{"model_name":"fake"}' , 46expected_host = "127.0.0.1" ,expected_instance = "one" , 47expected_attestation = "f" * 64 ,timeout = 1 ) 48self .assertTrue (result ["launch_attested" ]) 49self .assertFalse (result ["generation_ready" ]) 50self .assertEqual (request .call_count ,1 ) 51 52 53if __name__ == "__main__" : 54unittest .main ()