go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/checkout/strategy_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 TestNewCheckoutStrategy(t *testing.T) { 15 tests := []struct { 16 // A name for this test case. 17 name string 18 19 // The integration repo URL. 20 specRepoURL url.URL 21 22 // The Buildbucket build input. 23 input *buildbucketpb.Build_Input 24 25 // The spec ref. 26 specRef string 27 28 // The name of the expected strategy. 29 expectedStrategy strategy 30 31 // Whether to expect an error 32 expectErr bool 33 }{ 34 { 35 name: "should checkout from specRef if the gerrit change is for a different project", 36 specRepoURL: url.URL{ 37 Scheme: "https", 38 Host: "fuchsia.googlesource.com", 39 Path: "/repo", 40 }, 41 input: &buildbucketpb.Build_Input{ 42 GerritChanges: []*buildbucketpb.GerritChange{{ 43 Host: "fuchsia-review.googlesource.com", 44 Project: "different", 45 }}, 46 }, 47 specRef: "deadbeef", 48 expectedStrategy: &checkoutCommit{ 49 commit: &buildbucketpb.GitilesCommit{ 50 Host: "fuchsia.googlesource.com", 51 Project: "repo", 52 Id: "deadbeef", 53 }, 54 }, 55 }, 56 { 57 name: "should rebase on a parent change if the gerrit change is for the same project", 58 specRepoURL: url.URL{ 59 Scheme: "https", 60 Host: "fuchsia.googlesource.com", 61 Path: "/repo", 62 }, 63 input: &buildbucketpb.Build_Input{ 64 GitilesCommit: &buildbucketpb.GitilesCommit{ 65 Host: "fuchsia.googlesource.com", 66 Project: "repo", 67 Id: "deadbeef", 68 }, 69 GerritChanges: []*buildbucketpb.GerritChange{{ 70 Host: "fuchsia-review.googlesource.com", 71 Project: "repo", 72 }}, 73 }, 74 expectedStrategy: &checkoutChange{ 75 parent: &buildbucketpb.GitilesCommit{ 76 Host: "fuchsia.googlesource.com", 77 Project: "repo", 78 Id: "deadbeef", 79 }, 80 change: &buildbucketpb.GerritChange{ 81 Host: "fuchsia-review.googlesource.com", 82 Project: "repo", 83 }, 84 }, 85 }, 86 { 87 name: "should checkout gerrit change if no gitiles commit", 88 specRepoURL: url.URL{ 89 Scheme: "https", 90 Host: "fuchsia.googlesource.com", 91 Path: "/repo", 92 }, 93 input: &buildbucketpb.Build_Input{ 94 GerritChanges: []*buildbucketpb.GerritChange{{ 95 Host: "fuchsia-review.googlesource.com", 96 Project: "repo", 97 }}, 98 }, 99 expectedStrategy: &checkoutChange{ 100 change: &buildbucketpb.GerritChange{ 101 Host: "fuchsia-review.googlesource.com", 102 Project: "repo", 103 }, 104 }, 105 }, 106 { 107 name: "should checkout from specRef if the gitiles commit is for a different project", 108 specRepoURL: url.URL{ 109 Scheme: "https", 110 Host: "fuchsia.googlesource.com", 111 Path: "/repo", 112 }, 113 input: &buildbucketpb.Build_Input{ 114 GitilesCommit: &buildbucketpb.GitilesCommit{ 115 Host: "fuchsia.googlesource.com", 116 Project: "different", 117 }, 118 }, 119 specRef: "deadbeef", 120 expectedStrategy: &checkoutCommit{ 121 commit: &buildbucketpb.GitilesCommit{ 122 Host: "fuchsia.googlesource.com", 123 Project: "repo", 124 Id: "deadbeef", 125 }, 126 }, 127 }, 128 { 129 name: "should checkout from the gitiles commit if it is for the same project", 130 specRepoURL: url.URL{ 131 Scheme: "https", 132 Host: "fuchsia.googlesource.com", 133 Path: "/repo", 134 }, 135 input: &buildbucketpb.Build_Input{ 136 GitilesCommit: &buildbucketpb.GitilesCommit{ 137 Host: "fuchsia.googlesource.com", 138 Project: "repo", 139 Id: "deadbeef", 140 }, 141 }, 142 expectedStrategy: &checkoutCommit{ 143 commit: &buildbucketpb.GitilesCommit{ 144 Host: "fuchsia.googlesource.com", 145 Project: "repo", 146 Id: "deadbeef", 147 }, 148 }, 149 }, 150 151 // Cases where errors are expected. 152 153 { 154 name: "should fail if the build input has no gitiles commit or gerrit change", 155 specRepoURL: url.URL{ 156 Scheme: "https", 157 Host: "fuchsia.googlesource.com", 158 Path: "/repo", 159 }, 160 input: &buildbucketpb.Build_Input{}, 161 expectErr: true, 162 }, 163 { 164 name: "should fail if the repo URL has no scheme", 165 specRepoURL: url.URL{ 166 Host: "fuchsia.googlesource.com", 167 Path: "/repo", 168 }, 169 input: &buildbucketpb.Build_Input{}, 170 expectErr: true, 171 }, 172 } 173 174 for _, tt := range tests { 175 t.Run(tt.name, func(t *testing.T) { 176 if tt.specRef == "" { 177 tt.specRef = "HEAD" 178 } 179 strategy, err := newStrategy(tt.input, tt.specRepoURL, tt.specRef) 180 switch { 181 case err != nil && !tt.expectErr: 182 t.Errorf("unexpected error: %s", err) 183 return 184 case err == nil && tt.expectErr: 185 t.Errorf("wanted an error but got nil") 186 return 187 case err != nil && tt.expectErr: 188 return 189 } 190 191 expected := tt.expectedStrategy 192 actual := strategy 193 if !expected.Equal(actual) { 194 t.Errorf("wanted\n%+v\ngot\n%+v\n", expected, actual) 195 } 196 }) 197 } 198 } 199 200 func TestGitilesChangeRef(t *testing.T) { 201 test := []struct { 202 name string 203 change *buildbucketpb.GerritChange 204 expected string 205 }{ 206 { 207 name: "change no. < 10", 208 change: &buildbucketpb.GerritChange{ 209 Change: 9, 210 Patchset: 6, 211 }, 212 expected: "refs/changes/09/9/6", 213 }, 214 { 215 name: "change no. >= 10", 216 change: &buildbucketpb.GerritChange{ 217 Change: 56, 218 Patchset: 6, 219 }, 220 expected: "refs/changes/56/56/6", 221 }, 222 { 223 name: "change no. >= 100", 224 change: &buildbucketpb.GerritChange{ 225 Change: 754, 226 Patchset: 6, 227 }, 228 expected: "refs/changes/54/754/6", 229 }, 230 { 231 name: "change no. >= 1000", 232 change: &buildbucketpb.GerritChange{ 233 Change: 97643, 234 Patchset: 6, 235 }, 236 expected: "refs/changes/43/97643/6", 237 }, 238 } 239 240 for _, tt := range test { 241 t.Run(tt.name, func(t *testing.T) { 242 actual := GitilesChangeRef(tt.change) 243 if tt.expected != actual { 244 t.Errorf("wanted: %q, got: %q", tt.expected, actual) 245 } 246 }) 247 } 248 }