okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
16904848b
main
1"""The disk-room check of setup's own-files path (QA 0213): the low-RAM mode's copy of the experts is measured where it is written (the data 2folder's packs folder), and the refusal names that folder, not the model folder of files that are already there. 3 4python -m unittest tools.test_setup_disk_room 5""" 6from __future__import annotations 7 8import os 9import sys 10import tempfile 11import unittest 12from pathlib import Path 13 14ROOT = Path (__file__ ).resolve ().parents [1 ] 15sys .path .insert (0 ,str (ROOT )) 16import setup # noqa: E402 17 18APPROVED_HINT = ("Free some space, move Strata's data folder to a bigger drive (strata launch --data-dir), " 19"or set Low-RAM mode to Automatic to run without the copy." ) 20 21 22class DiskRoom (unittest .TestCase ): 23def folders (self ): 24tmp = tempfile .TemporaryDirectory () 25self .addCleanup (tmp .cleanup ) 26models = Path (tmp .name )/ "own-model-folder" 27pack = Path (tmp .name )/ "data" / "packs" / "iq3_s" 28models .mkdir (parents = True ) 29pack .parent .mkdir (parents = True ) 30return models ,pack 31 32def test_the_copy_of_the_experts_is_refused_in_the_words_the_owner_approved_and_names_the_packs_folder (self ): 33models ,pack = self .folders () 34problem = setup .disk_room_problem (models ,pack ,1.0 ,52.0 ,free = lambda _path :22.0 )# the QA case: 22 GB free, the model files are already there 35self .assertIsNotNone (problem ) 36message ,hint = problem 37self .assertEqual (message ,f"not enough free disk space for the low-RAM copy of the experts in { pack } : it needs about 53 GB and 22 GB are free there." ) 38self .assertEqual (hint ,APPROVED_HINT ) 39self .assertNotIn (str (models ),message )# the refusal no longer names the folder of the person's own model files 40 41def test_the_copy_is_measured_on_the_packs_drive_not_on_the_model_drive (self ): 42models ,pack = self .folders () 43free = {models :500.0 ,pack .parent :22.0 } 44real_stat = os .stat 45 46def other_device (path ,* a ,** k ):# the packs folder sits on another drive than the model folder 47result = real_stat (path ,* a ,** k ) 48if Path (path )== pack .parent : 49return os .stat_result ((result .st_mode ,result .st_ino ,result .st_dev + 1 ,* tuple (result )[3 :])) 50return result 51from unittest import mock 52with mock .patch .object (setup .os ,"stat" ,other_device ): 53problem = setup .disk_room_problem (models ,pack ,1.0 ,52.0 ,free = lambda path :free [path ]) 54self .assertIsNotNone (problem ) 55self .assertIn ("low-RAM copy of the experts" ,problem [0 ]) 56self .assertIn ("it needs about 52 GB and 22 GB are free there" ,problem [0 ])# no other files on that drive: just the copy 57# and the other way round: a roomy packs drive, a full model drive: the model folder's refusal 58free = {models :0.5 ,pack .parent :500.0 } 59with mock .patch .object (setup .os ,"stat" ,other_device ): 60problem = setup .disk_room_problem (models ,pack ,1.0 ,52.0 ,free = lambda path :free [path ]) 61self .assertIsNotNone (problem ) 62self .assertIn (f"not enough free disk space in { models } " ,problem [0 ]) 63 64def test_enough_room_and_no_copy (self ): 65models ,pack = self .folders () 66self .assertIsNone (setup .disk_room_problem (models ,pack ,1.0 ,52.0 ,free = lambda _path :200.0 )) 67self .assertIsNone (setup .disk_room_problem (models ,pack ,5.0 ,0 ,free = lambda _path :6.0 )) 68message ,_hint = setup .disk_room_problem (models ,pack ,5.0 ,0 ,free = lambda _path :4.0 ) 69self .assertTrue (message .startswith (f"not enough free disk space in { models } : need ~5 GB, 4 GB free" ))# a download that does not fit: the old sentence 70 71def test_the_old_refusal_for_the_copy_is_gone_from_the_run_path (self ): 72source = (ROOT / "setup.py" ).read_text (encoding = "utf-8" ) 73run = source [source .index ("copy_gb = MODELS[model]" ):] 74run = run [:run .index ("if free_now < need:" )] 75self .assertIn ("disk_room_problem(" ,run ) 76self .assertNotIn ("not enough free disk space in {models_dir}" ,run ) 77 78 79if __name__ == "__main__" : 80unittest .main ()