okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
39c5534ed
main
1"""The Linux ARM64 (DGX Spark) Terminal reads strata-feed-linux-aarch64.json; every other machine reads strata-feed.json, as before. 2 3Why: a Control older than 0.1.53 refuses a feed that names a platform it does not know, so the linux-aarch64 entries cannot be in the common 4feed. Same format, same signing key; only the file name differs. 5""" 6 7from __future__import annotations 8 9import shutil 10import sys 11import tempfile 12import unittest 13from pathlib import Path 14from types import SimpleNamespace 15from unittest import mock 16 17ROOT = Path (__file__ ).resolve ().parents [2 ] 18sys .path .insert (0 ,str (ROOT / "packaging" )) 19sys .path .insert (0 ,str (ROOT )) 20 21import feed_tool # noqa: E402 22from strata_cli import update_cmd # noqa: E402 23from strata_control import feed as feedmod # noqa: E402 24from strata_control import updater # noqa: E402 25 26COMMON = "strata-feed.json" 27ARM = "strata-feed-linux-aarch64.json" 28RELEASE = "https://github.com/Niko1221/Strata/releases/latest/download" 29 30 31def host (platform ): 32return mock .patch ("strata_control.feed.host_platform" ,return_value = platform ) 33 34 35class FeedFileName (unittest .TestCase ): 36def test_names (self ): 37self .assertEqual (feedmod .feed_name ("linux-aarch64" ),ARM ) 38for plat in ("windows-x86_64" ,"linux-x86_64" ,"macos-arm64" ,"macos-x86_64" ): 39self .assertEqual (feedmod .feed_name (plat ),COMMON ) 40 41def test_the_machine_decides (self ): 42with host ("linux-aarch64" ): 43self .assertEqual (feedmod .feed_name (),ARM ) 44with host ("linux-x86_64" ): 45self .assertEqual (feedmod .feed_name (),COMMON ) 46 47def test_an_unknown_os_reads_the_common_feed (self ): 48with mock .patch ("strata_control.feed.host_platform" ,side_effect = RuntimeError ("no" )): 49self .assertEqual (feedmod .feed_name (),COMMON ) 50 51def test_the_common_name_and_default_are_unchanged (self ): 52self .assertEqual (feedmod .FEED_NAME ,COMMON ) 53self .assertEqual (update_cmd .DEFAULT_FEED ,RELEASE + "/" + COMMON ) 54 55 56class DefaultSource (unittest .TestCase ): 57def source (self ,remembered = None ,feed = None ,env = None ): 58with tempfile .TemporaryDirectory ()as tmp : 59root = Path (tmp ) 60if remembered : 61state = updater .State (root ) 62state .data ["feed" ]= {"source" :remembered } 63state .save () 64with mock .patch .dict ("os.environ" , {},clear = False ): 65import os 66os .environ .pop (update_cmd .ENV_FEED ,None ) 67if env : 68os .environ [update_cmd .ENV_FEED ]= env 69return update_cmd ._feed_source (SimpleNamespace (feed = feed ),root ) 70 71def test_arm64_linux_defaults_to_its_own_file (self ): 72with host ("linux-aarch64" ): 73self .assertEqual (update_cmd .default_feed (),RELEASE + "/" + ARM ) 74self .assertEqual (self .source (),RELEASE + "/" + ARM ) 75 76def test_the_common_feed_remembered_from_before_does_not_stick_on_arm64 (self ): 77with host ("linux-aarch64" ): 78self .assertEqual (self .source (remembered = update_cmd .DEFAULT_FEED ),RELEASE + "/" + ARM ) 79 80def test_other_machines_are_unchanged (self ): 81for plat in ("windows-x86_64" ,"linux-x86_64" ,"macos-arm64" ): 82with host (plat ): 83self .assertEqual (self .source (),update_cmd .DEFAULT_FEED ) 84self .assertEqual (self .source (remembered = update_cmd .DEFAULT_FEED ),update_cmd .DEFAULT_FEED ) 85 86def test_an_explicit_feed_wins_on_arm64 (self ): 87with host ("linux-aarch64" ): 88self .assertEqual (self .source (feed = "https://mirror.example/f.json" ),"https://mirror.example/f.json" ) 89self .assertEqual (self .source (env = "/tmp/feed" ),"/tmp/feed" ) 90self .assertEqual (self .source (feed = "http://127.0.0.1:9" ,env = "/tmp/feed" ),"http://127.0.0.1:9" ) 91 92def test_a_remembered_mirror_is_kept_on_arm64 (self ): 93with host ("linux-aarch64" ): 94self .assertEqual (self .source (remembered = "https://mirror.example/f.json" ),"https://mirror.example/f.json" ) 95 96 97class ReadsTheRightFile (unittest .TestCase ): 98"""feed.fetch against a folder and against a release address: which file is asked for.""" 99 100def setUp (self ): 101self ._tmp = tempfile .TemporaryDirectory () 102self .addCleanup (self ._tmp .cleanup ) 103self .dir = Path (self ._tmp .name ) 104feed_tool .keygen (self .dir / "keys" ) 105self .key = feed_tool ._load_key (self .dir / "keys" / "feed-key.json" ) 106self .trusted = {self .key ["key_id" ]:__import__ ("base64" ).b64decode (self .key ["public" ])} 107 108def write (self ,folder ,name ,sequence ): 109tmp = folder / "_tmp" 110feed_tool .build_feed (tmp ,key = self .key ,sequence = sequence ) 111for suffix in ("" ,".sig" ): 112shutil .copy2 (tmp / ("strata-feed.json" + suffix ),folder / (name + suffix )) 113shutil .rmtree (tmp ) 114 115def test_a_folder_with_both_files (self ): 116folder = self .dir / "both" 117folder .mkdir () 118self .write (folder ,COMMON ,115 ) 119self .write (folder ,ARM ,116 ) 120with host ("linux-aarch64" ): 121self .assertEqual (feedmod .fetch (str (folder ),self .trusted ).sequence ,116 ) 122for plat in ("windows-x86_64" ,"linux-x86_64" ,"macos-arm64" ): 123with host (plat ): 124self .assertEqual (feedmod .fetch (str (folder ),self .trusted ).sequence ,115 ) 125 126def test_a_folder_made_for_the_common_feed_still_works_on_arm64 (self ): 127folder = self .dir / "common" 128folder .mkdir () 129self .write (folder ,COMMON ,7 ) 130with host ("linux-aarch64" ): 131self .assertEqual (feedmod .fetch (str (folder ),self .trusted ).sequence ,7 ) 132 133def test_a_named_file_is_used_as_given (self ): 134folder = self .dir / "named" 135folder .mkdir () 136self .write (folder ,COMMON ,115 ) 137self .write (folder ,ARM ,116 ) 138with host ("linux-aarch64" ): 139self .assertEqual (feedmod .fetch (str (folder / COMMON ),self .trusted ).sequence ,115 ) 140with host ("linux-x86_64" ): 141self .assertEqual (feedmod .fetch (str (folder / ARM ),self .trusted ).sequence ,116 ) 142 143def test_a_release_address_asks_for_the_machines_file (self ): 144asked = [] 145 146def read_small (location ,** kw ): 147asked .append (location ) 148raise feedmod .transfer .TransferError ("stop here" ) 149 150with mock .patch ("strata_control.feed.transfer.read_small" ,side_effect = read_small ): 151for plat ,want in (("linux-aarch64" ,ARM ), ("linux-x86_64" ,COMMON ), ("windows-x86_64" ,COMMON ), ("macos-arm64" ,COMMON )): 152asked .clear () 153with host (plat ),self .assertRaises (feedmod .FeedError ): 154feedmod .fetch (RELEASE ,self .trusted ) 155self .assertEqual (asked [0 ],RELEASE + "/" + want ,plat ) 156asked .clear () 157with host ("linux-aarch64" ),self .assertRaises (feedmod .FeedError ): 158feedmod .fetch (RELEASE + "/" + COMMON ,self .trusted ) 159self .assertEqual (asked [0 ],RELEASE + "/" + COMMON ) 160 161 162class CommonFeedStaysReadableByOldClients (unittest .TestCase ): 163def test_the_published_parser_set_is_the_reason (self ): 164"""The platforms a 0.1.52 Control accepts are the four below; a feed with a fifth is refused whole, which is why linux-aarch64 has its own file.""" 165self .assertEqual (sorted (set (feedmod .PLATFORMS )- {"linux-aarch64" }), 166 ["linux-x86_64" ,"macos-arm64" ,"macos-x86_64" ,"windows-x86_64" ]) 167 168 169if __name__ == "__main__" : 170unittest .main ()