go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/cmd/bbagent/main_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  	"context"
    19  	"flag"
    20  	"os/exec"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"go.chromium.org/luci/buildbucket"
    25  	bbpb "go.chromium.org/luci/buildbucket/proto"
    26  	"go.chromium.org/luci/common/errors"
    27  	"go.chromium.org/luci/common/logging/memlogger"
    28  	"go.chromium.org/luci/gae/impl/memory"
    29  	"go.chromium.org/luci/lucictx"
    30  	"go.chromium.org/luci/luciexe"
    31  
    32  	. "github.com/smartystreets/goconvey/convey"
    33  	. "go.chromium.org/luci/common/testing/assertions"
    34  )
    35  
    36  func TestReadyToFinalize(t *testing.T) {
    37  	ctx := memory.Use(context.Background())
    38  	ctx = memlogger.Use(ctx)
    39  	outputFlag := luciexe.AddOutputFlagToSet(&flag.FlagSet{})
    40  	finalBuild := &bbpb.Build{}
    41  
    42  	Convey("Ready to finalize: success", t, func() {
    43  		isReady := readyToFinalize(ctx, finalBuild, nil, nil, outputFlag)
    44  		So(isReady, ShouldEqual, true)
    45  
    46  	})
    47  
    48  	Convey("Not ready to finalize: fatal error not nil", t, func() {
    49  		isReady := readyToFinalize(ctx, finalBuild, errors.New("Fatal Error Happened"), nil, outputFlag)
    50  		So(isReady, ShouldEqual, false)
    51  
    52  	})
    53  }
    54  
    55  func TestBackFillTaskInfo(t *testing.T) {
    56  	Convey("backFillTaskInfo", t, func() {
    57  		ctx := lucictx.SetSwarming(context.Background(), &lucictx.Swarming{
    58  			Task: &lucictx.Task{
    59  				BotDimensions: []string{
    60  					"cpu:x86",
    61  					"cpu:x86-64",
    62  					"id:bot_id",
    63  					"gcp:google.com:chromecompute",
    64  				},
    65  			},
    66  		})
    67  
    68  		build := &bbpb.Build{
    69  			Infra: &bbpb.BuildInfra{
    70  				Swarming: &bbpb.BuildInfra_Swarming{},
    71  			},
    72  		}
    73  		input := clientInput{input: &bbpb.BBAgentArgs{Build: build}}
    74  
    75  		So(backFillTaskInfo(ctx, input), ShouldEqual, 0)
    76  		So(build.Infra.Swarming.BotDimensions, ShouldResembleProto, []*bbpb.StringPair{
    77  			{
    78  				Key:   "cpu",
    79  				Value: "x86",
    80  			},
    81  			{
    82  				Key:   "cpu",
    83  				Value: "x86-64",
    84  			},
    85  			{
    86  				Key:   "id",
    87  				Value: "bot_id",
    88  			},
    89  			{
    90  				Key:   "gcp",
    91  				Value: "google.com:chromecompute",
    92  			},
    93  		})
    94  	})
    95  }
    96  
    97  func TestDownloadInputs(t *testing.T) {
    98  	resultsFilePath = filepath.Join(t.TempDir(), "cipd_ensure_results.json")
    99  	build := &bbpb.Build{
   100  		Id: 123,
   101  		Infra: &bbpb.BuildInfra{
   102  			Buildbucket: &bbpb.BuildInfra_Buildbucket{
   103  				Agent: &bbpb.BuildInfra_Buildbucket_Agent{
   104  					Input: &bbpb.BuildInfra_Buildbucket_Agent_Input{
   105  						CipdSource: map[string]*bbpb.InputDataRef{
   106  							"cipddir": {
   107  								DataType: &bbpb.InputDataRef_Cipd{
   108  									Cipd: &bbpb.InputDataRef_CIPD{
   109  										Server: "chrome-infra-packages.appspot.com",
   110  										Specs: []*bbpb.InputDataRef_CIPD_PkgSpec{
   111  											{
   112  												Package: "infra/tools/cipd/${platform}",
   113  												Version: "latest",
   114  											},
   115  										},
   116  									},
   117  								},
   118  							},
   119  						},
   120  						Data: map[string]*bbpb.InputDataRef{
   121  							"path_a": {
   122  								DataType: &bbpb.InputDataRef_Cipd{
   123  									Cipd: &bbpb.InputDataRef_CIPD{
   124  										Specs: []*bbpb.InputDataRef_CIPD_PkgSpec{{Package: "pkg_a", Version: "latest"}},
   125  									},
   126  								},
   127  								OnPath: []string{"path_a/bin", "path_a"},
   128  							},
   129  						},
   130  					},
   131  					Output: &bbpb.BuildInfra_Buildbucket_Agent_Output{},
   132  				},
   133  			},
   134  		},
   135  		Input: &bbpb.Build_Input{
   136  			Experiments: []string{"luci.buildbucket.agent.cipd_installation"},
   137  		},
   138  		CancelTime: nil,
   139  	}
   140  
   141  	Convey("downloadCipdPackages", t, func(c C) {
   142  		Convey("success", func() {
   143  			testCase = "success"
   144  
   145  			ctx := memory.Use(context.Background())
   146  			ctx = memlogger.Use(ctx)
   147  			execCommandContext = fakeExecCommand
   148  			defer func() { execCommandContext = exec.CommandContext }()
   149  			tempDir := t.TempDir()
   150  
   151  			bbclient := &testBBClient{}
   152  			input := &bbpb.BBAgentArgs{Build: build}
   153  			rc := downloadInputs(ctx, tempDir, "cache", clientInput{bbclient, input})
   154  
   155  			So(rc, ShouldEqual, 0)
   156  			So(len(bbclient.requests), ShouldEqual, 2)
   157  			So(bbclient.requests[0].Build.Infra.Buildbucket.Agent.Output.Status, ShouldEqual, bbpb.Status_STARTED)
   158  			So(bbclient.requests[1].Build.Infra.Buildbucket.Agent.Output.Status, ShouldEqual, bbpb.Status_SUCCESS)
   159  		})
   160  	})
   161  
   162  }
   163  
   164  func TestStartBuild(t *testing.T) {
   165  	Convey("startBuild", t, func() {
   166  		ctx := context.Background()
   167  		Convey("pass", func() {
   168  			bbclient := &testBBClient{}
   169  			res, err := startBuild(ctx, bbclient, 87654321, "pass")
   170  			So(err, ShouldBeNil)
   171  			So(res, ShouldNotBeNil)
   172  		})
   173  
   174  		Convey("duplicate", func() {
   175  			bbclient := &testBBClient{}
   176  			res, err := startBuild(ctx, bbclient, 87654321, "duplicate")
   177  			So(res, ShouldBeNil)
   178  			So(buildbucket.DuplicateTask.In(err), ShouldBeTrue)
   179  		})
   180  	})
   181  }
   182  
   183  func TestChooseCacheDir(t *testing.T) {
   184  	input := &bbpb.BBAgentArgs{
   185  		Build:    &bbpb.Build{},
   186  		CacheDir: "inputCacheDir",
   187  	}
   188  
   189  	Convey("use input.CacheDir no backend", t, func() {
   190  		cacheDir := chooseCacheDir(input, "")
   191  		So(cacheDir, ShouldEqual, "inputCacheDir")
   192  
   193  	})
   194  
   195  	Convey("use input.CacheDir backend exists", t, func() {
   196  		input.Build.Infra = &bbpb.BuildInfra{
   197  			Backend: &bbpb.BuildInfra_Backend{},
   198  		}
   199  		cacheDir := chooseCacheDir(input, "")
   200  		So(cacheDir, ShouldEqual, "inputCacheDir")
   201  
   202  	})
   203  
   204  	Convey("use cache-base flag backend exists", t, func() {
   205  		input.Build.Infra = &bbpb.BuildInfra{
   206  			Backend: &bbpb.BuildInfra_Backend{},
   207  		}
   208  		cacheDir := chooseCacheDir(input, "cache")
   209  		So(cacheDir, ShouldEqual, "cache")
   210  
   211  	})
   212  }