go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/impl/handler/post_action_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  
    20  	cfgpb "go.chromium.org/luci/cv/api/config/v2"
    21  	"go.chromium.org/luci/cv/internal/configs/prjcfg/prjcfgtest"
    22  	"go.chromium.org/luci/cv/internal/cvtesting"
    23  	"go.chromium.org/luci/cv/internal/run"
    24  	"go.chromium.org/luci/cv/internal/run/eventpb"
    25  	"go.chromium.org/luci/cv/internal/run/impl/state"
    26  
    27  	. "github.com/smartystreets/goconvey/convey"
    28  	. "go.chromium.org/luci/common/testing/assertions"
    29  )
    30  
    31  func TestOnCompletedPostAction(t *testing.T) {
    32  	t.Parallel()
    33  
    34  	Convey("onCompletedPostAction", t, func() {
    35  		ct := cvtesting.Test{}
    36  		ctx, cancel := ct.SetUp(t)
    37  		defer cancel()
    38  		const (
    39  			lProject = "chromium"
    40  			opID     = "1-1"
    41  		)
    42  		prjcfgtest.Create(ctx, lProject, &cfgpb.Config{
    43  			ConfigGroups: []*cfgpb.ConfigGroup{{Name: "single"}}})
    44  		opPayload := &run.OngoingLongOps_Op_ExecutePostActionPayload{
    45  			Name: "label-vote",
    46  			Kind: &run.OngoingLongOps_Op_ExecutePostActionPayload_ConfigAction{
    47  				ConfigAction: &cfgpb.ConfigGroup_PostAction{
    48  					Name: "label-vote",
    49  					Action: &cfgpb.ConfigGroup_PostAction_VoteGerritLabels_{
    50  						VoteGerritLabels: &cfgpb.ConfigGroup_PostAction_VoteGerritLabels{},
    51  					},
    52  				},
    53  			},
    54  		}
    55  		opResult := &eventpb.LongOpCompleted{
    56  			OperationId: opID,
    57  			Result: &eventpb.LongOpCompleted_ExecutePostAction{
    58  				ExecutePostAction: &eventpb.LongOpCompleted_ExecutePostActionResult{},
    59  			},
    60  		}
    61  		rs := &state.RunState{
    62  			Run: run.Run{
    63  				ID:            lProject + "/1111111111111-1-deadbeef",
    64  				Status:        run.Status_SUCCEEDED,
    65  				Mode:          run.DryRun,
    66  				ConfigGroupID: prjcfgtest.MustExist(ctx, lProject).ConfigGroupIDs[0],
    67  				OngoingLongOps: &run.OngoingLongOps{
    68  					Ops: map[string]*run.OngoingLongOps_Op{
    69  						opID: {
    70  							Work: &run.OngoingLongOps_Op_ExecutePostAction{
    71  								ExecutePostAction: opPayload,
    72  							},
    73  						},
    74  					},
    75  				},
    76  			},
    77  		}
    78  		h, _ := makeTestHandler(&ct)
    79  		var err error
    80  		var res *Result
    81  		Convey("execution summary is set", func() {
    82  			opResult.Status = eventpb.LongOpCompleted_CANCELLED
    83  			opResult.GetExecutePostAction().Summary = "this is a summary"
    84  			res, err = h.OnLongOpCompleted(ctx, rs, opResult)
    85  			So(err, ShouldBeNil)
    86  			So(res.State.LogEntries[0].GetInfo(), ShouldResembleProto, &run.LogEntry_Info{
    87  				Label:   "PostAction[label-vote]",
    88  				Message: "this is a summary",
    89  			})
    90  		})
    91  		Convey("execution summary is not set", func() {
    92  			var expected string
    93  			Convey("the op succeeded", func() {
    94  				opResult.Status = eventpb.LongOpCompleted_SUCCEEDED
    95  				expected = "the execution succeeded"
    96  			})
    97  			Convey("the op expired", func() {
    98  				opResult.Status = eventpb.LongOpCompleted_EXPIRED
    99  				expected = "the execution deadline was exceeded"
   100  			})
   101  			Convey("the op cancelled", func() {
   102  				opResult.Status = eventpb.LongOpCompleted_CANCELLED
   103  				expected = "the execution was cancelled"
   104  			})
   105  			Convey("the op failed", func() {
   106  				opResult.Status = eventpb.LongOpCompleted_FAILED
   107  				expected = "the execution failed"
   108  			})
   109  			res, err = h.OnLongOpCompleted(ctx, rs, opResult)
   110  			So(err, ShouldBeNil)
   111  			So(res.State.LogEntries[0].GetInfo(), ShouldResembleProto, &run.LogEntry_Info{
   112  				Label:   "PostAction[label-vote]",
   113  				Message: expected,
   114  			})
   115  		})
   116  		So(res.State.OngoingLongOps, ShouldBeNil)
   117  		So(res.SideEffectFn, ShouldBeNil)
   118  		So(res.PreserveEvents, ShouldBeFalse)
   119  	})
   120  }