go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/tryjob/execution_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 tryjob 16 17 import ( 18 "testing" 19 "time" 20 21 timestamppb "google.golang.org/protobuf/types/known/timestamppb" 22 23 "go.chromium.org/luci/gae/service/datastore" 24 25 "go.chromium.org/luci/cv/internal/common" 26 "go.chromium.org/luci/cv/internal/cvtesting" 27 28 . "github.com/smartystreets/goconvey/convey" 29 ) 30 31 func TestLoadExecutionLogs(t *testing.T) { 32 t.Parallel() 33 34 Convey("LoadExecutionLogs works", t, func() { 35 ct := cvtesting.Test{} 36 ctx, cancel := ct.SetUp(t) 37 defer cancel() 38 39 ev := int64(1) 40 put := func(runID common.RunID, entries ...*ExecutionLogEntry) { 41 So(datastore.Put(ctx, &executionLog{ 42 ID: ev, 43 Run: datastore.MakeKey(ctx, common.RunKind, string(runID)), 44 Entries: &ExecutionLogEntries{ 45 Entries: entries, 46 }, 47 }), ShouldBeNil) 48 ev += 1 49 } 50 51 var runID = common.MakeRunID("test_proj", ct.Clock.Now().UTC(), 1, []byte("deadbeef")) 52 53 put( 54 runID, 55 &ExecutionLogEntry{ 56 Time: timestamppb.New(ct.Clock.Now()), 57 Kind: &ExecutionLogEntry_TryjobsLaunched_{ 58 TryjobsLaunched: &ExecutionLogEntry_TryjobsLaunched{ 59 Tryjobs: []*ExecutionLogEntry_TryjobSnapshot{ 60 {Id: 1}, 61 }, 62 }, 63 }, 64 }, 65 &ExecutionLogEntry{ 66 Time: timestamppb.New(ct.Clock.Now()), 67 Kind: &ExecutionLogEntry_TryjobsLaunched_{ 68 TryjobsLaunched: &ExecutionLogEntry_TryjobsLaunched{ 69 Tryjobs: []*ExecutionLogEntry_TryjobSnapshot{ 70 {Id: 2}, 71 }, 72 }, 73 }, 74 }, 75 ) 76 ct.Clock.Add(time.Minute) 77 put( 78 runID, 79 &ExecutionLogEntry{ 80 Time: timestamppb.New(ct.Clock.Now()), 81 Kind: &ExecutionLogEntry_TryjobsEnded_{ 82 TryjobsEnded: &ExecutionLogEntry_TryjobsEnded{ 83 Tryjobs: []*ExecutionLogEntry_TryjobSnapshot{ 84 {Id: 1}, 85 }, 86 }, 87 }, 88 }, 89 ) 90 ct.Clock.Add(time.Minute) 91 put( 92 runID, 93 &ExecutionLogEntry{ 94 Time: timestamppb.New(ct.Clock.Now()), 95 Kind: &ExecutionLogEntry_TryjobsEnded_{ 96 TryjobsEnded: &ExecutionLogEntry_TryjobsEnded{ 97 Tryjobs: []*ExecutionLogEntry_TryjobSnapshot{ 98 {Id: 2}, 99 }, 100 }, 101 }, 102 }, 103 ) 104 105 ret, err := LoadExecutionLogs(ctx, runID) 106 So(err, ShouldBeNil) 107 So(ret, ShouldHaveLength, 4) 108 So(ret[0].GetTryjobsLaunched().GetTryjobs()[0].GetId(), ShouldEqual, 1) 109 So(ret[1].GetTryjobsLaunched().GetTryjobs()[0].GetId(), ShouldEqual, 2) 110 So(ret[2].GetTryjobsEnded().GetTryjobs()[0].GetId(), ShouldEqual, 1) 111 So(ret[3].GetTryjobsEnded().GetTryjobs()[0].GetId(), ShouldEqual, 2) 112 }) 113 }