| 1 | package equipotest |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "reflect" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/testci" |
| 14 | ) |
| 15 | |
| 16 | func TestReleaseStampContract(t *testing.T) { |
| 17 | for _, tc := range []struct { |
| 18 | name, arch, dirty string |
| 19 | args []string |
| 20 | }{ |
| 21 | {name: "defaults", arch: "arm64"}, |
| 22 | {name: "version override", arch: "arm64", args: []string{"VERSION=unrelated-version"}}, |
| 23 | {name: "amd64 override", arch: "amd64", args: []string{"RELEASE_GOARCH=amd64"}}, |
| 24 | {name: "unstaged", dirty: "unstaged"}, |
| 25 | {name: "staged", dirty: "staged"}, |
| 26 | {name: "unstaged with version", dirty: "unstaged", args: []string{"VERSION=unrelated-version"}}, |
| 27 | {name: "staged with version", dirty: "staged", args: []string{"VERSION=unrelated-version"}}, |
| 28 | } { |
| 29 | t.Run(tc.name, func(t *testing.T) { |
| 30 | dir := t.TempDir() |
| 31 | copyReleaseTrackedTree(t, dir) |
| 32 | bin := t.TempDir() |
| 33 | write := func(name, contents string, mode os.FileMode) { |
| 34 | t.Helper() |
| 35 | if err := os.WriteFile(filepath.Join(dir, name), []byte(contents), mode); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | } |
| 39 | write("tracked", "original\n", 0600) |
| 40 | writeBin := func(name, contents string) { |
| 41 | t.Helper() |
| 42 | executionMust(t, os.WriteFile(filepath.Join(bin, name), []byte(contents), 0700)) |
| 43 | } |
| 44 | writeBin("go", `#!/bin/sh |
| 45 | set -eu |
| 46 | if [ "$1" = test ]; then exit 0; fi |
| 47 | if [ "$1" = env ]; then echo go1.26.11; exit 0; fi |
| 48 | if [ "$1" = version ]; then |
| 49 | case "$3" in */gitleaks) printf '\tmod\tgithub.com/zricethezav/gitleaks/v8\tv8.30.1\th1:fixture\n'; exit 0;; esac |
| 50 | printf '%s: go1.26.11\n\tbuild\tvcs.revision=%s\n\tbuild\tvcs.modified=false\n' "$3" "$(git rev-parse HEAD)"; exit 0; fi |
| 51 | printf '%s\n' "CGO_ENABLED=$CGO_ENABLED" "GOOS=$GOOS" "GOARCH=$GOARCH" "$@" > "$RELEASE_CAPTURE" |
| 52 | while [ $# -gt 0 ]; do |
| 53 | if [ "$1" = -o ]; then shift; printf 'built %s for %s\n' "$1" "$GOOS/$GOARCH" > "$1"; fi |
| 54 | shift |
| 55 | done |
| 56 | `) |
| 57 | // Catch an accidental deployment without invoking the real CLI. |
| 58 | writeBin("carlos", "#!/bin/sh\ntouch \"$RELEASE_DEPLOYED\"\nexit 1\n") |
| 59 | writeBin("codex", "#!/bin/sh\necho 'codex-cli 0.165.0'\n") |
| 60 | writeBin("bwrap", "#!/bin/sh\necho 'bubblewrap 0.11.0'\n") |
| 61 | installed := releasePinsFixture(t, dir, bin, "clean") |
| 62 | capture := filepath.Join(bin, "go.args") |
| 63 | deployed := filepath.Join(bin, "carlos.called") |
| 64 | // Isolate Git and Make from the caller's repository and overrides. |
| 65 | env := []string{ |
| 66 | "PATH=" + bin + string(os.PathListSeparator) + os.Getenv("PATH"), |
| 67 | "TMPDIR=" + dir, |
| 68 | "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null", |
| 69 | "RELEASE_CAPTURE=" + capture, "RELEASE_DEPLOYED=" + deployed, |
| 70 | } |
| 71 | env = append(env, installed...) |
| 72 | run := func(name string, args ...string) ([]byte, error) { |
| 73 | cmd := exec.Command(name, args...) |
| 74 | cmd.Dir, cmd.Env = dir, env |
| 75 | return cmd.CombinedOutput() |
| 76 | } |
| 77 | git := func(args ...string) string { |
| 78 | t.Helper() |
| 79 | out, err := run("git", args...) |
| 80 | if err != nil { |
| 81 | t.Fatalf("git %v: %v\n%s", args, err, out) |
| 82 | } |
| 83 | return strings.TrimSpace(string(out)) |
| 84 | } |
| 85 | git("init", "--quiet") |
| 86 | git("config", "user.name", "Release test") |
| 87 | git("config", "user.email", "release@example.invalid") |
| 88 | git("add", "--force", ".") |
| 89 | git("commit", "--quiet", "-m", "Release fixture") |
| 90 | commit := git("rev-parse", "--short", "HEAD") |
| 91 | if tc.dirty != "" { |
| 92 | write("tracked", "changed\n", 0600) |
| 93 | if tc.dirty == "staged" { |
| 94 | git("add", "tracked") |
| 95 | } |
| 96 | } |
| 97 | out, buildErr := run("make", append([]string{"--no-print-directory", "release"}, tc.args...)...) |
| 98 | if _, err := os.Stat(deployed); !os.IsNotExist(err) { |
| 99 | t.Fatalf("release invoked carlos: %v\n%s", err, out) |
| 100 | } |
| 101 | if tc.dirty != "" { |
| 102 | if buildErr == nil { |
| 103 | t.Errorf("release accepted %s tracked changes\n%s", tc.dirty, out) |
| 104 | } |
| 105 | if _, err := os.Stat(capture); !os.IsNotExist(err) { |
| 106 | t.Errorf("release must reject dirty tracked files before invoking go: %v", err) |
| 107 | } |
| 108 | return |
| 109 | } |
| 110 | if buildErr != nil { |
| 111 | t.Fatalf("make release: %v\n%s", buildErr, out) |
| 112 | } |
| 113 | captured, err := os.ReadFile(capture) |
| 114 | if err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | args := strings.Split(strings.TrimSuffix(string(captured), "\n"), "\n") |
| 118 | wantEnv := []string{"CGO_ENABLED=0", "GOOS=linux", "GOARCH=" + tc.arch, "build"} |
| 119 | if len(args) < len(wantEnv) || !reflect.DeepEqual(args[:len(wantEnv)], wantEnv) { |
| 120 | t.Fatalf("build environment and command = %q, want prefix %q", args, wantEnv) |
| 121 | } |
| 122 | var trimpath bool |
| 123 | var ldflags, output, source, tags string |
| 124 | for i := len(wantEnv); i < len(args); i++ { |
| 125 | switch arg := args[i]; { |
| 126 | case arg == "-tags" && i+1 < len(args): |
| 127 | i++ |
| 128 | tags = args[i] |
| 129 | case arg == "-trimpath": |
| 130 | trimpath = true |
| 131 | case (arg == "-ldflags" || arg == "-o") && i+1 < len(args): |
| 132 | i++ |
| 133 | if arg == "-ldflags" { |
| 134 | ldflags = args[i] |
| 135 | } else { |
| 136 | output = args[i] |
| 137 | } |
| 138 | case strings.HasPrefix(arg, "-ldflags="): |
| 139 | ldflags = strings.TrimPrefix(arg, "-ldflags=") |
| 140 | default: |
| 141 | source = arg |
| 142 | } |
| 143 | } |
| 144 | if tags != "runnerassets" { |
| 145 | t.Errorf("release is missing embedded runner assets: tags %q", tags) |
| 146 | } |
| 147 | if !trimpath { |
| 148 | t.Error("release is missing -trimpath") |
| 149 | } |
| 150 | // The instance is linked with the digest of the tool proxy this |
| 151 | // release built, so it serves no other. |
| 152 | proxy, err := os.ReadFile(filepath.Join(dir, "releases/equipo-runner-linux-amd64-"+commit, "equipo-tool-proxy")) |
| 153 | if err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | wantFlags := []string{"-s", "-w", "-X", "amadan.net/rastrillo/rastrillo.BuildVersion=" + commit, "-X", fmt.Sprintf("amadan.net/oficina/equipo/internal/release.toolProxySHA256=%x", sha256.Sum256(proxy))} |
| 157 | if got := strings.Fields(strings.ReplaceAll(ldflags, "'", "")); !reflect.DeepEqual(got, wantFlags) { |
| 158 | t.Errorf("linker flags = %q, want %q", got, wantFlags) |
| 159 | } |
| 160 | wantOutput := "releases/equipo-linux-" + tc.arch + "-" + commit |
| 161 | if output != wantOutput || source != "./cmd/equipo" { |
| 162 | t.Errorf("output/source = %q/%q, want %q/./cmd/equipo", output, source, wantOutput) |
| 163 | } |
| 164 | // Production receives only what staging passed: the release names |
| 165 | // the staged-artifact gate and the drained cutover, never a bare deploy. |
| 166 | deploy := fmt.Sprintf("make staged-check && scripts/cutover-thin.sh %s", commit) |
| 167 | if !strings.Contains(string(out), deploy) || strings.Contains(string(out), "carlos ship") || strings.Contains(string(out), "carlos deploy") { |
| 168 | t.Errorf("release must print %q and never carlos ship or a bare deploy:\n%s", deploy, out) |
| 169 | } |
| 170 | }) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func copyReleaseTrackedTree(t *testing.T, dest string) { |
| 175 | t.Helper() |
| 176 | testci.RepoFiles(t) |
| 177 | root, err := filepath.Abs("../..") |
| 178 | executionMust(t, err) |
| 179 | cmd := exec.Command("git", "-C", root, "ls-files", "-z") |
| 180 | files, err := cmd.Output() |
| 181 | executionMust(t, err) |
| 182 | for _, name := range strings.Split(strings.TrimSuffix(string(files), "\x00"), "\x00") { |
| 183 | source, target := filepath.Join(root, name), filepath.Join(dest, name) |
| 184 | info, err := os.Lstat(source) |
| 185 | executionMust(t, err) |
| 186 | executionMust(t, os.MkdirAll(filepath.Dir(target), 0700)) |
| 187 | if info.Mode()&os.ModeSymlink != 0 { |
| 188 | link, err := os.Readlink(source) |
| 189 | executionMust(t, err) |
| 190 | executionMust(t, os.Symlink(link, target)) |
| 191 | continue |
| 192 | } |
| 193 | raw, err := os.ReadFile(source) |
| 194 | executionMust(t, err) |
| 195 | executionMust(t, os.WriteFile(target, raw, info.Mode().Perm())) |
| 196 | } |
| 197 | } |
| 198 | |