| 1 | package forge |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "sync/atomic" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 13 | ) |
| 14 | |
| 15 | // TestMergeBranchLocalRefusalIsDefinite: a merge Equipo refuses itself |
| 16 | // before any request leaves for the hub (no usable credential, say) is a |
| 17 | // definite refusal with its own reason, never "outcome unknown": the |
| 18 | // remote is not read again to reconcile, the record goes back to the phase |
| 19 | // it had (prepared), and a later call asks the hub as usual. |
| 20 | func TestMergeBranchLocalRefusalIsDefinite(t *testing.T) { |
| 21 | h := &fakeHub{head: strings.Repeat("a", 40), answer: mergeNow} |
| 22 | m := newMerge(t, h) |
| 23 | var inspects atomic.Int32 |
| 24 | m.Inspect = func(ctx context.Context) (RemoteState, error) { inspects.Add(1); return h.inspect(ctx) } |
| 25 | refusing := m |
| 26 | refusing.Request = func(context.Context, MergeRequest) (MergeResult, error) { |
| 27 | return MergeResult{}, NotSent(m.Repo, ErrMergeNoCredential) |
| 28 | } |
| 29 | _, err := MergeBranch(context.Background(), refusing) |
| 30 | if !errors.Is(err, ErrMergeNoCredential) || errors.Is(err, ErrMergeUncertain) { |
| 31 | t.Fatalf("local refusal: %v", err) |
| 32 | } |
| 33 | if !strings.Contains(err.Error(), m.Repo) || strings.Contains(err.Error(), "outcome unknown") { |
| 34 | t.Fatalf("local refusal words: %v", err) |
| 35 | } |
| 36 | if inspects.Load() != 1 { |
| 37 | t.Fatalf("a local refusal read the remote %d times", inspects.Load()) |
| 38 | } |
| 39 | if rec, ok, _ := m.load(); !ok || rec.Phase != mergePrepared { |
| 40 | t.Fatalf("record after a local refusal: %+v", rec) |
| 41 | } |
| 42 | out, err := MergeBranch(context.Background(), m) |
| 43 | if err != nil || out.Source != SourceHub || len(h.requests) != 1 { |
| 44 | t.Fatalf("after the refusal %+v %v (%d requests)", out, err, len(h.requests)) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // TestNotSentNamesOnlyAValidRepository: the refusal's words name the |
| 49 | // repository only when it is a valid hub key. |
| 50 | func TestNotSentNamesOnlyAValidRepository(t *testing.T) { |
| 51 | if err := NotSent("amadan.net/../x\nInjected", ErrMergeRefused); strings.Contains(err.Error(), "Injected") || !errors.Is(err, ErrMergeRefused) { |
| 52 | t.Fatalf("invalid repository named: %v", err) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // TestHubMergeRefusesLocallyBeforeSending: HubMerge's own refusals (no |
| 57 | // bearer, a bad repository or branch) are made before any request and |
| 58 | // say so; the hub is never contacted. |
| 59 | func TestHubMergeRefusesLocallyBeforeSending(t *testing.T) { |
| 60 | var hits atomic.Int32 |
| 61 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { hits.Add(1); w.WriteHeader(500) })) |
| 62 | defer srv.Close() |
| 63 | req := MergeRequest{ExpectSHA: strings.Repeat("a", 40), Strategy: "merge"} |
| 64 | for _, c := range []struct { |
| 65 | cred Credential |
| 66 | repo, branch string |
| 67 | want error |
| 68 | }{ |
| 69 | {Credential{Agent: "equipo~p@example.com"}, "amadan.net/oficina/sample", "equipo/tk1-x", ErrMergeNoCredential}, |
| 70 | {Credential{Agent: "equipo~p@example.com", Bearer: "a\nb"}, "amadan.net/oficina/sample", "equipo/tk1-x", ErrMergeNoCredential}, |
| 71 | {Credential{Agent: "equipo~p@example.com", Bearer: "token"}, "github.com/x/y", "equipo/tk1-x", ErrMergeRefused}, |
| 72 | {Credential{Agent: "equipo~p@example.com", Bearer: "token"}, "amadan.net/oficina/sample", "bad..ref", ErrMergeRefused}, |
| 73 | } { |
| 74 | _, err := HubMerge(context.Background(), srv.Client(), srv.URL, c.cred, c.repo, c.branch, req) |
| 75 | var ns *NotSentError |
| 76 | if !errors.Is(err, c.want) || !errors.As(err, &ns) || errors.Is(err, ErrMergeUncertain) { |
| 77 | t.Fatalf("%+v %s %s: %v", c.cred.Agent, c.repo, c.branch, err) |
| 78 | } |
| 79 | if strings.Contains(err.Error(), "token") { |
| 80 | t.Fatalf("bearer in the refusal: %v", err) |
| 81 | } |
| 82 | } |
| 83 | if hits.Load() != 0 { |
| 84 | t.Fatalf("the hub was contacted %d times", hits.Load()) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // TestMergeBranchLocalRefusalAfterAnUnresolvedRequest: a first request |
| 89 | // whose answer was lost (it may still land) leaves the record requested. |
| 90 | // A later local refusal must not end that as definite: it stays uncertain, |
| 91 | // the remote is read again, and once the earlier request lands a restart |
| 92 | // settles it from the default branch with no new request. |
| 93 | func TestMergeBranchLocalRefusalAfterAnUnresolvedRequest(t *testing.T) { |
| 94 | h := &fakeHub{head: strings.Repeat("a", 40), answer: mergeNow} |
| 95 | m := newMerge(t, h) |
| 96 | var delayed MergeRequest |
| 97 | lost := m |
| 98 | lost.Request = func(_ context.Context, req MergeRequest) (MergeResult, error) { |
| 99 | delayed = req // the hub has it, and has not acted yet |
| 100 | return MergeResult{}, errors.New("answer lost") |
| 101 | } |
| 102 | if _, err := MergeBranch(context.Background(), lost); !errors.Is(err, ErrMergeUncertain) { |
| 103 | t.Fatalf("lost answer: %v", err) |
| 104 | } |
| 105 | var inspects atomic.Int32 |
| 106 | refusing := m |
| 107 | refusing.Inspect = func(ctx context.Context) (RemoteState, error) { inspects.Add(1); return h.inspect(ctx) } |
| 108 | refusing.Request = func(context.Context, MergeRequest) (MergeResult, error) { |
| 109 | return MergeResult{}, NotSent(m.Repo, ErrMergeNoCredential) |
| 110 | } |
| 111 | _, err := MergeBranch(context.Background(), refusing) |
| 112 | if !errors.Is(err, ErrMergeUncertain) || errors.Is(err, ErrMergeNoCredential) { |
| 113 | t.Fatalf("local refusal after an unresolved request: %v", err) |
| 114 | } |
| 115 | if inspects.Load() != 2 { |
| 116 | t.Fatalf("not reconciled: %d inspections", inspects.Load()) |
| 117 | } |
| 118 | if rec, ok, _ := m.load(); !ok || rec.Phase != mergeRequested { |
| 119 | t.Fatalf("record %+v", rec) |
| 120 | } |
| 121 | // A preflight refusal with the request still unresolved is uncertain too. |
| 122 | preflight := m |
| 123 | preflight.Preflight = func(context.Context) error { return NotSent(m.Repo, ErrMergeNoCredential) } |
| 124 | if _, err := MergeBranch(context.Background(), preflight); !errors.Is(err, ErrMergeUncertain) { |
| 125 | t.Fatalf("preflight after an unresolved request: %v", err) |
| 126 | } |
| 127 | // The delayed request lands; a restart settles it, asking nothing. |
| 128 | h.mu.Lock() |
| 129 | mergeNow(h, delayed) |
| 130 | h.mu.Unlock() |
| 131 | out, err := MergeBranch(context.Background(), m) |
| 132 | if err != nil || !out.Reconciled || out.SHA != strings.Repeat("8", 40) || len(h.requests) != 0 { |
| 133 | t.Fatalf("settled %+v %v (%d requests)", out, err, len(h.requests)) |
| 134 | } |
| 135 | if rec, ok, _ := m.load(); !ok || rec.Phase != mergeMerged { |
| 136 | t.Fatalf("record after settling %+v", rec) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // TestMergeBranchPreflight: a local refusal found before the remote is |
| 141 | // read (the inspection itself needs the credential) is definite when no |
| 142 | // request was ever sent: nothing is inspected, confirmed or requested. |
| 143 | func TestMergeBranchPreflight(t *testing.T) { |
| 144 | h := &fakeHub{head: strings.Repeat("a", 40), answer: mergeNow} |
| 145 | m := newMerge(t, h) |
| 146 | var inspects, confirms atomic.Int32 |
| 147 | m.Inspect = func(ctx context.Context) (RemoteState, error) { inspects.Add(1); return h.inspect(ctx) } |
| 148 | m.Confirm = func(context.Context) error { confirms.Add(1); return nil } |
| 149 | refused := m |
| 150 | refused.Preflight = func(context.Context) error { return NotSent(m.Repo, ErrMergeNoCredential) } |
| 151 | _, err := MergeBranch(context.Background(), refused) |
| 152 | if !errors.Is(err, ErrMergeNoCredential) || errors.Is(err, ErrMergeUncertain) || inspects.Load() != 0 || confirms.Load() != 0 || len(h.requests) != 0 { |
| 153 | t.Fatalf("preflight: %v (%d inspections, %d confirms, %d requests)", err, inspects.Load(), confirms.Load(), len(h.requests)) |
| 154 | } |
| 155 | // Passing, it merges as usual. |
| 156 | m.Preflight = func(context.Context) error { return nil } |
| 157 | if out, err := MergeBranch(context.Background(), m); err != nil || out.Source != SourceHub { |
| 158 | t.Fatalf("after %+v %v", out, err) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // TestMergeBranchReconcilesBeforeTheWriteCheck: an earlier request whose |
| 163 | // answer was lost landed meanwhile. The write check fails now (a revoked |
| 164 | // grant, a hub list that cannot be fetched), yet the default branch reads |
| 165 | // fine: the merge settles from it, and the write check is never asked. |
| 166 | func TestMergeBranchReconcilesBeforeTheWriteCheck(t *testing.T) { |
| 167 | for _, refusal := range []error{ErrMergeRefused, workspace.ErrHubUnavailable} { |
| 168 | h := &fakeHub{head: strings.Repeat("a", 40), answer: mergeNow} |
| 169 | m := newMerge(t, h) |
| 170 | lost := m |
| 171 | lost.Request = func(_ context.Context, req MergeRequest) (MergeResult, error) { |
| 172 | mergeNow(h, req) // landed; the answer is lost |
| 173 | return MergeResult{}, errors.New("answer lost") |
| 174 | } |
| 175 | lost.Inspect = func(context.Context) (RemoteState, error) { |
| 176 | if rec, ok, _ := m.load(); ok && rec.Phase == mergeRequested { |
| 177 | return RemoteState{}, errors.New("process killed") |
| 178 | } |
| 179 | return h.inspect(context.Background()) |
| 180 | } |
| 181 | if _, err := MergeBranch(context.Background(), lost); !errors.Is(err, ErrMergeUncertain) { |
| 182 | t.Fatalf("lost: %v", err) |
| 183 | } |
| 184 | var preflights atomic.Int32 |
| 185 | m.Preflight = func(context.Context) error { preflights.Add(1); return NotSent(m.Repo, refusal) } |
| 186 | out, err := MergeBranch(context.Background(), m) |
| 187 | if err != nil || !out.Reconciled || out.SHA != strings.Repeat("8", 40) || preflights.Load() != 0 || len(h.requests) != 0 { |
| 188 | t.Fatalf("%v: settled %+v %v (%d preflights, %d requests)", refusal, out, err, preflights.Load(), len(h.requests)) |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // TestPushRemoteUnavailableIsNotRefused: a remote the hub could not be |
| 194 | // asked about (write access unconfirmed) is a failure to try again, never |
| 195 | // the definite refusal a missing credential is. |
| 196 | func TestPushRemoteUnavailableIsNotRefused(t *testing.T) { |
| 197 | b := BranchPush{Repo: "amadan.net/oficina/sample", Remote: func(context.Context) (workspace.Remote, error) { |
| 198 | return workspace.Remote{}, workspace.ErrHubUnavailable |
| 199 | }} |
| 200 | _, err := b.remote(context.Background()) |
| 201 | if !errors.Is(err, workspace.ErrHubUnavailable) || errors.Is(err, ErrPushRefused) { |
| 202 | t.Fatalf("unavailable: %v", err) |
| 203 | } |
| 204 | b.Remote = func(context.Context) (workspace.Remote, error) { return workspace.Remote{}, workspace.ErrGitPassword } |
| 205 | if _, err := b.remote(context.Background()); !errors.Is(err, ErrPushRefused) { |
| 206 | t.Fatalf("no credential: %v", err) |
| 207 | } |
| 208 | } |
| 209 | |