github.com/bartle-stripe/trillian@v1.2.1/testonly/mock_server.go (about)

     1  // Copyright 2018 Google Inc. All Rights Reserved.
     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 testonly
    16  
    17  import (
    18  	"net"
    19  
    20  	"github.com/golang/mock/gomock"
    21  	"github.com/google/trillian"
    22  	"github.com/google/trillian/testonly/tmock"
    23  	"google.golang.org/grpc"
    24  )
    25  
    26  // MockServer implements the TrillianAdminServer, the TrillianMapServer, and
    27  // TrillianLogServer.
    28  type MockServer struct {
    29  	Admin       *tmock.MockTrillianAdminServer
    30  	Log         *tmock.MockTrillianLogServer
    31  	Map         *tmock.MockTrillianMapServer
    32  	AdminClient trillian.TrillianAdminClient
    33  	LogClient   trillian.TrillianLogClient
    34  	MapClient   trillian.TrillianMapClient
    35  	Addr        string
    36  }
    37  
    38  // NewMockServer starts a server on a random port.
    39  // Returns the started server, the listener it's using for connection and a
    40  // close function that must be defer-called on the scope the server is meant to
    41  // stop.
    42  func NewMockServer(ctrl *gomock.Controller) (*MockServer, func(), error) {
    43  	grpcServer := grpc.NewServer()
    44  	mapServer := tmock.NewMockTrillianMapServer(ctrl)
    45  	logServer := tmock.NewMockTrillianLogServer(ctrl)
    46  	adminServer := tmock.NewMockTrillianAdminServer(ctrl)
    47  	trillian.RegisterTrillianMapServer(grpcServer, mapServer)
    48  	trillian.RegisterTrillianLogServer(grpcServer, logServer)
    49  	trillian.RegisterTrillianAdminServer(grpcServer, adminServer)
    50  
    51  	lis, err := net.Listen("tcp", "127.0.0.1:0")
    52  	if err != nil {
    53  		return nil, nil, err
    54  	}
    55  	go grpcServer.Serve(lis)
    56  
    57  	cc, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
    58  	if err != nil {
    59  		grpcServer.Stop()
    60  		lis.Close()
    61  		return nil, nil, err
    62  	}
    63  
    64  	stopFn := func() {
    65  		cc.Close()
    66  		grpcServer.Stop()
    67  		lis.Close()
    68  	}
    69  
    70  	return &MockServer{
    71  		Map:         mapServer,
    72  		Log:         logServer,
    73  		Admin:       adminServer,
    74  		MapClient:   trillian.NewTrillianMapClient(cc),
    75  		LogClient:   trillian.NewTrillianLogClient(cc),
    76  		AdminClient: trillian.NewTrillianAdminClient(cc),
    77  		Addr:        lis.Addr().String(),
    78  	}, stopFn, nil
    79  }