okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6e1085c1d
main
1"""Step 1's page-file check: only the size the page files have for sure counts - their size now (the commit limit 2minus RAM), or a file's configured initial size when larger - never what Windows may grow them to, and the warning 3(below 4 GB, as before) advises a fixed size. When the Virtual memory setting (the registry's PagingFiles) has Windows 4grow a file on demand ("System managed", or an initial size below the maximum), the warning says why that is not 5enough (issue #60). The registry, the drives and GlobalMemoryStatusEx are mocked, so it runs on Linux too. 6 7python -m unittest tools.test_setup_page_file 8""" 9from __future__import annotations 10 11import contextlib 12import sys 13import types 14import unittest 15from pathlib import Path 16from unittest import mock 17 18ROOT = Path (__file__ ).resolve ().parents [1 ] 19sys .path .insert (0 ,str (ROOT )) 20sys .path .insert (0 ,str (ROOT / "tools" )) 21import setup # noqa: E402 22from test_setup_golden import PROFILES ,install # noqa: E402 23 24PAGE_FILE_GB ,PAGE_FILE_SETTING = setup .page_file_gb ,setup .page_file_setting # the golden harness mocks the first 25GROWS = "It is set to grow on demand" 26FIXED = f"Custom size with initial and maximum size { setup . PAGE_FILE_FIXED_MB } MB" 27 28 29def windows (paging ,ram = 31.9 ,now_mb = 0 ,drives = None ,existing = ("\\??\\C:\\pagefile.sys" ,)): 30"""What Windows answers page_file_gb() and page_file_setting(), mocked: PagingFiles (None: it cannot be read), 31ExistingPageFiles, each drive's (free MB, page file MB now), and GlobalMemoryStatusEx (commit limit = RAM + the 32files' size now).""" 33drives = {"C" : (400000 ,now_mb )}if drives is None else drives 34ms = types .SimpleNamespace (ullTotalPhys = int (ram * 2 ** 30 ),ullTotalPageFile = int (ram * 2 ** 30 )+ now_mb * 2 ** 20 ) 35return [mock .patch .object (setup ,"_mm_multi_sz" ,lambda name :None if paging is None else 36list (paging )if name == "PagingFiles" else list (existing )), 37mock .patch .object (setup ,"_drive_mb" ,lambda d :drives .get (d )), 38mock .patch .object (setup ,"_memory_status" ,lambda :ms )] 39 40 41def on_windows (fn ): 42def call (* a ): 43with mock .patch .object (setup ,"WIN" ,True ): 44return fn (* a ) 45return call 46 47 48class Parse (unittest .TestCase ): 49def test_entries (self ): 50self .assertEqual (setup .parse_paging_file ("c:\\pagefile.sys 64000 64000" ), ("C" ,64000 ,64000 )) 51self .assertEqual (setup .parse_paging_file ("D:\\pagefile.sys 1024 32768" ), ("D" ,1024 ,32768 )) 52for managed in ("C:\\pagefile.sys 0 0" ,"C:\\pagefile.sys" ,"C:\\pagefile.sys x y" ): 53self .assertEqual (setup .parse_paging_file (managed ), ("C" ,- 1 ,- 1 ),managed ) 54self .assertEqual (setup .parse_paging_file ("?:\\pagefile.sys" ), ("?" ,- 1 ,- 1 ))# every drive automatic 55for junk in ("" ,"x" ,"pagefile.sys 1 2" ): 56self .assertIsNone (setup .parse_paging_file (junk ),junk ) 57 58 59class Reading (unittest .TestCase ): 60def read (self ,paging ,** k ): 61with contextlib .ExitStack ()as st : 62for p in windows (paging ,** k ): 63st .enter_context (p ) 64files = on_windows (PAGE_FILE_SETTING )() 65return on_windows (PAGE_FILE_GB )(),files 66 67def test_two_fixed_files (self ):# this PR's author: 2 x 64000 MB on C: and D: 68gb ,files = self .read (["c:\\pagefile.sys 64000 64000" ,"d:\\pagefile.sys 64000 64000" ],ram = 127.2 , 69now_mb = 128000 ,drives = {"C" : (334983 ,64000 ),"D" : (178396 ,64000 )}) 70self .assertEqual (gb ,125.0 ) 71self .assertFalse (setup .page_file_grows (files )) 72 73def test_a_growing_file_counts_its_size_now (self ): 74for paging in (["?:\\pagefile.sys" ], ["C:\\pagefile.sys 0 0" ], ["C:\\pagefile.sys" ]):# system-managed 75with self .subTest (paging ): 76gb ,files = self .read (paging ,now_mb = 4083 ) 77self .assertAlmostEqual (gb ,4083 / 1024 )# not 3 x RAM: it may not grow in time (#60) 78self .assertTrue (setup .page_file_grows (files )) 79gb ,files = self .read (["C:\\pagefile.sys 1024 32768" ],now_mb = 1024 ) 80self .assertEqual (gb ,1.0 )# its initial size, not its maximum 81self .assertTrue (setup .page_file_grows (files )) 82_ ,files = self .read (["?:\\pagefile.sys" ],now_mb = 4083 ,existing = ("\\??\\D:\\pagefile.sys" ,), 83drives = {"D" : (100000 ,4083 )}) 84self .assertEqual (files , [("D" ,- 1 ,- 1 ,4083 ,100000 )])# "?:" is where Windows keeps it 85 86def test_an_initial_size_larger_than_the_file_now_counts (self ): 87self .assertEqual (self .read (["C:\\pagefile.sys 8192 8192" ],now_mb = 2048 ,drives = {"C" : (400000 ,2048 )})[0 ],8.0 ) 88gb ,_ = self .read (["C:\\pagefile.sys 8192 8192" ],now_mb = 2048 ,drives = {"C" : (1000 ,2048 )})# a full drive 89self .assertAlmostEqual (gb ,3048 / 1024 ) 90 91def test_off_and_unread (self ): 92gb ,files = self .read ([]) 93self .assertEqual ((gb ,files ), (0.0 , [])) 94self .assertFalse (setup .page_file_grows (files )) 95gb ,files = self .read (None ,now_mb = 4083 )# the setting cannot be read: the commit limit alone 96self .assertAlmostEqual (gb ,4083 / 1024 ) 97self .assertIsNone (files ) 98self .assertFalse (setup .page_file_grows (files )) 99 100def test_not_windows (self ): 101with mock .patch .object (setup ,"WIN" ,False ): 102self .assertIsNone (setup .page_file_gb ()) 103self .assertIsNone (setup .page_file_setting ()) 104 105 106class Advice (unittest .TestCase ): 107def test_fixed_size_always_and_why_when_it_grows (self ): 108fixed = setup .page_file_advice (2.0 , [("C" ,2048 ,2048 ,2048 ,400000 )]) 109self .assertIn ("Windows' page file is 2.0 GB: the graphics card's memory needs room there too" ,fixed ) 110self .assertIn (FIXED ,fixed ) 111self .assertNotIn (GROWS ,fixed ) 112self .assertNotIn ("Set it to \"System managed\"" ,fixed ) 113for files in ([("C" ,- 1 ,- 1 ,4083 ,400000 )], [("C" ,1024 ,32768 ,1024 ,400000 )], 114 [("C" ,2048 ,2048 ,2048 ,400000 ), ("D" ,- 1 ,- 1 ,1024 ,400000 )]): 115with self .subTest (files ): 116text = setup .page_file_advice (3.99 ,files ) 117self .assertIn (GROWS ,text ) 118self .assertIn ("a fixed 64 GB worked" ,text ) 119self .assertIn (FIXED ,text ) 120self .assertNotIn (GROWS ,setup .page_file_advice (0.0 , [])) 121self .assertNotIn (GROWS ,setup .page_file_advice (1.0 ,None ))# the setting unread 122 123 124class StepOne (unittest .TestCase ): 125"""setup --check on the harness's 32 GB PC with the page-file reading of a Windows PC: warned below 4 GB.""" 126WARN = "Windows' page file is" 127 128def check (self ,paging ,now_mb ,ram = None ): 129ram0 ,found = PROFILES ["32GB-2x24GB" ] 130ram = ram or ram0 131extra = [mock .patch .object (setup ,"page_file_gb" ,on_windows (PAGE_FILE_GB )), 132mock .patch .object (setup ,"page_file_setting" ,on_windows (PAGE_FILE_SETTING )), 133* windows (paging ,ram = ram ,now_mb = now_mb )] 134code ,out ,_ ,_ = install (ram ,found , ["--check" ],extra = extra ) 135self .assertEqual (code ,0 ,out [- 3000 :]) 136return next ((x for x in out .splitlines ()if self .WARN in x ),None ) 137 138def test_a_system_managed_file_is_warned_with_the_reason (self ): 139line = self .check (["?:\\pagefile.sys" ],4083 ) 140self .assertIn ("Windows' page file is 4.0 GB" ,line ) 141self .assertIn (GROWS ,line ) 142self .assertIn (FIXED ,line ) 143 144def test_a_small_fixed_file_and_none (self ): 145for paging ,now in ((["C:\\pagefile.sys 2048 2048" ],2048 ), ([],0 )): 146with self .subTest (paging ): 147line = self .check (paging ,now ) 148self .assertIn (f"Windows' page file is { now / 1024 :.1f } GB" ,line ) 149self .assertIn (FIXED ,line ) 150self .assertNotIn (GROWS ,line ) 151 152def test_the_threshold_is_unchanged (self ): 153self .assertIsNone (self .check (["?:\\pagefile.sys" ],8192 ))# 8 GB now: not warned, as before 154self .assertIsNone (self .check (["C:\\pagefile.sys 64000 64000" ],64000 )) 155 156 157if __name__ == "__main__" : 158unittest .main ()