| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package checkout |
| |
| import ( |
| "net/url" |
| "testing" |
| |
| buildbucketpb "go.chromium.org/luci/buildbucket/proto" |
| ) |
| |
| func TestResolveRepo(t *testing.T) { |
| t.Parallel() |
| |
| tests := []struct { |
| input *buildbucketpb.Build_Input |
| fallbackRemote string |
| expectedHost string |
| expectedProject string |
| expectErr bool |
| }{ |
| { |
| input: &buildbucketpb.Build_Input{ |
| GitilesCommit: &buildbucketpb.GitilesCommit{ |
| Host: "fuchsia.googlesource.com", |
| Project: "project", |
| }, |
| }, |
| fallbackRemote: "https://fuchsia.googlesource.com/integration", |
| expectedHost: "fuchsia.googlesource.com", |
| expectedProject: "project", |
| }, |
| { |
| input: &buildbucketpb.Build_Input{ |
| GitilesCommit: &buildbucketpb.GitilesCommit{ |
| Host: "fuchsia.googlesource.com", |
| Project: "project", |
| }, |
| GerritChanges: []*buildbucketpb.GerritChange{ |
| { |
| Host: "fuchsia-review.googlesource.com", |
| Project: "should-be-ignored", |
| }, |
| }, |
| }, |
| fallbackRemote: "https://fuchsia.googlesource.com/integration", |
| expectedHost: "fuchsia.googlesource.com", |
| expectedProject: "project", |
| }, |
| { |
| input: &buildbucketpb.Build_Input{ |
| GerritChanges: []*buildbucketpb.GerritChange{ |
| { |
| Host: "fuchsia-review.googlesource.com", |
| Project: "project", |
| }, |
| }, |
| }, |
| fallbackRemote: "https://fuchsia.googlesource.com/integration", |
| expectedHost: "fuchsia.googlesource.com", |
| expectedProject: "project", |
| }, |
| { |
| input: &buildbucketpb.Build_Input{}, |
| fallbackRemote: "https://fuchsia.googlesource.com/integration", |
| expectedHost: "fuchsia.googlesource.com", |
| expectedProject: "integration", |
| }, |
| } |
| |
| for _, test := range tests { |
| host, project, err := ResolveRepo(test.input, test.fallbackRemote) |
| if err == nil { |
| if test.expectErr { |
| t.Fatalf("expected error, got nil") |
| } |
| if host != test.expectedHost { |
| t.Fatalf("host %q does not match expected %q", host, test.expectedHost) |
| } |
| if project != test.expectedProject { |
| t.Fatalf("project %q does not match expected %q", project, test.expectedProject) |
| } |
| } else if !test.expectErr { |
| t.Fatalf("got unexpected error: %s", err) |
| } |
| } |
| } |
| |
| func TestResolveIntegrationURL(t *testing.T) { |
| t.Parallel() |
| |
| tests := []struct { |
| gerritHost string |
| gitilesHost string |
| project string |
| hostOverride string |
| expectedURL string |
| expectErr bool |
| }{ |
| { |
| gerritHost: "fuchsia-review.googlesource.com", |
| project: "integration", |
| expectedURL: "https://fuchsia.googlesource.com/integration", |
| }, |
| { |
| gitilesHost: "fuchsia.googlesource.com", |
| project: "integration", |
| expectedURL: "https://fuchsia.googlesource.com/integration", |
| }, |
| { |
| gitilesHost: "fuchsia.googlesource.com", |
| project: "fuchsia", |
| expectedURL: "https://fuchsia.googlesource.com/integration", |
| }, |
| { |
| gitilesHost: "foo.googlesource.com", |
| project: "bar", |
| hostOverride: "override.googlesource.com", |
| expectedURL: "https://override.googlesource.com/integration", |
| }, |
| { |
| expectErr: true, |
| }, |
| } |
| |
| for _, test := range tests { |
| buildInput := buildbucketpb.Build_Input{} |
| if test.gerritHost != "" { |
| buildInput.GerritChanges = []*buildbucketpb.GerritChange{{Host: test.gerritHost, Project: test.project}} |
| } else if test.gitilesHost != "" { |
| buildInput.GitilesCommit = &buildbucketpb.GitilesCommit{Host: test.gitilesHost, Project: test.project} |
| } |
| expectedURL, err := url.Parse(test.expectedURL) |
| if err != nil { |
| t.Fatalf("invalid test input URL %q", test.expectedURL) |
| } |
| integrationURL, err := ResolveIntegrationURL(&buildInput, test.hostOverride) |
| if err == nil { |
| if test.expectErr { |
| t.Fatalf("expected error, got nil") |
| } |
| if integrationURL.String() != expectedURL.String() { |
| t.Fatalf("integration URL %q does not match expected %q", integrationURL, expectedURL) |
| } |
| } else if !test.expectErr { |
| t.Fatalf("got unexpected error: %s", err) |
| } |
| } |
| } |
| |
| func TestHasRepoChange(t *testing.T) { |
| t.Parallel() |
| |
| tests := []struct { |
| input *buildbucketpb.Build_Input |
| remote string |
| expected bool |
| }{ |
| { |
| input: &buildbucketpb.Build_Input{ |
| GerritChanges: []*buildbucketpb.GerritChange{ |
| { |
| Host: "fuchsia-review.googlesource.com", |
| Project: "infra/recipes", |
| }, |
| }, |
| }, |
| remote: "https://fuchsia.googlesource.com/infra/recipes", |
| expected: true, |
| }, |
| { |
| input: &buildbucketpb.Build_Input{ |
| GerritChanges: []*buildbucketpb.GerritChange{ |
| { |
| Host: "foo-review.googlesource.com", |
| Project: "infra/recipes", |
| }, |
| }, |
| }, |
| remote: "https://fuchsia.googlesource.com/infra/recipes", |
| }, |
| { |
| input: &buildbucketpb.Build_Input{ |
| GerritChanges: []*buildbucketpb.GerritChange{ |
| { |
| Host: "fuchsia-review.googlesource.com", |
| Project: "fuchsia", |
| }, |
| }, |
| }, |
| remote: "https://fuchsia.googlesource.com/infra/recipes", |
| }, |
| { |
| input: &buildbucketpb.Build_Input{ |
| GerritChanges: []*buildbucketpb.GerritChange{ |
| { |
| Host: "fuchsia-review.googlesource.com", |
| Project: "fuchsia", |
| }, |
| { |
| Host: "fuchsia-review.googlesource.com", |
| Project: "other", |
| }, |
| }, |
| }, |
| remote: "https://fuchsia.googlesource.com/fuchsia", |
| expected: true, |
| }, |
| } |
| |
| for _, test := range tests { |
| ok, err := HasRepoChange(test.input, test.remote) |
| if err != nil { |
| t.Fatalf("got unexpected error: %s", err) |
| } |
| if ok != test.expected { |
| t.Fatalf("expected %t, got %t", ok, test.expected) |
| } |
| } |
| } |