go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/internal/buildcron/sync_backend_tasks_test.go (about) 1 // Copyright 2023 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 buildcron 16 17 import ( 18 "sort" 19 "testing" 20 21 "go.chromium.org/luci/gae/service/datastore" 22 23 "go.chromium.org/luci/buildbucket/appengine/model" 24 taskdefs "go.chromium.org/luci/buildbucket/appengine/tasks/defs" 25 pb "go.chromium.org/luci/buildbucket/proto" 26 27 . "github.com/smartystreets/goconvey/convey" 28 ) 29 30 func TestTriggerSyncBackendTasks(t *testing.T) { 31 t.Parallel() 32 Convey("TriggerSyncBackendTasks", t, func() { 33 ctx, _, sch := setUp() 34 35 b1 := &model.Build{ 36 ID: 1, 37 Proto: &pb.Build{ 38 Builder: &pb.BuilderID{ 39 Project: "p1", 40 }, 41 }, 42 } 43 b2 := &model.Build{ 44 ID: 1, 45 BackendTarget: "b1", 46 Proto: &pb.Build{ 47 Builder: &pb.BuilderID{ 48 Project: "p1", 49 }, 50 }, 51 } 52 b3 := &model.Build{ 53 ID: 3, 54 BackendTarget: "b1", 55 Proto: &pb.Build{ 56 Builder: &pb.BuilderID{ 57 Project: "p1", 58 }, 59 }, 60 } 61 b4 := &model.Build{ 62 ID: 4, 63 BackendTarget: "b2", 64 Proto: &pb.Build{ 65 Builder: &pb.BuilderID{ 66 Project: "p1", 67 }, 68 }, 69 } 70 b5 := &model.Build{ 71 ID: 5, 72 BackendTarget: "b1", 73 Proto: &pb.Build{ 74 Builder: &pb.BuilderID{ 75 Project: "p2", 76 }, 77 }, 78 } 79 b6 := &model.Build{ 80 ID: 6, 81 BackendTarget: "b3", 82 Proto: &pb.Build{ 83 Builder: &pb.BuilderID{ 84 Project: "p3", 85 }, 86 }, 87 } 88 89 So(datastore.Put(ctx, b1, b2, b3, b4, b5, b6), ShouldBeNil) 90 So(TriggerSyncBackendTasks(ctx), ShouldBeNil) 91 So(sch.Tasks(), ShouldHaveLength, 4) 92 pairs := make([]*projectBackendPair, 4) 93 for i, t := range sch.Tasks() { 94 switch v := t.Payload.(type) { 95 case *taskdefs.SyncBuildsWithBackendTasks: 96 pairs[i] = &projectBackendPair{ 97 project: v.Project, 98 backend: v.Backend, 99 } 100 default: 101 panic("invalid task payload") 102 } 103 } 104 sort.Slice(pairs, func(i, j int) bool { 105 switch { 106 case pairs[i].project < pairs[j].project: 107 return true 108 case pairs[i].project > pairs[j].project: 109 return false 110 default: 111 return pairs[i].backend < pairs[j].backend 112 } 113 }) 114 115 expected := []*projectBackendPair{ 116 { 117 project: "p1", 118 backend: "b1", 119 }, 120 { 121 project: "p1", 122 backend: "b2", 123 }, 124 { 125 project: "p2", 126 backend: "b1", 127 }, 128 { 129 project: "p3", 130 backend: "b3", 131 }, 132 } 133 So(pairs, ShouldResemble, expected) 134 }) 135 }