okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
09c18d3e9
main
1"""The one-line installers and the local test feed: syntax, the placeholder guard, the feed builder, and (POSIX) a real install.sh run against a feed.""" 2 3from __future__import annotations 4 5import hashlib 6import json 7import os 8import platform 9import shutil 10import subprocess 11import sys 12import tarfile 13import tempfile 14import threading 15import unittest 16from functools import partial 17from http .server import SimpleHTTPRequestHandler ,ThreadingHTTPServer 18from pathlib import Path 19 20ROOT = Path (__file__ ).resolve ().parents [2 ] 21sys .path .insert (0 ,str (ROOT / "packaging" )) 22sys .path .insert (0 ,str (ROOT )) 23import make_local_feed as mlf # noqa: E402 24 25# the fixture Terminal is built for the machine the test runs on: install.sh picks its archive by uname 26PLAT = "linux-aarch64" if sys .platform .startswith ("linux" )and platform .machine ().lower ()in ("aarch64" ,"arm64" )else "linux-x86_64" 27 28 29class Scripts (unittest .TestCase ): 30def test_install_sh_syntax_and_guard (self ): 31sh = shutil .which ("sh" )or shutil .which ("bash" ) 32if not sh : 33self .skipTest ("no sh" ) 34r = subprocess .run ([sh ,"-n" ,str (ROOT / "packaging" / "install.sh" )],capture_output = True ,text = True ) 35self .assertEqual (r .returncode ,0 ,r .stderr ) 36text = (ROOT / "packaging" / "install.sh" ).read_text (encoding = "utf-8" ) 37self .assertNotIn ("__FEED__" ,text )# no placeholder: the GitHub Releases address is the built-in default 38self .assertIn (":-" + mlf .RELEASE_FEED + "}" ,text )# the default a local feed bakes its own address over 39self .assertIn ("Installed. Run: " ,text ) 40self .assertNotIn ("\r" ,text ) 41 42def test_install_ps1_parses_and_guard (self ): 43text = (ROOT / "packaging" / "install.ps1" ).read_text (encoding = "utf-8" ) 44self .assertNotIn ("__FEED__" ,text ) 45self .assertIn ("else { '" + mlf .RELEASE_FEED + "' }" ,text ) 46self .assertIn ("Installed. Run: strata launch" ,text ) 47ps = shutil .which ("powershell" )or shutil .which ("pwsh" ) 48if not ps : 49self .skipTest ("no PowerShell" ) 50cmd = ("$e=$null; $t=$null; [System.Management.Automation.Language.Parser]::ParseFile('%s',[ref]$t,[ref]$e) | Out-Null; " 51"if ($e.Count) { $e | ForEach-Object { $_.Message }; exit 1 } else { 'ok' }" % str (ROOT / "packaging" / "install.ps1" ).replace ("'" ,"''" )) 52r = subprocess .run ([ps ,"-NoProfile" ,"-Command" ,cmd ],capture_output = True ,text = True ) 53self .assertEqual (r .returncode ,0 ,r .stdout + r .stderr ) 54 55def test_the_built_in_address_is_the_default_feed (self ): 56"""install.sh / install.ps1 and `strata update` use the same GitHub Releases folder; STRATA_FEED stays an override.""" 57from strata_cli import update_cmd 58self .assertEqual (update_cmd .DEFAULT_FEED ,mlf .RELEASE_FEED + "/strata-feed.json" ) 59self .assertIn (":-" + mlf .RELEASE_FEED + "}" , (ROOT / "packaging" / "install.sh" ).read_text (encoding = "utf-8" )) 60self .assertIn ("$env:STRATA_FEED" , (ROOT / "packaging" / "install.ps1" ).read_text (encoding = "utf-8" )) 61 62 63class Feed (unittest .TestCase ): 64def make_bundle (self ,work :Path )-> Path : 65"""A tiny terminal archive that is a working `strata` (the repo's own source with the system Python).""" 66top = work / "bundle" / ("strata-terminal-9.9.9-" + PLAT ) 67top .mkdir (parents = True ) 68for name in ("strata.py" ,"strata_cli" ,"strata_control" ,"serve" ): 69src = ROOT / name 70 (shutil .copytree (src ,top / name ,ignore = shutil .ignore_patterns ("__pycache__" ,"*.pyc" ))if src .is_dir ()else shutil .copy2 (src ,top / name )) 71 (top / "bin" ).mkdir (parents = True ,exist_ok = True ) 72shutil .copy2 (ROOT / "bin" / "strata" ,top / "bin" / "strata" ) 73 (top / "bin" / "strata" ).chmod (0o755 ) 74 (top / "VERSION" ).write_text ("9.9.9\n" ,encoding = "utf-8" ) 75archive = work / ("strata-terminal-9.9.9-" + PLAT + ".tar.gz" ) 76with tarfile .open (archive ,"w:gz" )as tf : 77tf .add (top ,arcname = top .name ) 78return archive 79 80def test_make_local_feed_and_a_real_install (self ): 81import make_local_feed 82with tempfile .TemporaryDirectory ()as tmp : 83work = Path (tmp ) 84archive = self .make_bundle (work ) 85feed = work / "feed" 86rc = make_local_feed .main (["--out" ,str (feed ),"--terminal" ,str (archive ),"--keys" ,str (work / "keys" ),"--url" ,"http://127.0.0.1:1" ]) 87self .assertEqual (rc ,0 ) 88for name in ("strata-feed.json" ,"strata-feed.json.sig" ,"install-manifest.txt" ,"install.sh" ,"install.ps1" ,"test-trust.json" ): 89self .assertTrue ((feed / name ).is_file (),name ) 90line = (feed / "install-manifest.txt" ).read_text ().split () 91self .assertEqual (line [0 ],PLAT ) 92self .assertEqual (line [1 ],"9.9.9" ) 93control = feed / line [2 ] 94self .assertEqual (hashlib .sha256 (control .read_bytes ()).hexdigest (),line [3 ]) 95self .assertEqual (control .stat ().st_size ,int (line [4 ])) 96sh_text = (feed / "install.sh" ).read_text () 97self .assertIn ("http://127.0.0.1:1" ,sh_text ) 98self .assertNotIn (mlf .RELEASE_FEED ,sh_text )# the GitHub address was replaced everywhere by the local one 99self .assertIn ("Installed. Run:" , (feed / "install.ps1" ).read_text ()) 100doc = json .loads ((feed / "strata-feed.json" ).read_text ()) 101self .assertEqual (doc ["control" ]["packages" ][0 ]["sha256" ],line [3 ]) 102self .assertNotIn ("seed" , (feed / "test-trust.json" ).read_text ())# only the public key is published 103if os .name == "nt" or not (shutil .which ("sh" )and shutil .which ("curl" )and shutil .which ("tar" )): 104self .skipTest ("the real install.sh run needs POSIX sh, curl and tar" ) 105handler = partial (SimpleHTTPRequestHandler ,directory = str (feed )) 106handler .log_message = lambda * a ,** k :None # type: ignore[attr-defined] 107server = ThreadingHTTPServer (("127.0.0.1" ,0 ),handler ) 108threading .Thread (target = server .serve_forever ,daemon = True ).start () 109try : 110url = f"http://127.0.0.1: { server . server_address [ 1 ] } " 111home = work / "home" 112home .mkdir () 113env = {** os .environ ,"HOME" :str (home ),"STRATA_FEED" :url ,"STRATA_INSTALL_TEST_TRUST" :"1" ,"STRATA_PYTHON" :sys .executable , 114"PATH" :os .environ ["PATH" ],"STRATA_NO_ACQUIRE" :"" } 115for k in ("XDG_DATA_HOME" ,"STRATA_UPDATE_TRUSTED_KEYS" ): 116env .pop (k ,None ) 117r = subprocess .run (["sh" ,str (feed / "install.sh" )],capture_output = True ,text = True ,env = env ,timeout = 240 ) 118self .assertEqual (r .returncode ,0 ,r .stdout + r .stderr ) 119self .assertIn ("Checksum ok" ,r .stdout ) 120self .assertIn ("Signature ok" ,r .stdout ) 121self .assertIn ("Installed. Run:" ,r .stdout ) 122self .assertTrue ((home / ".local" / "bin" / "strata" ).exists ()) 123v = subprocess .run ([str (home / ".local" / "bin" / "strata" ),"--version" ],capture_output = True ,text = True ,env = env ,timeout = 60 ) 124self .assertEqual (v .returncode ,0 ,v .stderr ) 125# an archive with one top folder (a terminal archive, not the feed's control package) installs too 126data = archive .read_bytes () 127 (feed / "top.tar.gz" ).write_bytes (data ) 128 (feed / "install-manifest.txt" ).write_text (f" { PLAT } 9.9.9 top.tar.gz { hashlib . sha256 ( data ). hexdigest () } { len ( data ) } " + chr (10 )) 129home3 = work / "home3" 130home3 .mkdir () 131r = subprocess .run (["sh" ,str (feed / "install.sh" )],capture_output = True ,text = True ,env = {** env ,"HOME" :str (home3 )},timeout = 240 ) 132self .assertEqual (r .returncode ,0 ,r .stdout + r .stderr ) 133self .assertTrue ((home3 / ".local" / "share" / "strata" / "terminal" / "9.9.9" / "bin" / "strata" ).exists ()) 134 (feed / "install-manifest.txt" ).write_text (" " .join (line )+ chr (10 )) 135# a damaged download is refused and nothing is installed 136data = (feed / line [2 ]).read_bytes () 137 (feed / line [2 ]).write_bytes (data [:- 10 ]+ b"x" * 10 ) 138home2 = work / "home2" 139home2 .mkdir () 140r = subprocess .run (["sh" ,str (feed / "install.sh" )],capture_output = True ,text = True ,env = {** env ,"HOME" :str (home2 )},timeout = 240 ) 141self .assertNotEqual (r .returncode ,0 ) 142self .assertIn ("does not match the release's checksum" ,r .stderr ) 143self .assertFalse ((home2 / ".local" / "share" / "strata" / "terminal" / "9.9.9" ).exists ()) 144finally : 145server .shutdown () 146 147def test_the_release_path_writes_install_manifest_and_the_unchanged_install_sh_installs_from_it (self ): 148"""pack-control (flat names, as for GitHub assets) + build-feed, the files a release uploads; not make_local_feed.""" 149import feed_tool 150with tempfile .TemporaryDirectory ()as tmp : 151work = Path (tmp ) 152archive = self .make_bundle (work ) 153unpacked = work / "unpacked" 154unpacked .mkdir () 155folder = mlf .extract_terminal (archive ,unpacked ) 156release = work / "release" 157feed_tool .keygen (work / "keys" ) 158key = feed_tool ._load_key (work / "keys" / "feed-key.json" ) 159entry = feed_tool .pack_control (folder ,release / "control" ,version = "9.9.9" ,platform = PLAT ,url_prefix = "" ) 160shutil .move (str (release / "control" / entry ["url" ]),str (release / entry ["url" ]))# GitHub assets are flat 161feed_tool .build_feed (release ,key = key ,sequence = 1 ,control_dir = release / "control" ) 162line = (release / "install-manifest.txt" ).read_text ().split () 163self .assertEqual (line , [PLAT ,"9.9.9" ,entry ["url" ],entry ["sha256" ],str (entry ["size" ])]) 164self .assertEqual (json .loads ((release / "strata-feed.json" ).read_text ())["control" ]["packages" ][0 ]["sha256" ],entry ["sha256" ]) 165bad = work / "bad" # a Windows pack that is not a zip cannot be installed by install.ps1: left out of the manifest 166feed_tool .build_feed (bad ,key = key ,sequence = 1 ,control_dir = self ._windows_tar_pack (folder ,work )) 167self .assertFalse ((bad / "install-manifest.txt" ).exists ()) 168if PLAT == "linux-aarch64" :# the Linux ARM64 Terminal reads its own feed file (same bytes here) 169for suffix in ("" ,".sig" ): 170shutil .copy2 (release / ("strata-feed.json" + suffix ),release / ("strata-feed-linux-aarch64.json" + suffix )) 171shutil .copy2 (ROOT / "packaging" / "install.sh" ,release / "install.sh" )# uploaded unchanged 172shutil .copy2 (work / "keys" / "feed-key.pub.json" ,release / "test-trust.json" ) 173if os .name == "nt" or not (shutil .which ("sh" )and shutil .which ("curl" )and shutil .which ("tar" )): 174self .skipTest ("the real install.sh run needs POSIX sh, curl and tar" ) 175handler = partial (SimpleHTTPRequestHandler ,directory = str (release )) 176handler .log_message = lambda * a ,** k :None # type: ignore[attr-defined] 177server = ThreadingHTTPServer (("127.0.0.1" ,0 ),handler ) 178threading .Thread (target = server .serve_forever ,daemon = True ).start () 179try : 180home = work / "home" 181home .mkdir () 182env = {** os .environ ,"HOME" :str (home ),"STRATA_FEED" :f"http://127.0.0.1: { server . server_address [ 1 ] } " , 183"STRATA_INSTALL_TEST_TRUST" :"1" ,"STRATA_PYTHON" :sys .executable ,"STRATA_NO_ACQUIRE" :"" } 184for k in ("XDG_DATA_HOME" ,"STRATA_UPDATE_TRUSTED_KEYS" ): 185env .pop (k ,None ) 186r = subprocess .run (["sh" ,str (release / "install.sh" )],capture_output = True ,text = True ,env = env ,timeout = 240 ) 187self .assertEqual (r .returncode ,0 ,r .stdout + r .stderr ) 188self .assertIn ("Checksum ok" ,r .stdout ) 189self .assertIn ("Signature ok" ,r .stdout ) 190self .assertTrue ((home / ".local" / "bin" / "strata" ).exists ()) 191finally : 192server .shutdown () 193 194def _windows_tar_pack (self ,folder :Path ,work :Path )-> Path : 195import feed_tool 196out = work / "badcontrol" 197feed_tool .pack_control (folder ,out ,version = "9.9.9" ,platform = "windows-x86_64" ,fmt = "tar.gz" ,url_prefix = "" ) 198return out 199 200 201if __name__ == "__main__" : 202unittest .main ()