| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/base64" |
| 6 | "errors" |
| 7 | "net/http" |
| 8 | "reflect" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner/forge" |
| 12 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 13 | "amadan.net/oficina/equipo/internal/wire" |
| 14 | "amadan.net/oficina/equipo/internal/wire/forgekey" |
| 15 | ) |
| 16 | |
| 17 | type routeForge struct { |
| 18 | Forge |
| 19 | host string |
| 20 | calls []string |
| 21 | uses []Use |
| 22 | repos []string |
| 23 | rechecks int |
| 24 | } |
| 25 | |
| 26 | func (f *routeForge) Host() string { return f.host } |
| 27 | func (f *routeForge) Repos() []string { return f.repos } |
| 28 | func (f *routeForge) Reachable(context.Context) []string { return f.repos } |
| 29 | func (f *routeForge) Recheck() { f.rechecks++ } |
| 30 | func (f *routeForge) Agent() string { return f.host + "-agent" } |
| 31 | func (f *routeForge) CreateIn(context.Context) []string { return []string{f.host + "-namespace"} } |
| 32 | func (f *routeForge) Remote(_ context.Context, repo string, use Use) (workspace.Remote, error) { |
| 33 | f.calls = append(f.calls, repo) |
| 34 | f.uses = append(f.uses, use) |
| 35 | return workspace.Remote{URL: "https://" + repo}, nil |
| 36 | } |
| 37 | func (f *routeForge) Exists(_ context.Context, repo string) (bool, error) { |
| 38 | f.calls = append(f.calls, repo) |
| 39 | return true, nil |
| 40 | } |
| 41 | func (f *routeForge) Create(_ context.Context, repo string) error { |
| 42 | f.calls = append(f.calls, repo) |
| 43 | return nil |
| 44 | } |
| 45 | func (f *routeForge) Propose(_ context.Context, p Proposal) (ProposalRef, error) { |
| 46 | f.calls = append(f.calls, p.Repo) |
| 47 | return ProposalRef{URL: p.Branch}, nil |
| 48 | } |
| 49 | func (f *routeForge) Merge(_ context.Context, repo, branch string, req forge.MergeRequest) (forge.MergeResult, error) { |
| 50 | f.calls = append(f.calls, repo) |
| 51 | return forge.MergeResult{SHA: req.ExpectSHA, Into: branch}, nil |
| 52 | } |
| 53 | |
| 54 | func TestForgesRouteWithoutFallback(t *testing.T) { |
| 55 | ctx := context.Background() |
| 56 | hub := &routeForge{host: forgekey.Hub} |
| 57 | gh := &routeForge{host: forgekey.GitHub} |
| 58 | f, err := NewForges(hub, gh) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | for _, repo := range []string{"oficina/site", "github.com/Org/Repo"} { |
| 63 | if _, err = f.Remote(ctx, repo, Read); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | if _, err = f.Remote(ctx, repo, Push); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | if _, err = f.Exists(ctx, repo); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if err = f.Create(ctx, repo); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if ref, e := f.Propose(ctx, Proposal{Repo: repo, Branch: "equipo/test"}); e != nil || ref.URL != "equipo/test" { |
| 76 | t.Fatalf("proposal: %+v %v", ref, e) |
| 77 | } |
| 78 | if res, e := f.Merge(ctx, repo, "main", forge.MergeRequest{ExpectSHA: "judged"}); e != nil || res.SHA != "judged" || res.Into != "main" { |
| 79 | t.Fatalf("merge: %+v %v", res, e) |
| 80 | } |
| 81 | } |
| 82 | for _, want := range []struct { |
| 83 | f *routeForge |
| 84 | repo string |
| 85 | }{{hub, "amadan.net/oficina/site"}, {gh, "github.com/Org/Repo"}} { |
| 86 | if len(want.f.calls) != 6 || !reflect.DeepEqual(want.f.uses, []Use{Read, Push}) { |
| 87 | t.Fatalf("calls: %+v", want.f) |
| 88 | } |
| 89 | for _, got := range want.f.calls { |
| 90 | if got != want.repo { |
| 91 | t.Fatalf("route: %q", got) |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | onlyHub, _ := NewForges(hub) |
| 96 | for _, repo := range []string{"amadan.net/a", "github.com/a", "github.com/Org/Repo", "evil.com/a/b", "amadan.net/../b"} { |
| 97 | n := len(hub.calls) |
| 98 | if _, err = onlyHub.Remote(ctx, repo, Read); err == nil { |
| 99 | t.Fatalf("remote accepted %q", repo) |
| 100 | } |
| 101 | if _, err = onlyHub.Exists(ctx, repo); err == nil { |
| 102 | t.Fatal("exists fallback") |
| 103 | } |
| 104 | if err = onlyHub.Create(ctx, repo); err == nil { |
| 105 | t.Fatal("create fallback") |
| 106 | } |
| 107 | if _, err = onlyHub.Propose(ctx, Proposal{Repo: repo}); err == nil { |
| 108 | t.Fatal("proposal fallback") |
| 109 | } |
| 110 | if _, err = onlyHub.Merge(ctx, repo, "main", forge.MergeRequest{}); err == nil { |
| 111 | t.Fatal("merge fallback") |
| 112 | } |
| 113 | if len(hub.calls) != n { |
| 114 | t.Fatal("unregistered key reached hub") |
| 115 | } |
| 116 | } |
| 117 | n := len(hub.calls) |
| 118 | if _, err = f.Remote(ctx, "oficina/site", Use(99)); err == nil || len(hub.calls) != n { |
| 119 | t.Fatal("unknown use reached forge") |
| 120 | } |
| 121 | for _, fs := range [][]Forge{{hub, hub}, {&routeForge{host: "evil.com"}}, {nil}} { |
| 122 | if _, err := NewForges(fs...); err == nil { |
| 123 | t.Fatal("invalid registration") |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestForgesCatalogAndHubIdentity(t *testing.T) { |
| 129 | hub := &routeForge{host: forgekey.Hub, repos: []string{"amadan.net/o/b", "amadan.net/o/a", "github.com/o/c", "amadan.net/o/a"}} |
| 130 | gh := &routeForge{host: forgekey.GitHub, repos: []string{"github.com/o/c", "amadan.net/o/d"}} |
| 131 | f, _ := NewForges(hub, gh) |
| 132 | want := []string{"amadan.net/o/a", "amadan.net/o/b", "github.com/o/c"} |
| 133 | if !reflect.DeepEqual(f.Repos(), want) || !reflect.DeepEqual(f.Reachable(context.Background()), want) { |
| 134 | t.Fatal("catalog crosses host or loses order") |
| 135 | } |
| 136 | if f.Agent() != "amadan.net-agent" || !reflect.DeepEqual(f.CreateIn(context.Background()), []string{"amadan.net-namespace"}) { |
| 137 | t.Fatal("hub identity changed") |
| 138 | } |
| 139 | f.Recheck() |
| 140 | if hub.rechecks != 1 || gh.rechecks != 1 { |
| 141 | t.Fatal("recheck not forwarded") |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestHubProposalIsOnlyALink(t *testing.T) { |
| 146 | f := accessForge(t, func(http.ResponseWriter, *http.Request) { t.Fatal("proposal contacted hub") }) |
| 147 | ref, err := f.Propose(context.Background(), Proposal{Repo: "amadan.net/oficina/site", Branch: "equipo/t1-a"}) |
| 148 | if err != nil || ref.URL != "https://amadan.net/oficina/site?branch=equipo/t1-a" { |
| 149 | t.Fatalf("proposal %+v %v", ref, err) |
| 150 | } |
| 151 | for _, p := range []Proposal{{Repo: "github.com/o/r", Branch: "equipo/t1-a"}, {Repo: "amadan.net/o/r", Branch: "main"}, {Repo: "amadan.net/../r", Branch: "equipo/t1-a"}} { |
| 152 | if _, err := f.Propose(context.Background(), p); err == nil { |
| 153 | t.Fatalf("proposal admitted %+v", p) |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestRemoteUsername(t *testing.T) { |
| 159 | for _, tc := range []struct{ user, want string }{{"", "x"}, {"x-access-token", "x-access-token"}} { |
| 160 | env, _, err := remoteEnv(workspace.Remote{URL: "https://amadan.net/o/r", GitPassword: "secret", GitUsername: tc.user}) |
| 161 | want := "Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte(tc.want+":secret")) |
| 162 | if err != nil || env["GIT_CONFIG_VALUE_9"] != want { |
| 163 | t.Fatalf("username: %v %v", env, err) |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | type serviceForge struct { |
| 169 | *routeForge |
| 170 | serviceCalls []string |
| 171 | } |
| 172 | |
| 173 | func (f *serviceForge) CIHosted(_ context.Context, repo string) (bool, error) { |
| 174 | f.serviceCalls = append(f.serviceCalls, "hosted:"+repo) |
| 175 | return true, nil |
| 176 | } |
| 177 | func (f *serviceForge) CIStatus(_ context.Context, repo, branch, logs string) (forge.CIStatus, error) { |
| 178 | f.serviceCalls = append(f.serviceCalls, "status:"+repo+":"+branch+":"+logs) |
| 179 | return forge.CIStatus{}, nil |
| 180 | } |
| 181 | func (f *serviceForge) RequestAccess(_ context.Context, c wire.AccessCommand) (wire.AccessReport, error) { |
| 182 | f.serviceCalls = append(f.serviceCalls, "request:"+c.Target) |
| 183 | return wire.AccessReport{}, nil |
| 184 | } |
| 185 | func (f *serviceForge) PollAccess(_ context.Context, id string, c wire.AccessCommand) (wire.AccessReport, error) { |
| 186 | f.serviceCalls = append(f.serviceCalls, "poll:"+id+":"+c.Target) |
| 187 | return wire.AccessReport{}, nil |
| 188 | } |
| 189 | func TestForgesForwardOptionalHubServices(t *testing.T) { |
| 190 | ctx := context.Background() |
| 191 | hub := &serviceForge{routeForge: &routeForge{host: forgekey.Hub}} |
| 192 | gh := &serviceForge{routeForge: &routeForge{host: forgekey.GitHub}} |
| 193 | f, _ := NewForges(hub, gh) |
| 194 | for _, repo := range []string{"amadan.net/o/r", "github.com/O/R"} { |
| 195 | if hosted, err := f.CIHosted(ctx, repo); err != nil || !hosted { |
| 196 | t.Fatal("hosted not forwarded", err) |
| 197 | } |
| 198 | if _, err := f.CIStatus(ctx, repo, "equipo/fix", forge.CILogsFailed); err != nil { |
| 199 | t.Fatal(err) |
| 200 | } |
| 201 | } |
| 202 | c := wire.AccessCommand{Target: "o/r"} |
| 203 | if _, err := f.RequestAccess(ctx, c); err != nil { |
| 204 | t.Fatal(err) |
| 205 | } |
| 206 | if _, err := f.PollAccess(ctx, "a1", c); err != nil { |
| 207 | t.Fatal(err) |
| 208 | } |
| 209 | want := []string{"hosted:amadan.net/o/r", "status:amadan.net/o/r:equipo/fix:" + forge.CILogsFailed, "request:o/r", "poll:a1:o/r"} |
| 210 | if !reflect.DeepEqual(hub.serviceCalls, want) { |
| 211 | t.Fatalf("hub services %v", hub.serviceCalls) |
| 212 | } |
| 213 | want = []string{"hosted:github.com/O/R", "status:github.com/O/R:equipo/fix:" + forge.CILogsFailed} |
| 214 | if !reflect.DeepEqual(gh.serviceCalls, want) { |
| 215 | t.Fatalf("other services %v", gh.serviceCalls) |
| 216 | } |
| 217 | empty, _ := NewForges() |
| 218 | if _, err := empty.RequestAccess(ctx, c); err == nil { |
| 219 | t.Fatal("missing access forge admitted") |
| 220 | } |
| 221 | if _, err := empty.PollAccess(ctx, "a1", c); err == nil { |
| 222 | t.Fatal("missing poll forge admitted") |
| 223 | } |
| 224 | if _, err := empty.CIHosted(ctx, "amadan.net/o/r"); err == nil { |
| 225 | t.Fatal("missing CI forge admitted") |
| 226 | } |
| 227 | if _, err := empty.CIStatus(ctx, "amadan.net/o/r", "b", forge.CILogsNone); err == nil { |
| 228 | t.Fatal("missing status forge admitted") |
| 229 | } |
| 230 | plain, _ := NewForges(hub.routeForge) |
| 231 | if hosted, err := plain.CIHosted(ctx, "amadan.net/o/r"); err != nil || hosted { |
| 232 | t.Fatal("absent CI capability changed") |
| 233 | } |
| 234 | if _, err := plain.CIStatus(ctx, "amadan.net/o/r", "b", forge.CILogsNone); !errors.Is(err, forge.ErrCIRead) { |
| 235 | t.Fatal("absent status capability changed") |
| 236 | } |
| 237 | } |
| 238 | func TestFileForgeRejectsUnknownUseBeforeIO(t *testing.T) { |
| 239 | f := accessForge(t, func(http.ResponseWriter, *http.Request) { t.Fatal("invalid use contacted hub") }) |
| 240 | if _, err := f.Remote(context.Background(), "amadan.net/o/r", Use(99)); err == nil { |
| 241 | t.Fatal("invalid use admitted") |
| 242 | } |
| 243 | } |
| 244 | |