| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 5 | "amadan.net/oficina/equipo/internal/wire" |
| 6 | "context" |
| 7 | "errors" |
| 8 | "path/filepath" |
| 9 | "reflect" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | type proposalSpy struct { |
| 15 | Forge |
| 16 | calls []Proposal |
| 17 | failure error |
| 18 | at func(Proposal) |
| 19 | } |
| 20 | |
| 21 | func (f *proposalSpy) Propose(ctx context.Context, p Proposal) (ProposalRef, error) { |
| 22 | f.calls = append(f.calls, p) |
| 23 | if f.at != nil { |
| 24 | f.at(p) |
| 25 | } |
| 26 | if f.failure != nil { |
| 27 | return ProposalRef{}, f.failure |
| 28 | } |
| 29 | return f.Forge.Propose(ctx, p) |
| 30 | } |
| 31 | func TestEngineeringProposesAfterPush(t *testing.T) { |
| 32 | e, f, r, _ := hubRig(t) |
| 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 | spy := &proposalSpy{Forge: f, at: func(p Proposal) { |
| 40 | if p.Repo != r.Repo || p.Branch != r.Branch || gitOut(t, f.dir, "rev-parse", p.Branch) != r.Candidate { |
| 41 | t.Fatal("proposal before push or wrong branch") |
| 42 | } |
| 43 | }} |
| 44 | authority := &useSpy{Forge: spy} |
| 45 | e.Forge = authority |
| 46 | rep := e.publish(context.Background(), r, wire.StepReport{}) |
| 47 | if !reflect.DeepEqual(authority.uses, []Use{Push}) { |
| 48 | t.Fatalf("publish authority %v", authority.uses) |
| 49 | } |
| 50 | if !rep.Pushed || len(spy.calls) != 1 { |
| 51 | t.Fatalf("report %+v proposals %+v", rep, spy.calls) |
| 52 | } |
| 53 | } |
| 54 | func TestRelayProposesAfterPush(t *testing.T) { |
| 55 | r := newRelayRig(t) |
| 56 | spy := &proposalSpy{Forge: r.f, at: func(p Proposal) { |
| 57 | if p.Repo != r.header().Repo || p.Branch != relayBranch || r.head(t) != r.commit { |
| 58 | t.Fatal("proposal before push or wrong branch") |
| 59 | } |
| 60 | }} |
| 61 | authority := &useSpy{Forge: spy} |
| 62 | r.e.Forge = authority |
| 63 | r.receive(t, r.pack, r.header()) |
| 64 | r.publish(t, "pb_1") |
| 65 | r.relay.Once(context.Background()) |
| 66 | if !reflect.DeepEqual(authority.uses, []Use{Push}) { |
| 67 | t.Fatalf("relay authority %v", authority.uses) |
| 68 | } |
| 69 | if len(spy.calls) != 1 { |
| 70 | t.Fatalf("proposals %+v", spy.calls) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Repository initialization is also a write, even before its first task. |
| 75 | type useSpy struct { |
| 76 | Forge |
| 77 | uses []Use |
| 78 | } |
| 79 | |
| 80 | func (f *useSpy) Remote(ctx context.Context, repo string, use Use) (workspace.Remote, error) { |
| 81 | f.uses = append(f.uses, use) |
| 82 | return f.Forge.Remote(ctx, repo, use) |
| 83 | } |
| 84 | func TestStartDefaultRequestsPushAuthority(t *testing.T) { |
| 85 | f, c := startRig(t) |
| 86 | const repo = "amadan.net/oficina/site" |
| 87 | if err := f.Create(context.Background(), repo); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | spy := &useSpy{Forge: f} |
| 91 | c.Forge = spy |
| 92 | if err := c.StartDefault(context.Background(), repo); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | if len(spy.uses) != 1 || spy.uses[0] != Push { |
| 96 | t.Fatalf("initialization authority %v", spy.uses) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestEngineeringProposalFailureKeepsPush(t *testing.T) { |
| 101 | e, f, r, _ := hubRig(t) |
| 102 | r.Specialist, r.StepID, r.Branch = wire.SpecialistEngineering, "s2", "equipo/tk1-another-fix" |
| 103 | base := gitOut(t, f.dir, "rev-parse", "main") |
| 104 | gitDir := e.tree(r, base).GitDir |
| 105 | gitOut(t, filepath.Dir(f.dir), "clone", "-q", "--bare", f.dir, gitDir) |
| 106 | tree := gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "rev-parse", base+"^{tree}") |
| 107 | r.Base, r.Candidate = base, gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "commit-tree", tree, "-p", base, "-m", "another fix") |
| 108 | spy := &proposalSpy{Forge: f, failure: errors.New("private raw forge diagnostic"), at: func(p Proposal) { |
| 109 | if p.Repo != r.Repo || p.Branch != r.Branch || gitOut(t, f.dir, "rev-parse", p.Branch) != r.Candidate { |
| 110 | t.Fatal("proposal before push or wrong branch") |
| 111 | } |
| 112 | }} |
| 113 | authority := &useSpy{Forge: spy} |
| 114 | e.Forge = authority |
| 115 | rep := e.publish(context.Background(), r, wire.StepReport{}) |
| 116 | if !reflect.DeepEqual(authority.uses, []Use{Push}) { |
| 117 | t.Fatalf("publish authority %v", authority.uses) |
| 118 | } |
| 119 | if !rep.Pushed || rep.State != wire.StepDone || rep.Reason != "proposal_failed" || len(spy.calls) != 1 || strings.Contains(rep.LogTail, "private raw") { |
| 120 | t.Fatalf("report %+v proposals %+v", rep, spy.calls) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func TestRelayProposalFailureKeepsPush(t *testing.T) { |
| 125 | r := newRelayRig(t) |
| 126 | spy := &proposalSpy{Forge: r.f, failure: errors.New("private raw forge diagnostic"), at: func(p Proposal) { |
| 127 | if p.Repo != r.header().Repo || p.Branch != relayBranch || r.head(t) != r.commit { |
| 128 | t.Fatal("proposal before push or wrong branch") |
| 129 | } |
| 130 | }} |
| 131 | authority := &useSpy{Forge: spy} |
| 132 | r.e.Forge = authority |
| 133 | r.receive(t, r.pack, r.header()) |
| 134 | r.publish(t, "pb_1") |
| 135 | r.relay.Once(context.Background()) |
| 136 | if !reflect.DeepEqual(authority.uses, []Use{Push}) { |
| 137 | t.Fatalf("relay authority %v", authority.uses) |
| 138 | } |
| 139 | r.relay.Once(context.Background()) |
| 140 | reps := r.reports(t) |
| 141 | if len(reps) != 1 || !reps[0].Pushed || reps[0].Reason != "proposal_failed" || r.pushCount() != 1 { |
| 142 | t.Fatalf("publication: %+v pushes %d", reps, r.pushCount()) |
| 143 | } |
| 144 | if len(spy.calls) != 1 { |
| 145 | t.Fatalf("proposals %+v", spy.calls) |
| 146 | } |
| 147 | } |
| 148 | |