go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/tasks/cancel_backend_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 tasks 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "github.com/golang/mock/gomock" 23 "google.golang.org/api/googleapi" 24 "google.golang.org/protobuf/types/known/durationpb" 25 26 "go.chromium.org/luci/common/clock/testclock" 27 "go.chromium.org/luci/gae/impl/memory" 28 "go.chromium.org/luci/server/caching" 29 30 "go.chromium.org/luci/buildbucket/appengine/internal/clients" 31 "go.chromium.org/luci/buildbucket/appengine/internal/config" 32 pb "go.chromium.org/luci/buildbucket/proto" 33 34 . "github.com/smartystreets/goconvey/convey" 35 . "go.chromium.org/luci/common/testing/assertions" 36 ) 37 38 func TestHandleCancelBackendTask(t *testing.T) { 39 t.Parallel() 40 Convey("TestHandleCancelBackendTask", t, func() { 41 ctl := gomock.NewController(t) 42 defer ctl.Finish() 43 mockBackend := clients.NewMockTaskBackendClient(ctl) 44 now := testclock.TestRecentTimeUTC 45 ctx, _ := testclock.UseTime(context.Background(), now) 46 ctx = context.WithValue(ctx, clients.MockTaskBackendClientKey, mockBackend) 47 ctx = caching.WithEmptyProcessCache(ctx) 48 ctx = memory.UseWithAppID(ctx, "dev~app-id") 49 backendSetting := []*pb.BackendSetting{ 50 &pb.BackendSetting{ 51 Target: "swarming://chromium-swarm-dev", 52 Hostname: "hostname2", 53 Mode: &pb.BackendSetting_FullMode_{ 54 FullMode: &pb.BackendSetting_FullMode{ 55 BuildSyncSetting: &pb.BackendSetting_BuildSyncSetting{ 56 Shards: 5, 57 SyncIntervalSeconds: 300, 58 }, 59 }, 60 }, 61 TaskCreatingTimeout: durationpb.New(8 * time.Minute), 62 }, 63 } 64 settingsCfg := &pb.SettingsCfg{Backends: backendSetting} 65 err := config.SetTestSettingsCfg(ctx, settingsCfg) 66 So(err, ShouldBeNil) 67 68 Convey("ok", func() { 69 mockBackend.EXPECT().CancelTasks(gomock.Any(), gomock.Any()).Return(&pb.CancelTasksResponse{ 70 Tasks: []*pb.Task{ 71 &pb.Task{ 72 Id: &pb.TaskID{Id: "abc123", Target: "swarming://chromium-swarm"}, 73 Link: "this_is_a_url_link", 74 UpdateId: 1, 75 }, 76 }, 77 }, nil) 78 So(HandleCancelBackendTask(ctx, "project:bucket", "swarming://chromium-swarm-dev", "a83908f94as40"), ShouldBeNil) 79 }) 80 81 Convey("failed CancelTasks RPC call 400", func() { 82 mockBackend.EXPECT().CancelTasks(gomock.Any(), gomock.Any()).Return(nil, &googleapi.Error{Code: 400}) 83 err := HandleCancelBackendTask(ctx, "project:bucket", "swarming://chromium-swarm-dev", "a83908f94as40") 84 So(err, ShouldErrLike, "fatal error in cancelling task a83908f94as40 for target swarming://chromium-swarm-dev: googleapi: got HTTP response code 400") 85 }) 86 87 Convey("failed CancelTasks RPC call 500", func() { 88 mockBackend.EXPECT().CancelTasks(gomock.Any(), gomock.Any()).Return(nil, &googleapi.Error{Code: 500}) 89 err := HandleCancelBackendTask(ctx, "project:bucket", "swarming://chromium-swarm-dev", "a83908f94as40") 90 So(err, ShouldErrLike, "transient error in canceling task a83908f94as40 for target swarming://chromium-swarm-dev: googleapi: got HTTP response code 500") 91 }) 92 }) 93 }