github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/e2e/framework/provisioning/preprovision.go (about) 1 package provisioning 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 capi "github.com/hashicorp/consul/api" 9 napi "github.com/hashicorp/nomad/api" 10 "github.com/hashicorp/nomad/helper/uuid" 11 vapi "github.com/hashicorp/vault/api" 12 ) 13 14 // PreProvisioner returns a provisioner that deploys Nomad to a cluster 15 // at the start of the whole test run and leaves it in place. 16 func PreProvisioner(targets *ProvisioningTargets) Provisioner { 17 return &preProvisioner{ 18 Servers: targets.Servers, 19 Clients: targets.Clients, 20 } 21 } 22 23 type preProvisioner struct { 24 Servers []*ProvisioningTarget // servers get provisioned before clients 25 Clients []*ProvisioningTarget // leave empty for nodes that are both 26 } 27 28 // SetupTestRun deploys a Nomad cluster to the target environment. If the target 29 // environment has a Nomad cluster already, it will upgrade it to the desired 30 // version of leave it in place if it matches the ProvisioningTarget config. 31 func (p *preProvisioner) SetupTestRun(t *testing.T, opts SetupOptions) (*ClusterInfo, error) { 32 33 for _, server := range p.Servers { 34 err := deploy(t, server) 35 if err != nil { 36 return nil, err 37 } 38 } 39 40 for _, client := range p.Clients { 41 err := deploy(t, client) 42 if err != nil { 43 return nil, err 44 } 45 } 46 47 return &ClusterInfo{ID: "framework", Name: "framework"}, nil 48 } 49 50 // SetupTestSuite is a no-op. 51 func (p *preProvisioner) SetupTestSuite(t *testing.T, opts SetupOptions) (*ClusterInfo, error) { 52 return &ClusterInfo{ID: opts.Name, Name: opts.Name}, nil 53 } 54 55 // SetupTestCase in creates new clients and embeds the TestCase name into 56 // the ClusterInfo handle. 57 func (p *preProvisioner) SetupTestCase(t *testing.T, opts SetupOptions) (*ClusterInfo, error) { 58 // Build ID based off given name 59 info := &ClusterInfo{ 60 ID: uuid.Generate()[:8], 61 Name: opts.Name, 62 } 63 64 // Build Nomad api client 65 nomadClient, err := napi.NewClient(napi.DefaultConfig()) 66 if err != nil { 67 return nil, err 68 } 69 info.NomadClient = nomadClient 70 71 if opts.ExpectConsul { 72 consulClient, err := capi.NewClient(capi.DefaultConfig()) 73 if err != nil && opts.ExpectConsul { 74 return nil, fmt.Errorf( 75 "consul client expected but no Consul available: %v", err) 76 } 77 info.ConsulClient = consulClient 78 } 79 80 if len(os.Getenv(vapi.EnvVaultAddress)) != 0 { 81 vaultClient, err := vapi.NewClient(vapi.DefaultConfig()) 82 if err != nil && opts.ExpectVault { 83 return nil, err 84 } 85 info.VaultClient = vaultClient 86 } else if opts.ExpectVault { 87 return nil, fmt.Errorf( 88 "vault client expected but Vault available: %v", err) 89 } 90 91 return info, err 92 } 93 94 // all TearDown* methods of preProvisioner leave the test environment in place 95 96 func (p *preProvisioner) TearDownTestCase(_ *testing.T, _ string) error { return nil } 97 func (p *preProvisioner) TearDownTestSuite(_ *testing.T, _ string) error { return nil } 98 func (p *preProvisioner) TearDownTestRun(_ *testing.T, _ string) error { return nil }