2018-06-28 17:29:26 +00:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// TestSuite defines a set of test cases and under what conditions to run them
|
2018-06-28 17:29:26 +00:00
|
|
|
type TestSuite struct {
|
2018-06-28 19:55:53 +00:00
|
|
|
Component string // Name of the component/system/feature tested
|
2018-06-28 17:29:26 +00:00
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
CanRunLocal bool // Flags if the cases are safe to run on a local nomad cluster
|
|
|
|
Cases []TestCase // Cases to run
|
|
|
|
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
|
2018-06-30 02:53:59 +00:00
|
|
|
|
|
|
|
// API Clients
|
|
|
|
Consul bool
|
|
|
|
Vault bool
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
2018-06-30 02:39:04 +00:00
|
|
|
// Constraints that must be satisfied for a TestSuite to run
|
2018-06-28 17:29:26 +00:00
|
|
|
type Constraints struct {
|
2018-06-28 19:55:53 +00:00
|
|
|
Provider string // Cloud provider ex. 'aws', 'azure', 'gcp'
|
|
|
|
OS string // Operating system ex. 'windows', 'linux'
|
|
|
|
Arch string // CPU architecture ex. 'amd64', 'arm64'
|
|
|
|
Environment string // Environment name ex. 'simple'
|
|
|
|
Tags []string // Generic tags that must all exist in the environment
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c Constraints) matches(env Environment) error {
|
2018-06-28 19:55:53 +00:00
|
|
|
if len(c.Provider) != 0 && c.Provider != env.Provider {
|
2018-06-28 17:29:26 +00:00
|
|
|
return fmt.Errorf("provider constraint does not match environment")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.OS) != 0 && c.OS != env.OS {
|
|
|
|
return fmt.Errorf("os constraint does not match environment")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Arch) != 0 && c.Arch != env.Arch {
|
|
|
|
return fmt.Errorf("arch constraint does not match environment")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Environment) != 0 && c.Environment != env.Name {
|
|
|
|
return fmt.Errorf("environment constraint does not match environment name")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range c.Tags {
|
|
|
|
if _, ok := env.Tags[t]; !ok {
|
|
|
|
return fmt.Errorf("tags constraint failed, tag '%s' is not included in environment")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-30 02:39:04 +00:00
|
|
|
// TC is the base test case which should be embedded in TestCase implementations.
|
|
|
|
// It also embeds testify assertions configured with the current *testing.T
|
|
|
|
// context. For more information on assertions:
|
|
|
|
// https://godoc.org/github.com/stretchr/testify/assert#Assertions
|
2018-06-28 17:29:26 +00:00
|
|
|
type TC struct {
|
|
|
|
*assert.Assertions
|
|
|
|
require *require.Assertions
|
|
|
|
t *testing.T
|
|
|
|
|
|
|
|
cluster *ClusterInfo
|
|
|
|
prefix string
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// Nomad returns a configured nomad api client
|
2018-06-28 17:29:26 +00:00
|
|
|
func (tc *TC) Nomad() *api.Client {
|
|
|
|
return tc.cluster.NomadClient
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// Prefix will return a test case unique prefix which can be used to scope resources
|
|
|
|
// during parallel tests.
|
2018-06-28 17:29:26 +00:00
|
|
|
func (tc *TC) Prefix() string {
|
|
|
|
return fmt.Sprintf("%s-", tc.cluster.ID)
|
|
|
|
}
|
|
|
|
|
2018-06-30 02:39:04 +00:00
|
|
|
// Name returns the name of the test case which is set to the name of the
|
|
|
|
// implementing type.
|
2018-06-28 17:29:26 +00:00
|
|
|
func (tc *TC) Name() string {
|
|
|
|
return tc.cluster.Name
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// T retrieves the current *testing.T context
|
2018-06-28 17:29:26 +00:00
|
|
|
func (tc *TC) T() *testing.T {
|
|
|
|
return tc.t
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// SetT sets the current *testing.T context
|
2018-06-28 17:29:26 +00:00
|
|
|
func (tc *TC) SetT(t *testing.T) {
|
|
|
|
tc.t = t
|
|
|
|
tc.Assertions = assert.New(t)
|
|
|
|
tc.require = require.New(t)
|
|
|
|
}
|
|
|
|
|
2018-06-30 02:39:04 +00:00
|
|
|
// Require fetches a require flavor of testify assertions
|
|
|
|
// https://godoc.org/github.com/stretchr/testify/require
|
|
|
|
func (tc *TC) Require() *require.Assertions {
|
|
|
|
return tc.require
|
|
|
|
}
|
|
|
|
|
2018-06-28 17:29:26 +00:00
|
|
|
func (tc *TC) setClusterInfo(info *ClusterInfo) {
|
|
|
|
tc.cluster = info
|
|
|
|
}
|