go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/rdb/rdb_test.go (about) 1 // Copyright 2023 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 rdb 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/golang/mock/gomock" 22 . "github.com/smartystreets/goconvey/convey" 23 "google.golang.org/protobuf/types/known/emptypb" 24 25 "google.golang.org/grpc/codes" 26 27 "go.chromium.org/luci/common/errors" 28 "go.chromium.org/luci/common/proto" 29 "go.chromium.org/luci/common/retry/transient" 30 . "go.chromium.org/luci/common/testing/assertions" 31 "go.chromium.org/luci/cv/internal/cvtesting" 32 "go.chromium.org/luci/grpc/appstatus" 33 rdbpb "go.chromium.org/luci/resultdb/proto/v1" 34 ) 35 36 func TestRecorderClient(t *testing.T) { 37 Convey(`MarkInvocationSubmitted`, t, func() { 38 ct := cvtesting.Test{} 39 ctx, cancel := ct.SetUp(t) 40 defer cancel() 41 42 mcf := NewMockRecorderClientFactory(ct.GoMockCtl) 43 rc, err := mcf.MakeClient(ctx, "rdbhost") 44 So(err, ShouldBeNil) 45 46 Convey(`OK`, func() { 47 inv := "invocations/build-100001" 48 mcf.mc.EXPECT().MarkInvocationSubmitted(gomock.Any(), proto.MatcherEqual(&rdbpb.MarkInvocationSubmittedRequest{ 49 Invocation: inv, 50 })).Return(&emptypb.Empty{}, nil) 51 52 err := rc.MarkInvocationSubmitted(ctx, inv) 53 So(err, ShouldBeNil) 54 }) 55 56 Convey(`Permission Denied`, func() { 57 inv := "invocations/build-100001" 58 mcf.mc.EXPECT().MarkInvocationSubmitted(gomock.Any(), proto.MatcherEqual(&rdbpb.MarkInvocationSubmittedRequest{ 59 Invocation: inv, 60 })).Return(&emptypb.Empty{}, appstatus.Error(codes.PermissionDenied, "permission denied")) 61 62 err := rc.MarkInvocationSubmitted(ctx, inv) 63 So(err, ShouldHaveAppStatus, codes.PermissionDenied) 64 So(err, ShouldErrLike, fmt.Sprintf("failed to mark %s submitted", inv)) 65 So(transient.Tag.In(err), ShouldBeFalse) 66 }) 67 68 Convey(`Invalid Argument`, func() { 69 inv := "invocations/build-100001" 70 mcf.mc.EXPECT().MarkInvocationSubmitted(gomock.Any(), proto.MatcherEqual(&rdbpb.MarkInvocationSubmittedRequest{ 71 Invocation: inv, 72 })).Return(&emptypb.Empty{}, appstatus.Error(codes.InvalidArgument, "invalid argument")) 73 74 err := rc.MarkInvocationSubmitted(ctx, inv) 75 So(err, ShouldHaveAppStatus, codes.InvalidArgument) 76 So(transient.Tag.In(err), ShouldBeFalse) 77 So(err, ShouldErrLike, fmt.Sprintf("failed to mark %s submitted", inv)) 78 }) 79 80 Convey(`No Code`, func() { 81 inv := "invocations/build-100001" 82 mcf.mc.EXPECT().MarkInvocationSubmitted(gomock.Any(), proto.MatcherEqual(&rdbpb.MarkInvocationSubmittedRequest{ 83 Invocation: inv, 84 })).Return(&emptypb.Empty{}, errors.New("random error")) 85 86 err := rc.MarkInvocationSubmitted(ctx, inv) 87 _, ok := appstatus.Get(err) 88 So(ok, ShouldBeFalse) 89 So(transient.Tag.In(err), ShouldBeFalse) 90 So(err, ShouldErrLike, "random error") 91 }) 92 93 Convey(`Transient Error`, func() { 94 inv := "invocations/build-100001" 95 mcf.mc.EXPECT().MarkInvocationSubmitted(gomock.Any(), proto.MatcherEqual(&rdbpb.MarkInvocationSubmittedRequest{ 96 Invocation: inv, 97 })).Return(&emptypb.Empty{}, appstatus.Error(codes.Unknown, "???")) 98 99 err := rc.MarkInvocationSubmitted(ctx, inv) 100 So(err, ShouldHaveAppStatus, codes.Unknown) 101 So(transient.Tag.In(err), ShouldBeTrue) 102 So(err, ShouldErrLike, "???") 103 }) 104 }) 105 }