github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/e2e/framework/provisioner.go (about)

     1  package framework
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	capi "github.com/hashicorp/consul/api"
     8  	napi "github.com/hashicorp/nomad/api"
     9  	"github.com/hashicorp/nomad/helper/uuid"
    10  	vapi "github.com/hashicorp/vault/api"
    11  )
    12  
    13  // ProvisionerOptions defines options to be given to the Provisioner when calling
    14  // ProvisionCluster
    15  type ProvisionerOptions struct {
    16  	Name         string
    17  	ExpectConsul bool // If true, fails if a Consul client can't be configured
    18  	ExpectVault  bool // If true, fails if a Vault client can't be configured
    19  }
    20  
    21  type ClusterInfo struct {
    22  	ID           string
    23  	Name         string
    24  	NomadClient  *napi.Client
    25  	ConsulClient *capi.Client
    26  	VaultClient  *vapi.Client
    27  }
    28  
    29  // Provisioner interface is used by the test framework to provision a Nomad
    30  // cluster for each TestCase
    31  type Provisioner interface {
    32  	ProvisionCluster(opts ProvisionerOptions) (*ClusterInfo, error)
    33  	DestroyCluster(clusterID string) error
    34  }
    35  
    36  // DefaultProvisioner is a noop provisioner that builds clients from environment
    37  // variables according to the respective client configuration
    38  var DefaultProvisioner Provisioner = new(singleClusterProvisioner)
    39  
    40  type singleClusterProvisioner struct{}
    41  
    42  func (p *singleClusterProvisioner) ProvisionCluster(opts ProvisionerOptions) (*ClusterInfo, error) {
    43  	// Build ID based off given name
    44  	info := &ClusterInfo{
    45  		ID:   uuid.Generate()[:8],
    46  		Name: opts.Name,
    47  	}
    48  
    49  	// Build Nomad api client
    50  	nomadClient, err := napi.NewClient(napi.DefaultConfig())
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	info.NomadClient = nomadClient
    55  
    56  	if len(os.Getenv(capi.HTTPAddrEnvName)) != 0 {
    57  		consulClient, err := capi.NewClient(capi.DefaultConfig())
    58  		if err != nil && opts.ExpectConsul {
    59  			return nil, err
    60  		}
    61  		info.ConsulClient = consulClient
    62  	} else if opts.ExpectConsul {
    63  		return nil, fmt.Errorf("consul client expected but environment variable %s not set",
    64  			capi.HTTPAddrEnvName)
    65  	}
    66  
    67  	if len(os.Getenv(vapi.EnvVaultAddress)) != 0 {
    68  		vaultClient, err := vapi.NewClient(vapi.DefaultConfig())
    69  		if err != nil && opts.ExpectVault {
    70  			return nil, err
    71  		}
    72  		info.VaultClient = vaultClient
    73  	} else if opts.ExpectVault {
    74  		return nil, fmt.Errorf("vault client expected but environment variable %s not set",
    75  			vapi.EnvVaultAddress)
    76  	}
    77  
    78  	return info, err
    79  }
    80  
    81  func (p *singleClusterProvisioner) DestroyCluster(_ string) error {
    82  	//Maybe try to GC things based on id?
    83  	return nil
    84  }