github.com/rohankumardubey/nomad@v0.11.8/e2e/framework/provisioning/default.go (about)

     1  package provisioning
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	capi "github.com/hashicorp/consul/api"
     9  	napi "github.com/hashicorp/nomad/api"
    10  	"github.com/hashicorp/nomad/helper/uuid"
    11  	vapi "github.com/hashicorp/vault/api"
    12  )
    13  
    14  // DefaultProvisioner is a noop provisioner that builds clients from environment
    15  // variables according to the respective client configuration
    16  var DefaultProvisioner Provisioner = new(singleClusterProvisioner)
    17  
    18  type singleClusterProvisioner struct{}
    19  
    20  // SetupTestRun in the default case is a no-op.
    21  func (p *singleClusterProvisioner) SetupTestRun(t *testing.T, opts SetupOptions) (*ClusterInfo, error) {
    22  	return &ClusterInfo{}, nil
    23  }
    24  
    25  // SetupTestSuite in the default case is a no-op.
    26  func (p *singleClusterProvisioner) SetupTestSuite(t *testing.T, opts SetupOptions) (*ClusterInfo, error) {
    27  	return &ClusterInfo{}, nil
    28  }
    29  
    30  // SetupTestCase in the default case only creates new clients and embeds the
    31  // TestCase name into the ClusterInfo handle.
    32  func (p *singleClusterProvisioner) SetupTestCase(t *testing.T, opts SetupOptions) (*ClusterInfo, error) {
    33  	// Build ID based off given name
    34  	info := &ClusterInfo{
    35  		ID:   uuid.Generate()[:8],
    36  		Name: opts.Name,
    37  	}
    38  
    39  	// Build Nomad api client
    40  	nomadClient, err := napi.NewClient(napi.DefaultConfig())
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	info.NomadClient = nomadClient
    45  
    46  	if opts.ExpectConsul {
    47  		consulClient, err := capi.NewClient(capi.DefaultConfig())
    48  		if err != nil {
    49  			return nil, fmt.Errorf("expected Consul: %v", err)
    50  		}
    51  		info.ConsulClient = consulClient
    52  	}
    53  
    54  	if len(os.Getenv(vapi.EnvVaultAddress)) != 0 {
    55  		vaultClient, err := vapi.NewClient(vapi.DefaultConfig())
    56  		if err != nil && opts.ExpectVault {
    57  			return nil, err
    58  		}
    59  		info.VaultClient = vaultClient
    60  	} else if opts.ExpectVault {
    61  		return nil, fmt.Errorf("vault client expected but environment variable %s not set",
    62  			vapi.EnvVaultAddress)
    63  	}
    64  
    65  	return info, err
    66  }
    67  
    68  // all TearDown* methods of the default provisioner leave the test environment in place
    69  
    70  func (p *singleClusterProvisioner) TearDownTestCase(_ *testing.T, _ string) error  { return nil }
    71  func (p *singleClusterProvisioner) TearDownTestSuite(_ *testing.T, _ string) error { return nil }
    72  func (p *singleClusterProvisioner) TearDownTestRun(_ *testing.T, _ string) error   { return nil }