okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
061a28583
main
1"""The offline path of the first-run card: an engine package the person downloaded and chose is checked (signature, every file, fit for 2this PC), test-started and switched to, with no feed and no network. The file they chose stays where it is.""" 3 4import tarfile 5from pathlib import Path 6from unittest import mock 7 8from strata_control import updater 9from tests .terminal import update_fixture as fx 10from feed_tool import pack_engine # noqa: E402 (update_fixture puts packaging/ on the path) 11from tests .terminal .test_updater import PLATFORM ,UpdaterCase 12 13 14class InstallEngineFile (UpdaterCase ): 15def package (self ,version = "0.1.46" ,key = "default" ,** options ): 16folder = fx .build_package (self .root / f"file- { version } " ,version ,key = self .key if key == "default" else key ,** options ) 17out = self .root / f"file-out- { version } " 18out .mkdir (exist_ok = True ) 19pack_engine (folder ,out ) 20return next (p for p in sorted (out .iterdir ())if p .name .endswith ((".tar.gz" ,".zip" ))) 21 22def install (self ,archive ,** over ): 23with mock .patch .object (updater ,"discover_run_config" ,return_value = None ): 24return updater .install_engine_file (self .ctx (run_config = None ,** over ),archive ) 25 26def test_a_signed_package_is_installed_without_any_network (self ): 27archive = self .package () 28out = self .install (archive ) 29self .assertEqual ((out ["status" ],out ["changed" ]), ("updated" ,True ),out ) 30self .assertEqual (self .resolved (),"0.1.46" ) 31self .assertTrue (archive .is_file (),"the person's file is not deleted" ) 32self .assertEqual (self .server .requests , [],"nothing was fetched" ) 33self .assertEqual (sorted (p .name for p in (self .components / ".s" ).iterdir ()), [],"staging is cleaned" ) 34 35def test_an_unsigned_package_is_refused (self ): 36archive = self .package (key = None ) 37with self .assertRaises (updater .UpdateError )as caught : 38self .install (archive ) 39self .assertIn ("security check" ,str (caught .exception )) 40self .assertEqual (self .engine_folders (), []) 41 42def test_a_package_signed_by_another_key_is_refused (self ): 43archive = self .package (key = fx .make_key ()) 44with self .assertRaises (updater .UpdateError ): 45self .install (archive ) 46self .assertEqual (self .engine_folders (), []) 47 48def test_a_package_that_does_not_fit_this_card_says_so (self ): 49archive = self .package (archs = ("sm_100" ,)) 50with self .assertRaises (updater .UpdateError )as caught : 51self .install (archive ) 52self .assertIn ("not a Strata engine for this PC" ,str (caught .exception )) 53self .assertEqual (self .engine_folders (), []) 54 55def test_something_that_is_not_a_package_is_refused_in_plain_words (self ): 56junk = self .root / "notes.txt" 57junk .write_text ("hello" ) 58with self .assertRaises (updater .UpdateError )as caught : 59self .install (junk ) 60self .assertIn ("not a Strata engine package" ,str (caught .exception )) 61bad = self .root / "bad.tar.gz" 62bad .write_bytes (b"this is not gzip" ) 63with self .assertRaises (updater .UpdateError ): 64self .install (bad ) 65 66def test_an_older_package_does_not_replace_a_newer_engine (self ): 67self .install_current ("0.1.47" ) 68out = self .install (self .package ("0.1.46" )) 69self .assertEqual ((out ["status" ],out ["changed" ]), ("current" ,False )) 70self .assertEqual (self .resolved (),"0.1.47" ) 71 72def test_a_package_with_a_changed_file_is_refused (self ): 73archive = self .package () 74tampered = self .root / "tampered.tar.gz" 75with tarfile .open (archive )as src ,tarfile .open (tampered ,"w:gz" )as dst : 76for member in src .getmembers (): 77data = src .extractfile (member )if member .isfile ()else None 78if member .isfile ()and member .name .endswith ("engine_main.py" ): 79import io 80payload = b"print('changed')\n" 81member .size = len (payload ) 82data = io .BytesIO (payload ) 83dst .addfile (member ,data ) 84with self .assertRaises (updater .UpdateError ): 85self .install (tampered ) 86self .assertEqual (self .engine_folders (), [])