github.com/hashicorp/vault/sdk@v0.11.0/helper/testcluster/docker/replication.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package docker
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"os"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/hashicorp/go-hclog"
    14  	"github.com/hashicorp/vault/sdk/helper/logging"
    15  	"github.com/hashicorp/vault/sdk/helper/testcluster"
    16  )
    17  
    18  func DefaultOptions(t *testing.T) *DockerClusterOptions {
    19  	return &DockerClusterOptions{
    20  		ImageRepo:   "hashicorp/vault",
    21  		ImageTag:    "latest",
    22  		VaultBinary: os.Getenv("VAULT_BINARY"),
    23  		ClusterOptions: testcluster.ClusterOptions{
    24  			NumCores:    3,
    25  			ClusterName: strings.ReplaceAll(t.Name(), "/", "-"),
    26  			VaultNodeConfig: &testcluster.VaultNodeConfig{
    27  				LogLevel: "TRACE",
    28  			},
    29  		},
    30  	}
    31  }
    32  
    33  func NewReplicationSetDocker(t *testing.T, opts *DockerClusterOptions) (*testcluster.ReplicationSet, error) {
    34  	binary := os.Getenv("VAULT_BINARY")
    35  	if binary == "" {
    36  		t.Skip("only running docker test when $VAULT_BINARY present")
    37  	}
    38  
    39  	r := &testcluster.ReplicationSet{
    40  		Clusters: map[string]testcluster.VaultCluster{},
    41  		Logger:   logging.NewVaultLogger(hclog.Trace).Named(t.Name()),
    42  	}
    43  
    44  	// clusterName is used for container name as well.
    45  	// A container name should not exceed 64 chars.
    46  	// There are additional chars that are added to the name as well
    47  	// like "-A-core0". So, setting a max limit for a cluster name.
    48  	if len(opts.ClusterName) > MaxClusterNameLength {
    49  		return nil, fmt.Errorf("cluster name length exceeded the maximum allowed length of %v", MaxClusterNameLength)
    50  	}
    51  
    52  	r.Builder = func(ctx context.Context, name string, baseLogger hclog.Logger) (testcluster.VaultCluster, error) {
    53  		myOpts := *opts
    54  		myOpts.Logger = baseLogger.Named(name)
    55  		if myOpts.ClusterName == "" {
    56  			myOpts.ClusterName = strings.ReplaceAll(t.Name(), "/", "-")
    57  		}
    58  		myOpts.ClusterName += "-" + strings.ReplaceAll(name, "/", "-")
    59  		myOpts.CA = r.CA
    60  		return NewTestDockerCluster(t, &myOpts), nil
    61  	}
    62  
    63  	a, err := r.Builder(context.TODO(), "A", r.Logger)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	r.Clusters["A"] = a
    68  	r.CA = a.(*DockerCluster).CA
    69  
    70  	return r, err
    71  }