go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/impl/handler/post_gerrit_msg_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 handler 16 17 import ( 18 "testing" 19 "time" 20 21 "google.golang.org/protobuf/types/known/timestamppb" 22 23 cfgpb "go.chromium.org/luci/cv/api/config/v2" 24 "go.chromium.org/luci/cv/internal/configs/prjcfg/prjcfgtest" 25 "go.chromium.org/luci/cv/internal/cvtesting" 26 "go.chromium.org/luci/cv/internal/run" 27 "go.chromium.org/luci/cv/internal/run/eventpb" 28 "go.chromium.org/luci/cv/internal/run/impl/state" 29 30 . "github.com/smartystreets/goconvey/convey" 31 ) 32 33 func TestOnCompletedPostGerritMessage(t *testing.T) { 34 t.Parallel() 35 36 Convey("onCompletedPostGerritMessage works", t, func() { 37 ct := cvtesting.Test{} 38 ctx, cancel := ct.SetUp(t) 39 defer cancel() 40 41 const ( 42 lProject = "chromium" 43 opID = "1-1" 44 ) 45 46 prjcfgtest.Create(ctx, lProject, &cfgpb.Config{ConfigGroups: []*cfgpb.ConfigGroup{{Name: "single"}}}) 47 48 rs := &state.RunState{ 49 Run: run.Run{ 50 ID: lProject + "/1111111111111-1-deadbeef", 51 Status: run.Status_PENDING, 52 Mode: run.DryRun, 53 ConfigGroupID: prjcfgtest.MustExist(ctx, lProject).ConfigGroupIDs[0], 54 OngoingLongOps: &run.OngoingLongOps{ 55 Ops: map[string]*run.OngoingLongOps_Op{ 56 opID: { 57 Work: &run.OngoingLongOps_Op_PostGerritMessage_{ 58 PostGerritMessage: &run.OngoingLongOps_Op_PostGerritMessage{ 59 Message: "foo", 60 }, 61 }, 62 }, 63 }, 64 }, 65 }, 66 } 67 result := &eventpb.LongOpCompleted{ 68 OperationId: opID, 69 } 70 h, _ := makeTestHandler(&ct) 71 72 Convey("on cancellation, cleans up Run's state", func() { 73 result.Status = eventpb.LongOpCompleted_CANCELLED 74 res, err := h.OnLongOpCompleted(ctx, rs, result) 75 So(err, ShouldBeNil) 76 So(res.State.Status, ShouldEqual, run.Status_PENDING) 77 So(res.State.OngoingLongOps, ShouldBeNil) 78 So(res.SideEffectFn, ShouldBeNil) 79 So(res.PreserveEvents, ShouldBeFalse) 80 }) 81 82 Convey("on success, cleans Run's state", func() { 83 result.Status = eventpb.LongOpCompleted_SUCCEEDED 84 postedAt := ct.Clock.Now().Add(-time.Second) 85 result.Result = &eventpb.LongOpCompleted_PostGerritMessage_{ 86 PostGerritMessage: &eventpb.LongOpCompleted_PostGerritMessage{ 87 Time: timestamppb.New(postedAt), 88 }, 89 } 90 res, err := h.OnLongOpCompleted(ctx, rs, result) 91 So(err, ShouldBeNil) 92 So(res.State.Status, ShouldEqual, run.Status_PENDING) 93 So(res.State.OngoingLongOps, ShouldBeNil) 94 So(res.SideEffectFn, ShouldBeNil) 95 So(res.PreserveEvents, ShouldBeFalse) 96 So(res.State.LogEntries[0].GetTime().AsTime(), ShouldResemble, postedAt.UTC()) 97 }) 98 99 Convey("on failure, cleans Run's state and record reasons", func() { 100 result.Status = eventpb.LongOpCompleted_FAILED 101 res, err := h.OnLongOpCompleted(ctx, rs, result) 102 So(err, ShouldBeNil) 103 So(res.State.Status, ShouldEqual, run.Status_PENDING) 104 So(res.State.OngoingLongOps, ShouldBeNil) 105 So(res.SideEffectFn, ShouldBeNil) 106 So(res.PreserveEvents, ShouldBeFalse) 107 So(res.State.LogEntries[0].GetInfo().GetMessage(), ShouldContainSubstring, "Failed to post gerrit message") 108 }) 109 110 Convey("on expiration,cleans Run's state and record reasons", func() { 111 result.Status = eventpb.LongOpCompleted_EXPIRED 112 res, err := h.OnLongOpCompleted(ctx, rs, result) 113 So(err, ShouldBeNil) 114 So(res.State.Status, ShouldEqual, run.Status_PENDING) 115 So(res.State.OngoingLongOps, ShouldBeNil) 116 So(res.SideEffectFn, ShouldBeNil) 117 So(res.PreserveEvents, ShouldBeFalse) 118 So(res.State.LogEntries[0].GetInfo().GetMessage(), ShouldContainSubstring, "Failed to post the message to gerrit") 119 }) 120 }) 121 }