go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/services/bugupdater/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 bugupdater 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "google.golang.org/protobuf/proto" 23 "google.golang.org/protobuf/types/known/timestamppb" 24 25 "go.chromium.org/luci/server/tq" 26 27 "go.chromium.org/luci/analysis/internal/bugs/buganizer" 28 "go.chromium.org/luci/analysis/internal/tasks/taskspb" 29 "go.chromium.org/luci/analysis/internal/testutil" 30 31 . "github.com/smartystreets/goconvey/convey" 32 . "go.chromium.org/luci/common/testing/assertions" 33 ) 34 35 func TestSchedule(t *testing.T) { 36 Convey(`TestSchedule`, t, func() { 37 ctx, skdr := tq.TestingContext(testutil.TestingContext(), nil) 38 39 task := &taskspb.UpdateBugs{ 40 Project: "chromium", 41 ReclusteringAttemptMinute: timestamppb.New(time.Date(2025, time.January, 1, 12, 13, 0, 0, time.UTC)), 42 Deadline: timestamppb.New(time.Date(2025, time.January, 1, 12, 28, 0, 0, time.UTC)), 43 } 44 expected := proto.Clone(task).(*taskspb.UpdateBugs) 45 So(Schedule(ctx, task), ShouldBeNil) 46 So(skdr.Tasks().Payloads()[0], ShouldResembleProto, expected) 47 }) 48 } 49 50 func TestValidate(t *testing.T) { 51 Convey(`Validate`, t, func() { 52 task := &taskspb.UpdateBugs{ 53 Project: "chromium", 54 ReclusteringAttemptMinute: timestamppb.New(time.Date(2025, time.January, 1, 12, 13, 0, 0, time.UTC)), 55 Deadline: timestamppb.New(time.Date(2025, time.January, 1, 12, 28, 0, 0, time.UTC)), 56 } 57 Convey(`baseline`, func() { 58 err := validateTask(task) 59 So(err, ShouldBeNil) 60 }) 61 Convey(`invalid project`, func() { 62 task.Project = "" 63 err := validateTask(task) 64 So(err, ShouldErrLike, "project: unspecified") 65 }) 66 Convey(`invalid reclustering attempt minute`, func() { 67 task.ReclusteringAttemptMinute = nil 68 err := validateTask(task) 69 So(err, ShouldErrLike, "reclustering_attempt_minute: missing or invalid timestamp") 70 }) 71 Convey(`unaligned reclustering attempt minute`, func() { 72 task.ReclusteringAttemptMinute = timestamppb.New(time.Date(2025, time.January, 1, 12, 13, 59, 0, time.UTC)) 73 err := validateTask(task) 74 So(err, ShouldErrLike, "reclustering_attempt_minute: must be aligned to the start of a minute") 75 }) 76 Convey(`invalid deadline`, func() { 77 task.Deadline = nil 78 err := validateTask(task) 79 So(err, ShouldErrLike, "deadline: missing or invalid timestamp") 80 }) 81 }) 82 } 83 84 func TestBuganizerClient(t *testing.T) { 85 ctx := context.Background() 86 Convey("Buganizer client", t, func() { 87 Convey("no value in context means no buganizer client", func() { 88 client, err := createBuganizerClient(ctx) 89 So(err, ShouldBeNil) 90 So(client, ShouldBeNil) 91 }) 92 93 Convey("`disable` mode doesn't create any client", func() { 94 ctx = context.WithValue(ctx, &buganizer.BuganizerClientModeKey, buganizer.ModeDisable) 95 client, err := createBuganizerClient(ctx) 96 So(err, ShouldBeNil) 97 So(client, ShouldBeNil) 98 }) 99 }) 100 }