github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/acceptance/test_acceptance.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  // +build acceptance
    12  
    13  // Acceptance tests are comparatively slow to run, so we use the above build
    14  // tag to separate invocations of `go test` which are intended to run the
    15  // acceptance tests from those which are not. The corollary file to this one
    16  // is test_main.go
    17  
    18  package acceptance
    19  
    20  import (
    21  	"context"
    22  	"os"
    23  	"os/signal"
    24  	"testing"
    25  
    26  	"github.com/cockroachdb/cockroach/pkg/acceptance/cluster"
    27  	"github.com/cockroachdb/cockroach/pkg/security"
    28  	"github.com/cockroachdb/cockroach/pkg/security/securitytest"
    29  	"github.com/cockroachdb/cockroach/pkg/server"
    30  	"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
    31  	"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
    32  	"github.com/cockroachdb/cockroach/pkg/util/randutil"
    33  )
    34  
    35  func MainTest(m *testing.M) {
    36  	security.SetAssetLoader(securitytest.EmbeddedAssets)
    37  	serverutils.InitTestServerFactory(server.TestServerFactory)
    38  	serverutils.InitTestClusterFactory(testcluster.TestClusterFactory)
    39  	os.Exit(RunTests(m))
    40  }
    41  
    42  // RunTests runs the tests in a package while gracefully handling interrupts.
    43  func RunTests(m *testing.M) int {
    44  	randutil.SeedForTests()
    45  
    46  	ctx := context.Background()
    47  	defer cluster.GenerateCerts(ctx)()
    48  
    49  	go func() {
    50  		// Shut down tests when interrupted (for example CTRL+C).
    51  		sig := make(chan os.Signal, 1)
    52  		signal.Notify(sig, os.Interrupt)
    53  		<-sig
    54  		select {
    55  		case <-stopper.ShouldStop():
    56  		default:
    57  			// There is a very tiny race here: the cluster might be closing
    58  			// the stopper simultaneously.
    59  			stopper.Stop(ctx)
    60  		}
    61  	}()
    62  	return m.Run()
    63  }