go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/ingestion/join/invocation_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 join
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"google.golang.org/protobuf/types/known/timestamppb"
    22  
    23  	"go.chromium.org/luci/server/tq"
    24  
    25  	controlpb "go.chromium.org/luci/analysis/internal/ingestion/control/proto"
    26  	"go.chromium.org/luci/analysis/internal/tasks/taskspb"
    27  	"go.chromium.org/luci/analysis/internal/testutil"
    28  	pb "go.chromium.org/luci/analysis/proto/v1"
    29  
    30  	_ "go.chromium.org/luci/analysis/internal/services/resultingester" // Needed to ensure task class is registered.
    31  
    32  	. "github.com/smartystreets/goconvey/convey"
    33  	. "go.chromium.org/luci/common/testing/assertions"
    34  )
    35  
    36  func TestHandleInvocationFinalization(t *testing.T) {
    37  	Convey(`Test InvocationFinalizedPubSubHandler`, t, func() {
    38  		ctx := testutil.IntegrationTestContext(t)
    39  		ctx, skdr := tq.TestingContext(ctx, nil)
    40  
    41  		// Buildbucket timestamps are only in microsecond precision.
    42  		t := time.Now().Truncate(time.Nanosecond * 1000)
    43  
    44  		build := newBuildBuilder(6363636363).
    45  			WithCreateTime(t).
    46  			WithInvocation()
    47  
    48  		expectedTask := &taskspb.IngestTestResults{
    49  			PartitionTime: timestamppb.New(t),
    50  			Build:         build.ExpectedResult(),
    51  		}
    52  
    53  		assertTasksExpected := func() {
    54  			// Verify exactly one ingestion has been created.
    55  			So(len(skdr.Tasks().Payloads()), ShouldEqual, 1)
    56  			resultsTask := skdr.Tasks().Payloads()[0].(*taskspb.IngestTestResults)
    57  			So(resultsTask, ShouldResembleProto, expectedTask)
    58  		}
    59  
    60  		Convey(`Without build ingested previously`, func() {
    61  			So(ingestFinalization(ctx, build.buildID), ShouldBeNil)
    62  
    63  			So(len(skdr.Tasks().Payloads()), ShouldEqual, 0)
    64  		})
    65  		Convey(`With non-presubmit build ingested previously`, func() {
    66  			So(ingestBuild(ctx, build), ShouldBeNil)
    67  
    68  			So(len(skdr.Tasks().Payloads()), ShouldEqual, 0)
    69  
    70  			So(ingestFinalization(ctx, build.buildID), ShouldBeNil)
    71  
    72  			assertTasksExpected()
    73  
    74  			// Repeated messages should not trigger new ingestions.
    75  			So(ingestFinalization(ctx, build.buildID), ShouldBeNil)
    76  
    77  			assertTasksExpected()
    78  		})
    79  		Convey(`With presubmit build ingested previously`, func() {
    80  			build = build.WithTags([]string{"user_agent:cq"})
    81  			expectedTask.Build = build.ExpectedResult()
    82  			So(ingestBuild(ctx, build), ShouldBeNil)
    83  
    84  			So(len(skdr.Tasks().Payloads()), ShouldEqual, 0)
    85  
    86  			Convey(`With LUCI CV run ingested previously`, func() {
    87  				So(ingestCVRun(ctx, []int64{build.buildID}), ShouldBeNil)
    88  
    89  				expectedTask.PartitionTime = timestamppb.New(cvCreateTime)
    90  				expectedTask.PresubmitRun = &controlpb.PresubmitResult{
    91  					PresubmitRunId: &pb.PresubmitRunId{
    92  						System: "luci-cv",
    93  						Id:     "cvproject/123e4567-e89b-12d3-a456-426614174000",
    94  					},
    95  					Status:       pb.PresubmitRunStatus_PRESUBMIT_RUN_STATUS_FAILED,
    96  					Owner:        "automation",
    97  					Mode:         pb.PresubmitRunMode_FULL_RUN,
    98  					CreationTime: timestamppb.New(cvCreateTime),
    99  				}
   100  
   101  				So(len(skdr.Tasks().Payloads()), ShouldEqual, 0)
   102  
   103  				So(ingestFinalization(ctx, build.buildID), ShouldBeNil)
   104  
   105  				assertTasksExpected()
   106  
   107  				// Check metrics were reported correctly for this sequence.
   108  				isPresubmit := true
   109  				hasInvocation := true
   110  				So(bbBuildInputCounter.Get(ctx, "buildproject", isPresubmit, hasInvocation), ShouldEqual, 1)
   111  				So(bbBuildOutputCounter.Get(ctx, "buildproject", isPresubmit, hasInvocation), ShouldEqual, 1)
   112  				So(cvBuildInputCounter.Get(ctx, "cvproject"), ShouldEqual, 1)
   113  				So(cvBuildOutputCounter.Get(ctx, "cvproject"), ShouldEqual, 1)
   114  				So(rdbBuildInputCounter.Get(ctx, "invproject"), ShouldEqual, 1)
   115  				So(rdbBuildOutputCounter.Get(ctx, "invproject"), ShouldEqual, 1)
   116  			})
   117  			Convey(`Without LUCI CV run ingested previously`, func() {
   118  				So(ingestFinalization(ctx, build.buildID), ShouldBeNil)
   119  
   120  				So(len(skdr.Tasks().Payloads()), ShouldEqual, 0)
   121  			})
   122  		})
   123  	})
   124  }