1// Copyright 2020 Google LLC 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// https://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 15package testutil 16 17import ( 18 "context" 19 20 "github.com/golang/protobuf/proto" 21 instancepb "google.golang.org/genproto/googleapis/spanner/admin/instance/v1" 22) 23 24// InMemInstanceAdminServer contains the InstanceAdminServer interface plus a couple 25// of specific methods for setting mocked results. 26type InMemInstanceAdminServer interface { 27 instancepb.InstanceAdminServer 28 Stop() 29 Resps() []proto.Message 30 SetResps([]proto.Message) 31 Reqs() []proto.Message 32 SetReqs([]proto.Message) 33 SetErr(error) 34} 35 36// inMemInstanceAdminServer implements InMemInstanceAdminServer interface. Note that 37// there is no mutex protecting the data structures, so it is not safe for 38// concurrent use. 39type inMemInstanceAdminServer struct { 40 instancepb.InstanceAdminServer 41 reqs []proto.Message 42 // If set, all calls return this error 43 err error 44 // responses to return if err == nil 45 resps []proto.Message 46} 47 48// NewInMemInstanceAdminServer creates a new in-mem test server. 49func NewInMemInstanceAdminServer() InMemInstanceAdminServer { 50 res := &inMemInstanceAdminServer{} 51 return res 52} 53 54// GetInstance returns the metadata of a spanner instance. 55func (s *inMemInstanceAdminServer) GetInstance(ctx context.Context, req *instancepb.GetInstanceRequest) (*instancepb.Instance, error) { 56 s.reqs = append(s.reqs, req) 57 if s.err != nil { 58 defer func() { s.err = nil }() 59 return nil, s.err 60 } 61 return s.resps[0].(*instancepb.Instance), nil 62} 63 64func (s *inMemInstanceAdminServer) Stop() { 65 // do nothing 66} 67 68func (s *inMemInstanceAdminServer) Resps() []proto.Message { 69 return s.resps 70} 71 72func (s *inMemInstanceAdminServer) SetResps(resps []proto.Message) { 73 s.resps = resps 74} 75 76func (s *inMemInstanceAdminServer) Reqs() []proto.Message { 77 return s.reqs 78} 79 80func (s *inMemInstanceAdminServer) SetReqs(reqs []proto.Message) { 81 s.reqs = reqs 82} 83 84func (s *inMemInstanceAdminServer) SetErr(err error) { 85 s.err = err 86} 87