blob: 5b237b267b19a1364b5c3dee4ddb61b0a9147ab1 [file] [log] [blame]
// Copyright 2022 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 main
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
buildbucketpb "go.chromium.org/luci/buildbucket/proto"
cvpb "go.chromium.org/luci/cv/api/config/v2"
)
func TestGetAvailablePresubmitBuilders(t *testing.T) {
testCases := []struct {
name string
gerritProject string
gerritHost string
lucicfg LUCIConfigurationFiles
cmd *rptCmd
expected []*buildbucketpb.BuilderID
}{
{
name: "Can add presubmit builders that match the Change.Project and Change.Host",
gerritProject: "project",
gerritHost: "host.com",
lucicfg: LUCIConfigurationFiles{
crBuildBucketConfigs: map[string]*buildbucketpb.BuildbucketCfg{
"project": {
Buckets: []*buildbucketpb.Bucket{
{
Name: "bucket",
Swarming: &buildbucketpb.Swarming{
Builders: []*buildbucketpb.BuilderConfig{
{
Name: "builder-a",
Properties: "{ \"$fuchsia/builder_tags\": { \"run_postsubmit_tryjobs_include\": true }}",
},
},
},
},
},
},
},
commitQueueConfig: &cvpb.Config{
ConfigGroups: []*cvpb.ConfigGroup{
{
Gerrit: []*cvpb.ConfigGroup_Gerrit{
{
Url: "https://host.com",
Projects: []*cvpb.ConfigGroup_Gerrit_Project{
{
Name: "project",
},
},
},
},
Verifiers: &cvpb.Verifiers{
Tryjob: &cvpb.Verifiers_Tryjob{
Builders: []*cvpb.Verifiers_Tryjob_Builder{
{
Name: "project/bucket/builder-a",
},
{
Name: "project/bucket/builder-c",
},
},
},
},
},
},
},
},
cmd: &rptCmd{},
expected: []*buildbucketpb.BuilderID{
{
Bucket: "bucket",
Builder: "builder-a",
Project: "project",
},
},
},
{
name: "Does not pick up builders from incompatible projects",
gerritProject: "project",
gerritHost: "host.com",
lucicfg: LUCIConfigurationFiles{
crBuildBucketConfigs: map[string]*buildbucketpb.BuildbucketCfg{
"project": {
Buckets: []*buildbucketpb.Bucket{
{
Name: "bucket",
Swarming: &buildbucketpb.Swarming{
Builders: []*buildbucketpb.BuilderConfig{
{
Name: "builder-a",
Properties: "{ \"$fuchsia/builder_tags\": { \"run_postsubmit_tryjobs_include\": true }}",
},
{
Name: "builder-b",
Properties: "{ \"$fuchsia/builder_tags\": { \"run_postsubmit_tryjobs_include\": true }}",
},
},
},
},
},
},
},
commitQueueConfig: &cvpb.Config{
ConfigGroups: []*cvpb.ConfigGroup{
{
Gerrit: []*cvpb.ConfigGroup_Gerrit{
{
Url: "https://host.com",
Projects: []*cvpb.ConfigGroup_Gerrit_Project{
{
Name: "project",
},
},
},
},
Verifiers: &cvpb.Verifiers{
Tryjob: &cvpb.Verifiers_Tryjob{
Builders: []*cvpb.Verifiers_Tryjob_Builder{
{
Name: "project/bucket/builder-a",
},
},
},
},
},
{
Gerrit: []*cvpb.ConfigGroup_Gerrit{
{
Url: "https://host.com",
Projects: []*cvpb.ConfigGroup_Gerrit_Project{
{
Name: "not-project",
},
},
},
},
Verifiers: &cvpb.Verifiers{
Tryjob: &cvpb.Verifiers_Tryjob{
Builders: []*cvpb.Verifiers_Tryjob_Builder{
{
Name: "not-project/bucket/builder-b",
},
},
},
},
},
},
},
},
cmd: &rptCmd{},
expected: []*buildbucketpb.BuilderID{
{
Bucket: "bucket",
Builder: "builder-a",
Project: "project",
},
},
},
{
name: "Does not pick up builders from incompatible hosts",
gerritProject: "project",
gerritHost: "host.com",
lucicfg: LUCIConfigurationFiles{
crBuildBucketConfigs: map[string]*buildbucketpb.BuildbucketCfg{
"project": {
Buckets: []*buildbucketpb.Bucket{
{
Name: "bucket",
Swarming: &buildbucketpb.Swarming{
Builders: []*buildbucketpb.BuilderConfig{
{
Name: "builder-a",
Properties: "{ \"$fuchsia/builder_tags\": { \"run_postsubmit_tryjobs_include\": true }}",
},
{
Name: "builder-b",
Properties: "{ \"$fuchsia/builder_tags\": { \"run_postsubmit_tryjobs_include\": true }}",
},
},
},
},
},
},
},
commitQueueConfig: &cvpb.Config{
ConfigGroups: []*cvpb.ConfigGroup{
{
Gerrit: []*cvpb.ConfigGroup_Gerrit{
{
Url: "https://host.com",
Projects: []*cvpb.ConfigGroup_Gerrit_Project{
{
Name: "project",
},
},
},
},
Verifiers: &cvpb.Verifiers{
Tryjob: &cvpb.Verifiers_Tryjob{
Builders: []*cvpb.Verifiers_Tryjob_Builder{
{
Name: "project/bucket/builder-a",
},
},
},
},
},
{
Gerrit: []*cvpb.ConfigGroup_Gerrit{
{
Url: "https://not-host.com",
Projects: []*cvpb.ConfigGroup_Gerrit_Project{
{
Name: "project",
},
},
},
},
Verifiers: &cvpb.Verifiers{
Tryjob: &cvpb.Verifiers_Tryjob{
Builders: []*cvpb.Verifiers_Tryjob_Builder{
{
Name: "project/bucket/builder-b",
},
},
},
},
},
},
},
},
cmd: &rptCmd{},
expected: []*buildbucketpb.BuilderID{
{
Bucket: "bucket",
Builder: "builder-a",
Project: "project",
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
availablePresubmitBuilders := tc.cmd.getAvailablePresubmitBuilders(tc.lucicfg, tc.gerritHost, tc.gerritProject)
if diff := cmp.Diff(availablePresubmitBuilders, tc.expected, cmpopts.IgnoreUnexported(buildbucketpb.BuilderID{})); diff != "" {
t.Fatalf("unexpected output: (-got +want):\n%s", diff)
}
})
}
}
func TestGetMissingBuilders(t *testing.T) {
t.Run("Returns the diff between TriggeredPresubmitBuilders and AvailablePresubmitBuilders", func(t *testing.T) {
triggeredPresubmitBuilds := []*buildbucketpb.Build{
{
Builder: &buildbucketpb.BuilderID{
Bucket: "bucket",
Builder: "builder-a",
Project: "project",
},
},
}
availablePresubmitBuilders := []*buildbucketpb.BuilderID{
{
Bucket: "bucket",
Builder: "builder-a",
Project: "project",
},
{
Bucket: "bucket",
Builder: "builder-b",
Project: "project",
},
}
cmd := &rptCmd{}
expected := []*buildbucketpb.BuilderID{
{
Bucket: "bucket",
Builder: "builder-b",
Project: "project",
},
}
buildersToTrigger := cmd.getMissingBuilders(availablePresubmitBuilders, triggeredPresubmitBuilds)
if diff := cmp.Diff(buildersToTrigger, expected, cmpopts.IgnoreUnexported(buildbucketpb.BuilderID{})); diff != "" {
t.Fatalf("unexpected output: (-got +want):\n%s", diff)
}
})
}
func TestGetBuildersFlaggedForRpt(t *testing.T) {
t.Run("Marks builders flagged for run_postsubmit_tryjobs_include", func(t *testing.T) {
bucketWithFlag := &buildbucketpb.Bucket{
Swarming: &buildbucketpb.Swarming{
Builders: []*buildbucketpb.BuilderConfig{
{
Name: "builder-a",
Properties: "{ \"$fuchsia/builder_tags\": { \"run_postsubmit_tryjobs_include\": true }}",
},
{
Name: "builder-b",
Properties: "{ \"$fuchsia/builder_tags\": { \"run_postsubmit_tryjobs_include\": false }}",
},
},
},
}
res := getBuildersFlaggedForRpt(bucketWithFlag)
expected := map[string]bool{
"builder-a": true,
"builder-b": false,
}
if diff := cmp.Diff(res, expected); diff != "" {
t.Fatalf("unexpected output: (-got +want):\n%s", diff)
}
})
t.Run("Marks builders that do not have run_postsubmit_tryjobs set as false", func(t *testing.T) {
bucketWithoutFlag := &buildbucketpb.Bucket{
Swarming: &buildbucketpb.Swarming{
Builders: []*buildbucketpb.BuilderConfig{
{
Name: "builder-a",
Properties: "",
},
},
},
}
res := getBuildersFlaggedForRpt(bucketWithoutFlag)
expected := map[string]bool{"builder-a": false}
if diff := cmp.Diff(res, expected); diff != "" {
t.Fatalf("unexpected output: (-got +want):\n%s", diff)
}
})
}