go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/app/resultdb_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 app
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"io"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"testing"
    24  
    25  	"google.golang.org/protobuf/encoding/protojson"
    26  
    27  	"go.chromium.org/luci/common/tsmon"
    28  	resultpb "go.chromium.org/luci/resultdb/proto/v1"
    29  	"go.chromium.org/luci/server/router"
    30  
    31  	_ "go.chromium.org/luci/analysis/internal/services/resultingester" // Needed to ensure task class is registered.
    32  
    33  	. "github.com/smartystreets/goconvey/convey"
    34  	. "go.chromium.org/luci/common/testing/assertions"
    35  )
    36  
    37  func TestInvocationFinalizedHandler(t *testing.T) {
    38  	Convey(`Test InvocationFinalizedHandler`, t, func() {
    39  		ctx, _ := tsmon.WithDummyInMemory(context.Background())
    40  
    41  		h := &InvocationFinalizedHandler{}
    42  		rsp := httptest.NewRecorder()
    43  		rctx := &router.Context{
    44  			Writer: rsp,
    45  		}
    46  
    47  		Convey(`Valid message`, func() {
    48  			called := false
    49  			var processed bool
    50  			h.handleInvocation = func(ctx context.Context, notification *resultpb.InvocationFinalizedNotification) (bool, error) {
    51  				So(called, ShouldBeFalse)
    52  				So(notification, ShouldResembleProto, &resultpb.InvocationFinalizedNotification{
    53  					Invocation: "invocations/build-6363636363",
    54  					Realm:      "invproject:realm",
    55  				})
    56  				called = true
    57  				return processed, nil
    58  			}
    59  
    60  			// Process invocation finalization.
    61  			rctx.Request = (&http.Request{Body: makeInvocationFinalizedReq(6363636363, "invproject:realm")}).WithContext(ctx)
    62  
    63  			Convey(`Processed`, func() {
    64  				processed = true
    65  
    66  				h.Handle(rctx)
    67  				So(rsp.Code, ShouldEqual, http.StatusOK)
    68  				So(invocationsFinalizedCounter.Get(ctx, "invproject", "success"), ShouldEqual, 1)
    69  				So(called, ShouldBeTrue)
    70  			})
    71  			Convey(`Not processed`, func() {
    72  				processed = false
    73  
    74  				h.Handle(rctx)
    75  				So(rsp.Code, ShouldEqual, http.StatusNoContent)
    76  				So(invocationsFinalizedCounter.Get(ctx, "invproject", "ignored"), ShouldEqual, 1)
    77  				So(called, ShouldBeTrue)
    78  			})
    79  		})
    80  		Convey(`Invalid message`, func() {
    81  			h.handleInvocation = func(ctx context.Context, notification *resultpb.InvocationFinalizedNotification) (bool, error) {
    82  				panic("Should not be reached.")
    83  			}
    84  
    85  			rctx.Request = (&http.Request{Body: makeReq([]byte("Hello"), nil)}).WithContext(ctx)
    86  
    87  			h.Handle(rctx)
    88  			So(rsp.Code, ShouldEqual, http.StatusAccepted)
    89  			So(invocationsFinalizedCounter.Get(ctx, "unknown", "permanent-failure"), ShouldEqual, 1)
    90  		})
    91  	})
    92  }
    93  
    94  func makeInvocationFinalizedReq(buildID int64, realm string) io.ReadCloser {
    95  	blob, _ := protojson.Marshal(&resultpb.InvocationFinalizedNotification{
    96  		Invocation: fmt.Sprintf("invocations/build-%v", buildID),
    97  		Realm:      realm,
    98  	})
    99  	return makeReq(blob, nil)
   100  }