open-nomad/e2e/framework/provisioning/provisioning.go
Tim Gross 2edbdfc8be
e2e: update framework to allow deploying Nomad (#6969)
The e2e framework instantiates clients for Nomad/Consul but the
provisioning of the actual Nomad cluster is left to Terraform. The
Terraform provisioning process uses `remote-exec` to deploy specific
versions of Nomad so that we don't have to bake an AMI every time we
want to test a new version. But Terraform treats the resulting
instances as immutable, so we can't use the same tooling to update the
version of Nomad in-place. This is a prerequisite for upgrade testing.

This changeset extends the e2e framework to provide the option of
deploying Nomad (and, in the future, Consul/Vault) with specific
versions to running infrastructure. This initial implementation is
focused on deploying to a single cluster via `ssh` (because that's our
current need), but provides interfaces to hook the test run at the
start of the run, the start of each suite, or the start of a given
test case.

Terraform work includes:
* provides Terraform output that written to JSON used by the framework
  to configure provisioning via `terraform output provisioning`.
* provides Terraform output that can be used by test operators to
  configure their shell via `$(terraform output environment)`
* drops `remote-exec` provisioning steps from Terraform
* makes changes to the deployment scripts to ensure they can be run
  multiple times w/ different versions against the same host.
2020-01-22 08:48:52 -05:00

83 lines
2.7 KiB
Go

package provisioning
import (
"testing"
capi "github.com/hashicorp/consul/api"
napi "github.com/hashicorp/nomad/api"
vapi "github.com/hashicorp/vault/api"
)
func NewProvisioner(config ProvisionerConfig) Provisioner {
if config.IsLocal {
return DefaultProvisioner
}
if config.VagrantBox != "" {
return PreProvisioner(ProvisionerConfigVagrant(config))
}
if config.TerraformConfig != "" {
return PreProvisioner(ProvisionerConfigTerraform(config))
}
return DefaultProvisioner
}
// ProvisionerConfig defines options for the entire lifecycle of the provisioner.
type ProvisionerConfig struct {
IsLocal bool
VagrantBox string
TerraformConfig string
NomadSha string
NomadVersion string
NomadLocalBinary string
}
// Provisioner interface is used by the test framework to provision Nomad.
// The Setup* methods should be used to create a Nomad cluster at the
// appropriate stage. The returned ClusterInfo handle helps TestCases
// isolate test state by using the ClusterInfo.ID as part of job IDs.
type Provisioner interface {
// SetupTestRun is called at the start of the entire test run.
SetupTestRun(t *testing.T, opts SetupOptions) (*ClusterInfo, error)
// SetupTestSuite is called at the start of each TestSuite.
// TODO: no current provisioner implementation uses this, but we
// could use it to provide each TestSuite with an entirely separate
// Nomad cluster.
SetupTestSuite(t *testing.T, opts SetupOptions) (*ClusterInfo, error)
// SetupTestCase is called at the start of each TestCase in every TestSuite.
SetupTestCase(t *testing.T, opts SetupOptions) (*ClusterInfo, error)
// TODO: no current provisioner implementation uses any of these,
// but it's the obvious need if we setup/teardown after each TestSuite
// or TestCase.
// TearDownTestCase is called after each TestCase in every TestSuite.
TearDownTestCase(t *testing.T, clusterID string) error
// TearDownTestSuite is called after every TestSuite.
TearDownTestSuite(t *testing.T, clusterID string) error
// TearDownTestRun is called at the end of the entire test run.
TearDownTestRun(t *testing.T, clusterID string) error
}
// SetupOptions defines options to be given to the Provisioner when
// calling Setup* methods.
type SetupOptions struct {
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
}
// ClusterInfo is a handle to a provisioned cluster, along with clients
// a test run can use to connect to the cluster.
type ClusterInfo struct {
ID string
Name string
NomadClient *napi.Client
ConsulClient *capi.Client
VaultClient *vapi.Client
}