github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/main_test.go (about)

     1  // Copyright 2015 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package kvserver_test
    12  
    13  import (
    14  	"fmt"
    15  	"os"
    16  	"reflect"
    17  	"testing"
    18  
    19  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
    20  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    21  	"github.com/cockroachdb/cockroach/pkg/security"
    22  	"github.com/cockroachdb/cockroach/pkg/security/securitytest"
    23  	"github.com/cockroachdb/cockroach/pkg/server"
    24  	"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
    25  	"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
    26  	"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
    27  	"github.com/cockroachdb/cockroach/pkg/util/randutil"
    28  )
    29  
    30  //go:generate ../../util/leaktest/add-leaktest.sh *_test.go
    31  
    32  func init() {
    33  	security.SetAssetLoader(securitytest.EmbeddedAssets)
    34  }
    35  
    36  var verifyBelowRaftProtos bool
    37  
    38  func TestMain(m *testing.M) {
    39  	randutil.SeedForTests()
    40  	serverutils.InitTestServerFactory(server.TestServerFactory)
    41  	serverutils.InitTestClusterFactory(testcluster.TestClusterFactory)
    42  
    43  	// Create a set of all protos we believe to be marshaled downstream of raft.
    44  	// After the tests are run, we'll subtract the encountered protos from this
    45  	// set.
    46  	notBelowRaftProtos := make(map[reflect.Type]struct{}, len(belowRaftGoldenProtos))
    47  	for typ := range belowRaftGoldenProtos {
    48  		notBelowRaftProtos[typ] = struct{}{}
    49  	}
    50  
    51  	// Before running the tests, enable instrumentation that tracks protos which
    52  	// are marshaled downstream of raft.
    53  	stopTrackingAndGetTypes := kvserver.TrackRaftProtos()
    54  
    55  	code := m.Run()
    56  
    57  	// Only do this verification if the associated test was run. Without this
    58  	// condition, the verification here would spuriously fail when running a
    59  	// small subset of tests e.g. as we often do with `stress`.
    60  	if verifyBelowRaftProtos {
    61  		failed := false
    62  		// Retrieve all the observed downstream-of-raft protos and confirm that they
    63  		// are all present in our expected set.
    64  		for _, typ := range stopTrackingAndGetTypes() {
    65  			if _, ok := belowRaftGoldenProtos[typ]; ok {
    66  				delete(notBelowRaftProtos, typ)
    67  			} else {
    68  				failed = true
    69  				fmt.Printf("%s: missing fixture! Please adjust belowRaftGoldenProtos if necessary\n", typ)
    70  			}
    71  		}
    72  
    73  		// Confirm that our expected set is now empty; we don't want to cement any
    74  		// protos needlessly.
    75  		// Two exceptions: these are sometimes observed below raft, sometimes not.
    76  		// It supposedly depends on whether any test server runs for long enough to
    77  		// write internal time series.
    78  		delete(notBelowRaftProtos, reflect.TypeOf(&roachpb.InternalTimeSeriesData{}))
    79  		delete(notBelowRaftProtos, reflect.TypeOf(&enginepb.MVCCMetadataSubsetForMergeSerialization{}))
    80  		for typ := range notBelowRaftProtos {
    81  			failed = true
    82  			fmt.Printf("%s: not observed below raft!\n", typ)
    83  		}
    84  
    85  		// Make sure our error messages make it out.
    86  		if failed && code == 0 {
    87  			code = 1
    88  		}
    89  	}
    90  
    91  	os.Exit(code)
    92  }