2018-06-28 17:29:26 +00:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
capi "github.com/hashicorp/consul/api"
|
|
|
|
napi "github.com/hashicorp/nomad/api"
|
2018-07-24 16:33:30 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2018-06-28 17:29:26 +00:00
|
|
|
vapi "github.com/hashicorp/vault/api"
|
|
|
|
)
|
|
|
|
|
2018-06-30 02:53:59 +00:00
|
|
|
// ProvisionerOptions defines options to be given to the Provisioner when calling
|
|
|
|
// ProvisionCluster
|
2018-06-28 17:29:26 +00:00
|
|
|
type ProvisionerOptions struct {
|
2018-06-30 02:53:59 +00:00
|
|
|
Name string
|
|
|
|
ExpectConsul bool // If true, fails if a Consul client can't be configured
|
|
|
|
ExpectVault bool // If true, fails if a Vault client can't be configured
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ClusterInfo struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
NomadClient *napi.Client
|
|
|
|
ConsulClient *capi.Client
|
|
|
|
VaultClient *vapi.Client
|
|
|
|
}
|
|
|
|
|
2018-06-30 02:53:59 +00:00
|
|
|
// Provisioner interface is used by the test framework to provision a Nomad
|
|
|
|
// cluster for each TestCase
|
2018-06-28 17:29:26 +00:00
|
|
|
type Provisioner interface {
|
|
|
|
ProvisionCluster(opts ProvisionerOptions) (*ClusterInfo, error)
|
|
|
|
DestroyCluster(clusterID string) error
|
|
|
|
}
|
|
|
|
|
2018-06-30 02:53:59 +00:00
|
|
|
// DefaultProvisioner is a noop provisioner that builds clients from environment
|
|
|
|
// variables according to the respective client configuration
|
2018-06-28 17:29:26 +00:00
|
|
|
var DefaultProvisioner Provisioner = new(singleClusterProvisioner)
|
|
|
|
|
|
|
|
type singleClusterProvisioner struct{}
|
|
|
|
|
|
|
|
func (p *singleClusterProvisioner) ProvisionCluster(opts ProvisionerOptions) (*ClusterInfo, error) {
|
2018-06-30 02:53:59 +00:00
|
|
|
// Build ID based off given name
|
2018-06-28 17:29:26 +00:00
|
|
|
info := &ClusterInfo{
|
2018-07-24 16:33:30 +00:00
|
|
|
ID: uuid.Generate()[:8],
|
2018-06-28 17:29:26 +00:00
|
|
|
Name: opts.Name,
|
|
|
|
}
|
2018-06-29 02:58:57 +00:00
|
|
|
|
2018-06-30 02:53:59 +00:00
|
|
|
// Build Nomad api client
|
2018-06-29 02:58:57 +00:00
|
|
|
nomadClient, err := napi.NewClient(napi.DefaultConfig())
|
2018-06-28 17:29:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info.NomadClient = nomadClient
|
|
|
|
|
2019-01-26 05:06:50 +00:00
|
|
|
if opts.ExpectConsul {
|
2018-06-29 02:58:57 +00:00
|
|
|
consulClient, err := capi.NewClient(capi.DefaultConfig())
|
2018-06-30 02:53:59 +00:00
|
|
|
if err != nil && opts.ExpectConsul {
|
2018-06-29 02:58:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info.ConsulClient = consulClient
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(os.Getenv(vapi.EnvVaultAddress)) != 0 {
|
|
|
|
vaultClient, err := vapi.NewClient(vapi.DefaultConfig())
|
2018-06-30 02:53:59 +00:00
|
|
|
if err != nil && opts.ExpectVault {
|
2018-06-29 02:58:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info.VaultClient = vaultClient
|
2018-06-30 02:53:59 +00:00
|
|
|
} else if opts.ExpectVault {
|
|
|
|
return nil, fmt.Errorf("vault client expected but environment variable %s not set",
|
|
|
|
vapi.EnvVaultAddress)
|
2018-06-29 02:58:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 17:29:26 +00:00
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *singleClusterProvisioner) DestroyCluster(_ string) error {
|
|
|
|
//Maybe try to GC things based on id?
|
|
|
|
return nil
|
|
|
|
}
|