go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/impl/longops/execute_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 longops
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"testing"
    21  	"time"
    22  
    23  	"google.golang.org/protobuf/types/known/timestamppb"
    24  
    25  	cfgpb "go.chromium.org/luci/cv/api/config/v2"
    26  	"go.chromium.org/luci/cv/internal/cvtesting"
    27  	"go.chromium.org/luci/cv/internal/run"
    28  	"go.chromium.org/luci/cv/internal/run/eventpb"
    29  
    30  	. "github.com/smartystreets/goconvey/convey"
    31  )
    32  
    33  func TestExecutePostActionOp(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	Convey("report", t, func() {
    37  		ct := cvtesting.Test{}
    38  		ctx, cancel := ct.SetUp(t)
    39  		defer cancel()
    40  
    41  		postActionCfg := &cfgpb.ConfigGroup_PostAction{
    42  			Name: "vote verification labels",
    43  			Action: &cfgpb.ConfigGroup_PostAction_VoteGerritLabels_{
    44  				VoteGerritLabels: &cfgpb.ConfigGroup_PostAction_VoteGerritLabels{},
    45  			},
    46  		}
    47  		op := &ExecutePostActionOp{
    48  			Base: &Base{
    49  				Op: &run.OngoingLongOps_Op{
    50  					Deadline:        timestamppb.New(ct.Clock.Now().Add(time.Minute)),
    51  					CancelRequested: false,
    52  					Work: &run.OngoingLongOps_Op_ExecutePostAction{
    53  						ExecutePostAction: &run.OngoingLongOps_Op_ExecutePostActionPayload{
    54  							Name: postActionCfg.GetName(),
    55  							Kind: &run.OngoingLongOps_Op_ExecutePostActionPayload_ConfigAction{
    56  								ConfigAction: postActionCfg,
    57  							},
    58  						},
    59  					},
    60  				},
    61  				IsCancelRequested: func() bool { return false },
    62  			},
    63  			GFactory: ct.GFactory(),
    64  		}
    65  
    66  		Convey("returns status", func() {
    67  			exeErr := errors.New("this is a successful error")
    68  
    69  			Convey("CANCELLED", func() {
    70  				op.IsCancelRequested = func() bool { return true }
    71  				res := op.report(ctx, exeErr, "votes cancelled")
    72  				So(res.GetStatus(), ShouldEqual, eventpb.LongOpCompleted_CANCELLED)
    73  				So(res.GetExecutePostAction().GetSummary(), ShouldEqual, "votes cancelled")
    74  			})
    75  			Convey("EXPIRED", func() {
    76  				nctx, cancel := context.WithCancel(ctx)
    77  				cancel()
    78  				res := op.report(nctx, exeErr, "votes expired")
    79  				So(res.GetStatus(), ShouldEqual, eventpb.LongOpCompleted_EXPIRED)
    80  				So(res.GetExecutePostAction().GetSummary(), ShouldEqual, "votes expired")
    81  			})
    82  			Convey("FAILED", func() {
    83  				res := op.report(ctx, exeErr, "votes failed")
    84  				So(res.GetStatus(), ShouldEqual, eventpb.LongOpCompleted_FAILED)
    85  				So(res.GetExecutePostAction().GetSummary(), ShouldEqual, "votes failed")
    86  			})
    87  		})
    88  	})
    89  }