go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/led/job/jobexport/swarming_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 jobexport
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"flag"
    21  	"fmt"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/golang/protobuf/jsonpb"
    26  	. "github.com/smartystreets/goconvey/convey"
    27  
    28  	"go.chromium.org/luci/common/clock/testclock"
    29  	"go.chromium.org/luci/common/data/rand/cryptorand"
    30  	"go.chromium.org/luci/led/job"
    31  	swarmingpb "go.chromium.org/luci/swarming/proto/api_v2"
    32  )
    33  
    34  var train = flag.Bool("train", false, "If set, write testdata/*.swarm.json")
    35  
    36  func readTestFixture(fixtureBaseName string) *swarmingpb.NewTaskRequest {
    37  	jobFile, err := os.Open(fmt.Sprintf("testdata/%s.job.json", fixtureBaseName))
    38  	So(err, ShouldBeNil)
    39  	defer jobFile.Close()
    40  
    41  	jd := &job.Definition{}
    42  	So(jsonpb.Unmarshal(jobFile, jd), ShouldBeNil)
    43  	So(jd, ShouldNotBeNil)
    44  
    45  	ctx := cryptorand.MockForTest(context.Background(), 0)
    46  	ctx, _ = testclock.UseTime(ctx, testclock.TestTimeUTC)
    47  	So(jd.FlattenToSwarming(ctx, "testuser@example.com", "293109284abc", job.NoKitchenSupport(), "off"),
    48  		ShouldBeNil)
    49  
    50  	ret := jd.GetSwarming().GetTask()
    51  	So(err, ShouldBeNil)
    52  
    53  	outFile := fmt.Sprintf("testdata/%s.swarm.json", fixtureBaseName)
    54  	if *train {
    55  		oFile, err := os.Create(outFile)
    56  		So(err, ShouldBeNil)
    57  		defer oFile.Close()
    58  
    59  		enc := json.NewEncoder(oFile)
    60  		enc.SetIndent("", "  ")
    61  		So(enc.Encode(ret), ShouldBeNil)
    62  	} else {
    63  		current, err := os.ReadFile(outFile)
    64  		So(err, ShouldBeNil)
    65  
    66  		actual, err := json.MarshalIndent(ret, "", "  ")
    67  		So(err, ShouldBeNil)
    68  
    69  		So(string(actual)+"\n", ShouldEqual, string(current))
    70  	}
    71  
    72  	return ret
    73  }
    74  
    75  func TestExportRaw(t *testing.T) {
    76  	t.Parallel()
    77  
    78  	Convey(`export raw swarming task with rbe-cas input`, t, func() {
    79  		req := readTestFixture("raw_cas")
    80  		So(req, ShouldNotBeNil)
    81  	})
    82  }
    83  
    84  func TestExportBBagent(t *testing.T) {
    85  	t.Parallel()
    86  
    87  	Convey(`export bbagent task with rbe-cas input`, t, func() {
    88  		req := readTestFixture("bbagent_cas")
    89  		So(req, ShouldNotBeNil)
    90  	})
    91  }