| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "regexp" |
| 13 | "slices" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | |
| 17 | "amadan.net/oficina/equipo/internal/testci" |
| 18 | ) |
| 19 | |
| 20 | // rendererPin is internal/release/renderer.json: the renderer's full |
| 21 | // package closure for Linux amd64, data for the release build only. |
| 22 | type rendererPin struct { |
| 23 | Version string `json:"version"` |
| 24 | Snapshots map[string]string `json:"snapshots"` |
| 25 | Program string `json:"program"` |
| 26 | AMD64 []rendererPackage `json:"amd64"` |
| 27 | ARM64 json.RawMessage `json:"arm64,omitempty"` |
| 28 | } |
| 29 | |
| 30 | type rendererPackage struct { |
| 31 | Package string `json:"package"` |
| 32 | Version string `json:"version"` |
| 33 | Architecture string `json:"architecture"` |
| 34 | URL string `json:"url"` |
| 35 | SHA256 string `json:"sha256"` |
| 36 | Size int64 `json:"size"` |
| 37 | Base bool `json:"base"` |
| 38 | } |
| 39 | |
| 40 | type ciChromiumPin struct { |
| 41 | Version string `json:"version"` |
| 42 | Snapshots map[string]string `json:"snapshots"` |
| 43 | Program string `json:"program"` |
| 44 | AMD64 []rendererPackage `json:"amd64"` |
| 45 | } |
| 46 | |
| 47 | var pinDigest = regexp.MustCompile(`^[a-f0-9]{64}$`) |
| 48 | |
| 49 | // rendererPinProblems lists every way renderer disagrees with ci (both raw |
| 50 | // JSON): the browser packages, those not marked base, must be exactly |
| 51 | // ci-chromium.json's amd64 packages at the same versions, URLs and |
| 52 | // digests; the rest are the Debian base the CI image already has, so they |
| 53 | // are not in ci-chromium.json. Each entry is fetchable and checkable. |
| 54 | func rendererPinProblems(ciRaw, rendererRaw []byte) []string { |
| 55 | var ci ciChromiumPin |
| 56 | var r rendererPin |
| 57 | if err := json.Unmarshal(ciRaw, &ci); err != nil { |
| 58 | return []string{"ci-chromium.json: " + err.Error()} |
| 59 | } |
| 60 | if err := json.Unmarshal(rendererRaw, &r); err != nil { |
| 61 | return []string{"renderer.json: " + err.Error()} |
| 62 | } |
| 63 | var problems []string |
| 64 | add := func(format string, args ...any) { problems = append(problems, fmt.Sprintf(format, args...)) } |
| 65 | if r.Version != ci.Version || r.Program != ci.Program || fmt.Sprint(r.Snapshots) != fmt.Sprint(ci.Snapshots) { |
| 66 | add("renderer pins %s %s %v, CI pins %s %s %v", r.Version, r.Program, r.Snapshots, ci.Version, ci.Program, ci.Snapshots) |
| 67 | } |
| 68 | if r.ARM64 != nil { |
| 69 | add("renderer.json pins arm64; the renderer is Linux amd64 only") |
| 70 | } |
| 71 | ciByName := map[string]rendererPackage{} |
| 72 | for _, p := range ci.AMD64 { |
| 73 | ciByName[p.Package] = p |
| 74 | } |
| 75 | seen := map[string]bool{} |
| 76 | var names []string |
| 77 | base := false |
| 78 | for _, p := range r.AMD64 { |
| 79 | names = append(names, p.Package) |
| 80 | if seen[p.Package] { |
| 81 | add("%s pinned twice", p.Package) |
| 82 | } |
| 83 | seen[p.Package] = true |
| 84 | if !strings.HasPrefix(p.URL, "https://snapshot.debian.org/archive/") || !strings.HasSuffix(p.URL, ".deb") || |
| 85 | !pinDigest.MatchString(p.SHA256) || p.Size <= 0 || p.Version == "" || |
| 86 | (p.Architecture != "amd64" && p.Architecture != "all") { |
| 87 | add("%s: want a snapshot .deb URL, a SHA-256, a size, a version and amd64 or all, got %+v", p.Package, p) |
| 88 | } |
| 89 | want, browser := ciByName[p.Package] |
| 90 | switch { |
| 91 | case p.Base && browser: |
| 92 | add("%s is a browser package in ci-chromium.json but marked base", p.Package) |
| 93 | case !p.Base && !browser: |
| 94 | add("%s %s is not in ci-chromium.json and not marked base", p.Package, p.Version) |
| 95 | case browser && (p.Version != want.Version || p.URL != want.URL || p.SHA256 != want.SHA256 || p.Size != want.Size): |
| 96 | add("%s: renderer pins %s %s %s, CI pins %s %s %s", p.Package, p.Version, p.URL, p.SHA256, want.Version, want.URL, want.SHA256) |
| 97 | } |
| 98 | base = base || p.Base |
| 99 | } |
| 100 | for _, p := range ci.AMD64 { |
| 101 | if !seen[p.Package] { |
| 102 | add("%s %s is in ci-chromium.json but not in renderer.json", p.Package, p.Version) |
| 103 | } |
| 104 | } |
| 105 | if !slices.IsSorted(names) { |
| 106 | add("renderer.json packages are not sorted by name") |
| 107 | } |
| 108 | if !base { |
| 109 | add("renderer.json pins no base packages: the tree would need the host's") |
| 110 | } |
| 111 | return problems |
| 112 | } |
| 113 | |
| 114 | // The renderer tree is the CI browser plus the base it needs: the same |
| 115 | // browser packages at the same versions as scripts/ci-chromium.json |
| 116 | // (one script writes both, so CI and runners move together), and the |
| 117 | // loader and libc from the pin, not the host. |
| 118 | func TestRendererPinMatchesCIChromium(t *testing.T) { |
| 119 | ciRaw, err := os.ReadFile("../../scripts/ci-chromium.json") |
| 120 | if err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | rendererRaw, err := os.ReadFile("renderer.json") |
| 124 | if err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | for _, problem := range rendererPinProblems(ciRaw, rendererRaw) { |
| 128 | t.Error(problem) |
| 129 | } |
| 130 | var r rendererPin |
| 131 | if err = json.Unmarshal(rendererRaw, &r); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | pinned := map[string]rendererPackage{} |
| 135 | for _, p := range r.AMD64 { |
| 136 | pinned[p.Package] = p |
| 137 | } |
| 138 | // No pruning of the base: the loader, libc and debconf come from the pin. |
| 139 | for _, name := range []string{"libc6", "libgcc-s1", "debconf", "zlib1g"} { |
| 140 | if p, ok := pinned[name]; !ok || !p.Base { |
| 141 | t.Errorf("%s: want it pinned as base, got %+v (present %v)", name, p, ok) |
| 142 | } |
| 143 | } |
| 144 | if p := pinned["chromium-headless-shell"]; p.Version != r.Version || p.Base { |
| 145 | t.Errorf("chromium-headless-shell: want %s as a browser package, got %+v", r.Version, p) |
| 146 | } |
| 147 | |
| 148 | // The check itself: an untouched copy passes, and each kind of drift |
| 149 | // between the two files is reported as that drift. |
| 150 | tamper := func(name, want string, edit func(r *rendererPin)) { |
| 151 | t.Run(name, func(t *testing.T) { |
| 152 | var copied rendererPin |
| 153 | if err := json.Unmarshal(rendererRaw, &copied); err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | edit(&copied) |
| 157 | raw, err := json.Marshal(copied) |
| 158 | if err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | problems := rendererPinProblems(ciRaw, raw) |
| 162 | if want == "" && len(problems) != 0 || want != "" && (len(problems) != 1 || !strings.Contains(problems[0], want)) { |
| 163 | t.Errorf("got %q, want only a problem containing %q", problems, want) |
| 164 | } |
| 165 | }) |
| 166 | } |
| 167 | index := func(r *rendererPin, name string) int { |
| 168 | i := slices.IndexFunc(r.AMD64, func(p rendererPackage) bool { return p.Package == name }) |
| 169 | if i < 0 { |
| 170 | panic("no " + name + " in renderer.json") |
| 171 | } |
| 172 | return i |
| 173 | } |
| 174 | tamper("untouched", "", func(r *rendererPin) {}) |
| 175 | tamper("browser version differs", "libnss3: renderer pins 2:3.110-1+deb13u5+1", func(r *rendererPin) { r.AMD64[index(r, "libnss3")].Version += "+1" }) |
| 176 | tamper("browser digest differs", "chromium-common: renderer pins", func(r *rendererPin) { |
| 177 | r.AMD64[index(r, "chromium-common")].SHA256 = strings.Repeat("0", 64) |
| 178 | }) |
| 179 | tamper("browser package missing", "libnss3 2:3.110-1+deb13u5 is in ci-chromium.json but not in renderer.json", func(r *rendererPin) { |
| 180 | r.AMD64 = slices.Delete(r.AMD64, index(r, "libnss3"), index(r, "libnss3")+1) |
| 181 | }) |
| 182 | tamper("browser package marked base", "libnss3 is a browser package", func(r *rendererPin) { r.AMD64[index(r, "libnss3")].Base = true }) |
| 183 | tamper("unknown package not base", "libc6 2.41-12+deb13u4 is not in ci-chromium.json", func(r *rendererPin) { r.AMD64[index(r, "libc6")].Base = false }) |
| 184 | tamper("shell version differs", "renderer pins 1.0-1", func(r *rendererPin) { r.Version = "1.0-1" }) |
| 185 | tamper("no base", "pins no base packages", func(r *rendererPin) { |
| 186 | r.AMD64 = slices.DeleteFunc(r.AMD64, func(p rendererPackage) bool { return p.Base }) |
| 187 | }) |
| 188 | tamper("architecture missing", "libc6: want a snapshot .deb URL", func(r *rendererPin) { r.AMD64[index(r, "libc6")].Architecture = "" }) |
| 189 | tamper("arm64 pinned", "pins arm64", func(r *rendererPin) { r.ARM64 = json.RawMessage(`[]`) }) |
| 190 | } |
| 191 | |
| 192 | // pinFixture is a tiny Debian archive: trixie and trixie-security Packages |
| 193 | // indexes for both architectures, as the pin script reads them. |
| 194 | func pinFixture() map[string]string { |
| 195 | const debian, security = "20990101T000000Z", "20990102T000000Z" |
| 196 | type pkg struct{ name, version, arch, priority, depends, provides string } |
| 197 | release := []pkg{ |
| 198 | {"chromium-headless-shell", "1.0-1", "", "optional", "chromium-common (= 1.0-1), libc6 (>= 2.38), libfoo1", ""}, |
| 199 | {"chromium-common", "1.0-1", "", "optional", "libc6, x11-utils, libnope | virtual-thing", ""}, |
| 200 | {"libfoo1", "1.0-1", "", "optional", "zlib1g", ""}, |
| 201 | {"libvirt-impl", "2.0-1", "all", "optional", "", "virtual-thing (= 2.0)"}, |
| 202 | {"x11-utils", "7.7-1", "", "optional", "libxt6", ""}, |
| 203 | {"libxt6", "1.2-1", "", "optional", "", ""}, |
| 204 | {"libc6", "2.41-1", "", "required", "libgcc-s1", ""}, |
| 205 | {"libgcc-s1", "14.2-1", "", "required", "", ""}, |
| 206 | {"zlib1g", "1:1.3-1", "", "required", "", ""}, |
| 207 | {"dash", "0.5-1", "", "required", "", ""}, |
| 208 | {"unrelated", "1.0-1", "", "optional", "libc6", ""}, |
| 209 | } |
| 210 | updates := []pkg{ |
| 211 | {"chromium-headless-shell", "1.0-1~deb13u1", "", "optional", "chromium-common (= 1.0-1~deb13u1), libc6 (>= 2.38), libfoo1", ""}, |
| 212 | {"chromium-common", "1.0-1~deb13u1", "", "optional", "libc6, x11-utils, libnope | virtual-thing", ""}, |
| 213 | {"libfoo1", "1.0-1+deb13u1", "", "optional", "zlib1g", ""}, |
| 214 | } |
| 215 | write := func(archive, stamp string, packages []pkg, arch string) string { |
| 216 | var b strings.Builder |
| 217 | for _, p := range packages { |
| 218 | a := arch |
| 219 | if p.arch != "" { |
| 220 | a = p.arch |
| 221 | } |
| 222 | deb := fmt.Sprintf("pool/main/%c/%s/%s_%s_%s.deb", p.name[0], p.name, p.name, p.version, a) |
| 223 | sum := sha256.Sum256([]byte(archive + "/" + deb)) |
| 224 | fmt.Fprintf(&b, "Package: %s\nVersion: %s\nArchitecture: %s\nPriority: %s\n", p.name, p.version, a, p.priority) |
| 225 | if p.depends != "" { |
| 226 | fmt.Fprintf(&b, "Depends: %s\n", p.depends) |
| 227 | } |
| 228 | if p.provides != "" { |
| 229 | fmt.Fprintf(&b, "Provides: %s\n", p.provides) |
| 230 | } |
| 231 | fmt.Fprintf(&b, "Description: fixture\n multi-line field\nFilename: %s\nSize: %d\nSHA256: %s\n\n", |
| 232 | deb, 1000+len(p.name), hex.EncodeToString(sum[:])) |
| 233 | } |
| 234 | return b.String() |
| 235 | } |
| 236 | fixture := map[string]string{} |
| 237 | for _, arch := range []string{"amd64", "arm64"} { |
| 238 | fixture[fmt.Sprintf("https://snapshot.debian.org/archive/debian/%s/dists/trixie/main/binary-%s/Packages.xz", debian, arch)] = write("debian", debian, release, arch) |
| 239 | fixture[fmt.Sprintf("https://snapshot.debian.org/archive/debian-security/%s/dists/trixie-security/main/binary-%s/Packages.xz", security, arch)] = write("debian-security", security, updates, arch) |
| 240 | } |
| 241 | return fixture |
| 242 | } |
| 243 | |
| 244 | // runPin runs scripts/ci-chromium-pin.py against the fixture archive, |
| 245 | // writing its two files into dir. |
| 246 | func runPin(t *testing.T, dir, version string) ([]byte, error) { |
| 247 | t.Helper() |
| 248 | fixture, err := json.Marshal(pinFixture()) |
| 249 | if err != nil { |
| 250 | t.Fatal(err) |
| 251 | } |
| 252 | const prelude = ` |
| 253 | import importlib.util, json, lzma, os, sys |
| 254 | spec = importlib.util.spec_from_file_location("pin", "../../scripts/ci-chromium-pin.py") |
| 255 | m = importlib.util.module_from_spec(spec) |
| 256 | spec.loader.exec_module(m) |
| 257 | fixture, out = json.loads(sys.argv[1]), sys.argv[2] |
| 258 | m.fetch = lambda url: lzma.compress(fixture[url].encode()) |
| 259 | m.OUT = os.path.join(out, "ci-chromium.json") |
| 260 | m.RENDERER_OUT = os.path.join(out, "renderer.json") |
| 261 | sys.exit(m.main(sys.argv[3:])) |
| 262 | ` |
| 263 | cmd := exec.Command("python3", "-I", "-c", prelude, string(fixture), dir, version, "20990101T000000Z", "20990102T000000Z") |
| 264 | cmd.Env = append(os.Environ(), "PYTHONDONTWRITEBYTECODE=1") |
| 265 | return cmd.CombinedOutput() |
| 266 | } |
| 267 | |
| 268 | // The pin script writes both files from one resolution: ci-chromium.json |
| 269 | // without the Debian base (the CI image has it), renderer.json for amd64 |
| 270 | // with it, the browser packages identical in both. Its output does not |
| 271 | // depend on when it runs, and a snapshot without the wanted version |
| 272 | // writes neither file. |
| 273 | func TestRendererPinScriptWritesBoth(t *testing.T) { |
| 274 | testci.Files(t, "../../scripts/ci-chromium-pin.py") |
| 275 | testci.Tools(t, "python3") |
| 276 | dir := t.TempDir() |
| 277 | out, err := runPin(t, dir, "1.0-1~deb13u1") |
| 278 | if err != nil { |
| 279 | t.Fatalf("pin: %v\n%s", err, out) |
| 280 | } |
| 281 | ciRaw, err := os.ReadFile(filepath.Join(dir, "ci-chromium.json")) |
| 282 | if err != nil { |
| 283 | t.Fatal(err) |
| 284 | } |
| 285 | rendererRaw, err := os.ReadFile(filepath.Join(dir, "renderer.json")) |
| 286 | if err != nil { |
| 287 | t.Fatal(err) |
| 288 | } |
| 289 | for _, problem := range rendererPinProblems(ciRaw, rendererRaw) { |
| 290 | t.Error(problem) |
| 291 | } |
| 292 | |
| 293 | var ci struct { |
| 294 | AMD64, ARM64 []rendererPackage |
| 295 | } |
| 296 | if err = json.Unmarshal(ciRaw, &ci); err != nil { |
| 297 | t.Fatal(err) |
| 298 | } |
| 299 | var r rendererPin |
| 300 | if err = json.Unmarshal(rendererRaw, &r); err != nil { |
| 301 | t.Fatal(err) |
| 302 | } |
| 303 | names := func(packages []rendererPackage, base bool) []string { |
| 304 | var out []string |
| 305 | for _, p := range packages { |
| 306 | if p.Base == base { |
| 307 | out = append(out, p.Package) |
| 308 | } |
| 309 | } |
| 310 | return out |
| 311 | } |
| 312 | browser := []string{"chromium-common", "chromium-headless-shell", "libfoo1", "libvirt-impl"} |
| 313 | for arch, packages := range map[string][]rendererPackage{"amd64": ci.AMD64, "arm64": ci.ARM64} { |
| 314 | if got := names(packages, false); !slices.Equal(got, browser) { |
| 315 | t.Errorf("ci-chromium.json %s: got %v, want %v", arch, got, browser) |
| 316 | } |
| 317 | } |
| 318 | if got := names(r.AMD64, false); !slices.Equal(got, browser) { |
| 319 | t.Errorf("renderer.json browser packages: got %v, want %v", got, browser) |
| 320 | } |
| 321 | // The base the browser needs, transitively, and nothing else from it. |
| 322 | if got, want := names(r.AMD64, true), []string{"libc6", "libgcc-s1", "zlib1g"}; !slices.Equal(got, want) { |
| 323 | t.Errorf("renderer.json base packages: got %v, want %v", got, want) |
| 324 | } |
| 325 | byName := map[string]rendererPackage{} |
| 326 | for _, p := range r.AMD64 { |
| 327 | byName[p.Package] = p |
| 328 | } |
| 329 | if p := byName["libfoo1"]; p.Version != "1.0-1+deb13u1" || !strings.Contains(p.URL, "/debian-security/20990102T000000Z/pool/") || p.Architecture != "amd64" { |
| 330 | t.Errorf("libfoo1: want the security update, got %+v", p) |
| 331 | } |
| 332 | if p := byName["libvirt-impl"]; p.Architecture != "all" || !strings.HasSuffix(p.URL, "_all.deb") { |
| 333 | t.Errorf("libvirt-impl: want the arch-independent deb, got %+v", p) |
| 334 | } |
| 335 | if p := byName["zlib1g"]; p.Version != "1:1.3-1" || p.Size != 1006 || !pinDigest.MatchString(p.SHA256) { |
| 336 | t.Errorf("zlib1g: got %+v", p) |
| 337 | } |
| 338 | |
| 339 | // Same archive, same bytes. |
| 340 | again := t.TempDir() |
| 341 | if out, err = runPin(t, again, "1.0-1~deb13u1"); err != nil { |
| 342 | t.Fatalf("pin again: %v\n%s", err, out) |
| 343 | } |
| 344 | for _, name := range []string{"ci-chromium.json", "renderer.json"} { |
| 345 | first, _ := os.ReadFile(filepath.Join(dir, name)) |
| 346 | second, _ := os.ReadFile(filepath.Join(again, name)) |
| 347 | if !bytes.Equal(first, second) { |
| 348 | t.Errorf("%s differs between two runs on the same archive", name) |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | // A version the snapshots do not have: an error, and neither file. |
| 353 | none := t.TempDir() |
| 354 | if out, err = runPin(t, none, "2.0-1"); err == nil { |
| 355 | t.Fatalf("pin of a missing version succeeded:\n%s", out) |
| 356 | } |
| 357 | if entries, _ := os.ReadDir(none); len(entries) != 0 { |
| 358 | t.Errorf("a failed pin wrote %v", entries) |
| 359 | } |
| 360 | } |
| 361 | |