go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/postaction/conditions_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 postaction
    16  
    17  import (
    18  	"testing"
    19  
    20  	cfgpb "go.chromium.org/luci/cv/api/config/v2"
    21  	apipb "go.chromium.org/luci/cv/api/v1"
    22  	"go.chromium.org/luci/cv/internal/run"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  )
    26  
    27  func TestIsTriggeringConditionMet(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	Convey("IsTriggeringConditionMet", t, func() {
    31  		pa := &cfgpb.ConfigGroup_PostAction{Name: "action q"}
    32  		addCond := func(m string, sts ...apipb.Run_Status) {
    33  			pa.Conditions = append(pa.Conditions, &cfgpb.ConfigGroup_PostAction_TriggeringCondition{
    34  				Mode:     m,
    35  				Statuses: sts,
    36  			})
    37  		}
    38  		r := &run.Run{}
    39  		shouldMeet := func(m run.Mode, st run.Status) {
    40  			r.Mode, r.Status = m, st
    41  			So(IsTriggeringConditionMet(pa, r), ShouldBeTrue)
    42  		}
    43  		shouldNotMeet := func(m run.Mode, st run.Status) {
    44  			r.Mode, r.Status = m, st
    45  			So(IsTriggeringConditionMet(pa, r), ShouldBeFalse)
    46  		}
    47  		addCond("DRY_RUN", apipb.Run_SUCCEEDED, apipb.Run_CANCELLED)
    48  		addCond("FULL_RUN", apipb.Run_SUCCEEDED, apipb.Run_FAILED)
    49  		addCond("CUSTOM_RUN", apipb.Run_SUCCEEDED)
    50  
    51  		Convey("dry_run mode", func() {
    52  			shouldMeet(run.DryRun, run.Status_SUCCEEDED)
    53  			shouldMeet(run.DryRun, run.Status_CANCELLED)
    54  			shouldNotMeet(run.DryRun, run.Status_FAILED)
    55  		})
    56  		Convey("full_run mode", func() {
    57  			shouldMeet(run.FullRun, run.Status_SUCCEEDED)
    58  			shouldNotMeet(run.FullRun, run.Status_CANCELLED)
    59  			shouldMeet(run.FullRun, run.Status_FAILED)
    60  		})
    61  		Convey("custom_run mode", func() {
    62  			CustomRun := run.Mode("CUSTOM_RUN")
    63  			shouldMeet(CustomRun, run.Status_SUCCEEDED)
    64  			shouldNotMeet(CustomRun, run.Status_CANCELLED)
    65  			shouldNotMeet(CustomRun, run.Status_FAILED)
    66  		})
    67  		Convey("a not matched run mode", func() {
    68  			MyRun := run.Mode("MY_RUN")
    69  			shouldNotMeet(MyRun, run.Status_SUCCEEDED)
    70  			shouldNotMeet(MyRun, run.Status_CANCELLED)
    71  			shouldNotMeet(MyRun, run.Status_FAILED)
    72  		})
    73  	})
    74  }