okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bcf88b738
main
1"""0.2.2 update rules: a slow first run of Control is waited for, a busy card is not held against an engine, a pre-release Control is 2handed over to (or not) by its number, and a feed that looks expired on a PC with a wrong clock says so.""" 3 4import datetime as dt 5import json 6import subprocess 7import tempfile 8import unittest 9from pathlib import Path 10from types import SimpleNamespace 11from unittest import mock 12 13from strata_cli import handover 14from strata_control import feed as feedmod ,signing ,updater 15from tests .terminal import update_fixture as fx 16from tests .terminal .test_handover import make_control 17from tests .terminal .test_update_feed import document ,signed 18from tests .terminal import test_updater 19 20 21class ControlVersionTest (unittest .TestCase ): 22def ctx (self ): 23said = [] 24return SimpleNamespace (environ = {},say = lambda * a ,** k :said .append (a )),said 25 26def test_a_first_run_that_times_out_is_tried_once_more_with_more_time (self ): 27ctx ,said = self .ctx () 28good = SimpleNamespace (returncode = 0 ,stdout = "strata 0.2.2\n" ) 29calls = [] 30 31def fake (cmd ,** kw ): 32calls .append (kw ["timeout" ]) 33if len (calls )== 1 : 34raise subprocess .TimeoutExpired (cmd ,kw ["timeout" ]) 35return good 36with mock .patch .object (updater .subprocess ,"run" ,fake ): 37done = updater ._control_version ("py" ,Path ("strata.py" ),Path ("." ),ctx ,"0.2.2" ) 38self .assertIs (done ,good ) 39self .assertEqual (calls ,list (updater .CONTROL_VERSION_TIMEOUTS )) 40self .assertGreater (calls [0 ],120 ) 41self .assertEqual (len (said ),1 ) 42 43def test_two_failures_end_as_a_failed_test_start (self ): 44ctx ,_ = self .ctx () 45with mock .patch .object (updater .subprocess ,"run" ,side_effect = subprocess .TimeoutExpired ("x" ,1 )): 46with self .assertRaises (updater .TestStartFailed ): 47updater ._control_version ("py" ,Path ("strata.py" ),Path ("." ),ctx ,"0.2.2" ) 48 49 50class BusyCardTest (test_updater .UpdaterCase ): 51def refuse (self ,reason ): 52def runner (stage_root ,staged ,ctx ): 53raise updater .TestStartFailed (staged .version ,reason ) 54return runner 55 56def test_a_busy_card_is_not_held_against_the_engine (self ): 57self .install_current () 58self .publish (("0.1.47" ,fx .GOOD_ENGINE )) 59out = updater .run_update (self .ctx (test_start = self .refuse ("it timed out waiting for the graphics card" )),control = False ) 60result = out ["results" ][0 ] 61self .assertEqual (result ["status" ],"failed" ) 62self .assertIn ("looks busy" ,result ["error" ]) 63again = updater .run_update (self .ctx (),control = False ) 64self .assertEqual (again ["results" ][0 ]["status" ],"updated" ,again ) 65 66def test_a_real_failure_is_remembered_for_a_day_and_then_tried_again (self ): 67self .install_current () 68self .publish (("0.1.47" ,fx .GOOD_ENGINE )) 69updater .run_update (self .ctx (test_start = self .refuse ("the engine crashed on start" )),control = False ) 70soon = updater .run_update (self .ctx (),control = False ) 71self .assertEqual (soon ["results" ][0 ]["status" ],"failed-before" ) 72later = dt .datetime .now (dt .timezone .utc )+ dt .timedelta (hours = 25 ) 73retry = updater .run_update (self .ctx (now = later ),control = False ) 74self .assertEqual (retry ["results" ][0 ]["status" ],"updated" ,retry ) 75 76 77class HandoverPreRelease (unittest .TestCase ): 78def setUp (self ): 79self .temp = tempfile .TemporaryDirectory (prefix = "strata-rc-" ) 80self .addCleanup (self .temp .cleanup ) 81self .comp = Path (self .temp .name )/ "components" 82self .comp .mkdir () 83self .env = {"STRATA_COMPONENTS_DIR" :str (self .comp )} 84 85def target (self ,running ): 86return handover .find_target (running ,self .env ) 87 88def test_the_order_of_releases_and_candidates (self ): 89key = handover .version_key 90self .assertLess (key ("0.2.2-rc1" ),key ("0.2.2" )) 91self .assertLess (key ("0.2.2-rc1" ),key ("0.2.2-rc2" )) 92self .assertLess (key ("0.2.2-beta" ),key ("0.2.2-rc1" )) 93self .assertLess (key ("0.2.1" ),key ("0.2.2-rc1" )) 94self .assertIsNone (key ("banana" )) 95 96def test_a_release_candidate_is_handed_over_to_from_an_older_release (self ): 97make_control (self .comp ,"0.2.2-rc1" ) 98folder ,version ,problem = self .target ("0.2.1" ) 99self .assertIsNone (problem ) 100self .assertEqual (version ,"0.2.2-rc1" ) 101self .assertEqual (folder .name ,"0.2.2-rc1" ) 102 103def test_the_final_release_replaces_its_candidate_but_not_the_other_way (self ): 104make_control (self .comp ,"0.2.2" ) 105self .assertEqual (self .target ("0.2.2-rc1" )[1 ],"0.2.2" ) 106make_control (self .comp ,"0.2.2-rc1" ,point = False ) 107 (self .comp / "control" / "active.json" ).write_text (json .dumps ({"version" :"0.2.2-rc1" ,"folder" :"0.2.2-rc1" })) 108self .assertEqual (self .target ("0.2.2" ), (None ,None ,None )) 109 110def test_a_version_nobody_can_read_is_said_not_ignored (self ): 111make_control (self .comp ,"banana" ) 112folder ,version ,problem = self .target ("0.2.1" ) 113self .assertIsNone (folder ) 114self .assertIn ("cannot be read" ,problem ) 115 116 117class ClockTest (unittest .TestCase ): 118def setUp (self ): 119self .key = signing .generate_key () 120self .trusted = fx .trusted_of (self .key ) 121 122def load (self ,** guards ): 123payload ,sig = signed (document (expires_at = "2026-10-01T00:00:00Z" ),self .key ) 124return feedmod .verify_and_parse (payload ,sig ,source = "/feeds/strata-feed.json" ,trusted = self .trusted ,** guards ) 125 126def test_the_server_clock_proves_the_pc_clock_wrong (self ): 127pc = dt .datetime (2027 ,3 ,1 ,tzinfo = dt .timezone .utc ) 128server = dt .datetime (2026 ,9 ,20 ,tzinfo = dt .timezone .utc ) 129with self .assertRaises (feedmod .FeedError )as caught : 130self .load (now = pc ,server_now = server ) 131text = str (caught .exception ) 132self .assertIn ("your PC clock is wrong" ,text ) 133self .assertIn ("2026-09-20" ,text ) 134self .assertNotIn ("refusing a stale feed" ,text ) 135 136def test_without_a_server_date_the_clock_is_named_as_a_cause (self ): 137with self .assertRaisesRegex (feedmod .FeedError ,"your PC clock is wrong.*new feed" ): 138self .load (now = dt .datetime (2027 ,3 ,1 ,tzinfo = dt .timezone .utc )) 139 140def test_a_feed_the_server_also_calls_expired_is_still_refused_as_expired (self ): 141pc = dt .datetime (2026 ,11 ,1 ,tzinfo = dt .timezone .utc ) 142with self .assertRaisesRegex (feedmod .FeedError ,"expired on" ): 143self .load (now = pc ,server_now = pc ) 144 145def test_a_certificate_error_hints_at_the_clock (self ): 146import urllib .error 147from strata_control import transfer 148err = urllib .error .URLError ("[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" ) 149with mock .patch .object (transfer ._opener ,"open" ,side_effect = err ): 150with self .assertRaisesRegex (transfer .TransferError ,"date and time" ): 151transfer .read_small ("https://example.org/strata-feed.json" ) 152 153 154if __name__ == "__main__" : 155unittest .main ()