okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ffb3d9b31
main
1"""feed_tool can gzip its tar.gz archives with several threads (OPT-IN, STRATA_PACK_THREADS) (Q4-READY 11-10: the gzip of one 180 MB engine archive was ~50 s on one core of a release box). 2 3What these tests pin: 4* a multi-threaded archive holds the same members, modes and bytes as the single-stream one (STRATA_PACK_THREADS=1, the gzip of before); 5* it is several gzip members, and Python's gzip / tarfile (what Control's updater and components.py read) and the system's tar read it as one stream; 6* the same tree always gives the same archive bytes (no file name or time in the gzip headers); 7* pack-engine's entry names the archive's real sha256 and size, and a zip is unchanged. 8""" 9from __future__import annotations 10 11import gzip 12import hashlib 13import io 14import os 15import shutil 16import subprocess 17import sys 18import tarfile 19import tempfile 20import unittest 21from pathlib import Path 22from unittest import mock 23 24ROOT = Path (__file__ ).resolve ().parents [2 ] 25sys .path .insert (0 ,str (ROOT )) 26sys .path .insert (0 ,str (ROOT / "packaging" )) 27 28import feed_tool # noqa: E402 29 30 31def sha (p :Path )-> str : 32return hashlib .sha256 (p .read_bytes ()).hexdigest () 33 34 35def members (archive :Path )-> list [tuple ]: 36with tarfile .open (archive ,"r:gz" )as tar : 37out = [] 38for m in tar .getmembers (): 39data = tar .extractfile (m ).read ()if m .isfile ()else b"" 40out .append ((m .name ,m .mode ,m .type ,m .linkname ,hashlib .sha256 (data ).hexdigest ())) 41return out 42 43 44class ParallelGzip (unittest .TestCase ): 45def setUp (self ): 46self .tmp = tempfile .TemporaryDirectory () 47self .addCleanup (self .tmp .cleanup ) 48self .root = Path (self .tmp .name ) 49self .tree = self .root / "tree" 50 (self .tree / "lib" ).mkdir (parents = True ) 51rnd = os .urandom (300_000 ) 52 (self .tree / "strata" ).write_bytes (b"\x7fELF" + rnd )# an "executable" (ELF magic) 53 (self .tree / "lib" / "big.so" ).write_bytes (rnd * 7 + b"tail" )# compressible + random, several blocks 54 (self .tree / "notes.txt" ).write_text ("hello\n" * 5000 ) 55 (self .tree / "empty.bin" ).write_bytes (b"" ) 56self .files = feed_tool ._tree (self .tree ) 57p = mock .patch .object (feed_tool ,"GZIP_BLOCK" ,64 * 1024 )# small blocks: many members even for a small tree 58p .start () 59self .addCleanup (p .stop ) 60 61def pack (self ,name :str ,threads :int )-> Path : 62out = self .root / name 63with mock .patch .dict (os .environ , {"STRATA_PACK_THREADS" :str (threads )}): 64feed_tool ._write_archive (self .files ,out ,"tar.gz" ,"strata" ) 65return out 66 67def test_the_same_members_and_bytes_as_the_single_stream_archive (self ): 68one = self .pack ("one.tar.gz" ,1 ) 69many = self .pack ("many.tar.gz" ,4 ) 70self .assertEqual (members (one ),members (many )) 71self .assertFalse ((self .root / "many.tar.gz.tar-part" ).exists (),"the plain tar beside the archive is removed" ) 72 73def test_several_gzip_members_read_as_one_stream (self ): 74many = self .pack ("many.tar.gz" ,4 ) 75raw = many .read_bytes () 76self .assertGreater (raw .count (b"\x1f\x8b\x08" ),4 ,"more than one gzip member" ) 77plain = self .root / "plain.tar" 78feed_tool ._write_tar (self .files ,plain ,"w" ,"strata" ) 79self .assertEqual (gzip .decompress (raw ),plain .read_bytes ())# Python's gzip reads every member 80with tarfile .open (fileobj = io .BytesIO (raw ),mode = "r:gz" )as tar :# tarfile's r:gz (strata_control/components.py, updater.py) 81self .assertEqual (len (tar .getmembers ()),len (members (many ))) 82tar_exe = shutil .which ("tar" ) 83if tar_exe and os .name != "nt" : 84listed = subprocess .run ([tar_exe ,"-tzf" ,str (many )],capture_output = True ,text = True ) 85self .assertEqual (listed .returncode ,0 ,listed .stderr ) 86self .assertEqual (sorted (l .rstrip ("/" )for l in listed .stdout .split ()),sorted (m [0 ]for m in members (many ))) 87dest = self .root / "x" 88dest .mkdir () 89self .assertEqual (subprocess .run ([tar_exe ,"-xzf" ,str (many ),"-C" ,str (dest )]).returncode ,0 ) 90self .assertEqual ((dest / "lib" / "big.so" ).read_bytes (), (self .tree / "lib" / "big.so" ).read_bytes ()) 91 92def test_the_same_tree_gives_the_same_bytes (self ): 93a = self .pack ("a.tar.gz" ,4 ) 94b = self .pack ("b.tar.gz" ,3 ) 95self .assertEqual (sha (a ),sha (b ))# pieces are cut by bytes, not by thread count 96 97def test_the_thread_count (self ): 98with mock .patch .dict (os .environ , {"STRATA_PACK_THREADS" :"1" }): 99self .assertEqual (feed_tool .gzip_threads (),1 ) 100with mock .patch .dict (os .environ , {"STRATA_PACK_THREADS" :"6" }): 101self .assertEqual (feed_tool .gzip_threads (),6 ) 102for unset in ("" ,"x" ,"0" ):# OPT-IN: no setting (or a bad one) is the single stream 103with mock .patch .dict (os .environ , {"STRATA_PACK_THREADS" :unset }),mock .patch .object (os ,"cpu_count" ,return_value = 32 ): 104self .assertEqual (feed_tool .gzip_threads (),1 ) 105env = {k :v for k ,v in os .environ .items ()if k != "STRATA_PACK_THREADS" } 106with mock .patch .dict (os .environ ,env ,clear = True ): 107self .assertEqual (feed_tool .gzip_threads (),1 ) 108 109def test_the_default_archive_is_the_single_stream_gzip_of_before (self ): 110env = {k :v for k ,v in os .environ .items ()if k != "STRATA_PACK_THREADS" } 111out = self .root / "default.tar.gz" 112with mock .patch .dict (os .environ ,env ,clear = True ),mock .patch .object (feed_tool ,"_parallel_gzip" ,side_effect = AssertionError ("threaded by default" )): 113feed_tool ._write_archive (self .files ,out ,"tar.gz" ,"strata" ) 114self .assertEqual (out .read_bytes ()[:3 ],b"\x1f\x8b\x08" ) 115with gzip .open (out )as g :# one stream, as tarfile w:gz writes it 116g .read () 117 118def test_a_zip_is_unchanged (self ): 119out = self .root / "c.zip" 120with mock .patch .dict (os .environ , {"STRATA_PACK_THREADS" :"4" }),mock .patch .object (feed_tool ,"_parallel_gzip" ,side_effect = AssertionError ("zip" )): 121feed_tool ._write_archive (self .files ,out ,"zip" ,"strata.exe" ) 122self .assertTrue (out .is_file ()) 123 124 125if __name__ == "__main__" : 126unittest .main ()