go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/internal/clients/backend_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 clients
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/common/clock/testclock"
    22  	"go.chromium.org/luci/gae/filter/txndefer"
    23  	"go.chromium.org/luci/gae/impl/memory"
    24  	"go.chromium.org/luci/gae/service/datastore"
    25  	"go.chromium.org/luci/server/secrets"
    26  	"go.chromium.org/luci/server/secrets/testsecrets"
    27  	"go.chromium.org/luci/server/tq"
    28  
    29  	"go.chromium.org/luci/buildbucket/appengine/internal/metrics"
    30  	pb "go.chromium.org/luci/buildbucket/proto"
    31  
    32  	gomock "github.com/golang/mock/gomock"
    33  	. "github.com/smartystreets/goconvey/convey"
    34  	. "go.chromium.org/luci/common/testing/assertions"
    35  )
    36  
    37  func TestBackendTaskClient(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	Convey("assert NewBackendClient", t, func() {
    41  		ctl := gomock.NewController(t)
    42  		defer ctl.Finish()
    43  		mockBackend := NewMockTaskBackendClient(ctl)
    44  		now := testclock.TestRecentTimeUTC
    45  		ctx, _ := testclock.UseTime(context.Background(), now)
    46  		ctx = context.WithValue(ctx, MockTaskBackendClientKey, mockBackend)
    47  		ctx = memory.UseWithAppID(ctx, "dev~app-id")
    48  		ctx = txndefer.FilterRDS(ctx)
    49  		ctx = metrics.WithServiceInfo(ctx, "svc", "job", "ins")
    50  		datastore.GetTestable(ctx).AutoIndex(true)
    51  		datastore.GetTestable(ctx).Consistent(true)
    52  		ctx, _ = tq.TestingContext(ctx, nil)
    53  		store := &testsecrets.Store{
    54  			Secrets: map[string]secrets.Secret{
    55  				"key": {Active: []byte("stuff")},
    56  			},
    57  		}
    58  		ctx = secrets.Use(ctx, store)
    59  		ctx = secrets.GeneratePrimaryTinkAEADForTest(ctx)
    60  
    61  		build := &pb.Build{
    62  			Builder: &pb.BuilderID{
    63  				Builder: "builder",
    64  				Bucket:  "bucket",
    65  				Project: "project",
    66  			},
    67  			Id: 1,
    68  		}
    69  
    70  		infra := &pb.BuildInfra{
    71  			Backend: &pb.BuildInfra_Backend{
    72  				Task: &pb.Task{
    73  					Id: &pb.TaskID{
    74  						Id:     "1",
    75  						Target: "swarming://mytarget",
    76  					},
    77  				},
    78  			},
    79  			Buildbucket: &pb.BuildInfra_Buildbucket{
    80  				Hostname: "some unique host name",
    81  			},
    82  		}
    83  
    84  		Convey("global settings not defined", func() {
    85  			_, err := NewBackendClient(ctx, build.Builder.Project, infra.Backend.Task.Id.Target, nil)
    86  			So(err, ShouldErrLike, "could not get global settings config")
    87  		})
    88  
    89  		Convey("target not in global config", func() {
    90  			backendSetting := []*pb.BackendSetting{}
    91  			settingsCfg := &pb.SettingsCfg{Backends: backendSetting}
    92  			_, err := NewBackendClient(ctx, build.Builder.Project, infra.Backend.Task.Id.Target, settingsCfg)
    93  			So(err, ShouldErrLike, "could not find target in global config settings")
    94  		})
    95  
    96  		Convey("target is in global config", func() {
    97  			backendSetting := []*pb.BackendSetting{}
    98  			backendSetting = append(backendSetting, &pb.BackendSetting{
    99  				Target:   "swarming://mytarget",
   100  				Hostname: "hostname",
   101  			})
   102  			settingsCfg := &pb.SettingsCfg{Backends: backendSetting}
   103  			_, err := NewBackendClient(ctx, build.Builder.Project, infra.Backend.Task.Id.Target, settingsCfg)
   104  			So(err, ShouldBeNil)
   105  		})
   106  	})
   107  }