okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
20b501743
main
1"""The CUDA Toolkit apt install: which distros it covers and which 13.x package it picks 2(Debian 13's repo has no cuda-toolkit-13-0). No GPU, downloads or installs are used. 3 4python -m unittest tools.test_setup_cuda_repo 5""" 6from __future__import annotations 7 8import contextlib 9import io 10import sys 11import types 12import unittest 13from pathlib import Path 14from unittest import mock 15 16ROOT = Path (__file__ ).resolve ().parents [1 ] 17sys .path .insert (0 ,str (ROOT )) 18import setup # noqa: E402 19 20 21class AptRepo (unittest .TestCase ): 22def test_known_and_unknown_distros (self ): 23for osr ,want in ({"ID" :"ubuntu" ,"VERSION_ID" :'"22.04"' },"ubuntu2204" ), \ 24 ({"ID" :"ubuntu" ,"VERSION_ID" :'"24.04"' },"ubuntu2404" ), \ 25 ({"ID" :"debian" ,"VERSION_ID" :'"12"' },"debian12" ), \ 26 ({"ID" :"debian" ,"VERSION_ID" :'"13"' },"debian13" ), \ 27 ({"ID" :"debian" ,"VERSION_ID" :'"11"' },None ), \ 28 ({"ID" :"fedora" ,"VERSION_ID" :'"42"' },None ), \ 29 ({},None ): 30with self .subTest (osr = osr ): 31self .assertEqual (setup .cuda_apt_repo (osr ),want ) 32 33def test_debian_13_falls_back_to_the_oldest_13_x_it_has (self ): 34# the repo has no 13-0: apt fails on it, setup must try 13-1 and stop there (not install 13-2+) 35installed = [] 36 37def fake_run (cmd ,** kw ): 38installed .append (cmd [- 1 ]) 39return types .SimpleNamespace (returncode = 100 if cmd [- 1 ]== "cuda-toolkit-13-0" else 0 ) 40 41with mock .patch .object (setup ,"WIN" ,False ), \ 42mock .patch .object (setup ,"find_nvcc" ,side_effect = [(None , (0 ,0 )), ("/usr/local/cuda-13.1/bin/nvcc" , (13 ,1 ))]), \ 43mock .patch .object (setup .shutil ,"which" ,lambda n :"/usr/bin/" + n ), \ 44mock .patch ("builtins.open" ,mock .mock_open (read_data = 'ID=debian\nVERSION_ID="13"\n' )), \ 45mock .patch .object (setup ,"download" )as download , \ 46mock .patch .object (setup ,"run" ,side_effect = fake_run ), \ 47contextlib .redirect_stdout (io .StringIO ()): 48nvcc ,_ = setup .install_build_tools ({"arch" :"86" ,"archs" : [86 ]},True ) 49self .assertEqual (nvcc ,"/usr/local/cuda-13.1/bin/nvcc" ) 50self .assertIn ("repos/debian13/x86_64/cuda-keyring_1.1-1_all.deb" ,download .call_args [0 ][0 ]) 51self .assertEqual (installed [- 2 :], ["cuda-toolkit-13-0" ,"cuda-toolkit-13-1" ]) 52 53def test_ubuntu_still_installs_exactly_13_0 (self ): 54installed = [] 55 56def fake_run (cmd ,** kw ): 57if cmd [0 :2 ]== ["sudo" ,"apt-get" ]and cmd [2 ]== "install" : 58installed .append (cmd [- 1 ]) 59return types .SimpleNamespace (returncode = 0 ) 60 61with mock .patch .object (setup ,"WIN" ,False ), \ 62mock .patch .object (setup ,"find_nvcc" ,side_effect = [(None , (0 ,0 )), ("/usr/local/cuda-13.0/bin/nvcc" , (13 ,0 ))]), \ 63mock .patch .object (setup .shutil ,"which" ,lambda n :"/usr/bin/" + n ), \ 64mock .patch ("builtins.open" ,mock .mock_open (read_data = 'ID=ubuntu\nVERSION_ID="24.04"\n' )), \ 65mock .patch .object (setup ,"download" ), \ 66mock .patch .object (setup ,"run" ,side_effect = fake_run ), \ 67contextlib .redirect_stdout (io .StringIO ()): 68setup .install_build_tools ({"arch" :"86" ,"archs" : [86 ]},True ) 69self .assertEqual (installed , ["cuda-toolkit-13-0" ]) 70 71def test_debian_without_any_13_x_stops_with_guidance (self ): 72out = io .StringIO () 73with mock .patch .object (setup ,"WIN" ,False ), \ 74mock .patch .object (setup ,"find_nvcc" ,return_value = (None , (0 ,0 ))), \ 75mock .patch .object (setup .shutil ,"which" ,lambda n :"/usr/bin/" + n ), \ 76mock .patch ("builtins.open" ,mock .mock_open (read_data = 'ID=debian\nVERSION_ID="12"\n' )), \ 77mock .patch .object (setup ,"download" ), \ 78mock .patch .object (setup ,"run" ,return_value = types .SimpleNamespace (returncode = 100 )), \ 79contextlib .redirect_stdout (out ): 80with self .assertRaises (SystemExit ): 81setup .install_build_tools ({"arch" :"86" ,"archs" : [86 ]},True ) 82self .assertIn ("no cuda-toolkit-13-x package" ,out .getvalue ()) 83 84def test_ubuntu_install_failure_still_stops_setup (self ): 85def fake_run (cmd ,check = True ,** kw ): 86failed = cmd [- 1 ]== "cuda-toolkit-13-0" 87if failed and check : 88raise SystemExit (1 )# setup.run's own fail() on a checked command 89return types .SimpleNamespace (returncode = 100 if failed else 0 ) 90 91with mock .patch .object (setup ,"WIN" ,False ), \ 92mock .patch .object (setup ,"find_nvcc" ,return_value = (None , (0 ,0 ))), \ 93mock .patch .object (setup .shutil ,"which" ,lambda n :"/usr/bin/" + n ), \ 94mock .patch ("builtins.open" ,mock .mock_open (read_data = 'ID=ubuntu\nVERSION_ID="22.04"\n' )), \ 95mock .patch .object (setup ,"download" ), \ 96mock .patch .object (setup ,"run" ,side_effect = fake_run ), \ 97contextlib .redirect_stdout (io .StringIO ()): 98with self .assertRaises (SystemExit ): 99setup .install_build_tools ({"arch" :"86" ,"archs" : [86 ]},True ) 100 101def test_unknown_distro_stops_with_guidance (self ): 102out = io .StringIO () 103with mock .patch .object (setup ,"WIN" ,False ), \ 104mock .patch .object (setup ,"find_nvcc" ,return_value = (None , (0 ,0 ))), \ 105mock .patch .object (setup .shutil ,"which" ,lambda n :"/usr/bin/" + n ), \ 106mock .patch ("builtins.open" ,mock .mock_open (read_data = 'ID=fedora\nVERSION_ID="42"\n' )), \ 107mock .patch .object (setup ,"download" ,side_effect = AssertionError ("download started" )), \ 108contextlib .redirect_stdout (out ): 109with self .assertRaises (SystemExit ): 110setup .install_build_tools ({"arch" :"86" ,"archs" : [86 ]},True ) 111self .assertIn ("Debian 12 / 13" ,out .getvalue ()) 112 113 114if __name__ == "__main__" : 115unittest .main ()