okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bcf88b738
main
1"""Disk-space checks stop only when the bytes to write do not fit; the working-room margin warns; a resumed download's partial file is not counted twice.""" 2 3import tempfile 4import unittest 5from collections import namedtuple 6from pathlib import Path 7from unittest import mock 8 9from strata_control import updater 10 11Usage = namedtuple ("Usage" ,"total used free" ) 12MiB = 1 << 20 13 14 15class Ctx : 16def __init__ (self ,root ): 17self .components = root 18self .said = [] 19 20def say (self ,phase ,message ,** extra ): 21self .said .append ((phase ,message )) 22 23 24class FreeSpace (unittest .TestCase ): 25def setUp (self ): 26self .tmp = tempfile .TemporaryDirectory () 27self .root = Path (self .tmp .name ) 28self .addCleanup (self .tmp .cleanup ) 29 30def check (self ,free_mib ,need_mib ,ctx = None ,sha = "" ): 31with mock .patch .object (updater .shutil ,"disk_usage" ,return_value = Usage (10 ** 12 ,0 ,free_mib * MiB )): 32updater ._free_space_check (self .root ,need_mib * MiB ,ctx ,sha ,"engine" ) 33 34def test_plenty_of_room_is_silent (self ): 35ctx = Ctx (self .root ) 36self .check (5000 ,1000 ,ctx ) 37self .assertEqual (ctx .said , []) 38 39def test_between_the_real_need_and_the_margin_warns_and_goes_on (self ): 40ctx = Ctx (self .root ) 41self .check (1050 ,1000 ,ctx )# 1.15x would be 1150 42self .assertEqual (len (ctx .said ),1 ) 43self .assertEqual (ctx .said [0 ][0 ],"warn" ) 44 45def test_below_the_real_need_stops_and_names_the_drive_and_the_step (self ): 46with self .assertRaises (updater .UpdateError )as caught : 47self .check (800 ,1000 ,Ctx (self .root )) 48self .assertEqual (caught .exception .exit_code ,6 ) 49self .assertIn ("800 MiB free" ,str (caught .exception )) 50self .assertIn ("Free up some space" ,str (caught .exception )) 51 52def test_the_partial_file_of_a_resumed_download_is_not_counted_twice (self ): 53ctx = Ctx (self .root ) 54sha = "ab" * 32 55part = updater .updates_dir (self .root )/ "partial" 56part .mkdir (parents = True ) 57 (part / f" { sha [: 16 ] } .part" ).write_bytes (b"x" * 600 ) 58with mock .patch .object (updater .shutil ,"disk_usage" ,return_value = Usage (10 ** 12 ,0 ,500 )): 59updater ._free_space_check (self .root ,1000 ,ctx ,sha ,"engine" )# 400 still to write, 500 free: fine and silent 60self .assertEqual (ctx .said , []) 61with self .assertRaises (updater .UpdateError ): 62updater ._free_space_check (self .root ,1000 ,ctx ,"" )# without the partial file's credit 1000 > 500 63 64 65if __name__ == "__main__" : 66unittest .main ()