okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01f8c3255
main
1"""#3469 #3437: the 0.2 releases carry no flat engine zip, so a source checkout (UPDATE.bat, setup) fetches the engine package 2for its card from the signed update feed and installs its files in engine/. 3 4python -m unittest tools.test_setup_feed_engine 5""" 6from __future__import annotations 7 8import json 9import sys 10import tempfile 11import types 12import unittest 13from pathlib import Path 14from unittest import mock 15 16ROOT = Path (__file__ ).resolve ().parents [1 ] 17sys .path .insert (0 ,str (ROOT )) 18sys .path .insert (0 ,str (ROOT / "tools" )) 19import setup # noqa: E402 20from strata_control import engine_component as ec # noqa: E402 21 22 23class FeedEngine (unittest .TestCase ): 24def setUp (self ): 25self .tmp = tempfile .TemporaryDirectory () 26self .addCleanup (self .tmp .cleanup ) 27self .root = Path (self .tmp .name ) 28self .eng = self .root / "engine" 29self .eng .mkdir () 30 (self .eng / "BUILD.json" ).write_text (json .dumps ({"version" :"0.1.45" ,"source" :"prebuilt" ,"archs" : [89 ]}),encoding = "utf-8" ) 31 (self .eng / setup .EXE ).write_bytes (b"old" ) 32self .package = self .root / "components" / "engine" / "0.1.46-test" 33self .package .mkdir (parents = True ) 34 (self .package / "BUILD.json" ).write_text (json .dumps ({"version" :"0.1.46" ,"archs" : [89 ]}),encoding = "utf-8" ) 35 (self .package / setup .EXE ).write_bytes (b"new" ) 36for name ,value in (("home" ,lambda :self .root ), ("say" ,lambda * a ,** k :None ), ("ok" ,lambda * a ,** k :None ), 37 ("warn" ,lambda * a ,** k :None )): 38patch = mock .patch .object (setup ,name ,value ) 39patch .start () 40self .addCleanup (patch .stop ) 41patch = mock .patch .object (setup .acquisition ,"require" ,lambda what :None ) 42patch .start () 43self .addCleanup (patch .stop ) 44 45def run_update (self ,code = 0 ): 46return mock .patch .object (setup .subprocess ,"run" ,return_value = types .SimpleNamespace (returncode = code ,stdout = "" ,stderr = "no feed" )) 47 48def test_the_package_becomes_the_installed_engine (self ): 49with self .run_update ()as run ,mock .patch .object (ec ,"resolve" ,return_value = types .SimpleNamespace (root = self .package )): 50got = setup .engine_from_feed (self .eng ,updating = True ) 51self .assertEqual (got ,self .eng ) 52cmd = run .call_args .args [0 ] 53self .assertIn ("update" ,cmd ) 54self .assertIn ("--no-control" ,cmd ) 55self .assertNotIn ("STRATA_NO_ACQUIRE" ,run .call_args .kwargs ["env" ])# this one step may fetch 56self .assertEqual ((self .eng / setup .EXE ).read_bytes (),b"new" ) 57meta = json .loads ((self .eng / "BUILD.json" ).read_text (encoding = "utf-8" )) 58self .assertEqual ((meta ["version" ],meta ["source" ]), ("0.1.46" ,"prebuilt" )) 59self .assertEqual ((self .eng / ".previous" / setup .EXE ).read_bytes (),b"old" )# one generation to roll back to 60 61def test_no_feed_keeps_the_installed_engine (self ): 62with self .run_update (code = 6 ): 63self .assertIsNone (setup .engine_from_feed (self .eng ,updating = True )) 64self .assertEqual ((self .eng / setup .EXE ).read_bytes (),b"old" ) 65 66def test_a_release_without_the_zip_falls_back_to_the_feed (self ): 67gpu = {"arch" :89 ,"archs" : [89 ],"vram_gb" :8.0 ,"index" :0 } 68 (self .eng / "BUILD.json" ).write_text (json .dumps ({"version" :"0.1.45" ,"source" :"prebuilt" ,"archs" : [89 ]}),encoding = "utf-8" ) 69with mock .patch .object (setup ,"engine_dir" ,lambda toolkit = 13 :self .eng ),mock .patch .object (setup ,"_urlopen" ,side_effect = OSError ("HTTP Error 404" )),mock .patch .object (setup ,"engine_from_feed" ,return_value = self .eng )as feed : 70got = setup .get_prebuilt (setup .PREBUILT_URL ,gpu ,"none" ,updating = True ) 71self .assertEqual (got ,self .eng ) 72feed .assert_called_once () 73feed .reset_mock () 74setup .get_prebuilt (setup .PREBUILT_URL ,gpu ,"none" ,updating = True ,toolkit = 12 ) 75feed .assert_not_called ()# the experimental CUDA 12 engine has no feed package here 76 77def test_the_minimum_is_the_0_2_engine (self ): 78self .assertLess ((0 ,1 ,45 ),setup .MIN_ENGINE ) 79self .assertGreaterEqual ((0 ,1 ,46 ),setup .WIN_HIP_MIN_ENGINE ) 80 81 82if __name__ == "__main__" : 83unittest .main ()