okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221setup: Ubuntu keeps its checked cuda-toolkit-13-0 install; Debian stops with a message when no 13.x installs (#2172 follow-up)20b501743

main
6.0 KiB115 linesraw
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
4    python -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):
22    def test_known_and_unknown_distros(self):
23        for 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):
30            with self.subTest(osr=osr):
31                self.assertEqual(setup.cuda_apt_repo(osr), want)
32
33    def 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+)
35        installed = []
36
37        def fake_run(cmd, **kw):
38            installed.append(cmd[-1])
39            return types.SimpleNamespace(returncode=100 if cmd[-1] == "cuda-toolkit-13-0" else 0)
40
41        with mock.patch.object(setup, "WIN", False), \
42                mock.patch.object(setup, "find_nvcc", side_effect=[(None, (0, 0)), ("/usr/local/cuda-13.1/bin/nvcc", (13, 1))]), \
43                mock.patch.object(setup.shutil, "which", lambda n: "/usr/bin/" + n), \
44                mock.patch("builtins.open", mock.mock_open(read_data='ID=debian\nVERSION_ID="13"\n')), \
45                mock.patch.object(setup, "download") as download, \
46                mock.patch.object(setup, "run", side_effect=fake_run), \
47                contextlib.redirect_stdout(io.StringIO()):
48            nvcc, _ = setup.install_build_tools({"arch": "86", "archs": [86]}, True)
49        self.assertEqual(nvcc, "/usr/local/cuda-13.1/bin/nvcc")
50        self.assertIn("repos/debian13/x86_64/cuda-keyring_1.1-1_all.deb", download.call_args[0][0])
51        self.assertEqual(installed[-2:], ["cuda-toolkit-13-0", "cuda-toolkit-13-1"])
52
53    def test_ubuntu_still_installs_exactly_13_0(self):
54        installed = []
55
56        def fake_run(cmd, **kw):
57            if cmd[0:2] == ["sudo", "apt-get"] and cmd[2] == "install":
58                installed.append(cmd[-1])
59            return types.SimpleNamespace(returncode=0)
60
61        with mock.patch.object(setup, "WIN", False), \
62                mock.patch.object(setup, "find_nvcc", side_effect=[(None, (0, 0)), ("/usr/local/cuda-13.0/bin/nvcc", (13, 0))]), \
63                mock.patch.object(setup.shutil, "which", lambda n: "/usr/bin/" + n), \
64                mock.patch("builtins.open", mock.mock_open(read_data='ID=ubuntu\nVERSION_ID="24.04"\n')), \
65                mock.patch.object(setup, "download"), \
66                mock.patch.object(setup, "run", side_effect=fake_run), \
67                contextlib.redirect_stdout(io.StringIO()):
68            setup.install_build_tools({"arch": "86", "archs": [86]}, True)
69        self.assertEqual(installed, ["cuda-toolkit-13-0"])
70
71    def test_debian_without_any_13_x_stops_with_guidance(self):
72        out = io.StringIO()
73        with mock.patch.object(setup, "WIN", False), \
74                mock.patch.object(setup, "find_nvcc", return_value=(None, (0, 0))), \
75                mock.patch.object(setup.shutil, "which", lambda n: "/usr/bin/" + n), \
76                mock.patch("builtins.open", mock.mock_open(read_data='ID=debian\nVERSION_ID="12"\n')), \
77                mock.patch.object(setup, "download"), \
78                mock.patch.object(setup, "run", return_value=types.SimpleNamespace(returncode=100)), \
79                contextlib.redirect_stdout(out):
80            with self.assertRaises(SystemExit):
81                setup.install_build_tools({"arch": "86", "archs": [86]}, True)
82        self.assertIn("no cuda-toolkit-13-x package", out.getvalue())
83
84    def test_ubuntu_install_failure_still_stops_setup(self):
85        def fake_run(cmd, check=True, **kw):
86            failed = cmd[-1] == "cuda-toolkit-13-0"
87            if failed and check:
88                raise SystemExit(1)                  # setup.run's own fail() on a checked command
89            return types.SimpleNamespace(returncode=100 if failed else 0)
90
91        with mock.patch.object(setup, "WIN", False), \
92                mock.patch.object(setup, "find_nvcc", return_value=(None, (0, 0))), \
93                mock.patch.object(setup.shutil, "which", lambda n: "/usr/bin/" + n), \
94                mock.patch("builtins.open", mock.mock_open(read_data='ID=ubuntu\nVERSION_ID="22.04"\n')), \
95                mock.patch.object(setup, "download"), \
96                mock.patch.object(setup, "run", side_effect=fake_run), \
97                contextlib.redirect_stdout(io.StringIO()):
98            with self.assertRaises(SystemExit):
99                setup.install_build_tools({"arch": "86", "archs": [86]}, True)
100
101    def test_unknown_distro_stops_with_guidance(self):
102        out = io.StringIO()
103        with mock.patch.object(setup, "WIN", False), \
104                mock.patch.object(setup, "find_nvcc", return_value=(None, (0, 0))), \
105                mock.patch.object(setup.shutil, "which", lambda n: "/usr/bin/" + n), \
106                mock.patch("builtins.open", mock.mock_open(read_data='ID=fedora\nVERSION_ID="42"\n')), \
107                mock.patch.object(setup, "download", side_effect=AssertionError("download started")), \
108                contextlib.redirect_stdout(out):
109            with self.assertRaises(SystemExit):
110                setup.install_build_tools({"arch": "86", "archs": [86]}, True)
111        self.assertIn("Debian 12 / 13", out.getvalue())
112
113
114if __name__ == "__main__":
115    unittest.main()