oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "context"
5 "log/slog"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/wire"
13)
14
15// TestPushFailureKeepsTheHubAnswer: when the hub refuses a step's push
16// (a hook, a protected branch), the runner's log line has git's answer
17// and the refusal, and the step report's log tail (what the task page
18// shows for a stopped step) leads with them, before Engineering's own
19// tail; redacted and bounded in both. The reason is as before.
20func TestPushFailureKeepsTheHubAnswer(t *testing.T) {
21 old := pushBackoff
22 pushBackoff = time.Millisecond
23 t.Cleanup(func() { pushBackoff = old })
24 e, f, r, _ := hubRig(t)
25 logs := &lockedBuffer{}
26 e.O.Logger = slog.New(slog.NewTextHandler(logs, nil))
27 secret := "Authorization: Bearer s3cr3t-hub-token, see https://agent:s3cr3t-pw@amadan.net/x"
28 hook := "#!/bin/sh\necho 'GL-HOOK-ERR: you cannot push to the default branch. " + secret + "' >&2\nexit 1\n"
29 if err := os.WriteFile(filepath.Join(f.dir, "hooks", "pre-receive"), []byte(hook), 0700); err != nil {
30 t.Fatal(err)
31 }
32 // The step's private repository holds a commit on main.
33 r.Specialist, r.StepID, r.Branch = wire.SpecialistEngineering, "s2", "equipo/tk1-another-fix"
34 base := gitOut(t, f.dir, "rev-parse", "main")
35 gitDir := e.tree(r, base).GitDir
36 gitOut(t, filepath.Dir(f.dir), "clone", "-q", "--bare", f.dir, gitDir)
37 tree := gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "rev-parse", base+"^{tree}")
38 r.Base, r.Candidate = base, gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "commit-tree", tree, "-p", base, "-m", "another fix")
39
40 rep := e.publish(context.Background(), r, wire.StepReport{LogTail: "Engineering's own tail"})
41 line := string(logs.Bytes())
42 if rep.State != wire.StepFailed || rep.Reason != "push_failed" || rep.Pushed {
43 t.Fatalf("report %+v", rep)
44 }
45 detail, rest, _ := strings.Cut(rep.LogTail, "\n\n")
46 if !strings.HasPrefix(detail, "refusal: refused\npush: ") || !strings.Contains(detail, "[remote rejected] (pre-receive hook declined)") || !strings.Contains(detail, "GL-HOOK-ERR: you cannot push to the default branch.") || rest != "Engineering's own tail" {
47 t.Fatalf("log tail %q", rep.LogTail)
48 }
49 if !strings.Contains(line, `msg="step push"`) || !strings.Contains(line, "refusal=refused") || !strings.Contains(line, "GL-HOOK-ERR: you cannot push to the default branch.") || !strings.Contains(line, "Authorization: ***") {
50 t.Fatalf("log %q", line)
51 }
52 if strings.Contains(rep.LogTail+line, "s3cr3t") || len(detail) > 500 || len(line) > 800 {
53 t.Fatalf("not redacted or bounded: %q / %q", rep.LogTail, line)
54 }
55
56 // The default branch cannot be read: the push is never tried, and the
57 // report and the log say why.
58 before := logs.Len()
59 f.dir = filepath.Join(t.TempDir(), "missing.git")
60 rep = e.publish(context.Background(), r, wire.StepReport{})
61 line = string(logs.Bytes()[before:])
62 if rep.Reason != "push_failed" || !strings.HasPrefix(rep.LogTail, "error: reading the default branch: ls-remote: exit status 128: ") || !strings.Contains(rep.LogTail, "does not appear to be a git repository") || strings.Contains(line, "refusal=") || !strings.Contains(line, "does not appear to be a git repository") {
63 t.Fatalf("unreadable default: %+v %q", rep, line)
64 }
65
66 // The remote cannot be resolved, in words that carry credentials.
67 before = logs.Len()
68 e.Clones.Forge = brokenRemote{f}
69 rep = e.publish(context.Background(), r, wire.StepReport{})
70 line = string(logs.Bytes()[before:])
71 want := "reading the default branch: dial https://***@amadan.net/x: Authorization: *** refused"
72 if rep.Reason != "push_failed" || rep.LogTail != "error: "+want || !strings.Contains(line, want) || strings.Contains(rep.LogTail+line, "s3cr3t") {
73 t.Fatalf("unresolved remote: %+v %q", rep, line)
74 }
75}
76