go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/impl/handler/cancel_test.go (about)

     1  // Copyright 2021 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  	"fmt"
    19  	"testing"
    20  	"time"
    21  
    22  	"go.chromium.org/luci/common/clock"
    23  	"go.chromium.org/luci/gae/service/datastore"
    24  
    25  	cfgpb "go.chromium.org/luci/cv/api/config/v2"
    26  	"go.chromium.org/luci/cv/internal/changelist"
    27  	"go.chromium.org/luci/cv/internal/common"
    28  	"go.chromium.org/luci/cv/internal/configs/prjcfg/prjcfgtest"
    29  	"go.chromium.org/luci/cv/internal/cvtesting"
    30  	"go.chromium.org/luci/cv/internal/prjmanager/pmtest"
    31  	"go.chromium.org/luci/cv/internal/run"
    32  	"go.chromium.org/luci/cv/internal/run/impl/state"
    33  
    34  	. "github.com/smartystreets/goconvey/convey"
    35  )
    36  
    37  func TestCancel(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	Convey("Cancel", t, func() {
    41  		ct := cvtesting.Test{}
    42  		ctx, close := ct.SetUp(t)
    43  		defer close()
    44  		ctx, _ = pmtest.MockDispatch(ctx)
    45  
    46  		const lProject = "chromium"
    47  		prjcfgtest.Create(ctx, lProject, &cfgpb.Config{
    48  			ConfigGroups: []*cfgpb.ConfigGroup{{Name: "main"}},
    49  		})
    50  		cgs, err := prjcfgtest.MustExist(ctx, lProject).GetConfigGroups(ctx)
    51  		So(err, ShouldBeNil)
    52  		cg := cgs[0]
    53  		runID := common.MakeRunID(lProject, ct.Clock.Now(), 1, []byte("deadbeef"))
    54  		clid := common.CLID(11)
    55  		rs := &state.RunState{
    56  			Run: run.Run{
    57  				ID:            runID,
    58  				ConfigGroupID: cg.ID,
    59  				CreateTime:    clock.Now(ctx).UTC().Add(-2 * time.Minute),
    60  				CLs:           common.CLIDs{clid},
    61  			},
    62  		}
    63  		So(datastore.Put(ctx, &changelist.CL{
    64  			ID: clid,
    65  			IncompleteRuns: common.RunIDs{
    66  				runID,
    67  				common.MakeRunID(lProject, ct.Clock.Now().Add(1*time.Minute), 1, []byte("cafecafe")),
    68  			},
    69  		}), ShouldBeNil)
    70  		h, _ := makeTestHandler(&ct)
    71  
    72  		now := ct.Clock.Now().UTC()
    73  		Convey("Cancel works", func() {
    74  			rs.Status = run.Status_RUNNING
    75  			rs.StartTime = now.Add(-1 * time.Minute)
    76  			res, err := h.Cancel(ctx, rs, []string{"user request"})
    77  			So(err, ShouldBeNil)
    78  			So(res.State.Status, ShouldEqual, run.Status_CANCELLED)
    79  			So(res.State.StartTime, ShouldResemble, now.Add(-1*time.Minute))
    80  			So(res.State.EndTime, ShouldResemble, now)
    81  			So(res.State.CancellationReasons, ShouldResemble, []string{"user request"})
    82  			So(res.SideEffectFn, ShouldNotBeNil)
    83  			So(res.PreserveEvents, ShouldBeFalse)
    84  		})
    85  
    86  		Convey("Filter cancellation reasons", func() {
    87  			rs.Status = run.Status_RUNNING
    88  			rs.StartTime = now.Add(-1 * time.Minute)
    89  			res, err := h.Cancel(ctx, rs, []string{"user request", "", "user request"})
    90  			So(err, ShouldBeNil)
    91  			So(res.State.CancellationReasons, ShouldResemble, []string{"user request"})
    92  		})
    93  
    94  		Convey("Cancels SUBMITTING Run", func() {
    95  			rs.Status = run.Status_SUBMITTING
    96  			res, err := h.Cancel(ctx, rs, []string{"user request"})
    97  			So(err, ShouldBeNil)
    98  			So(res.State, ShouldEqual, rs)
    99  			So(res.SideEffectFn, ShouldBeNil)
   100  			So(res.PreserveEvents, ShouldBeTrue)
   101  		})
   102  
   103  		statuses := []run.Status{
   104  			run.Status_SUCCEEDED,
   105  			run.Status_FAILED,
   106  			run.Status_CANCELLED,
   107  		}
   108  		for _, status := range statuses {
   109  			Convey(fmt.Sprintf("Noop when Run is %s", status), func() {
   110  				rs.Status = status
   111  				rs.StartTime = clock.Now(ctx).UTC().Add(-1 * time.Minute)
   112  				rs.EndTime = clock.Now(ctx).UTC().Add(-30 * time.Second)
   113  				res, err := h.Cancel(ctx, rs, []string{"user request"})
   114  				So(err, ShouldBeNil)
   115  				So(res.State, ShouldEqual, rs)
   116  				So(res.SideEffectFn, ShouldBeNil)
   117  				So(res.PreserveEvents, ShouldBeFalse)
   118  			})
   119  		}
   120  	})
   121  }