go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/led/job/jobcreate/create_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 jobcreate 16 17 import ( 18 "bytes" 19 "context" 20 "encoding/json" 21 "flag" 22 "fmt" 23 "os" 24 "testing" 25 "time" 26 27 "github.com/golang/protobuf/jsonpb" 28 "google.golang.org/protobuf/types/known/durationpb" 29 30 bbpb "go.chromium.org/luci/buildbucket/proto" 31 "go.chromium.org/luci/led/job" 32 swarmingpb "go.chromium.org/luci/swarming/proto/api_v2" 33 34 . "github.com/smartystreets/goconvey/convey" 35 . "go.chromium.org/luci/common/testing/assertions" 36 ) 37 38 var train = flag.Bool("train", false, "If set, write testdata/*out.json") 39 40 func readTestFixture(fixtureBaseName string) *job.Definition { 41 data, err := os.ReadFile(fmt.Sprintf("testdata/%s.json", fixtureBaseName)) 42 So(err, ShouldBeNil) 43 44 req := &swarmingpb.NewTaskRequest{} 45 So(json.NewDecoder(bytes.NewReader(data)).Decode(req), ShouldBeNil) 46 47 jd, err := FromNewTaskRequest( 48 context.Background(), req, 49 "test_name", "swarming.example.com", 50 job.NoKitchenSupport(), 10, nil, nil, nil) 51 So(err, ShouldBeNil) 52 So(jd, ShouldNotBeNil) 53 54 outFile := fmt.Sprintf("testdata/%s.job.json", fixtureBaseName) 55 marshaler := &jsonpb.Marshaler{ 56 OrigName: true, 57 Indent: " ", 58 } 59 if *train { 60 oFile, err := os.Create(outFile) 61 So(err, ShouldBeNil) 62 defer oFile.Close() 63 64 So(marshaler.Marshal(oFile, jd), ShouldBeNil) 65 } else { 66 current, err := os.ReadFile(outFile) 67 So(err, ShouldBeNil) 68 69 actual, err := marshaler.MarshalToString(jd) 70 So(err, ShouldBeNil) 71 72 So(actual, ShouldEqual, string(current)) 73 } 74 75 return jd 76 } 77 78 func TestCreateSwarmRaw(t *testing.T) { 79 t.Parallel() 80 81 Convey(`consume non-buildbucket swarming task with RBE-CAS prop`, t, func() { 82 jd := readTestFixture("raw_cas") 83 84 So(jd.GetSwarming(), ShouldNotBeNil) 85 So(jd.Info().SwarmingHostname(), ShouldEqual, "swarming.example.com") 86 So(jd.Info().TaskName(), ShouldEqual, "led: test_name") 87 }) 88 89 Convey(`consume non-buildbucket swarming task with resultdb enabling`, t, func() { 90 jd := readTestFixture("raw_cas") 91 Convey(`realm unset`, func() { 92 So(jd.FlattenToSwarming(context.Background(), "username", "parent_task_id", job.NoKitchenSupport(), "on"), ShouldErrLike, 93 "ResultDB cannot be enabled on raw swarming tasks if the realm field is unset") 94 So(jd.GetSwarming().GetTask().GetResultdb().GetEnable(), ShouldBeFalse) 95 }) 96 Convey(`realm set`, func() { 97 jd.GetSwarming().GetTask().Realm = "some:realm" 98 So(jd.FlattenToSwarming(context.Background(), "username", "parent_task_id", job.NoKitchenSupport(), "on"), ShouldBeNil) 99 So(jd.GetSwarming().GetTask().GetResultdb().GetEnable(), ShouldBeTrue) 100 }) 101 }) 102 } 103 104 func TestCreateBBagent(t *testing.T) { 105 t.Parallel() 106 107 Convey(`consume bbagent buildbucket swarming task with RBE-CAS prop`, t, func() { 108 jd := readTestFixture("bbagent_cas") 109 110 So(jd.GetBuildbucket(), ShouldNotBeNil) 111 So(jd.Info().SwarmingHostname(), ShouldEqual, "chromium-swarm-dev.appspot.com") 112 So(jd.Info().TaskName(), ShouldEqual, "led: test_name") 113 }) 114 115 Convey(`consume bbagent buildbucket swarming task with build`, t, func() { 116 bld := &bbpb.Build{ 117 Builder: &bbpb.BuilderID{ 118 Project: "project", 119 Bucket: "bucket", 120 Builder: "builder", 121 }, 122 Infra: &bbpb.BuildInfra{ 123 Bbagent: &bbpb.BuildInfra_BBAgent{ 124 PayloadPath: "path", 125 CacheDir: "dir", 126 KnownPublicGerritHosts: []string{"host"}, 127 }, 128 Swarming: &bbpb.BuildInfra_Swarming{ 129 Priority: 25, 130 }, 131 Buildbucket: &bbpb.BuildInfra_Buildbucket{ 132 KnownPublicGerritHosts: []string{"host"}, 133 }, 134 Logdog: &bbpb.BuildInfra_LogDog{}, 135 }, 136 Input: &bbpb.Build_Input{}, 137 Exe: &bbpb.Executable{}, 138 SchedulingTimeout: durationpb.New(time.Hour), 139 ExecutionTimeout: durationpb.New(2 * time.Hour), 140 GracePeriod: durationpb.New(time.Minute), 141 Tags: []*bbpb.StringPair{ 142 { 143 Key: "k", 144 Value: "v", 145 }, 146 }, 147 } 148 data, err := os.ReadFile(fmt.Sprintf("testdata/%s.json", "bbagent_cas")) 149 So(err, ShouldBeNil) 150 151 req := &swarmingpb.NewTaskRequest{} 152 So(json.NewDecoder(bytes.NewReader(data)).Decode(req), ShouldBeNil) 153 req.TaskSlices[0].Properties.Command = []string{ 154 "bbagent${EXECUTABLE_SUFFIX}", 155 "-host", 156 "cr-buildbucket.appspot.com", 157 "-build-id", 158 "123"} 159 160 jd, err := FromNewTaskRequest( 161 context.Background(), req, 162 "test_name", "swarming.example.com", 163 job.NoKitchenSupport(), 10, bld, []string{"k:v"}, nil) 164 So(err, ShouldBeNil) 165 So(jd, ShouldNotBeNil) 166 167 So(jd.GetBuildbucket(), ShouldNotBeNil) 168 So(jd.Info().TaskName(), ShouldEqual, "led: test_name") 169 So(jd.GetBuildbucket().BbagentArgs, ShouldResembleProto, &bbpb.BBAgentArgs{ 170 PayloadPath: bld.Infra.Bbagent.PayloadPath, 171 CacheDir: bld.Infra.Bbagent.CacheDir, 172 KnownPublicGerritHosts: bld.Infra.Bbagent.KnownPublicGerritHosts, 173 Build: bld, 174 }) 175 }) 176 177 Convey(`consume bbagent buildbucket swarming task led job`, t, func() { 178 jd := readTestFixture("bbagent_led") 179 180 So(jd.GetBuildbucket(), ShouldNotBeNil) 181 So(jd.Info().SwarmingHostname(), ShouldEqual, "chromium-swarm-dev.appspot.com") 182 So(jd.Info().TaskName(), ShouldEqual, "led: test_name") 183 }) 184 }