From c3e391963797cf919b4fe03d9b7316fe45a59d8b Mon Sep 17 00:00:00 2001 From: Nick Ethier Date: Fri, 29 Jun 2018 22:53:59 -0400 Subject: [PATCH] e2e/framework: cleanup provisioner and more docs --- e2e/framework/case.go | 4 ++++ e2e/framework/framework.go | 6 +++++- e2e/framework/provisioner.go | 27 ++++++++++++++++++++------- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/e2e/framework/case.go b/e2e/framework/case.go index b06c79104..c8bfaa43c 100644 --- a/e2e/framework/case.go +++ b/e2e/framework/case.go @@ -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 diff --git a/e2e/framework/framework.go b/e2e/framework/framework.go index 3d56b7793..3d155c409 100644 --- a/e2e/framework/framework.go +++ b/e2e/framework/framework.go @@ -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) } diff --git a/e2e/framework/provisioner.go b/e2e/framework/provisioner.go index a0e65df19..89eb9b1a9 100644 --- a/e2e/framework/provisioner.go +++ b/e2e/framework/provisioner.go @@ -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