go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/led/job/buildbucket_test.go (about)

     1  // Copyright 2020 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 job
    16  
    17  import (
    18  	"testing"
    19  
    20  	"google.golang.org/protobuf/types/known/structpb"
    21  
    22  	bbpb "go.chromium.org/luci/buildbucket/proto"
    23  	"go.chromium.org/luci/luciexe/exe"
    24  	swarmingpb "go.chromium.org/luci/swarming/proto/api_v2"
    25  
    26  	. "github.com/smartystreets/goconvey/convey"
    27  	. "go.chromium.org/luci/common/testing/assertions"
    28  )
    29  
    30  func TestBBEnsureBasics(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	Convey(`Buildbucket.EnsureBasics`, t, func() {
    34  		jd := testBBJob(false)
    35  		So(jd.GetBuildbucket().GetBbagentArgs().GetBuild(), ShouldBeNil)
    36  
    37  		jd.GetBuildbucket().EnsureBasics()
    38  
    39  		So(jd.GetBuildbucket().BbagentArgs.Build.Infra, ShouldNotBeNil)
    40  	})
    41  }
    42  
    43  func TestWriteProperties(t *testing.T) {
    44  	t.Parallel()
    45  
    46  	Convey(`Buildbucket.WriteProperties`, t, func() {
    47  		jd := testBBJob(false)
    48  		So(jd.GetBuildbucket().GetBbagentArgs().GetBuild().GetInput().GetProperties(), ShouldBeNil)
    49  
    50  		jd.GetBuildbucket().WriteProperties(map[string]any{
    51  			"hello": "world",
    52  		})
    53  		So(jd.GetBuildbucket().GetBbagentArgs().GetBuild().GetInput().GetProperties(), ShouldResembleProto, &structpb.Struct{
    54  			Fields: map[string]*structpb.Value{
    55  				"hello": {Kind: &structpb.Value_StringValue{StringValue: "world"}},
    56  			},
    57  		})
    58  	})
    59  }
    60  
    61  func TestUpdateBuildFromBbagentArgs(t *testing.T) {
    62  	t.Parallel()
    63  
    64  	Convey(`UpdateBuildFromBbagentArgs`, t, func() {
    65  		bb := testBBJob(false).GetBuildbucket()
    66  		So(bb.GetBbagentArgs().GetBuild().GetInfra().GetBuildbucket().GetAgent(), ShouldBeNil)
    67  
    68  		bb.BbagentArgs = &bbpb.BBAgentArgs{
    69  			PayloadPath:            "payload_path",
    70  			KnownPublicGerritHosts: []string{"host"},
    71  		}
    72  		bb.UpdateBuildFromBbagentArgs()
    73  
    74  		So(bb.GetBbagentArgs().GetBuild().GetInfra().GetBuildbucket(), ShouldResembleProto,
    75  			&bbpb.BuildInfra_Buildbucket{
    76  				Agent: &bbpb.BuildInfra_Buildbucket_Agent{
    77  					Purposes: map[string]bbpb.BuildInfra_Buildbucket_Agent_Purpose{
    78  						"payload_path": bbpb.BuildInfra_Buildbucket_Agent_PURPOSE_EXE_PAYLOAD,
    79  					},
    80  				},
    81  				KnownPublicGerritHosts: []string{"host"},
    82  			})
    83  	})
    84  }
    85  
    86  func TestUpdatePayloadPath(t *testing.T) {
    87  	t.Parallel()
    88  
    89  	Convey(`UpdatePayloadPath`, t, func() {
    90  		bb := testBBJob(false).GetBuildbucket()
    91  
    92  		bb.BbagentArgs = &bbpb.BBAgentArgs{
    93  			PayloadPath: "payload_path",
    94  		}
    95  		bb.UpdateBuildFromBbagentArgs()
    96  		bb.UpdatePayloadPath("new_path")
    97  
    98  		So(bb.GetBbagentArgs().GetPayloadPath(), ShouldEqual, "new_path")
    99  		So(bb.GetBbagentArgs().GetBuild().GetInfra().GetBuildbucket(), ShouldResembleProto,
   100  			&bbpb.BuildInfra_Buildbucket{
   101  				Agent: &bbpb.BuildInfra_Buildbucket_Agent{
   102  					Purposes: map[string]bbpb.BuildInfra_Buildbucket_Agent_Purpose{
   103  						"new_path": bbpb.BuildInfra_Buildbucket_Agent_PURPOSE_EXE_PAYLOAD,
   104  					},
   105  				},
   106  			})
   107  	})
   108  }
   109  
   110  func TestUpdateLedProperties(t *testing.T) {
   111  	t.Parallel()
   112  
   113  	Convey(`UpdateLedProperties`, t, func() {
   114  		bb := testBBJob(false).GetBuildbucket()
   115  		bb.EnsureBasics()
   116  		Convey(`cas input`, func() {
   117  			bld := bb.BbagentArgs.Build
   118  			bld.Infra.Buildbucket.Agent = &bbpb.BuildInfra_Buildbucket_Agent{
   119  				Input: &bbpb.BuildInfra_Buildbucket_Agent_Input{
   120  					Data: map[string]*bbpb.InputDataRef{
   121  						"kitchen-checkout": {
   122  							DataType: &bbpb.InputDataRef_Cas{
   123  								Cas: &bbpb.InputDataRef_CAS{
   124  									CasInstance: "projects/project/instances/instance",
   125  									Digest: &bbpb.InputDataRef_CAS_Digest{
   126  										Hash:      "hash",
   127  										SizeBytes: 1,
   128  									},
   129  								},
   130  							},
   131  						},
   132  					},
   133  				},
   134  				Purposes: map[string]bbpb.BuildInfra_Buildbucket_Agent_Purpose{
   135  					"kitchen-checkout": bbpb.BuildInfra_Buildbucket_Agent_PURPOSE_EXE_PAYLOAD,
   136  				},
   137  			}
   138  			bb.WriteProperties(map[string]any{
   139  				"$recipe_engine/led": &ledProperties{
   140  					ShadowedBucket: "bucket",
   141  				},
   142  			})
   143  
   144  			err := bb.UpdateLedProperties()
   145  			So(err, ShouldBeNil)
   146  			newProps := ledProperties{}
   147  			err = exe.ParseProperties(bld.Input.Properties, map[string]any{
   148  				"$recipe_engine/led": &newProps,
   149  			})
   150  			So(err, ShouldBeNil)
   151  			So(newProps, ShouldResemble, ledProperties{
   152  				RbeCasInput: &swarmingpb.CASReference{
   153  					CasInstance: "projects/project/instances/instance",
   154  					Digest: &swarmingpb.Digest{
   155  						Hash:      "hash",
   156  						SizeBytes: 1,
   157  					},
   158  				},
   159  				ShadowedBucket: "bucket",
   160  			})
   161  		})
   162  		Convey(`cipd input`, func() {
   163  			bld := bb.BbagentArgs.Build
   164  			bld.Infra.Buildbucket.Agent = &bbpb.BuildInfra_Buildbucket_Agent{
   165  				Input: &bbpb.BuildInfra_Buildbucket_Agent_Input{
   166  					Data: map[string]*bbpb.InputDataRef{
   167  						"kitchen-checkout": {
   168  							DataType: &bbpb.InputDataRef_Cipd{
   169  								Cipd: &bbpb.InputDataRef_CIPD{
   170  									Specs: []*bbpb.InputDataRef_CIPD_PkgSpec{
   171  										{
   172  											Package: "package",
   173  											Version: "version"},
   174  									},
   175  								},
   176  							},
   177  						},
   178  					},
   179  				},
   180  				Purposes: map[string]bbpb.BuildInfra_Buildbucket_Agent_Purpose{
   181  					"kitchen-checkout": bbpb.BuildInfra_Buildbucket_Agent_PURPOSE_EXE_PAYLOAD,
   182  				},
   183  			}
   184  
   185  			err := bb.UpdateLedProperties()
   186  			So(err, ShouldBeNil)
   187  			newProps := ledProperties{}
   188  			err = exe.ParseProperties(bld.Input.Properties, map[string]any{
   189  				"$recipe_engine/led": &newProps,
   190  			})
   191  			So(err, ShouldBeNil)
   192  			So(newProps, ShouldResemble, ledProperties{
   193  				CIPDInput: &cipdInput{
   194  					Package: "package",
   195  					Version: "version",
   196  				},
   197  			})
   198  		})
   199  	})
   200  }