go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/gerrit/poller/partition_test.go (about)

     1  // Copyright 2020 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package poller
    16  
    17  import (
    18  	"sort"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/common/data/stringset"
    22  
    23  	cfgpb "go.chromium.org/luci/cv/api/config/v2"
    24  	"go.chromium.org/luci/cv/internal/configs/prjcfg"
    25  
    26  	. "github.com/smartystreets/goconvey/convey"
    27  
    28  	. "go.chromium.org/luci/common/testing/assertions"
    29  )
    30  
    31  func TestPartitionConfig(t *testing.T) {
    32  	t.Parallel()
    33  
    34  	Convey("partitionConfig works", t, func() {
    35  
    36  		Convey("groups by prefix if possible", func() {
    37  			// makeCfgs merges several projects configs into one just to re-use
    38  			// singleRepoConfig.
    39  			makeCfgs := func(cfgs ...*cfgpb.Config) (ret []*prjcfg.ConfigGroup) {
    40  				for _, cfg := range cfgs {
    41  					for _, cg := range cfg.GetConfigGroups() {
    42  						ret = append(ret, &prjcfg.ConfigGroup{Content: cg})
    43  					}
    44  				}
    45  				return
    46  			}
    47  			cgs := makeCfgs(singleRepoConfig("h1", "infra/222", "infra/111"))
    48  			So(partitionConfig(cgs), ShouldResembleProto, []*QueryState{
    49  				{Host: "h1", OrProjects: []string{"infra/111", "infra/222"}},
    50  			})
    51  
    52  			cgs = append(cgs, makeCfgs(singleRepoConfig("h1", sharedPrefixRepos("infra", 30)...))...)
    53  			So(partitionConfig(cgs), ShouldResembleProto, []*QueryState{
    54  				{Host: "h1", CommonProjectPrefix: "infra"},
    55  			})
    56  			cgs = append(cgs, makeCfgs(singleRepoConfig("h2", "infra/499", "infra/132"))...)
    57  			So(partitionConfig(cgs), ShouldResembleProto, []*QueryState{
    58  				{Host: "h1", CommonProjectPrefix: "infra"},
    59  				{Host: "h2", OrProjects: []string{"infra/132", "infra/499"}},
    60  			})
    61  		})
    62  
    63  		Convey("evenly distributes repos among queries", func() {
    64  			So(minReposPerPrefixQuery, ShouldBeGreaterThan, 5)
    65  			repos := stringset.New(23)
    66  			repos.AddAll(sharedPrefixRepos("a", 5))
    67  			repos.AddAll(sharedPrefixRepos("b", 5))
    68  			repos.AddAll(sharedPrefixRepos("c", 3))
    69  			repos.AddAll(sharedPrefixRepos("d", 5))
    70  			repos.AddAll(sharedPrefixRepos("e", 5))
    71  			queries := partitionHostRepos(
    72  				"host",
    73  				repos.ToSlice(), // effectively shuffles repos
    74  				7,               // at most 7 per query.
    75  			)
    76  			So(queries, ShouldHaveLength, 4) // 7*3 < 23 < 7*4
    77  
    78  			for _, qs := range queries {
    79  				// Ensure each has 5..6 repos instead max of 7.
    80  				So(len(qs.GetOrProjects()), ShouldBeBetweenOrEqual, 5, 6)
    81  				So(sort.StringsAreSorted(qs.GetOrProjects()), ShouldBeTrue)
    82  				repos.DelAll(qs.GetOrProjects())
    83  			}
    84  
    85  			// Ensure no overlaps or missed repos.
    86  			So(repos.ToSortedSlice(), ShouldResemble, []string{})
    87  		})
    88  	})
    89  }