blob: d286dacfd630abdf962612c3f0d5318059d4547b [file]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package common
import (
"context"
"testing"
"github.com/golang/mock/gomock"
structpb "github.com/golang/protobuf/ptypes/struct"
. "github.com/smartystreets/goconvey/convey"
buildbucketpb "go.chromium.org/luci/buildbucket/proto"
"go.chromium.org/luci/common/logging/memlogger"
"go.chromium.org/luci/gae/impl/memory"
"google.golang.org/grpc"
admin "infra/tricium/api/admin/v1"
tricium "infra/tricium/api/v1"
)
func TestTrigger(t *testing.T) {
Convey("Triggers a build", t, func() {
w := &admin.Worker{
Name: "AnalyzerName",
Impl: &admin.Worker_Recipe{
Recipe: &tricium.Recipe{
Project: "tricium",
Bucket: "try",
Builder: "analyzer",
},
},
}
params := &TriggerParameters{
Worker: w,
PubsubUserdata: "data",
Patch: PatchDetails{
GerritHost: "https://chromium-review.googlesource.com",
GerritProject: "chromium/src",
GerritChange: "chromium~master~I8473b95934b5732ac55d26311a706c9c2bde9940",
GerritCl: "12345",
GerritPatch: "2",
},
}
ctx := memory.Use(memlogger.Use(context.Background()))
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := buildbucketpb.NewMockBuildsClient(ctrl)
scheduleBuild := func(ctx context.Context, req *buildbucketpb.ScheduleBuildRequest, opts ...grpc.CallOption) (*buildbucketpb.Build, error) {
res := &buildbucketpb.Build{Id: 1}
return res, nil
}
client.EXPECT().
ScheduleBuild(gomock.Any(), gomock.Any()).
AnyTimes().
DoAndReturn(scheduleBuild)
result, err := trigger(ctx, params, client)
So(err, ShouldBeNil)
So(result.BuildID, ShouldEqual, 1)
})
Convey("Different analyzers have different request IDs", t, func() {
patch := &PatchDetails{
GerritHost: "cr.example.com",
GerritProject: "foo",
GerritCl: "123",
GerritPatch: "7",
}
recipeX := &tricium.Recipe{Project: "cr", Bucket: "try", Builder: "x-lint"}
recipeY := &tricium.Recipe{Project: "cr", Bucket: "try", Builder: "y-lint"}
So(makeRequestID(patch, recipeX), ShouldEqual, "foo~123~7~cr.try.x-lint")
So(makeRequestID(patch, recipeY), ShouldEqual, "foo~123~7~cr.try.y-lint")
})
Convey("Request IDs do not contain slash", t, func() {
patch := &PatchDetails{
GerritHost: "cr-review.goo.com",
GerritProject: "foo/bar",
GerritCl: "123",
GerritPatch: "7",
}
recipe := &tricium.Recipe{Project: "cr", Bucket: "try", Builder: "x"}
So(makeRequestID(patch, recipe), ShouldEqual, "foo_bar~123~7~cr.try.x")
})
}
func TestCollect(t *testing.T) {
Convey("Collects a build", t, func() {
params := &CollectParameters{
BuildID: 1,
}
ctx := memory.Use(memlogger.Use(context.Background()))
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := buildbucketpb.NewMockBuildsClient(ctrl)
getBuild := func(ctx context.Context, req *buildbucketpb.GetBuildRequest, opts ...grpc.CallOption) (*buildbucketpb.Build, error) {
res := &buildbucketpb.Build{
Id: 1,
Status: buildbucketpb.Status_SUCCESS,
Output: &buildbucketpb.Build_Output{
Properties: &structpb.Struct{
Fields: map[string]*structpb.Value{
"tricium": {
Kind: &structpb.Value_StringValue{StringValue: "{\"tricium\": []}"},
},
}},
},
}
return res, nil
}
client.EXPECT().
GetBuild(gomock.Any(), gomock.Any()).
AnyTimes().
DoAndReturn(getBuild)
result, err := collect(ctx, params, client)
So(err, ShouldBeNil)
So(result.State, ShouldEqual, Success)
So(result.BuildbucketOutput, ShouldEqual, "{\"tricium\": []}")
})
}