go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/recipe_wrapper/checkout/checkout_test.go (about) 1 // Copyright 2019 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package checkout 6 7 import ( 8 "net/url" 9 "testing" 10 11 buildbucketpb "go.chromium.org/luci/buildbucket/proto" 12 ) 13 14 func TestResolveRepo(t *testing.T) { 15 t.Parallel() 16 17 tests := []struct { 18 input *buildbucketpb.Build_Input 19 fallbackRemote string 20 expectedHost string 21 expectedProject string 22 expectErr bool 23 }{ 24 { 25 input: &buildbucketpb.Build_Input{ 26 GitilesCommit: &buildbucketpb.GitilesCommit{ 27 Host: "fuchsia.googlesource.com", 28 Project: "project", 29 }, 30 }, 31 fallbackRemote: "https://fuchsia.googlesource.com/integration", 32 expectedHost: "fuchsia.googlesource.com", 33 expectedProject: "project", 34 }, 35 { 36 input: &buildbucketpb.Build_Input{ 37 GitilesCommit: &buildbucketpb.GitilesCommit{ 38 Host: "fuchsia.googlesource.com", 39 Project: "project", 40 }, 41 GerritChanges: []*buildbucketpb.GerritChange{ 42 { 43 Host: "fuchsia-review.googlesource.com", 44 Project: "should-be-ignored", 45 }, 46 }, 47 }, 48 fallbackRemote: "https://fuchsia.googlesource.com/integration", 49 expectedHost: "fuchsia.googlesource.com", 50 expectedProject: "project", 51 }, 52 { 53 input: &buildbucketpb.Build_Input{ 54 GerritChanges: []*buildbucketpb.GerritChange{ 55 { 56 Host: "fuchsia-review.googlesource.com", 57 Project: "project", 58 }, 59 }, 60 }, 61 fallbackRemote: "https://fuchsia.googlesource.com/integration", 62 expectedHost: "fuchsia.googlesource.com", 63 expectedProject: "project", 64 }, 65 { 66 input: &buildbucketpb.Build_Input{}, 67 fallbackRemote: "https://fuchsia.googlesource.com/integration", 68 expectedHost: "fuchsia.googlesource.com", 69 expectedProject: "integration", 70 }, 71 } 72 73 for _, test := range tests { 74 host, project, err := ResolveRepo(test.input, test.fallbackRemote) 75 if err == nil { 76 if test.expectErr { 77 t.Fatalf("expected error, got nil") 78 } 79 if host != test.expectedHost { 80 t.Fatalf("host %q does not match expected %q", host, test.expectedHost) 81 } 82 if project != test.expectedProject { 83 t.Fatalf("project %q does not match expected %q", project, test.expectedProject) 84 } 85 } else if !test.expectErr { 86 t.Fatalf("got unexpected error: %s", err) 87 } 88 } 89 } 90 91 func TestResolveIntegrationURL(t *testing.T) { 92 t.Parallel() 93 94 tests := []struct { 95 gerritHost string 96 gitilesHost string 97 project string 98 hostOverride string 99 integrationProjectOverride string 100 expectedURL string 101 expectErr bool 102 }{ 103 { 104 gerritHost: "fuchsia-review.googlesource.com", 105 project: "integration", 106 expectedURL: "https://fuchsia.googlesource.com/integration", 107 }, 108 { 109 gitilesHost: "fuchsia.googlesource.com", 110 project: "integration", 111 expectedURL: "https://fuchsia.googlesource.com/integration", 112 }, 113 { 114 gitilesHost: "fuchsia.googlesource.com", 115 project: "fuchsia", 116 expectedURL: "https://fuchsia.googlesource.com/integration", 117 }, 118 { 119 gitilesHost: "foo.googlesource.com", 120 project: "bar", 121 hostOverride: "override.googlesource.com", 122 expectedURL: "https://override.googlesource.com/integration", 123 }, 124 { 125 gitilesHost: "fuchsia.googlesource.com", 126 project: "fuchsia", 127 integrationProjectOverride: "override", 128 expectedURL: "https://fuchsia.googlesource.com/override", 129 }, 130 { 131 expectErr: true, 132 }, 133 } 134 135 for _, test := range tests { 136 buildInput := buildbucketpb.Build_Input{} 137 if test.gerritHost != "" { 138 buildInput.GerritChanges = []*buildbucketpb.GerritChange{{Host: test.gerritHost, Project: test.project}} 139 } else if test.gitilesHost != "" { 140 buildInput.GitilesCommit = &buildbucketpb.GitilesCommit{Host: test.gitilesHost, Project: test.project} 141 } 142 expectedURL, err := url.Parse(test.expectedURL) 143 if err != nil { 144 t.Fatalf("invalid test input URL %q", test.expectedURL) 145 } 146 integrationURL, err := ResolveIntegrationURL(&buildInput, test.hostOverride, test.integrationProjectOverride) 147 if err == nil { 148 if test.expectErr { 149 t.Fatalf("expected error, got nil") 150 } 151 if integrationURL.String() != expectedURL.String() { 152 t.Fatalf("integration URL %q does not match expected %q", integrationURL, expectedURL) 153 } 154 } else if !test.expectErr { 155 t.Fatalf("got unexpected error: %s", err) 156 } 157 } 158 } 159 160 func TestHasRepoChange(t *testing.T) { 161 t.Parallel() 162 163 tests := []struct { 164 input *buildbucketpb.Build_Input 165 remote string 166 expected bool 167 }{ 168 { 169 input: &buildbucketpb.Build_Input{ 170 GerritChanges: []*buildbucketpb.GerritChange{ 171 { 172 Host: "fuchsia-review.googlesource.com", 173 Project: "infra/recipes", 174 }, 175 }, 176 }, 177 remote: "https://fuchsia.googlesource.com/infra/recipes", 178 expected: true, 179 }, 180 { 181 input: &buildbucketpb.Build_Input{ 182 GerritChanges: []*buildbucketpb.GerritChange{ 183 { 184 Host: "foo-review.googlesource.com", 185 Project: "infra/recipes", 186 }, 187 }, 188 }, 189 remote: "https://fuchsia.googlesource.com/infra/recipes", 190 }, 191 { 192 input: &buildbucketpb.Build_Input{ 193 GerritChanges: []*buildbucketpb.GerritChange{ 194 { 195 Host: "fuchsia-review.googlesource.com", 196 Project: "fuchsia", 197 }, 198 }, 199 }, 200 remote: "https://fuchsia.googlesource.com/infra/recipes", 201 }, 202 { 203 input: &buildbucketpb.Build_Input{ 204 GerritChanges: []*buildbucketpb.GerritChange{ 205 { 206 Host: "fuchsia-review.googlesource.com", 207 Project: "fuchsia", 208 }, 209 { 210 Host: "fuchsia-review.googlesource.com", 211 Project: "other", 212 }, 213 }, 214 }, 215 remote: "https://fuchsia.googlesource.com/fuchsia", 216 expected: true, 217 }, 218 } 219 220 for _, test := range tests { 221 ok, err := HasRepoChange(test.input, test.remote) 222 if err != nil { 223 t.Fatalf("got unexpected error: %s", err) 224 } 225 if ok != test.expected { 226 t.Fatalf("expected %t, got %t", ok, test.expected) 227 } 228 } 229 }