go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/common/common_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  package common
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  
    20  	"go.chromium.org/luci/gae/impl/memory"
    21  	"go.chromium.org/luci/gae/service/datastore"
    22  
    23  	"go.chromium.org/luci/buildbucket/appengine/model"
    24  	pb "go.chromium.org/luci/buildbucket/proto"
    25  
    26  	. "github.com/smartystreets/goconvey/convey"
    27  	. "go.chromium.org/luci/common/testing/assertions"
    28  )
    29  
    30  func TestGetBuildEntities(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	ctx := memory.Use(context.Background())
    34  	Convey("GetBuild", t, func() {
    35  		Convey("ok", func() {
    36  			So(datastore.Put(ctx, &model.Build{
    37  				Proto: &pb.Build{
    38  					Id: 1,
    39  					Builder: &pb.BuilderID{
    40  						Project: "project",
    41  						Bucket:  "bucket",
    42  						Builder: "builder",
    43  					},
    44  					Status: pb.Status_SUCCESS,
    45  				},
    46  			}), ShouldBeNil)
    47  			b, err := GetBuild(ctx, 1)
    48  			So(err, ShouldBeNil)
    49  			So(b.Proto, ShouldResembleProto, &pb.Build{
    50  				Id: 1,
    51  				Builder: &pb.BuilderID{
    52  					Project: "project",
    53  					Bucket:  "bucket",
    54  					Builder: "builder",
    55  				},
    56  				Status: pb.Status_SUCCESS,
    57  			})
    58  		})
    59  
    60  		Convey("wrong BuildID", func() {
    61  			ctx := memory.Use(context.Background())
    62  			So(datastore.Put(ctx, &model.Build{
    63  				Proto: &pb.Build{
    64  					Id: 1,
    65  					Builder: &pb.BuilderID{
    66  						Project: "project",
    67  						Bucket:  "bucket",
    68  						Builder: "builder",
    69  					},
    70  					Status: pb.Status_SUCCESS,
    71  				},
    72  			}), ShouldBeNil)
    73  			_, err := GetBuild(ctx, 2)
    74  			So(err, ShouldNotBeNil)
    75  			So(err.Error(), ShouldContainSubstring, "resource not found")
    76  		})
    77  	})
    78  
    79  	Convey("GetBuildEntities", t, func() {
    80  		bld := &model.Build{
    81  			ID: 1,
    82  			Proto: &pb.Build{
    83  				Id: 1,
    84  				Builder: &pb.BuilderID{
    85  					Project: "project",
    86  					Bucket:  "bucket",
    87  					Builder: "builder",
    88  				},
    89  				Status: pb.Status_SUCCESS,
    90  			},
    91  		}
    92  		bk := datastore.KeyForObj(ctx, bld)
    93  		bs := &model.BuildStatus{Build: bk}
    94  		infra := &model.BuildInfra{Build: bk}
    95  		steps := &model.BuildSteps{Build: bk}
    96  		inputProp := &model.BuildInputProperties{Build: bk}
    97  		So(datastore.Put(ctx, bld, infra, bs, steps, inputProp), ShouldBeNil)
    98  
    99  		Convey("partial not found", func() {
   100  			_, err := GetBuildEntities(ctx, bld.Proto.Id, model.BuildOutputPropertiesKind)
   101  			So(err, ShouldErrLike, "not found")
   102  		})
   103  
   104  		Convey("pass", func() {
   105  			outputProp := &model.BuildOutputProperties{Build: bk}
   106  			So(datastore.Put(ctx, outputProp), ShouldBeNil)
   107  			Convey("all", func() {
   108  				entities, err := GetBuildEntities(ctx, bld.Proto.Id, model.BuildKind, model.BuildStatusKind, model.BuildStepsKind, model.BuildInfraKind, model.BuildInputPropertiesKind, model.BuildOutputPropertiesKind)
   109  				So(err, ShouldBeNil)
   110  				So(entities, ShouldHaveLength, 6)
   111  			})
   112  			Convey("only infra", func() {
   113  				entities, err := GetBuildEntities(ctx, bld.Proto.Id, model.BuildInfraKind)
   114  				So(err, ShouldBeNil)
   115  				So(entities, ShouldHaveLength, 1)
   116  			})
   117  		})
   118  	})
   119  }