| 1 | package testci_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider/claude" |
| 11 | "amadan.net/oficina/equipo/internal/runner/provider/codex" |
| 12 | "amadan.net/oficina/equipo/internal/runner/provider/opencode" |
| 13 | "amadan.net/oficina/equipo/internal/runner/provider/pi" |
| 14 | ) |
| 15 | |
| 16 | // The CI box fetches the pinned programs by the digests in |
| 17 | // scripts/ci-box.json. They must be the ones the tests check: the |
| 18 | // certified pins on amd64 and the arm64 pins beside them. |
| 19 | func TestBoxPinsMatchTheCode(t *testing.T) { |
| 20 | raw, err := os.ReadFile("../../scripts/ci-box.json") |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | var box struct { |
| 25 | Image string |
| 26 | Tools map[string]map[string]json.RawMessage |
| 27 | } |
| 28 | if err = json.Unmarshal(raw, &box); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | if !regexp.MustCompile(`@sha256:[a-f0-9]{64}$`).MatchString(box.Image) { |
| 32 | t.Errorf("the box image %q is not pinned by digest", box.Image) |
| 33 | } |
| 34 | digest := regexp.MustCompile(`^[a-f0-9]{64}$`) |
| 35 | for _, arch := range []string{"amd64", "arm64"} { |
| 36 | executable, host, ok := codex.ReleasePins(arch) |
| 37 | pin, okClaude := claude.ReleasePin(arch) |
| 38 | if !ok || !okClaude { |
| 39 | t.Fatalf("no pins for %s", arch) |
| 40 | } |
| 41 | want := map[string]string{"codex": executable, "codex-code-mode-host": host, "claude": pin, "bwrap": ""} |
| 42 | for name, sum := range want { |
| 43 | var entry struct{ URL, SHA256 string } |
| 44 | if err = json.Unmarshal(box.Tools[name][arch], &entry); err != nil { |
| 45 | t.Errorf("%s %s: %v", name, arch, err) |
| 46 | continue |
| 47 | } |
| 48 | if !strings.HasPrefix(entry.URL, "https://") || !digest.MatchString(entry.SHA256) { |
| 49 | t.Errorf("%s %s: want an https URL and a SHA-256, got %q %q", name, arch, entry.URL, entry.SHA256) |
| 50 | } |
| 51 | if sum != "" && entry.SHA256 != sum { |
| 52 | t.Errorf("%s %s: the box fetches %s, the code pins %s", name, arch, entry.SHA256, sum) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | // Every other program the box fetches is pinned for both architectures too. |
| 57 | for name, tool := range box.Tools { |
| 58 | for _, arch := range []string{"amd64", "arm64"} { |
| 59 | var entry struct{ URL, SHA256 string } |
| 60 | if err = json.Unmarshal(tool[arch], &entry); err != nil || !strings.HasPrefix(entry.URL, "https://") || !digest.MatchString(entry.SHA256) { |
| 61 | t.Errorf("%s %s: want an https URL and a SHA-256, got %q %q (%v)", name, arch, entry.URL, entry.SHA256, err) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // The box's Chromium is a tree of Debian packages (scripts/ci-chromium.json, |
| 68 | // written by scripts/ci-chromium-pin.py): every one fetched by an https URL |
| 69 | // and checked by SHA-256, on both architectures, and the headless shell |
| 70 | // among them at the pinned version. |
| 71 | func TestBoxChromiumPinned(t *testing.T) { |
| 72 | raw, err := os.ReadFile("../../scripts/ci-chromium.json") |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | var pinned map[string]json.RawMessage |
| 77 | if err = json.Unmarshal(raw, &pinned); err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | var version string |
| 81 | if err = json.Unmarshal(pinned["version"], &version); err != nil || version == "" { |
| 82 | t.Fatalf("no version: %v", err) |
| 83 | } |
| 84 | digest := regexp.MustCompile(`^[a-f0-9]{64}$`) |
| 85 | for _, arch := range []string{"amd64", "arm64"} { |
| 86 | var packages []struct{ Package, Version, URL, SHA256 string } |
| 87 | if err = json.Unmarshal(pinned[arch], &packages); err != nil || len(packages) == 0 { |
| 88 | t.Fatalf("%s: no packages (%v)", arch, err) |
| 89 | } |
| 90 | shell := false |
| 91 | for _, p := range packages { |
| 92 | if !strings.HasPrefix(p.URL, "https://") || !digest.MatchString(p.SHA256) { |
| 93 | t.Errorf("%s %s: want an https URL and a SHA-256, got %q %q", arch, p.Package, p.URL, p.SHA256) |
| 94 | } |
| 95 | shell = shell || (p.Package == "chromium-headless-shell" && p.Version == version) |
| 96 | } |
| 97 | if !shell { |
| 98 | t.Errorf("%s: no chromium-headless-shell %s among the pinned packages", arch, version) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // The CI box installs the pinned Pi for the real-Pi tests: Node by the |
| 104 | // archive and program digests the code pins, and the tree the lockfile |
| 105 | // makes, checked against the committed tree digest. They are the code's. |
| 106 | func TestBoxPiMatchesTheCode(t *testing.T) { |
| 107 | raw, err := os.ReadFile("../../scripts/ci-box.json") |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | var box struct { |
| 112 | Pi struct { |
| 113 | Version, Lockfile, Box string |
| 114 | TreeSHA256 string `json:"tree_sha256"` |
| 115 | Node map[string]struct { |
| 116 | URL, Root, SHA256 string |
| 117 | ArchiveSHA256 string `json:"archive_sha256"` |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | if err = json.Unmarshal(raw, &box); err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | if box.Pi.Version != pi.Version || box.Pi.Lockfile != "internal/runner/provider/pi/npm" || box.Pi.Box != "/opt/equipo-pi" { |
| 125 | t.Fatalf("pi %+v", box.Pi) |
| 126 | } |
| 127 | for _, arch := range []string{"amd64", "arm64"} { |
| 128 | tree, node, ok := pi.Pins(arch) |
| 129 | got := box.Pi.Node[arch] |
| 130 | if !ok || box.Pi.TreeSHA256 != tree || got.URL != node.URL || got.ArchiveSHA256 != node.SHA256 || got.SHA256 != node.BinarySHA256 || got.Root != node.Root { |
| 131 | t.Errorf("%s: the box installs %+v and tree %s, the code pins %+v and tree %s", arch, got, box.Pi.TreeSHA256, node, tree) |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // The CI box installs the pinned OpenCode for the real-OpenCode tests: |
| 137 | // the two packages the lockfile names, by the lockfile's integrities, the |
| 138 | // tree and the binary checked against the committed digests. They are |
| 139 | // the code's. |
| 140 | func TestBoxOpenCodeMatchesTheCode(t *testing.T) { |
| 141 | raw, err := os.ReadFile("../../scripts/ci-box.json") |
| 142 | if err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | type pkg struct { |
| 146 | Package, URL, Integrity string |
| 147 | BinarySHA256 string `json:"binary_sha256"` |
| 148 | TreeSHA256 string `json:"tree_sha256"` |
| 149 | } |
| 150 | var box struct { |
| 151 | OpenCode struct { |
| 152 | Version, Lockfile, Box string |
| 153 | CLI pkg |
| 154 | Platforms map[string]pkg |
| 155 | } |
| 156 | } |
| 157 | if err = json.Unmarshal(raw, &box); err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | oc := box.OpenCode |
| 161 | cli, ok := opencode.Lock(opencode.PackageName) |
| 162 | if oc.Version != opencode.Version || oc.Lockfile != "internal/runner/provider/opencode/npm" || oc.Box != "/opt/equipo-opencode" || !ok || oc.CLI.URL != cli.Resolved || oc.CLI.Integrity != cli.Integrity || cli.Integrity != opencode.Integrity { |
| 163 | t.Fatalf("opencode %+v", oc) |
| 164 | } |
| 165 | for _, arch := range []string{"amd64", "arm64"} { |
| 166 | tree, platform, ok := opencode.Pins(arch) |
| 167 | lock, locked := opencode.Lock(platform.Package) |
| 168 | got := oc.Platforms[arch] |
| 169 | if !ok || !locked || got.Package != platform.Package || got.URL != lock.Resolved || got.Integrity != lock.Integrity || got.BinarySHA256 != platform.BinarySHA256 || got.TreeSHA256 != tree { |
| 170 | t.Errorf("%s: the box installs %+v, the code pins %+v and tree %s", arch, got, platform, tree) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |