aboutsummaryrefslogtreecommitdiff
path: root/tests/installer-steps/test_framework_firmware_trim.py
blob: cdb6a602e01dfd53663d6bb39a118883e0b97bcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""Tests for is_framework_intel_laptop, the gate on trim_firmware.

trim_firmware runs `pacman -Rdd` against twelve linux-firmware packages,
keeping only the three a Framework 13 Intel needs (intel, atheros, realtek).
It is the most destructive conditional in the installer, so the condition is
what these tests pin.

The gate has to be exact in both directions. Too narrow and the step is dead
code: "framework" lives in DMI's sys_vendor, never in product_name, so a gate
reading product_name alone matches no Framework machine at all. Too wide and it
removes linux-firmware-amdgpu from a Framework Desktop, whose Ryzen AI Max iGPU
needs it to bring up a display. Both machines are real: velox is
Framework/"Laptop (13th Gen Intel Core)" and must trim; ratio is
Framework/"Desktop (AMD Ryzen AI Max 300 Series)" and must not.

These tests exercise the REAL function body, extracted from the `archsetup`
script at run time (not a copy), against a fixture DMI directory.

Run from repo root:
    python3 -m unittest tests.installer-steps.test_framework_firmware_trim
"""

import os
import shutil
import subprocess
import tempfile
import unittest


REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
ARCHSETUP = os.path.join(REPO_ROOT, "archsetup")


class FrameworkIntelGateHarness(unittest.TestCase):
    """Source is_framework_intel_laptop out of the real archsetup script."""

    def setUp(self):
        self.tmp = tempfile.mkdtemp(prefix="fw-trim-test-")
        self.dmi = os.path.join(self.tmp, "dmi")
        os.mkdir(self.dmi)
        self.wrapper = os.path.join(self.tmp, "run.sh")
        with open(self.wrapper, "w") as f:
            f.write(
                "#!/bin/bash\n"
                'ARCHSETUP="$1"; shift\n'
                "source <(sed -n "
                "'/^is_framework_intel_laptop() {/,/^}/p' \"$ARCHSETUP\")\n"
                'if is_framework_intel_laptop "$DMI_DIR"; then echo "TRIM=yes"; '
                'else echo "TRIM=no"; fi\n'
            )
        os.chmod(self.wrapper, 0o755)

    def tearDown(self):
        shutil.rmtree(self.tmp, ignore_errors=True)

    def dmi_write(self, **fields):
        for name, value in fields.items():
            with open(os.path.join(self.dmi, name), "w") as f:
                f.write(value + "\n")

    def trims(self, dmi_dir=None):
        env = dict(os.environ)
        env["DMI_DIR"] = self.dmi if dmi_dir is None else dmi_dir
        r = subprocess.run(["bash", self.wrapper, ARCHSETUP],
                           capture_output=True, text=True, env=env)
        if "TRIM=yes" in r.stdout:
            return True
        if "TRIM=no" in r.stdout:
            return False
        self.fail("no TRIM line; stdout=%r stderr=%r" % (r.stdout, r.stderr))

    # ---------------------------------------------------------- normal ----
    def test_velox_framework_13_intel_trims(self):
        # The exact strings read off velox on 2026-07-24.
        self.dmi_write(sys_vendor="Framework",
                       product_name="Laptop (13th Gen Intel Core)")
        self.assertTrue(self.trims())

    def test_framework_12_intel_trims(self):
        self.dmi_write(sys_vendor="Framework",
                       product_name="Laptop 12 (13th Gen Intel Core)")
        self.assertTrue(self.trims())

    # -------------------------------------------------------- boundary ----
    def test_ratio_framework_desktop_amd_does_not_trim(self):
        # The exact strings read off ratio on 2026-07-24. Trimming here would
        # remove linux-firmware-amdgpu from the machine whose iGPU needs it.
        self.dmi_write(sys_vendor="Framework",
                       product_name="Desktop (AMD Ryzen AI Max 300 Series)")
        self.assertFalse(self.trims())

    def test_framework_16_amd_laptop_does_not_trim(self):
        self.dmi_write(sys_vendor="Framework",
                       product_name="Laptop 16 (AMD Ryzen 7040 Series)")
        self.assertFalse(self.trims())

    def test_non_framework_intel_laptop_does_not_trim(self):
        # The firmware set is only known for Framework hardware. The model
        # deliberately carries both "Laptop" and "Intel", so the vendor check
        # is the only condition that can reject it -- a fixture that failed the
        # model checks too would pass no matter what the vendor check did.
        self.dmi_write(sys_vendor="Dell Inc.",
                       product_name="Laptop 15 (Intel Core i7)")
        self.assertFalse(self.trims())

    def test_framework_desktop_naming_an_intel_cpu_does_not_trim(self):
        # No such machine ships today -- the Framework Desktop is AMD -- but
        # "an Intel Framework desktop will never exist" is exactly the kind of
        # assumption that made the old gate wrong. The model must say Laptop,
        # so a desktop is refused on the model rather than on its CPU vendor.
        self.dmi_write(sys_vendor="Framework",
                       product_name="Desktop (Intel Core Ultra)")
        self.assertFalse(self.trims())

    def test_vendor_match_is_case_insensitive(self):
        self.dmi_write(sys_vendor="framework",
                       product_name="Laptop (13th Gen intel Core)")
        self.assertTrue(self.trims())

    def test_vendor_with_surrounding_whitespace_still_matches(self):
        # DMI strings are vendor-supplied and often padded.
        self.dmi_write(sys_vendor="  Framework  ",
                       product_name="Laptop (13th Gen Intel Core)")
        self.assertTrue(self.trims())

    # ----------------------------------------------------------- error ----
    def test_missing_dmi_directory_does_not_trim(self):
        self.assertFalse(self.trims(dmi_dir=os.path.join(self.tmp, "nope")))

    def test_missing_product_name_does_not_trim(self):
        self.dmi_write(sys_vendor="Framework")
        self.assertFalse(self.trims())

    def test_missing_sys_vendor_does_not_trim(self):
        self.dmi_write(product_name="Laptop (13th Gen Intel Core)")
        self.assertFalse(self.trims())

    def test_empty_dmi_values_do_not_trim(self):
        self.dmi_write(sys_vendor="", product_name="")
        self.assertFalse(self.trims())

class GpuVendorEvidence(unittest.TestCase):
    """trim_firmware deletes GPU firmware, so what it needs to know is which
    GPU is physically present -- and the kernel already answers that.

    The DMI model string cannot. It is a marketing name: it does not know a
    swapped card, and it cannot see hardware the allowlist has never met. The
    same file already reads PCI modalias twice (install_gpu_drivers, and
    nvidia_preflight_report, which parameterises its globs for exactly this
    kind of test). detect_gpu_vendors is that read, made reusable, so the trim
    can refuse on evidence rather than on a name.
    """

    def setUp(self):
        self.tmp = tempfile.mkdtemp(prefix="gpu-vendor-test-")
        self.drm = os.path.join(self.tmp, "drm")
        self.pci = os.path.join(self.tmp, "pci")
        os.makedirs(self.drm)
        os.makedirs(self.pci)
        self.wrapper = os.path.join(self.tmp, "run.sh")
        with open(self.wrapper, "w") as f:
            f.write(
                "#!/bin/bash\n"
                'ARCHSETUP="$1"; shift\n'
                "source <(sed -n "
                "'/^detect_gpu_vendors() {/,/^}/p' \"$ARCHSETUP\")\n"
                'detect_gpu_vendors "$DRM_GLOB" "$PCI_GLOB"\n'
            )
        os.chmod(self.wrapper, 0o755)

    def tearDown(self):
        shutil.rmtree(self.tmp, ignore_errors=True)

    def card(self, root, name, modalias):
        d = os.path.join(root, name, "device")
        os.makedirs(d, exist_ok=True)
        with open(os.path.join(d, "modalias"), "w") as f:
            f.write(modalias + "\n")

    def vendors(self):
        env = dict(os.environ)
        env["DRM_GLOB"] = os.path.join(self.drm, "card*", "device", "modalias")
        env["PCI_GLOB"] = os.path.join(self.pci, "*", "device", "modalias")
        r = subprocess.run(["bash", self.wrapper, ARCHSETUP],
                           capture_output=True, text=True, env=env)
        return sorted(v for v in r.stdout.split() if v)

    # ---------------------------------------------------------- normal ----
    def test_intel_igpu_detected(self):
        self.card(self.drm, "card0", "pci:v00008086d00007D55sv00001414bc03sc00i00")
        self.assertEqual(self.vendors(), ["intel"])

    def test_amd_igpu_detected(self):
        self.card(self.drm, "card0", "pci:v00001002d0000150Esv00001414bc03sc00i00")
        self.assertEqual(self.vendors(), ["amd"])

    # -------------------------------------------------------- boundary ----
    def test_nvidia_detected_in_either_hex_case(self):
        self.card(self.drm, "card0", "pci:v000010DEd00002684bc03sc00i00")
        self.assertEqual(self.vendors(), ["nvidia"])
        shutil.rmtree(os.path.join(self.drm, "card0"))
        self.card(self.drm, "card0", "pci:v000010ded00002684bc03sc00i00")
        self.assertEqual(self.vendors(), ["nvidia"])

    def test_hybrid_machine_reports_both(self):
        self.card(self.drm, "card0", "pci:v00008086d00007D55bc03sc00i00")
        self.card(self.drm, "card1", "pci:v000010DEd00002684bc03sc00i00")
        self.assertEqual(self.vendors(), ["intel", "nvidia"])

    def test_falls_back_to_pci_when_drm_is_empty(self):
        # Early boot / chroot: no DRM nodes yet, same ruling install_gpu_drivers
        # already makes.
        self.card(self.pci, "0000:00:02.0", "pci:v00001002d0000150Ebc03sc00i00")
        self.assertEqual(self.vendors(), ["amd"])

    def test_pci_fallback_ignores_non_display_devices(self):
        # An NVIDIA audio function on the same card is bc04, not bc03.
        self.card(self.pci, "0000:01:00.1", "pci:v000010DEd000022BAbc04sc03i00")
        self.assertEqual(self.vendors(), [])

    # ----------------------------------------------------------- error ----
    def test_no_gpu_anywhere_reports_nothing(self):
        self.assertEqual(self.vendors(), [])

    def test_unreadable_modalias_is_skipped_not_fatal(self):
        d = os.path.join(self.drm, "card0", "device")
        os.makedirs(d)
        os.mkdir(os.path.join(d, "modalias"))   # a directory, not a file
        self.assertEqual(self.vendors(), [])


if __name__ == "__main__":
    unittest.main()