go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/scheduler/appengine/task/buildbucket/buildbucket_fakes_test.go (about) 1 // Copyright 2021 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 buildbucket 16 17 import ( 18 "context" 19 "net/http/httptest" 20 21 bbpb "go.chromium.org/luci/buildbucket/proto" 22 "go.chromium.org/luci/grpc/prpc" 23 "go.chromium.org/luci/server/router" 24 ) 25 26 type BuildbucketFake struct { 27 ScheduleBuild func(*bbpb.ScheduleBuildRequest) (*bbpb.Build, error) 28 GetBuild func(*bbpb.GetBuildRequest) (*bbpb.Build, error) 29 CancelBuild func(*bbpb.CancelBuildRequest) (*bbpb.Build, error) 30 31 srv *httptest.Server 32 } 33 34 type fakeBuildsServer struct { 35 bbpb.UnimplementedBuildsServer 36 37 fake *BuildbucketFake 38 } 39 40 func (f *fakeBuildsServer) ScheduleBuild(ctx context.Context, req *bbpb.ScheduleBuildRequest) (*bbpb.Build, error) { 41 return f.fake.ScheduleBuild(req) 42 } 43 44 func (f *fakeBuildsServer) GetBuild(ctx context.Context, req *bbpb.GetBuildRequest) (*bbpb.Build, error) { 45 return f.fake.GetBuild(req) 46 } 47 48 func (f *fakeBuildsServer) CancelBuild(ctx context.Context, req *bbpb.CancelBuildRequest) (*bbpb.Build, error) { 49 return f.fake.CancelBuild(req) 50 } 51 52 func (bb *BuildbucketFake) Start() { 53 r := router.New() 54 55 rpcs := &prpc.Server{} 56 rpcs.InstallHandlers(r, nil) 57 bbpb.RegisterBuildsServer(rpcs, &fakeBuildsServer{fake: bb}) 58 59 bb.srv = httptest.NewServer(r) 60 } 61 62 func (bb *BuildbucketFake) Stop() { 63 bb.srv.Close() 64 } 65 66 func (bb *BuildbucketFake) URL() string { 67 return bb.srv.URL 68 }