go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/model/builder_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 model
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"go.chromium.org/luci/common/clock/testclock"
    23  	"go.chromium.org/luci/gae/impl/memory"
    24  	"go.chromium.org/luci/gae/service/datastore"
    25  
    26  	pb "go.chromium.org/luci/buildbucket/proto"
    27  
    28  	. "github.com/smartystreets/goconvey/convey"
    29  )
    30  
    31  func TestBuilderStat(t *testing.T) {
    32  	t.Parallel()
    33  
    34  	Convey("BuilderStat", t, func() {
    35  		ctx := memory.Use(context.Background())
    36  		datastore.GetTestable(ctx).AutoIndex(true)
    37  		datastore.GetTestable(ctx).Consistent(true)
    38  
    39  		t := testclock.TestTimeUTC
    40  		So(datastore.Put(ctx, &BuilderStat{
    41  			ID:            "proj:bucket:builder1",
    42  			LastScheduled: t,
    43  		}), ShouldBeNil)
    44  
    45  		Convey("update builder", func() {
    46  			builds := []*Build{
    47  				{
    48  					ID: 1,
    49  					Proto: &pb.Build{
    50  						Builder: &pb.BuilderID{
    51  							Project: "proj",
    52  							Bucket:  "bucket",
    53  							Builder: "builder1",
    54  						},
    55  					},
    56  				},
    57  				{
    58  					ID: 2,
    59  					Proto: &pb.Build{
    60  						Builder: &pb.BuilderID{
    61  							Project: "proj",
    62  							Bucket:  "bucket",
    63  							Builder: "builder2",
    64  						},
    65  					},
    66  				},
    67  				{
    68  					ID: 3,
    69  					Proto: &pb.Build{
    70  						Builder: &pb.BuilderID{
    71  							Project: "proj",
    72  							Bucket:  "bucket",
    73  							Builder: "builder1",
    74  						},
    75  					},
    76  				},
    77  			}
    78  			now := t.Add(3600 * time.Second)
    79  			err := UpdateBuilderStat(ctx, builds, now)
    80  			So(err, ShouldBeNil)
    81  
    82  			currentBuilders := []*BuilderStat{
    83  				{ID: "proj:bucket:builder1"},
    84  				{ID: "proj:bucket:builder2"},
    85  			}
    86  			err = datastore.Get(ctx, currentBuilders)
    87  			So(err, ShouldBeNil)
    88  			So(currentBuilders, ShouldResemble, []*BuilderStat{
    89  				{
    90  					ID:            "proj:bucket:builder1",
    91  					LastScheduled: datastore.RoundTime(now),
    92  				},
    93  				{
    94  					ID:            "proj:bucket:builder2",
    95  					LastScheduled: datastore.RoundTime(now),
    96  				},
    97  			})
    98  		})
    99  
   100  		Convey("uninitialized build.proto.builder", func() {
   101  			builds := []*Build{{ID: 1}}
   102  			So(func() { UpdateBuilderStat(ctx, builds, t) }, ShouldPanic)
   103  		})
   104  	})
   105  }