go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/compilefailureanalysis/cancelanalysis/cancel_analysis_test.go (about) 1 // Copyright 2022 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 cancelanalysis handles cancelation of existing analyses. 16 package cancelanalysis 17 18 import ( 19 "context" 20 "testing" 21 22 "github.com/golang/mock/gomock" 23 . "github.com/smartystreets/goconvey/convey" 24 25 bbpb "go.chromium.org/luci/buildbucket/proto" 26 27 "go.chromium.org/luci/bisection/internal/buildbucket" 28 "go.chromium.org/luci/bisection/model" 29 pb "go.chromium.org/luci/bisection/proto/v1" 30 "go.chromium.org/luci/bisection/util/testutil" 31 32 "go.chromium.org/luci/common/clock" 33 "go.chromium.org/luci/common/clock/testclock" 34 "go.chromium.org/luci/gae/impl/memory" 35 "go.chromium.org/luci/gae/service/datastore" 36 ) 37 38 func TestCancelAnalysis(t *testing.T) { 39 t.Parallel() 40 c := memory.Use(context.Background()) 41 testutil.UpdateIndices(c) 42 cl := testclock.New(testclock.TestTimeUTC) 43 c = clock.Set(c, cl) 44 45 ctl := gomock.NewController(t) 46 defer ctl.Finish() 47 mc := buildbucket.NewMockedClient(c, ctl) 48 c = mc.Ctx 49 50 Convey("Cancel Analysis", t, func() { 51 cfa := &model.CompileFailureAnalysis{ 52 Id: 123, 53 Status: pb.AnalysisStatus_RUNNING, 54 RunStatus: pb.AnalysisRunStatus_STARTED, 55 } 56 So(datastore.Put(c, cfa), ShouldBeNil) 57 datastore.GetTestable(c).CatchupIndexes() 58 59 ha := &model.CompileHeuristicAnalysis{ 60 ParentAnalysis: datastore.KeyForObj(c, cfa), 61 } 62 So(datastore.Put(c, ha), ShouldBeNil) 63 datastore.GetTestable(c).CatchupIndexes() 64 65 suspect := &model.Suspect{ 66 ParentAnalysis: datastore.KeyForObj(c, ha), 67 } 68 So(datastore.Put(c, suspect), ShouldBeNil) 69 datastore.GetTestable(c).CatchupIndexes() 70 71 nsa := &model.CompileNthSectionAnalysis{ 72 Status: pb.AnalysisStatus_RUNNING, 73 RunStatus: pb.AnalysisRunStatus_STARTED, 74 ParentAnalysis: datastore.KeyForObj(c, cfa), 75 } 76 So(datastore.Put(c, nsa), ShouldBeNil) 77 78 rb1 := &model.CompileRerunBuild{ 79 Id: 997, 80 } 81 rb2 := &model.CompileRerunBuild{ 82 Id: 998, 83 } 84 rb3 := &model.CompileRerunBuild{ 85 Id: 999, 86 } 87 So(datastore.Put(c, rb1), ShouldBeNil) 88 So(datastore.Put(c, rb2), ShouldBeNil) 89 So(datastore.Put(c, rb3), ShouldBeNil) 90 datastore.GetTestable(c).CatchupIndexes() 91 92 rr1 := &model.SingleRerun{ 93 RerunBuild: datastore.KeyForObj(c, rb1), 94 Analysis: datastore.KeyForObj(c, cfa), 95 Suspect: datastore.KeyForObj(c, suspect), 96 Status: pb.RerunStatus_RERUN_STATUS_IN_PROGRESS, 97 } 98 rr2 := &model.SingleRerun{ 99 RerunBuild: datastore.KeyForObj(c, rb2), 100 Analysis: datastore.KeyForObj(c, cfa), 101 Status: pb.RerunStatus_RERUN_STATUS_FAILED, 102 } 103 rr3 := &model.SingleRerun{ 104 RerunBuild: datastore.KeyForObj(c, rb3), 105 Analysis: datastore.KeyForObj(c, cfa), 106 Status: pb.RerunStatus_RERUN_STATUS_IN_PROGRESS, 107 } 108 So(datastore.Put(c, rr1), ShouldBeNil) 109 So(datastore.Put(c, rr2), ShouldBeNil) 110 So(datastore.Put(c, rr3), ShouldBeNil) 111 datastore.GetTestable(c).CatchupIndexes() 112 113 mc.Client.EXPECT().CancelBuild(gomock.Any(), gomock.Any(), gomock.Any()).Return(&bbpb.Build{}, nil).Times(2) 114 e := CancelAnalysis(c, 123) 115 So(e, ShouldBeNil) 116 117 datastore.GetTestable(c).CatchupIndexes() 118 So(datastore.Get(c, cfa), ShouldBeNil) 119 So(datastore.Get(c, nsa), ShouldBeNil) 120 So(datastore.Get(c, rr1), ShouldBeNil) 121 So(datastore.Get(c, rr3), ShouldBeNil) 122 So(datastore.Get(c, rb1), ShouldBeNil) 123 So(datastore.Get(c, rb3), ShouldBeNil) 124 So(datastore.Get(c, suspect), ShouldBeNil) 125 126 So(cfa.Status, ShouldEqual, pb.AnalysisStatus_NOTFOUND) 127 So(cfa.RunStatus, ShouldEqual, pb.AnalysisRunStatus_CANCELED) 128 So(nsa.Status, ShouldEqual, pb.AnalysisStatus_NOTFOUND) 129 So(nsa.RunStatus, ShouldEqual, pb.AnalysisRunStatus_CANCELED) 130 So(rr1.Status, ShouldEqual, pb.RerunStatus_RERUN_STATUS_CANCELED) 131 So(rr3.Status, ShouldEqual, pb.RerunStatus_RERUN_STATUS_CANCELED) 132 So(rb1.Status, ShouldEqual, bbpb.Status_CANCELED) 133 So(rb3.Status, ShouldEqual, bbpb.Status_CANCELED) 134 So(suspect.VerificationStatus, ShouldEqual, model.SuspectVerificationStatus_Canceled) 135 }) 136 }