2020-01-22 13:48:52 +00:00
|
|
|
package provisioning
|
2018-06-28 17:29:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-01-22 13:48:52 +00:00
|
|
|
"testing"
|
2018-06-28 17:29:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
// 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{}
|
|
|
|
|
2020-01-22 13:48:52 +00:00
|
|
|
// SetupTestRun in the default case is a no-op.
|
|
|
|
func (p *singleClusterProvisioner) SetupTestRun(t *testing.T, opts SetupOptions) (*ClusterInfo, error) {
|
|
|
|
return &ClusterInfo{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupTestSuite in the default case is a no-op.
|
|
|
|
func (p *singleClusterProvisioner) SetupTestSuite(t *testing.T, opts SetupOptions) (*ClusterInfo, error) {
|
|
|
|
return &ClusterInfo{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupTestCase in the default case only creates new clients and embeds the
|
|
|
|
// TestCase name into the ClusterInfo handle.
|
|
|
|
func (p *singleClusterProvisioner) SetupTestCase(t *testing.T, opts SetupOptions) (*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())
|
2020-01-22 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("expected Consul: %v", err)
|
2018-06-29 02:58:57 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:48:52 +00:00
|
|
|
// all TearDown* methods of the default provisioner leave the test environment in place
|
|
|
|
|
|
|
|
func (p *singleClusterProvisioner) TearDownTestCase(_ *testing.T, _ string) error { return nil }
|
|
|
|
func (p *singleClusterProvisioner) TearDownTestSuite(_ *testing.T, _ string) error { return nil }
|
|
|
|
func (p *singleClusterProvisioner) TearDownTestRun(_ *testing.T, _ string) error { return nil }
|