github.com/mre-fog/trillianxx@v1.1.2-0.20180615153820-ae375a99d36a/testonly/integration/mapenv.go (about)

     1  // Copyright 2017 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 integration
    16  
    17  import (
    18  	"context"
    19  	"database/sql"
    20  	"errors"
    21  
    22  	"github.com/golang/protobuf/proto"
    23  	"github.com/google/trillian"
    24  	"github.com/google/trillian/crypto/keys/der"
    25  	"github.com/google/trillian/crypto/keyspb"
    26  	"github.com/google/trillian/extension"
    27  	"github.com/google/trillian/monitoring"
    28  	"github.com/google/trillian/quota"
    29  	"github.com/google/trillian/server"
    30  	"github.com/google/trillian/server/admin"
    31  	"github.com/google/trillian/server/interceptor"
    32  	"github.com/google/trillian/storage/mysql"
    33  	"github.com/google/trillian/storage/testdb"
    34  	"google.golang.org/grpc"
    35  
    36  	_ "github.com/google/trillian/crypto/keys/der/proto" // Register PrivateKey ProtoHandler
    37  )
    38  
    39  // MapEnv is a map server and connected client.
    40  type MapEnv struct {
    41  	registry  extension.Registry
    42  	mapServer *server.TrillianMapServer
    43  
    44  	// Objects that need Close(), in order of creation.
    45  	DB         *sql.DB
    46  	grpcServer *grpc.Server
    47  	clientConn *grpc.ClientConn
    48  
    49  	// Public fields
    50  	Map   trillian.TrillianMapClient
    51  	Admin trillian.TrillianAdminClient
    52  }
    53  
    54  // NewMapEnvFromConn connects to a map server.
    55  func NewMapEnvFromConn(addr string) (*MapEnv, error) {
    56  	cc, err := grpc.Dial(addr, grpc.WithInsecure())
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return &MapEnv{
    62  		clientConn: cc,
    63  		Map:        trillian.NewTrillianMapClient(cc),
    64  		Admin:      trillian.NewTrillianAdminClient(cc),
    65  	}, nil
    66  }
    67  
    68  // NewMapEnv creates a fresh DB, map server, and client.
    69  func NewMapEnv(ctx context.Context) (*MapEnv, error) {
    70  	if !testdb.MySQLAvailable() {
    71  		return nil, errors.New("no MySQL available")
    72  	}
    73  
    74  	db, err := testdb.NewTrillianDB(ctx)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	registry := extension.Registry{
    80  		AdminStorage:  mysql.NewAdminStorage(db),
    81  		MapStorage:    mysql.NewMapStorage(db),
    82  		QuotaManager:  quota.Noop(),
    83  		MetricFactory: monitoring.InertMetricFactory{},
    84  		NewKeyProto: func(ctx context.Context, spec *keyspb.Specification) (proto.Message, error) {
    85  			return der.NewProtoFromSpec(spec)
    86  		},
    87  	}
    88  
    89  	ret, err := NewMapEnvWithRegistry(registry)
    90  	if err != nil {
    91  		db.Close()
    92  		return nil, err
    93  	}
    94  	ret.DB = db
    95  	return ret, nil
    96  }
    97  
    98  // NewMapEnvWithRegistry uses the passed in Registry to create a map server and
    99  // client.
   100  func NewMapEnvWithRegistry(registry extension.Registry) (*MapEnv, error) {
   101  	addr, lis, err := listen()
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	ti := interceptor.New(
   107  		registry.AdminStorage, registry.QuotaManager, false /* quotaDryRun */, registry.MetricFactory)
   108  	ci := interceptor.Combine(interceptor.ErrorWrapper, ti.UnaryInterceptor)
   109  
   110  	// Create Map Server.
   111  	grpcServer := grpc.NewServer(grpc.UnaryInterceptor(ci))
   112  	mapServer := server.NewTrillianMapServer(registry)
   113  	trillian.RegisterTrillianMapServer(grpcServer, mapServer)
   114  	trillian.RegisterTrillianAdminServer(grpcServer, admin.New(registry, nil /* allowedTreeTypes */))
   115  	go grpcServer.Serve(lis)
   116  
   117  	// Connect to the server.
   118  	cc, err := grpc.Dial(addr, grpc.WithInsecure())
   119  	if err != nil {
   120  		grpcServer.Stop()
   121  		return nil, err
   122  	}
   123  
   124  	return &MapEnv{
   125  		registry:   registry,
   126  		mapServer:  mapServer,
   127  		grpcServer: grpcServer,
   128  		clientConn: cc,
   129  		Map:        trillian.NewTrillianMapClient(cc),
   130  		Admin:      trillian.NewTrillianAdminClient(cc),
   131  	}, nil
   132  }
   133  
   134  // Close shuts down the server.
   135  func (env *MapEnv) Close() {
   136  	if env.clientConn != nil {
   137  		env.clientConn.Close()
   138  	}
   139  	if env.grpcServer != nil {
   140  		env.grpcServer.GracefulStop()
   141  	}
   142  	if env.DB != nil {
   143  		env.DB.Close()
   144  	}
   145  }