github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/integration/log_integration_test.go (about)

     1  // Copyright 2016 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  	"flag"
    20  	"testing"
    21  	"time"
    22  
    23  	"google.golang.org/grpc"
    24  
    25  	"github.com/google/trillian"
    26  	"github.com/google/trillian/client"
    27  	"github.com/google/trillian/extension"
    28  	"github.com/google/trillian/quota"
    29  	"github.com/google/trillian/storage/memory"
    30  	"github.com/google/trillian/storage/testdb"
    31  	"github.com/google/trillian/testonly/integration"
    32  
    33  	_ "github.com/google/trillian/crypto/keys/der/proto" // Register PrivateKey ProtoHandler
    34  	stestonly "github.com/google/trillian/storage/testonly"
    35  )
    36  
    37  var treeIDFlag = flag.Int64("treeid", -1, "The tree id to use")
    38  var serverFlag = flag.String("log_rpc_server", "localhost:8092", "Server address:port")
    39  var queueLeavesFlag = flag.Bool("queue_leaves", true, "If true queues leaves, false just reads from the log")
    40  var awaitSequencingFlag = flag.Bool("await_sequencing", true, "If true then waits until log size is at least num_leaves")
    41  var checkLogEmptyFlag = flag.Bool("check_log_empty", true, "If true ensures log is empty before queuing anything")
    42  var startLeafFlag = flag.Int64("start_leaf", 0, "The first leaf index to use")
    43  var numLeavesFlag = flag.Int64("num_leaves", 1000, "The number of leaves to submit and read back")
    44  var queueBatchSizeFlag = flag.Int("queue_batch_size", 50, "Batch size when queueing leaves")
    45  var sequencerBatchSizeFlag = flag.Int("sequencing_batch_size", 100, "Batch size for server sequencer")
    46  var readBatchSizeFlag = flag.Int64("read_batch_size", 50, "Batch size when getting leaves by index")
    47  var waitForSequencingFlag = flag.Duration("wait_for_sequencing", time.Second*60, "How long to wait for leaves to be sequenced")
    48  var waitBetweenQueueChecksFlag = flag.Duration("queue_poll_wait", time.Second*5, "How frequently to check the queue while waiting")
    49  var rpcRequestDeadlineFlag = flag.Duration("rpc_deadline", time.Second*10, "Deadline to use for all RPC requests")
    50  var customLeafPrefixFlag = flag.String("custom_leaf_prefix", "", "Prefix string added to all queued leaves")
    51  
    52  func TestLiveLogIntegration(t *testing.T) {
    53  	flag.Parse()
    54  	if *treeIDFlag == -1 {
    55  		t.Skip("Log integration test skipped as no tree ID provided")
    56  	}
    57  
    58  	// Initialize and connect to log server
    59  	params := TestParameters{
    60  		treeID:              *treeIDFlag,
    61  		checkLogEmpty:       *checkLogEmptyFlag,
    62  		queueLeaves:         *queueLeavesFlag,
    63  		awaitSequencing:     *awaitSequencingFlag,
    64  		startLeaf:           *startLeafFlag,
    65  		leafCount:           *numLeavesFlag,
    66  		queueBatchSize:      *queueBatchSizeFlag,
    67  		sequencerBatchSize:  *sequencerBatchSizeFlag,
    68  		readBatchSize:       *readBatchSizeFlag,
    69  		sequencingWaitTotal: *waitForSequencingFlag,
    70  		sequencingPollWait:  *waitBetweenQueueChecksFlag,
    71  		rpcRequestDeadline:  *rpcRequestDeadlineFlag,
    72  		customLeafPrefix:    *customLeafPrefixFlag,
    73  	}
    74  	if params.startLeaf < 0 || params.leafCount <= 0 {
    75  		t.Fatalf("Start leaf index must be >= 0 (%d) and number of leaves must be > 0 (%d)", params.startLeaf, params.leafCount)
    76  	}
    77  
    78  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    79  	defer cancel()
    80  
    81  	// TODO: Other options apart from insecure connections
    82  	conn, err := grpc.DialContext(ctx, *serverFlag, grpc.WithInsecure())
    83  	if err != nil {
    84  		t.Fatalf("Failed to connect to log server: %v", err)
    85  	}
    86  	defer conn.Close()
    87  
    88  	client := trillian.NewTrillianLogClient(conn)
    89  	if err := RunLogIntegration(client, params); err != nil {
    90  		t.Fatalf("Test failed: %v", err)
    91  	}
    92  }
    93  
    94  func TestInProcessLogIntegration(t *testing.T) {
    95  	testdb.SkipIfNoMySQL(t)
    96  	ctx := context.Background()
    97  	const numSequencers = 2
    98  	env, err := integration.NewLogEnv(ctx, numSequencers, "unused")
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	defer env.Close()
   103  
   104  	tree, err := client.CreateAndInitTree(ctx, &trillian.CreateTreeRequest{
   105  		Tree: stestonly.LogTree,
   106  	}, env.Admin, nil, env.Log)
   107  	if err != nil {
   108  		t.Fatalf("Failed to create log: %v", err)
   109  	}
   110  
   111  	params := DefaultTestParameters(tree.TreeId)
   112  	if err := RunLogIntegration(env.Log, params); err != nil {
   113  		t.Fatalf("Test failed: %v", err)
   114  	}
   115  }
   116  
   117  func TestInProcessLogIntegrationDuplicateLeaves(t *testing.T) {
   118  	ctx := context.Background()
   119  	const numSequencers = 2
   120  	ms := memory.NewLogStorage(nil)
   121  
   122  	reggie := extension.Registry{
   123  		AdminStorage: memory.NewAdminStorage(ms),
   124  		LogStorage:   ms,
   125  		QuotaManager: quota.Noop(),
   126  	}
   127  
   128  	env, err := integration.NewLogEnvWithRegistry(ctx, numSequencers, reggie)
   129  	if err != nil {
   130  		t.Fatal(err)
   131  	}
   132  	defer env.Close()
   133  
   134  	tree, err := client.CreateAndInitTree(ctx, &trillian.CreateTreeRequest{
   135  		Tree: stestonly.LogTree,
   136  	}, env.Admin, nil, env.Log)
   137  	if err != nil {
   138  		t.Fatalf("Failed to create log: %v", err)
   139  	}
   140  
   141  	params := DefaultTestParameters(tree.TreeId)
   142  	params.uniqueLeaves = 10
   143  	if err := RunLogIntegration(env.Log, params); err != nil {
   144  		t.Fatalf("Test failed: %v", err)
   145  	}
   146  }