| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/base64" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "slices" |
| 13 | "sort" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | |
| 17 | "amadan.net/oficina/equipo/internal/wire" |
| 18 | ) |
| 19 | |
| 20 | // Mac runners slice F, plan task 4: the primary's side of the base |
| 21 | // exchange. It resolves a worker attempt's base (prepare_base, then a |
| 22 | // durable base report), packs exactly the commits an Engineering header |
| 23 | // names, and, once it has pushed a worker's candidate, sends the change |
| 24 | // files and diffs bound to that publication. |
| 25 | |
| 26 | const sampleRepo = "amadan.net/oficina/sample" |
| 27 | |
| 28 | // bases are the base reports in the outbox, in order. |
| 29 | func (r *relayRig) bases(t *testing.T) []wire.BaseReport { |
| 30 | t.Helper() |
| 31 | frames, _, err := r.e.O.State.Unacked(context.Background(), 0) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | var out []wire.BaseReport |
| 36 | for _, f := range frames { |
| 37 | if f.Type == wire.FrameBase { |
| 38 | var b wire.BaseReport |
| 39 | if err := json.Unmarshal(f.Data, &b); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | out = append(out, b) |
| 43 | } |
| 44 | } |
| 45 | return out |
| 46 | } |
| 47 | |
| 48 | func (r *relayRig) prepare(t *testing.T, frame string, attempt int64) { |
| 49 | t.Helper() |
| 50 | if err := r.e.O.State.AcceptPrepareBase(context.Background(), frame, wire.PrepareBaseCommand{TaskID: "tk1", StepID: "s1", Attempt: attempt, Repo: sampleRepo}); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // reopen is a restart: the same state directory opened again, a new |
| 56 | // relay over it. |
| 57 | func (r *relayRig) reopen(t *testing.T) { |
| 58 | t.Helper() |
| 59 | r.e.O.State.Close() |
| 60 | s, err := OpenState(context.Background(), filepath.Join(r.root, "state")) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | t.Cleanup(func() { s.Close() }) |
| 65 | r.e.O.State = s |
| 66 | r.relay = &Relay{State: s, Engine: r.e} |
| 67 | } |
| 68 | |
| 69 | // The primary fetches and reports the default branch's tip for the |
| 70 | // attempt, once. The same attempt asked again (a restart, a resent |
| 71 | // command) is answered with the same SHA even after the branch moved; a |
| 72 | // new attempt gets the tip as it is now. |
| 73 | func TestPrepareBaseResolvesAndIsIdempotent(t *testing.T) { |
| 74 | r := newRelayRig(t) |
| 75 | ctx := context.Background() |
| 76 | r.prepare(t, "pb-1", 1) |
| 77 | r.relay.Once(ctx) |
| 78 | got := r.bases(t) |
| 79 | if len(got) != 1 || got[0] != (wire.BaseReport{TaskID: "tk1", StepID: "s1", Attempt: 1, BaseSHA: r.base}) || !got[0].Valid() { |
| 80 | t.Fatalf("base reports %+v, want %s", got, r.base) |
| 81 | } |
| 82 | // The default branch moves on. |
| 83 | gitOut(t, r.seed, "push", "-q", r.remote, "main") |
| 84 | r.reopen(t) |
| 85 | r.relay.Once(ctx) |
| 86 | if got := r.bases(t); len(got) != 1 { |
| 87 | t.Fatalf("reported again after a restart: %+v", got) |
| 88 | } |
| 89 | // The same frame again does nothing; the same attempt asked again is |
| 90 | // answered with the SHA it was given. |
| 91 | r.prepare(t, "pb-1", 1) |
| 92 | r.prepare(t, "pb-2", 1) |
| 93 | r.relay.Once(ctx) |
| 94 | got = r.bases(t) |
| 95 | if len(got) != 2 || got[1].BaseSHA != r.base || got[1].Attempt != 1 { |
| 96 | t.Fatalf("same attempt asked again: %+v", got) |
| 97 | } |
| 98 | r.prepare(t, "pb-3", 2) |
| 99 | r.relay.Once(ctx) |
| 100 | got = r.bases(t) |
| 101 | if len(got) != 3 || got[2].Attempt != 2 || got[2].BaseSHA != r.commit { |
| 102 | t.Fatalf("a new attempt: %+v, want %s", got, r.commit) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // A repository with no commits has no base for a worker (it is never Mac |
| 107 | // work): base_unavailable. So does a repository the primary does not |
| 108 | // publish to (before git runs) and a rebase round whose commits the clone |
| 109 | // does not have. |
| 110 | func TestPrepareBaseRefusesUnborn(t *testing.T) { |
| 111 | ctx := context.Background() |
| 112 | t.Run("unborn", func(t *testing.T) { |
| 113 | r := newRelayRig(t) |
| 114 | empty := filepath.Join(r.root, "empty.git") |
| 115 | gitOut(t, r.root, "init", "-q", "--bare", "-b", "main", empty) |
| 116 | r.f.dir = empty |
| 117 | r.prepare(t, "pb-1", 1) |
| 118 | r.relay.Once(ctx) |
| 119 | if got := r.bases(t); len(got) != 1 || got[0].Reason != wire.ReasonBaseUnavailable || got[0].BaseSHA != "" || !got[0].Valid() { |
| 120 | t.Fatalf("unborn: %+v", got) |
| 121 | } |
| 122 | }) |
| 123 | t.Run("not a hub repository", func(t *testing.T) { |
| 124 | r := newRelayRig(t) |
| 125 | r.e.O.Runner.GitPath, r.e.Clones.Git = "/nonexistent/git", "/nonexistent/git" |
| 126 | if err := r.e.O.State.AcceptPrepareBase(ctx, "pb-1", wire.PrepareBaseCommand{TaskID: "tk1", StepID: "s1", Attempt: 1, Repo: "github.com/someone/else"}); err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | r.relay.Once(ctx) |
| 130 | if got := r.bases(t); len(got) != 1 || got[0].Reason != wire.ReasonBaseUnavailable { |
| 131 | t.Fatalf("not a hub repository: %+v", got) |
| 132 | } |
| 133 | }) |
| 134 | t.Run("rebase commits missing", func(t *testing.T) { |
| 135 | r := newRelayRig(t) |
| 136 | if err := r.e.O.State.AcceptPrepareBase(ctx, "pb-1", wire.PrepareBaseCommand{TaskID: "tk1", StepID: "s1", Attempt: 1, Repo: sampleRepo, Rebase: true, FromBase: r.base, CandidateSHA: strings.Repeat("e", 40)}); err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | r.relay.Once(ctx) |
| 140 | if got := r.bases(t); len(got) != 1 || got[0].Reason != wire.ReasonBaseUnavailable { |
| 141 | t.Fatalf("rebase without its candidate: %+v", got) |
| 142 | } |
| 143 | }) |
| 144 | } |
| 145 | |
| 146 | // packRig is a remote with history: main at c1 on c0, and a task branch |
| 147 | // whose tip c2 sits on c0 (the old base of a rebase round onto c1). |
| 148 | func packRig(t *testing.T) (r *relayRig, c0, c1, c2 string) { |
| 149 | t.Helper() |
| 150 | r = newRelayRig(t) |
| 151 | work := filepath.Join(r.root, "history") |
| 152 | gitOut(t, r.root, "init", "-q", "-b", "main", work) |
| 153 | write := func(name, text string) { |
| 154 | os.MkdirAll(filepath.Dir(filepath.Join(work, name)), 0755) |
| 155 | os.WriteFile(filepath.Join(work, name), []byte(text), 0644) |
| 156 | } |
| 157 | write("README.md", "zero\n") |
| 158 | write("Sources/App.swift", "let a = 0\n") |
| 159 | gitOut(t, work, "add", "-A") |
| 160 | gitOut(t, work, "commit", "-q", "-m", "c0") |
| 161 | c0 = gitOut(t, work, "rev-parse", "HEAD") |
| 162 | write("Sources/App.swift", "let a = 1\n") |
| 163 | write("Sources/More.swift", "let m = 1\n") |
| 164 | gitOut(t, work, "add", "-A") |
| 165 | gitOut(t, work, "commit", "-q", "-m", "c1") |
| 166 | c1 = gitOut(t, work, "rev-parse", "HEAD") |
| 167 | gitOut(t, work, "checkout", "-q", "-b", relayBranch, c0) |
| 168 | write("Sources/Task.swift", "let t = 2\n") |
| 169 | gitOut(t, work, "add", "-A") |
| 170 | gitOut(t, work, "commit", "-q", "-m", "c2") |
| 171 | c2 = gitOut(t, work, "rev-parse", "HEAD") |
| 172 | remote := filepath.Join(r.root, "history.git") |
| 173 | gitOut(t, r.root, "clone", "-q", "--bare", "--no-single-branch", work, remote) |
| 174 | r.f.dir = remote |
| 175 | return r, c0, c1, c2 |
| 176 | } |
| 177 | |
| 178 | // packInventory indexes pack into an empty repository and lists the |
| 179 | // objects in the pack itself (verify-pack), by type. |
| 180 | func packInventory(t *testing.T, pack []byte) map[string][]string { |
| 181 | t.Helper() |
| 182 | dir := filepath.Join(t.TempDir(), "empty.git") |
| 183 | if out, err := exec.Command("git", "init", "-q", "--bare", dir).CombinedOutput(); err != nil { |
| 184 | t.Fatalf("%v: %s", err, out) |
| 185 | } |
| 186 | index := exec.Command("git", "--git-dir="+dir, "index-pack", "--stdin") |
| 187 | index.Stdin = bytes.NewReader(pack) |
| 188 | if out, err := index.CombinedOutput(); err != nil { |
| 189 | t.Fatalf("index-pack: %v: %s", err, out) |
| 190 | } |
| 191 | idx, _ := filepath.Glob(filepath.Join(dir, "objects", "pack", "*.idx")) |
| 192 | if len(idx) != 1 { |
| 193 | t.Fatalf("pack indexes %v", idx) |
| 194 | } |
| 195 | out, err := exec.Command("git", "verify-pack", "-v", idx[0]).Output() |
| 196 | if err != nil { |
| 197 | t.Fatal(err) |
| 198 | } |
| 199 | inv := map[string][]string{} |
| 200 | for _, line := range strings.Split(string(out), "\n") { |
| 201 | f := strings.Fields(line) |
| 202 | if len(f) >= 3 && shaPattern.MatchString(f[0]) { |
| 203 | inv[f[1]] = append(inv[f[1]], f[0]) |
| 204 | } |
| 205 | } |
| 206 | for _, v := range inv { |
| 207 | sort.Strings(v) |
| 208 | } |
| 209 | return inv |
| 210 | } |
| 211 | |
| 212 | // objectsOf is every object of the commits and their trees, no history, |
| 213 | // from the source repository. |
| 214 | func objectsOf(t *testing.T, gitDir string, commits ...string) []string { |
| 215 | t.Helper() |
| 216 | out, err := exec.Command("git", append([]string{"--git-dir=" + gitDir, "rev-list", "--objects", "--no-walk"}, commits...)...).Output() |
| 217 | if err != nil { |
| 218 | t.Fatal(err) |
| 219 | } |
| 220 | var all []string |
| 221 | for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") { |
| 222 | all = append(all, strings.Fields(line)[0]) |
| 223 | } |
| 224 | sort.Strings(all) |
| 225 | return all |
| 226 | } |
| 227 | |
| 228 | // An Engineering pack holds exactly the commits its header names, each |
| 229 | // with its complete tree, and no other commit (their parents stay out); |
| 230 | // a checks pack is still exactly the candidate. |
| 231 | func TestPackCommitsInventory(t *testing.T) { |
| 232 | r, c0, c1, c2 := packRig(t) |
| 233 | ctx := context.Background() |
| 234 | for _, c := range []struct { |
| 235 | name string |
| 236 | h wire.TransferHeader |
| 237 | want []string |
| 238 | }{ |
| 239 | {"first step", wire.TransferHeader{ID: "tr_e1", Direction: wire.TransferInbound, Kind: wire.TransferKindEngineering, Repo: sampleRepo, TaskID: "tk1", StepID: "s1", Attempt: 1, BaseSHA: c1}, []string{c1}}, |
| 240 | {"rebase round", wire.TransferHeader{ID: "tr_e2", Direction: wire.TransferInbound, Kind: wire.TransferKindEngineering, Repo: sampleRepo, TaskID: "tk1", StepID: "s2", Attempt: 1, BaseSHA: c1, OldBaseSHA: c0, CandidateSHA: c2}, []string{c1, c0, c2}}, |
| 241 | {"checks", wire.TransferHeader{ID: "tr_c", Direction: wire.TransferInbound, Repo: sampleRepo, TaskID: "tk1", StepID: "s3", Attempt: 1, BaseSHA: c0, CandidateSHA: c2}, []string{c2}}, |
| 242 | } { |
| 243 | t.Run(c.name, func(t *testing.T) { |
| 244 | pack, err := r.e.packCommits(ctx, c.h) |
| 245 | if err != nil { |
| 246 | t.Fatal(err) |
| 247 | } |
| 248 | inv := packInventory(t, pack) |
| 249 | want := slices.Clone(c.want) |
| 250 | sort.Strings(want) |
| 251 | if !slices.Equal(inv["commit"], want) { |
| 252 | t.Fatalf("commits in the pack %v, want exactly %v", inv["commit"], want) |
| 253 | } |
| 254 | var got []string |
| 255 | for _, kind := range []string{"commit", "tree", "blob", "tag"} { |
| 256 | got = append(got, inv[kind]...) |
| 257 | } |
| 258 | sort.Strings(got) |
| 259 | if all := objectsOf(t, r.e.Clones.Path(sampleRepo), c.want...); !slices.Equal(got, all) { |
| 260 | t.Fatalf("objects in the pack %v, want the commits' trees exactly %v", got, all) |
| 261 | } |
| 262 | }) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // Before git runs, a header naming something other than full SHAs, a |
| 267 | // repository that is not a hub repository or an invalid kind is refused; |
| 268 | // after the fetch, a commit the clone does not have is refused. |
| 269 | func TestPackCommitsRefuses(t *testing.T) { |
| 270 | r, c0, c1, c2 := packRig(t) |
| 271 | ctx := context.Background() |
| 272 | eng := wire.TransferHeader{ID: "tr_e", Direction: wire.TransferInbound, Kind: wire.TransferKindEngineering, Repo: sampleRepo, TaskID: "tk1", StepID: "s1", Attempt: 1, BaseSHA: c1} |
| 273 | before := map[string]wire.TransferHeader{} |
| 274 | h := eng |
| 275 | h.Repo = "github.com/someone/else" |
| 276 | before["not a hub repository"] = h |
| 277 | h = eng |
| 278 | h.BaseSHA = "main" |
| 279 | before["a ref, not a SHA"] = h |
| 280 | h = eng |
| 281 | h.BaseSHA = strings.ToUpper(c1) |
| 282 | before["not lowercase"] = h |
| 283 | h = eng |
| 284 | h.OldBaseSHA = c0 |
| 285 | before["old base without candidate"] = h |
| 286 | h = eng |
| 287 | h.Direction = wire.TransferOutbound |
| 288 | before["outbound engineering"] = h |
| 289 | h = eng |
| 290 | h.Kind = "everything" |
| 291 | before["unknown kind"] = h |
| 292 | h = wire.TransferHeader{ID: "tr_c", Direction: wire.TransferInbound, Repo: sampleRepo, TaskID: "tk1", StepID: "s1", Attempt: 1, CandidateSHA: "HEAD"} |
| 293 | before["checks candidate not a SHA"] = h |
| 294 | git := r.e.O.Runner.GitPath |
| 295 | r.e.O.Runner.GitPath, r.e.Clones.Git = "/nonexistent/git", "/nonexistent/git" |
| 296 | for name, h := range before { |
| 297 | if _, err := r.e.packCommits(ctx, h); !errors.Is(err, errPackRefused) { |
| 298 | t.Errorf("%s: %v, want refused before git runs", name, err) |
| 299 | } |
| 300 | } |
| 301 | r.e.O.Runner.GitPath, r.e.Clones.Git = git, git |
| 302 | missing := eng |
| 303 | missing.OldBaseSHA, missing.CandidateSHA = c0, strings.Repeat("e", 40) |
| 304 | if _, err := r.e.packCommits(ctx, missing); !errors.Is(err, errPackRefused) { |
| 305 | t.Fatalf("a commit the clone does not have: %v", err) |
| 306 | } |
| 307 | ok := eng |
| 308 | ok.OldBaseSHA, ok.CandidateSHA = c0, c2 |
| 309 | if _, err := r.e.packCommits(ctx, ok); err != nil { |
| 310 | t.Fatalf("the rebase round's pack: %v", err) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // changeDiffs are the change_diff uploads in the outbox, in order. |
| 315 | func (r *relayRig) changeDiffs(t *testing.T) (ups []wire.ChangeDiffUpload, raw []string) { |
| 316 | t.Helper() |
| 317 | frames, _, err := r.e.O.State.Unacked(context.Background(), 0) |
| 318 | if err != nil { |
| 319 | t.Fatal(err) |
| 320 | } |
| 321 | for _, f := range frames { |
| 322 | if f.Type == wire.FrameChangeDiff { |
| 323 | var v wire.ChangeDiffUpload |
| 324 | if err := json.Unmarshal(f.Data, &v); err != nil { |
| 325 | t.Fatal(err) |
| 326 | } |
| 327 | ups = append(ups, v) |
| 328 | raw = append(raw, string(f.Data)) |
| 329 | } |
| 330 | } |
| 331 | return ups, raw |
| 332 | } |
| 333 | |
| 334 | // After the push, the primary measures the worker's candidate on the |
| 335 | // base and queues its diffs, each naming the publication and the |
| 336 | // worker's task, step and attempt, before the published report that |
| 337 | // carries the files, the collection and the outcome the publish command |
| 338 | // named. The runner's bearer token never reaches a diff. |
| 339 | func TestPublishWorkerChangeFilesAndDiffs(t *testing.T) { |
| 340 | // Words no generic secret rule flags: only the known-secret exclusion |
| 341 | // keeps the runner's bearer token out of the diffs. |
| 342 | const token = "correcthorsebatterystaplerunnerbearer" |
| 343 | ctx := context.Background() |
| 344 | publish := func(r *relayRig, id string) { |
| 345 | t.Helper() |
| 346 | h := r.header() |
| 347 | if err := r.e.O.State.AcceptPublish(ctx, "pub-"+id, wire.PublishCommand{Publication: id, TaskID: h.TaskID, StepID: h.StepID, Attempt: h.Attempt, Repo: h.Repo, Branch: relayBranch, Person: "p@x", BaseSHA: r.base, CandidateSHA: r.commit, Transfer: h.ID, OutcomePhase: wire.OutcomeUnfinished}); err != nil { |
| 348 | t.Fatal(err) |
| 349 | } |
| 350 | } |
| 351 | rig := func(t *testing.T) *relayRig { |
| 352 | r := newRelayRigWith(t, map[string]string{"Config.txt": "server = example.com\nmotto = " + token + "\n", "Notes.md": "plain words\n"}) |
| 353 | r.e.DiffSecrets = []string{token} |
| 354 | r.e.O.Protocol = func() int { return wire.ProtocolMacEngineering } |
| 355 | r.receive(t, r.pack, r.header()) |
| 356 | return r |
| 357 | } |
| 358 | |
| 359 | t.Run("pushed", func(t *testing.T) { |
| 360 | r := rig(t) |
| 361 | publish(r, "pb_1") |
| 362 | r.relay.Once(ctx) |
| 363 | reps := r.reports(t) |
| 364 | if len(reps) != 1 || !reps[0].Pushed || reps[0].OutcomePhase != wire.OutcomeUnfinished { |
| 365 | t.Fatalf("published %+v", reps) |
| 366 | } |
| 367 | f := reps[0].ChangeFiles |
| 368 | if f == nil || f.Base != r.base || f.SHA != r.commit || f.Files != 3 || !wire.ValidChangeFiles(f) { |
| 369 | t.Fatalf("change files %+v", f) |
| 370 | } |
| 371 | ups, raw := r.changeDiffs(t) |
| 372 | if len(ups) != 3 { |
| 373 | t.Fatalf("%d diffs", len(ups)) |
| 374 | } |
| 375 | files := make([]wire.FileDiff, len(ups)) |
| 376 | for i, u := range ups { |
| 377 | if u.Publication != "pb_1" || u.TaskID != "tk1" || u.StepID != "s1" || u.Attempt != 1 || u.Base != r.base || u.Candidate != r.commit || u.Index != i || u.Total != 3 { |
| 378 | t.Fatalf("diff %d not bound to the publication: %+v", i, u) |
| 379 | } |
| 380 | patch, _ := base64.StdEncoding.DecodeString(u.Data) |
| 381 | files[i] = wire.FileDiff{Path: u.Path, Patch: string(patch), Reason: u.Reason, Truncated: u.Truncated} |
| 382 | if strings.Contains(string(patch), token) || strings.Contains(raw[i], token) || strings.Contains(raw[i], base64.StdEncoding.EncodeToString([]byte(token))) { |
| 383 | t.Fatalf("the bearer token in diff %d: %s", i, raw[i]) |
| 384 | } |
| 385 | switch u.Path { |
| 386 | case "Config.txt": |
| 387 | if u.Reason == "" || u.Size != 0 { |
| 388 | t.Fatalf("the secret-bearing file was shown: %+v", u) |
| 389 | } |
| 390 | case "Swipe.swift": |
| 391 | if !strings.Contains(string(patch), "+let back = true") { |
| 392 | t.Fatalf("swipe diff %q", patch) |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | if reps[0].DiffCollection == "" || reps[0].DiffCollection != wire.DiffCollection(files) { |
| 397 | t.Fatalf("collection %q", reps[0].DiffCollection) |
| 398 | } |
| 399 | // The diffs go before the report that names them. |
| 400 | frames, _, _ := r.e.O.State.Unacked(ctx, 0) |
| 401 | if last := frames[len(frames)-1]; last.Type != wire.FramePublished { |
| 402 | t.Fatalf("last frame %s", last.Type) |
| 403 | } |
| 404 | for _, fr := range frames { |
| 405 | if strings.Contains(string(fr.Data), token) { |
| 406 | t.Fatalf("the bearer token in %s", fr.Type) |
| 407 | } |
| 408 | } |
| 409 | }) |
| 410 | |
| 411 | t.Run("push failed", func(t *testing.T) { |
| 412 | r := rig(t) |
| 413 | publish(r, "pb_1") |
| 414 | // The hub refuses the push itself, after everything before it |
| 415 | // succeeded. |
| 416 | hook := filepath.Join(r.remote, "hooks", "pre-receive") |
| 417 | if err := os.WriteFile(hook, []byte("#!/bin/sh\nexit 1\n"), 0755); err != nil { |
| 418 | t.Fatal(err) |
| 419 | } |
| 420 | r.relay.Once(ctx) |
| 421 | reps := r.reports(t) |
| 422 | if len(reps) != 1 || reps[0].Pushed || reps[0].ChangeFiles != nil || reps[0].DiffCollection != "" || reps[0].OutcomePhase != wire.OutcomeUnfinished { |
| 423 | t.Fatalf("failed push %+v", reps) |
| 424 | } |
| 425 | if ups, _ := r.changeDiffs(t); len(ups) != 0 { |
| 426 | t.Fatalf("%d diffs queued without a push", len(ups)) |
| 427 | } |
| 428 | // Retry's publication pushes and then sends them. |
| 429 | os.Remove(hook) |
| 430 | publish(r, "pb_2") |
| 431 | r.relay.Once(ctx) |
| 432 | ups, _ := r.changeDiffs(t) |
| 433 | if reps = r.reports(t); len(reps) != 2 || !reps[1].Pushed || len(ups) != 3 || ups[0].Publication != "pb_2" || reps[1].DiffCollection == "" { |
| 434 | t.Fatalf("republished %+v, %d diffs", reps, len(ups)) |
| 435 | } |
| 436 | }) |
| 437 | |
| 438 | t.Run("diffs unavailable", func(t *testing.T) { |
| 439 | // The forge credential cannot be read, so its secrets cannot be |
| 440 | // left out: no diffs, and still a push. |
| 441 | r := rig(t) |
| 442 | r.e.O.Runner.AmadanCredentialPath = filepath.Join(r.root, "missing-credential") |
| 443 | publish(r, "pb_1") |
| 444 | r.relay.Once(ctx) |
| 445 | reps := r.reports(t) |
| 446 | if ups, _ := r.changeDiffs(t); len(reps) != 1 || !reps[0].Pushed || reps[0].DiffCollection != "" || len(ups) != 0 || r.pushCount() != 1 { |
| 447 | t.Fatalf("diagnostic, not a push failure: %+v, %d diffs", reps, len(ups)) |
| 448 | } |
| 449 | }) |
| 450 | |
| 451 | t.Run("older instance", func(t *testing.T) { |
| 452 | r := rig(t) |
| 453 | r.e.O.Protocol = func() int { return wire.ProtocolChangeDiffs } |
| 454 | publish(r, "pb_1") |
| 455 | r.relay.Once(ctx) |
| 456 | reps := r.reports(t) |
| 457 | if ups, _ := r.changeDiffs(t); len(reps) != 1 || !reps[0].Pushed || reps[0].ChangeFiles != nil || len(ups) != 0 { |
| 458 | t.Fatalf("below 37: %+v, %d diffs", reps, len(ups)) |
| 459 | } |
| 460 | }) |
| 461 | } |
| 462 | |
| 463 | // A file name, or a renamed file's old name, holding a secret this |
| 464 | // primary knows (its bearer token) would reach the instance in the |
| 465 | // change files and in every diff upload, whatever the worker scanned |
| 466 | // (the worker does not know the primary's secrets). Such a measurement |
| 467 | // is withheld: no files, no diffs, and the push stands. |
| 468 | func TestPublishWorkerSecretInFileName(t *testing.T) { |
| 469 | const token = "correcthorsebatterystaplerunnerbearer" |
| 470 | ctx := context.Background() |
| 471 | for _, c := range []struct { |
| 472 | name string |
| 473 | before, extra map[string]string |
| 474 | moves [][2]string |
| 475 | }{ |
| 476 | {name: "file name", extra: map[string]string{"keys-" + token + ".txt": "nothing secret inside\n"}}, |
| 477 | {name: "renamed from", before: map[string]string{"notes-" + token + ".md": "the same words\nin both names\n"}, moves: [][2]string{{"notes-" + token + ".md", "notes.md"}}}, |
| 478 | } { |
| 479 | t.Run(c.name, func(t *testing.T) { |
| 480 | r := newRelayRigFiles(t, c.before, c.extra, c.moves) |
| 481 | r.e.DiffSecrets = []string{token} |
| 482 | r.e.O.Protocol = func() int { return wire.ProtocolMacEngineering } |
| 483 | r.receive(t, r.pack, r.header()) |
| 484 | h := r.header() |
| 485 | if err := r.e.O.State.AcceptPublish(ctx, "pub-1", wire.PublishCommand{Publication: "pb_1", TaskID: h.TaskID, StepID: h.StepID, Attempt: h.Attempt, Repo: h.Repo, Branch: relayBranch, Person: "p@x", BaseSHA: r.base, CandidateSHA: r.commit, Transfer: h.ID}); err != nil { |
| 486 | t.Fatal(err) |
| 487 | } |
| 488 | r.relay.Once(ctx) |
| 489 | reps := r.reports(t) |
| 490 | if len(reps) != 1 || !reps[0].Pushed || reps[0].ChangeFiles != nil || reps[0].DiffCollection != "" { |
| 491 | t.Fatalf("published %+v", reps) |
| 492 | } |
| 493 | if ups, _ := r.changeDiffs(t); len(ups) != 0 { |
| 494 | t.Fatalf("%d diffs sent", len(ups)) |
| 495 | } |
| 496 | frames, _, err := r.e.O.State.Unacked(ctx, 0) |
| 497 | if err != nil { |
| 498 | t.Fatal(err) |
| 499 | } |
| 500 | for _, f := range frames { |
| 501 | if strings.Contains(string(f.Data), token) || strings.Contains(string(f.Data), base64.StdEncoding.EncodeToString([]byte(token))) { |
| 502 | t.Fatalf("the bearer token in %s: %s", f.Type, f.Data) |
| 503 | } |
| 504 | } |
| 505 | }) |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | // On a worker, unfinished work sent at protocol 37 is the work done, never |
| 510 | // pushed (only the primary's published report says that), with its |
| 511 | // outcome; the primary's own report is unchanged. |
| 512 | func TestWorkerUnfinishedReportNeverPushed(t *testing.T) { |
| 513 | e, _, r, _ := hubRig(t) |
| 514 | ctx := context.Background() |
| 515 | s := e.O.State |
| 516 | cand := strings.Repeat("2", 40) |
| 517 | r.Specialist, r.StepID = wire.SpecialistEngineering, "s9" |
| 518 | if err := s.AcceptDispatch(ctx, "d-s9", r.DispatchCommand); err != nil { |
| 519 | t.Fatal(err) |
| 520 | } |
| 521 | if err := s.setStepPhase(ctx, r, phaseCommittedUnfinished, "", cand); err != nil { |
| 522 | t.Fatal(err) |
| 523 | } |
| 524 | if err := s.ReportStep(ctx, r, wire.StepReport{TaskID: r.TaskID, StepID: "s9", Attempt: 1, State: wire.StepDone, Reason: "step_timeout", CandidateSHA: cand, Termination: true, Findings: []wire.Finding{}}); err != nil { |
| 525 | t.Fatal(err) |
| 526 | } |
| 527 | frames, _, err := s.Unacked(ctx, 0) |
| 528 | if err != nil || len(frames) != 1 { |
| 529 | t.Fatalf("queued %d: %v", len(frames), err) |
| 530 | } |
| 531 | read := func(f wire.Frame) *wire.StepReport { |
| 532 | body, err := f.Decode() |
| 533 | if err != nil { |
| 534 | t.Fatal(err) |
| 535 | } |
| 536 | return body.(*wire.StepReport) |
| 537 | } |
| 538 | sent, err := s.unfinishedReport(ctx, frames[0], wire.ProtocolMacEngineering, true) |
| 539 | if err != nil { |
| 540 | t.Fatal(err) |
| 541 | } |
| 542 | if v := read(sent); v.Pushed || v.State != wire.StepDone || v.CandidateSHA != cand || v.Reason != "step_timeout" || v.OutcomePhase != wire.OutcomeUnfinished { |
| 543 | t.Fatalf("a worker's unfinished work sent as %+v", v) |
| 544 | } |
| 545 | if v := read(must(s.unfinishedReport(ctx, frames[0], wire.ProtocolMacEngineering, false))); !v.Pushed || v.State != wire.StepDone || v.OutcomePhase != "" { |
| 546 | t.Fatalf("the primary's unfinished work sent as %+v", v) |
| 547 | } |
| 548 | if v := read(must(s.unfinishedReport(ctx, frames[0], wire.ProtocolChangeDiffs, true))); v.Pushed || v.State != wire.StepFailed { |
| 549 | t.Fatalf("a worker's unfinished work below 37 sent as %+v", v) |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | func must[T any](v T, err error) T { |
| 554 | if err != nil { |
| 555 | panic(err) |
| 556 | } |
| 557 | return v |
| 558 | } |
| 559 | |