go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/rpc/testutil/test_util.go (about)

     1  // Copyright 2022 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 testutil contains util functions for testing buildbucket RPCs.
    16  package testutil
    17  
    18  import (
    19  	"context"
    20  
    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  
    27  // PutBuilder saves a *model.Builder to datastore for test usage.
    28  func PutBuilder(ctx context.Context, project, bucket, builder string, backend string) {
    29  
    30  	bldr := &model.Builder{
    31  		Parent: model.BucketKey(ctx, project, bucket),
    32  		ID:     builder,
    33  		Config: &pb.BuilderConfig{
    34  			Name:         builder,
    35  			SwarmingHost: "host",
    36  		},
    37  	}
    38  
    39  	if backend != "" {
    40  		bldr.Config.Backend = &pb.BuilderConfig_Backend{
    41  			Target: backend,
    42  		}
    43  	}
    44  
    45  	// TODO: pass in testing.TB and Assert this instead.
    46  	if err := datastore.Put(ctx, bldr); err != nil {
    47  		panic(err)
    48  	}
    49  }
    50  
    51  // PutBucket saves a *model.Bucket to datastore for test usage.
    52  func PutBucket(ctx context.Context, project, bucket string, cfg *pb.Bucket) {
    53  	if cfg == nil {
    54  		cfg = &pb.Bucket{}
    55  	}
    56  	if cfg.Name == "" {
    57  		cfg.Name = bucket
    58  	}
    59  
    60  	// TODO: pass in testing.TB and Assert this instead.
    61  	err := datastore.Put(ctx, &model.Bucket{
    62  		Parent: model.ProjectKey(ctx, project),
    63  		ID:     bucket,
    64  		Proto:  cfg,
    65  	})
    66  	if err != nil {
    67  		panic(err)
    68  	}
    69  }