blob: e953e8fb8bba655b5e3eae01eb31ce8be7d2b53f [file] [log] [blame]
// 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 TestNewCheckoutStrategy(t *testing.T) {
tests := []struct {
// A name for this test case.
name string
// The integratoin repo URL.
specRepoURL url.URL
// The Buildbucket build input.
input buildbucketpb.Build_Input
// The spec ref.
specRef string
// The name of the expected strategy.
expectedStrategy strategy
// Whether to expect an error
expectErr bool
}{
{
name: "should checkout from specRef if the gerrit change is for a different project",
specRepoURL: url.URL{
Scheme: "https",
Host: "fuchsia.googlesource.com",
Path: "/repo",
},
input: buildbucketpb.Build_Input{
GerritChanges: []*buildbucketpb.GerritChange{{
Host: "fuchsia-review.googlesource.com",
Project: "different",
}},
},
specRef: "deadbeef",
expectedStrategy: &checkoutCommit{
commit: &buildbucketpb.GitilesCommit{
Host: "fuchsia.googlesource.com",
Project: "repo",
Id: "deadbeef",
},
},
},
{
name: "should rebase on a parent change if the gerrit change is for the same project",
specRepoURL: url.URL{
Scheme: "https",
Host: "fuchsia.googlesource.com",
Path: "/repo",
},
input: buildbucketpb.Build_Input{
GitilesCommit: &buildbucketpb.GitilesCommit{
Host: "fuchsia.googlesource.com",
Project: "repo",
Id: "deadbeef",
},
GerritChanges: []*buildbucketpb.GerritChange{{
Host: "fuchsia-review.googlesource.com",
Project: "repo",
}},
},
expectedStrategy: &checkoutChange{
parent: &buildbucketpb.GitilesCommit{
Host: "fuchsia.googlesource.com",
Project: "repo",
Id: "deadbeef",
},
change: &buildbucketpb.GerritChange{
Host: "fuchsia-review.googlesource.com",
Project: "repo",
},
},
},
{
name: "should checkout from specRef if the gitiles commit is for a different project",
specRepoURL: url.URL{
Scheme: "https",
Host: "fuchsia.googlesource.com",
Path: "/repo",
},
input: buildbucketpb.Build_Input{
GitilesCommit: &buildbucketpb.GitilesCommit{
Host: "fuchsia.googlesource.com",
Project: "different",
},
},
specRef: "deadbeef",
expectedStrategy: &checkoutCommit{
commit: &buildbucketpb.GitilesCommit{
Host: "fuchsia.googlesource.com",
Project: "repo",
Id: "deadbeef",
},
},
},
{
name: "should checkout from the gitiles commit if it is for the same project",
specRepoURL: url.URL{
Scheme: "https",
Host: "fuchsia.googlesource.com",
Path: "/repo",
},
input: buildbucketpb.Build_Input{
GitilesCommit: &buildbucketpb.GitilesCommit{
Host: "fuchsia.googlesource.com",
Project: "repo",
Id: "deadbeef",
},
},
expectedStrategy: &checkoutCommit{
commit: &buildbucketpb.GitilesCommit{
Host: "fuchsia.googlesource.com",
Project: "repo",
Id: "deadbeef",
},
},
},
// Cases where errors are expected.
{
name: "should fail if the build input has no gitiles commit or gerrit change",
specRepoURL: url.URL{
Scheme: "https",
Host: "fuchsia.googlesource.com",
Path: "/repo",
},
input: buildbucketpb.Build_Input{},
expectErr: true,
},
{
name: "should fail if the repo URL has no scheme",
specRepoURL: url.URL{
Host: "fuchsia.googlesource.com",
Path: "/repo",
},
input: buildbucketpb.Build_Input{},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.specRef == "" {
tt.specRef = "HEAD"
}
strategy, err := newStrategy(tt.input, tt.specRepoURL, tt.specRef)
switch {
case err != nil && !tt.expectErr:
t.Errorf("unexpected error: %v", err)
return
case err == nil && tt.expectErr:
t.Errorf("wanted an error but got %v", strategy)
return
case err != nil && tt.expectErr:
return
}
expected := tt.expectedStrategy
actual := strategy
if !expected.Equal(actual) {
t.Errorf("wanted\n%+v\ngot\n%+v\n", expected, actual)
}
})
}
}
func TestGitilesChangeRef(t *testing.T) {
test := []struct {
name string
change buildbucketpb.GerritChange
expected string
}{
{
name: "change no. < 10",
change: buildbucketpb.GerritChange{
Change: 9,
Patchset: 6,
},
expected: "refs/changes/09/9/6",
},
{
name: "change no. >= 10",
change: buildbucketpb.GerritChange{
Change: 56,
Patchset: 6,
},
expected: "refs/changes/56/56/6",
},
{
name: "change no. >= 100",
change: buildbucketpb.GerritChange{
Change: 754,
Patchset: 6,
},
expected: "refs/changes/54/754/6",
},
{
name: "change no. >= 1000",
change: buildbucketpb.GerritChange{
Change: 97643,
Patchset: 6,
},
expected: "refs/changes/43/97643/6",
},
}
for _, tt := range test {
t.Run(tt.name, func(t *testing.T) {
actual := gitilesChangeRef(tt.change)
if tt.expected != actual {
t.Errorf("wanted:%v got %v", tt.expected, actual)
}
})
}
}