open-nomad/e2e/framework/provisioning/preprovision.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

99 lines
2.8 KiB
Go

package provisioning
import (
"fmt"
"os"
"testing"
capi "github.com/hashicorp/consul/api"
napi "github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/helper/uuid"
vapi "github.com/hashicorp/vault/api"
)
// PreProvisioner returns a provisioner that deploys Nomad to a cluster
// at the start of the whole test run and leaves it in place.
func PreProvisioner(targets *ProvisioningTargets) Provisioner {
return &preProvisioner{
Servers: targets.Servers,
Clients: targets.Clients,
}
}
type preProvisioner struct {
Servers []*ProvisioningTarget // servers get provisioned before clients
Clients []*ProvisioningTarget // leave empty for nodes that are both
}
// SetupTestRun deploys a Nomad cluster to the target environment. If the target
// environment has a Nomad cluster already, it will upgrade it to the desired
// version of leave it in place if it matches the ProvisioningTarget config.
func (p *preProvisioner) SetupTestRun(t *testing.T, opts SetupOptions) (*ClusterInfo, error) {
for _, server := range p.Servers {
err := deploy(t, server)
if err != nil {
return nil, err
}
}
for _, client := range p.Clients {
err := deploy(t, client)
if err != nil {
return nil, err
}
}
return &ClusterInfo{ID: "framework", Name: "framework"}, nil
}
// SetupTestSuite is a no-op.
func (p *preProvisioner) SetupTestSuite(t *testing.T, opts SetupOptions) (*ClusterInfo, error) {
return &ClusterInfo{ID: opts.Name, Name: opts.Name}, nil
}
// SetupTestCase in creates new clients and embeds the TestCase name into
// the ClusterInfo handle.
func (p *preProvisioner) SetupTestCase(t *testing.T, opts SetupOptions) (*ClusterInfo, error) {
// Build ID based off given name
info := &ClusterInfo{
ID: uuid.Generate()[:8],
Name: opts.Name,
}
// Build Nomad api client
nomadClient, err := napi.NewClient(napi.DefaultConfig())
if err != nil {
return nil, err
}
info.NomadClient = nomadClient
if opts.ExpectConsul {
consulClient, err := capi.NewClient(capi.DefaultConfig())
if err != nil && opts.ExpectConsul {
return nil, fmt.Errorf(
"consul client expected but no Consul available: %v", err)
}
info.ConsulClient = consulClient
}
if len(os.Getenv(vapi.EnvVaultAddress)) != 0 {
vaultClient, err := vapi.NewClient(vapi.DefaultConfig())
if err != nil && opts.ExpectVault {
return nil, err
}
info.VaultClient = vaultClient
} else if opts.ExpectVault {
return nil, fmt.Errorf(
"vault client expected but Vault available: %v", err)
}
return info, err
}
// all TearDown* methods of preProvisioner leave the test environment in place
func (p *preProvisioner) TearDownTestCase(_ *testing.T, _ string) error { return nil }
func (p *preProvisioner) TearDownTestSuite(_ *testing.T, _ string) error { return nil }
func (p *preProvisioner) TearDownTestRun(_ *testing.T, _ string) error { return nil }