okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2dfb254f7
main
1"""`strata update` installs a newer Control; the Terminal on PATH hands over to it (it used to keep running the old one).""" 2 3import json 4import os 5import stat 6import subprocess 7import sys 8import tempfile 9import unittest 10from pathlib import Path 11 12from strata_cli import handover 13 14ROOT = Path (__file__ ).resolve ().parents [2 ] 15WINDOWS = os .name == "nt" 16 17 18def make_control (components :Path ,version :str ,folder :str | None = None ,whole :bool = True ,point :bool = True )-> Path : 19folder = folder or version 20d = components / "control" / folder 21 (d / "strata_control" ).mkdir (parents = True ,exist_ok = True ) 22 (d / "VERSION" ).write_text (version + "\n" ,encoding = "utf-8" ) 23 (d / "strata.py" ).write_text ("print('control', %r)\n" % version ,encoding = "utf-8" ) 24if whole : 25py = handover .bundled_python (d ) 26py .parent .mkdir (parents = True ,exist_ok = True ) 27py .write_text ("stub" ,encoding = "utf-8" ) 28if point : 29 (components / "control" / "active.json" ).write_text (json .dumps ({"version" :version ,"folder" :folder }),encoding = "utf-8" ) 30return d 31 32 33class Handover (unittest .TestCase ): 34def setUp (self ): 35self .temp = tempfile .TemporaryDirectory (prefix = "strata-handover-" ) 36self .addCleanup (self .temp .cleanup ) 37self .comp = Path (self .temp .name )/ "components" 38self .comp .mkdir () 39self .env = {"STRATA_COMPONENTS_DIR" :str (self .comp ),handover .ENV_CHECKOUT :"1" } 40self .calls ,self .warnings = [], [] 41 42def go (self ,running = "0.1.46" ,env = None ,argv = ("status" ,)): 43def fake (* args ,** kwargs ): 44self .calls .append ((args ,kwargs )) 45return 7 46env = {** self .env ,** (env or {})} 47return handover .maybe_hand_over (list (argv ),environ = env ,running = running ,execv = fake ,run = fake ,warn = self .warnings .append ) 48 49def test_a_source_checkout_never_hands_over (self ): 50make_control (self .comp ,"0.1.46.1" ) 51env = {k :v for k ,v in self .env .items ()if k != handover .ENV_CHECKOUT } 52original = handover .running_from_checkout 53handover .running_from_checkout = lambda :True 54try : 55result = handover .maybe_hand_over (["status" ],environ = env ,running = "0.1.46" ,execv = None , 56run = lambda * a ,** k :self .fail ("handed over" ),warn = self .warnings .append ) 57finally : 58handover .running_from_checkout = original 59self .assertIsNone (result ) 60self .assertEqual (self .warnings , []) 61 62def test_a_newer_control_takes_over_with_its_own_interpreter_and_the_same_arguments (self ): 63d = make_control (self .comp ,"0.1.46.1" ) 64result = self .go (argv = ("serve" ,"--port" ,"1" )) 65self .assertEqual (len (self .calls ),1 ) 66args ,kwargs = self .calls [0 ] 67python ,command = (args [0 ],args [1 ])if not WINDOWS else (args [0 ][0 ],args [0 ]) 68self .assertEqual (Path (python ),handover .bundled_python (d )) 69self .assertEqual (command [1 :], [str (d / "strata.py" ),"serve" ,"--port" ,"1" ]) 70env = kwargs .get ("env" )or args [2 ] 71self .assertEqual (env ["STRATA_HANDOVER" ],"1" ) 72if WINDOWS : 73self .assertEqual (result ,7 ,"the exit code of the newer Control is passed through" ) 74self .assertEqual (self .warnings , []) 75 76def test_no_loop_when_already_handed_over (self ): 77make_control (self .comp ,"0.1.46.1" ) 78self .assertIsNone (self .go (env = {"STRATA_HANDOVER" :"1" })) 79self .assertIsNone (self .go (env = {"STRATA_NO_HANDOVER" :"1" })) 80self .assertEqual (self .calls , []) 81 82def test_a_broken_target_falls_back_with_one_warning (self ): 83make_control (self .comp ,"0.1.46.1" ,whole = False ) 84self .assertIsNone (self .go ()) 85self .assertEqual (self .calls , []) 86self .assertEqual (len (self .warnings ),1 ) 87self .assertIn ("0.1.46.1" ,self .warnings [0 ]) 88self .assertIn ("running 0.1.46 instead" ,self .warnings [0 ]) 89 90def test_a_target_that_fails_to_start_falls_back (self ): 91make_control (self .comp ,"0.1.46.1" ) 92 93def boom (* a ,** k ): 94raise OSError (8 ,"Exec format error" ) 95result = handover .maybe_hand_over (["status" ],environ = self .env ,running = "0.1.46" ,execv = boom ,run = boom ,warn = self .warnings .append ) 96self .assertIsNone (result ) 97self .assertEqual (len (self .warnings ),1 ) 98 99def test_an_older_or_equal_target_is_ignored (self ): 100make_control (self .comp ,"0.1.45" ) 101self .assertIsNone (self .go (running = "0.1.46" )) 102make_control (self .comp ,"0.1.46" ) 103self .assertIsNone (self .go (running = "0.1.46" )) 104self .assertIsNone (self .go (running = "0.1.46.1" )) 105self .assertEqual ((self .calls ,self .warnings ), ([], [])) 106 107def test_the_version_in_the_folder_counts_not_the_pointers_claim (self ): 108d = make_control (self .comp ,"0.1.45" ) 109 (self .comp / "control" / "active.json" ).write_text (json .dumps ({"version" :"9.9.9" ,"folder" :"0.1.45" }),encoding = "utf-8" ) 110self .assertIsNone (self .go (running = "0.1.46" )) 111self .assertTrue (d .is_dir ()) 112 113def test_a_pointer_cannot_leave_the_control_folder (self ): 114outside = Path (self .temp .name )/ "etc" 115 (outside / "strata_control" ).mkdir (parents = True ) 116 (outside / "VERSION" ).write_text ("9.0.0" ,encoding = "utf-8" ) 117 (outside / "strata.py" ).write_text ("print('evil')" ,encoding = "utf-8" ) 118py = handover .bundled_python (outside ) 119py .parent .mkdir (parents = True ) 120py .write_text ("x" ,encoding = "utf-8" ) 121 (self .comp / "control" ).mkdir (exist_ok = True ) 122for folder in ("../../etc" ,".." ,"../etc" ,"a/b" ,"/etc" ,"..\\etc" ,"" ): 123 (self .comp / "control" / "active.json" ).write_text (json .dumps ({"version" :"9.0.0" ,"folder" :folder }),encoding = "utf-8" ) 124self .assertIsNone (self .go (),folder ) 125self .assertEqual (self .calls , []) 126 127@ unittest . skipIf ( WINDOWS , "needs a symlink" ) 128def test_a_folder_that_is_a_link_out_of_the_control_folder_is_refused (self ): 129real = make_control (Path (self .temp .name )/ "elsewhere" ,"9.0.0" ,point = False ) 130 (self .comp / "control" ).mkdir (exist_ok = True ) 131 (self .comp / "control" / "9.0.0" ).symlink_to (real ,target_is_directory = True ) 132 (self .comp / "control" / "active.json" ).write_text (json .dumps ({"version" :"9.0.0" ,"folder" :"9.0.0" }),encoding = "utf-8" ) 133self .assertIsNone (self .go ()) 134self .assertEqual (self .calls , []) 135 136def test_no_pointer_or_garbage_means_nothing_happens (self ): 137self .assertIsNone (self .go ()) 138 (self .comp / "control" ).mkdir () 139for text in ("{" ,"[]" ,"null" ,json .dumps ({"folder" :"x" })): 140 (self .comp / "control" / "active.json" ).write_text (text ,encoding = "utf-8" ) 141self .assertIsNone (self .go (),text ) 142self .assertEqual ((self .calls ,self .warnings ), ([], [])) 143 144@ unittest . skipIf ( WINDOWS , "a shell stub stands in for the bundled interpreter" ) 145def test_end_to_end_the_real_entry_point_hands_over_and_does_not_loop (self ): 146newer = make_control (self .comp ,"99.0.0" ) 147py = handover .bundled_python (newer ) 148py .write_text ('#!/bin/sh\nexec "%s" "$@"\n' % sys .executable ,encoding = "utf-8" ) 149py .chmod (py .stat ().st_mode | stat .S_IXUSR ) 150 (newer / "strata.py" ).write_text ("import os,sys\nprint('control 99', os.environ.get('STRATA_HANDOVER'), sys.argv[1:])\nraise SystemExit(3)\n" , 151encoding = "utf-8" ) 152done = subprocess .run ([sys .executable ,str (ROOT / "strata.py" ),"--version" ],capture_output = True ,text = True , 153env = {** os .environ ,"STRATA_COMPONENTS_DIR" :str (self .comp ),handover .ENV_CHECKOUT :"1" }) 154self .assertEqual (done .returncode ,3 ,done .stderr ) 155self .assertIn ("control 99 1 ['--version']" ,done .stdout ) 156# with the guard set the current Control runs, and says its own version 157done = subprocess .run ([sys .executable ,str (ROOT / "strata.py" ),"--version" ],capture_output = True ,text = True , 158env = {** os .environ ,"STRATA_COMPONENTS_DIR" :str (self .comp ),"STRATA_HANDOVER" :"1" }) 159self .assertEqual (done .returncode ,0 ,done .stderr ) 160self .assertTrue (done .stdout .startswith ("strata " ),done .stdout ) 161 162def test_the_entry_point_falls_back_when_the_target_is_broken (self ): 163make_control (self .comp ,"99.0.0" ,whole = False ) 164done = subprocess .run ([sys .executable ,str (ROOT / "strata.py" ),"--version" ],capture_output = True ,text = True , 165env = {** os .environ ,"STRATA_COMPONENTS_DIR" :str (self .comp ),handover .ENV_CHECKOUT :"1" }) 166self .assertEqual (done .returncode ,0 ,done .stderr ) 167self .assertTrue (done .stdout .startswith ("strata " ),done .stdout ) 168self .assertEqual (len (done .stderr .strip ().splitlines ()),1 ,done .stderr ) 169self .assertIn ("99.0.0" ,done .stderr ) 170 171 172if __name__ == "__main__" : 173unittest .main ()