go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/impl/state/state_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 state
    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/common"
    25  	"go.chromium.org/luci/cv/internal/common/tree"
    26  	"go.chromium.org/luci/cv/internal/configs/prjcfg"
    27  	"go.chromium.org/luci/cv/internal/configs/prjcfg/prjcfgtest"
    28  	"go.chromium.org/luci/cv/internal/cvtesting"
    29  	"go.chromium.org/luci/cv/internal/run"
    30  
    31  	. "github.com/smartystreets/goconvey/convey"
    32  	. "go.chromium.org/luci/common/testing/assertions"
    33  )
    34  
    35  func TestCheckTree(t *testing.T) {
    36  	t.Parallel()
    37  
    38  	Convey("CheckTree", t, func() {
    39  		ct := cvtesting.Test{}
    40  		ctx, cancel := ct.SetUp(t)
    41  		defer cancel()
    42  		const lProject = "chromium"
    43  		rs := &RunState{
    44  			Run: run.Run{
    45  				ID:         common.MakeRunID(lProject, ct.Clock.Now().Add(-2*time.Minute), 1, []byte("deadbeef")),
    46  				Submission: &run.Submission{},
    47  			},
    48  		}
    49  		prjcfgtest.Create(ctx, lProject, &cfgpb.Config{
    50  			ConfigGroups: []*cfgpb.ConfigGroup{
    51  				{
    52  					Name: "main",
    53  					Verifiers: &cfgpb.Verifiers{
    54  						TreeStatus: &cfgpb.Verifiers_TreeStatus{
    55  							Url: "tree.example.com",
    56  						},
    57  					},
    58  				},
    59  			},
    60  		})
    61  		meta, err := prjcfg.GetLatestMeta(ctx, lProject)
    62  		So(err, ShouldBeNil)
    63  		So(meta.ConfigGroupIDs, ShouldHaveLength, 1)
    64  		rs.Run.ConfigGroupID = meta.ConfigGroupIDs[0]
    65  		client := ct.TreeFake.Client()
    66  
    67  		Convey("Open", func() {
    68  			ct.TreeFake.ModifyState(ctx, tree.Open)
    69  			open, err := rs.CheckTree(ctx, client)
    70  			So(err, ShouldBeNil)
    71  			So(open, ShouldBeTrue)
    72  			So(rs.Run.Submission.TreeOpen, ShouldBeTrue)
    73  			So(rs.Run.Submission.LastTreeCheckTime, ShouldResembleProto, timestamppb.New(ct.Clock.Now().UTC()))
    74  		})
    75  
    76  		Convey("Closed", func() {
    77  			ct.TreeFake.ModifyState(ctx, tree.Closed)
    78  			open, err := rs.CheckTree(ctx, client)
    79  			So(err, ShouldBeNil)
    80  			So(open, ShouldBeFalse)
    81  			So(rs.Run.Submission.TreeOpen, ShouldBeFalse)
    82  			So(rs.Run.Submission.LastTreeCheckTime, ShouldResembleProto, timestamppb.New(ct.Clock.Now().UTC()))
    83  		})
    84  
    85  		Convey("Closed but ignored", func() {
    86  			ct.TreeFake.ModifyState(ctx, tree.Closed)
    87  			rs.Run.Options = &run.Options{SkipTreeChecks: true}
    88  			open, err := rs.CheckTree(ctx, client)
    89  			So(err, ShouldBeNil)
    90  			So(open, ShouldBeTrue)
    91  			So(rs.Run.Submission.TreeOpen, ShouldBeTrue)
    92  			So(rs.Run.Submission.LastTreeCheckTime, ShouldResembleProto, timestamppb.New(ct.Clock.Now().UTC()))
    93  		})
    94  
    95  		Convey("Tree not defined", func() {
    96  			ct.TreeFake.ModifyState(ctx, tree.Closed)
    97  			prjcfgtest.Update(ctx, lProject, &cfgpb.Config{
    98  				ConfigGroups: []*cfgpb.ConfigGroup{
    99  					{Name: "main"},
   100  					// No Tree defined
   101  				},
   102  			})
   103  			meta, err := prjcfg.GetLatestMeta(ctx, lProject)
   104  			So(err, ShouldBeNil)
   105  			So(meta.ConfigGroupIDs, ShouldHaveLength, 1)
   106  			rs.Run.ConfigGroupID = meta.ConfigGroupIDs[0]
   107  			open, err := rs.CheckTree(ctx, client)
   108  			So(err, ShouldBeNil)
   109  			So(open, ShouldBeTrue)
   110  			So(rs.Run.Submission.TreeOpen, ShouldBeTrue)
   111  			So(rs.Run.Submission.LastTreeCheckTime, ShouldResembleProto, timestamppb.New(ct.Clock.Now().UTC()))
   112  		})
   113  	})
   114  }