go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/cmd/bbagent/cas_test.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 main
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  	"runtime"
    21  	"testing"
    22  
    23  	pb "go.chromium.org/luci/buildbucket/proto"
    24  
    25  	. "github.com/smartystreets/goconvey/convey"
    26  	. "go.chromium.org/luci/common/testing/assertions"
    27  )
    28  
    29  func skipWin(t *testing.T) {
    30  	if runtime.GOOS == "windows" {
    31  		t.Skip("Skipping testing in Win")
    32  	}
    33  }
    34  
    35  func TestFindCasClient(t *testing.T) {
    36  	skipWin(t)
    37  	Convey("findCasClient", t, func() {
    38  		build := &pb.Build{
    39  			Id: 123,
    40  			Infra: &pb.BuildInfra{
    41  				Buildbucket: &pb.BuildInfra_Buildbucket{
    42  					Agent: &pb.BuildInfra_Buildbucket_Agent{
    43  						Input: &pb.BuildInfra_Buildbucket_Agent_Input{
    44  							Data: map[string]*pb.InputDataRef{
    45  								"path_a": {
    46  									DataType: &pb.InputDataRef_Cipd{
    47  										Cipd: &pb.InputDataRef_CIPD{
    48  											Specs: []*pb.InputDataRef_CIPD_PkgSpec{{Package: "cas_client", Version: "latest"}},
    49  										},
    50  									},
    51  								},
    52  								"path_b": {
    53  									DataType: &pb.InputDataRef_Cas{
    54  										Cas: &pb.InputDataRef_CAS{
    55  											CasInstance: "projects/project/instances/instance",
    56  											Digest: &pb.InputDataRef_CAS_Digest{
    57  												Hash:      "hash",
    58  												SizeBytes: 1,
    59  											},
    60  										},
    61  									},
    62  								},
    63  							},
    64  						},
    65  						Output: &pb.BuildInfra_Buildbucket_Agent_Output{},
    66  						Purposes: map[string]pb.BuildInfra_Buildbucket_Agent_Purpose{
    67  							"path_a": pb.BuildInfra_Buildbucket_Agent_PURPOSE_BBAGENT_UTILITY,
    68  							"path_b": pb.BuildInfra_Buildbucket_Agent_PURPOSE_EXE_PAYLOAD,
    69  						},
    70  					},
    71  				},
    72  			},
    73  		}
    74  
    75  		Convey("pass", func() {
    76  			casClient, err := findCasClient(build)
    77  			So(err, ShouldBeNil)
    78  			cwd, _ := os.Getwd()
    79  			So(casClient, ShouldEqual, filepath.Join(cwd, "path_a/cas"))
    80  		})
    81  
    82  		Convey("fail", func() {
    83  			build.Infra.Buildbucket.Agent.Purposes = nil
    84  			_, err := findCasClient(build)
    85  			So(err, ShouldErrLike, "Failed to find bbagent utility packages")
    86  		})
    87  	})
    88  }