e2e/framework: cleanup provisioner and more docs

This commit is contained in:
Nick Ethier 2018-06-29 22:53:59 -04:00
parent 5d4caa5698
commit c3e3919637
No known key found for this signature in database
GPG key ID: 07C1A3ECED90D24A
3 changed files with 29 additions and 8 deletions

View file

@ -18,6 +18,10 @@ type TestSuite struct {
Constraints Constraints // Environment constraints to follow
Parallel bool // If true, will run test cases in parallel
Slow bool // Slow test suites don't run by default
// API Clients
Consul bool
Vault bool
}
// Constraints that must be satisfied for a TestSuite to run

View file

@ -132,7 +132,11 @@ func (f *Framework) runSuite(t *testing.T, s *TestSuite) (skip bool, err error)
name := fmt.Sprintf("%T", c)
// Each TestCase is provisioned a nomad cluster
info, err := f.provisioner.ProvisionCluster(ProvisionerOptions{Name: name})
info, err := f.provisioner.ProvisionCluster(ProvisionerOptions{
Name: name,
ExpectConsul: s.Consul,
ExpectVault: s.Vault,
})
if err != nil {
return false, fmt.Errorf("could not provision cluster for case: %v", err)
}

View file

@ -11,32 +11,37 @@ import (
vapi "github.com/hashicorp/vault/api"
)
// ProvisionerOptions defines options to be given to the Provisioner when calling
// ProvisionCluster
type ProvisionerOptions struct {
Name string
Servers int
Clients int
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
}
type ClusterInfo struct {
ID string
Name string
Servers []string
Clients []string
NomadClient *napi.Client
ConsulClient *capi.Client
VaultClient *vapi.Client
}
// Provisioner interface is used by the test framework to provision a Nomad
// cluster for each TestCase
type Provisioner interface {
ProvisionCluster(opts ProvisionerOptions) (*ClusterInfo, error)
DestroyCluster(clusterID string) error
}
// DefaultProvisioner is a noop provisioner that builds clients from environment
// variables according to the respective client configuration
var DefaultProvisioner Provisioner = new(singleClusterProvisioner)
type singleClusterProvisioner struct{}
func (p *singleClusterProvisioner) ProvisionCluster(opts ProvisionerOptions) (*ClusterInfo, error) {
// Build ID based off given name
h := md5.New()
h.Write([]byte(opts.Name))
info := &ClusterInfo{
@ -44,10 +49,12 @@ func (p *singleClusterProvisioner) ProvisionCluster(opts ProvisionerOptions) (*C
Name: opts.Name,
}
// Nomad client is required
if len(os.Getenv("NOMAD_ADDR")) == 0 {
return nil, fmt.Errorf("environment variable NOMAD_ADDR not set")
}
// Build Nomad api client
nomadClient, err := napi.NewClient(napi.DefaultConfig())
if err != nil {
return nil, err
@ -56,18 +63,24 @@ func (p *singleClusterProvisioner) ProvisionCluster(opts ProvisionerOptions) (*C
if len(os.Getenv(capi.HTTPAddrEnvName)) != 0 {
consulClient, err := capi.NewClient(capi.DefaultConfig())
if err != nil {
if err != nil && opts.ExpectConsul {
return nil, err
}
info.ConsulClient = consulClient
} else if opts.ExpectConsul {
return nil, fmt.Errorf("consul client expected but environment variable %s not set",
capi.HTTPAddrEnvName)
}
if len(os.Getenv(vapi.EnvVaultAddress)) != 0 {
vaultClient, err := vapi.NewClient(vapi.DefaultConfig())
if err != nil {
if err != nil && opts.ExpectVault {
return nil, err
}
info.VaultClient = vaultClient
} else if opts.ExpectVault {
return nil, fmt.Errorf("vault client expected but environment variable %s not set",
vapi.EnvVaultAddress)
}
return info, err