go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/services/recorder/finalize_invocation_test.go (about)

     1  // Copyright 2019 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 recorder
    16  
    17  import (
    18  	"testing"
    19  
    20  	"google.golang.org/grpc/metadata"
    21  	"google.golang.org/protobuf/reflect/protoreflect"
    22  
    23  	"go.chromium.org/luci/server/span"
    24  	"go.chromium.org/luci/server/tq"
    25  
    26  	"go.chromium.org/luci/resultdb/internal/invocations"
    27  	"go.chromium.org/luci/resultdb/internal/tasks/taskspb"
    28  	"go.chromium.org/luci/resultdb/internal/testutil"
    29  	"go.chromium.org/luci/resultdb/internal/testutil/insert"
    30  	pb "go.chromium.org/luci/resultdb/proto/v1"
    31  
    32  	. "github.com/smartystreets/goconvey/convey"
    33  	. "go.chromium.org/luci/common/testing/assertions"
    34  )
    35  
    36  func TestValidateFinalizeInvocationRequest(t *testing.T) {
    37  	Convey(`TestValidateFinalizeInvocationRequest`, t, func() {
    38  		Convey(`Valid`, func() {
    39  			err := validateFinalizeInvocationRequest(&pb.FinalizeInvocationRequest{
    40  				Name: "invocations/a",
    41  			})
    42  			So(err, ShouldBeNil)
    43  		})
    44  
    45  		Convey(`Invalid name`, func() {
    46  			err := validateFinalizeInvocationRequest(&pb.FinalizeInvocationRequest{
    47  				Name: "x",
    48  			})
    49  			So(err, ShouldErrLike, `name: does not match`)
    50  		})
    51  	})
    52  }
    53  
    54  func TestFinalizeInvocation(t *testing.T) {
    55  	Convey(`TestFinalizeInvocation`, t, func() {
    56  		ctx := testutil.SpannerTestContext(t)
    57  		ctx, sched := tq.TestingContext(ctx, nil)
    58  		recorder := newTestRecorderServer()
    59  
    60  		token, err := generateInvocationToken(ctx, "inv")
    61  		So(err, ShouldBeNil)
    62  		ctx = metadata.NewIncomingContext(ctx, metadata.Pairs(pb.UpdateTokenMetadataKey, token))
    63  
    64  		Convey(`Idempotent`, func() {
    65  			testutil.MustApply(ctx, insert.Invocation("inv", pb.Invocation_ACTIVE, nil))
    66  
    67  			inv, err := recorder.FinalizeInvocation(ctx, &pb.FinalizeInvocationRequest{Name: "invocations/inv"})
    68  			So(err, ShouldBeNil)
    69  			So(inv.State, ShouldEqual, pb.Invocation_FINALIZING)
    70  
    71  			inv, err = recorder.FinalizeInvocation(ctx, &pb.FinalizeInvocationRequest{Name: "invocations/inv"})
    72  			So(err, ShouldBeNil)
    73  			So(inv.State, ShouldEqual, pb.Invocation_FINALIZING)
    74  
    75  			So(sched.Tasks(), ShouldHaveLength, 1)
    76  		})
    77  
    78  		Convey(`Success`, func() {
    79  			testutil.MustApply(ctx, insert.Invocation("inv", pb.Invocation_ACTIVE, nil))
    80  			inv, err := recorder.FinalizeInvocation(ctx, &pb.FinalizeInvocationRequest{Name: "invocations/inv"})
    81  			So(err, ShouldBeNil)
    82  			So(inv.State, ShouldEqual, pb.Invocation_FINALIZING)
    83  
    84  			// Read the invocation from Spanner to confirm it's really FINALIZING.
    85  			inv, err = invocations.Read(span.Single(ctx), "inv")
    86  			So(err, ShouldBeNil)
    87  			So(inv.State, ShouldEqual, pb.Invocation_FINALIZING)
    88  
    89  			// Enqueued the finalization task.
    90  			So(sched.Tasks().Payloads(), ShouldResembleProto, []protoreflect.ProtoMessage{
    91  				&taskspb.TryFinalizeInvocation{InvocationId: "inv"},
    92  			})
    93  		})
    94  	})
    95  }