2018-06-28 17:29:26 +00:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-07-05 20:01:30 +00:00
|
|
|
var fEnv = flag.String("env", "", "name of the environment executing against")
|
|
|
|
var fProvider = flag.String("env.provider", "", "cloud provider for which environment is executing against")
|
|
|
|
var fOS = flag.String("env.os", "", "operating system for which the environment is executing against")
|
|
|
|
var fArch = flag.String("env.arch", "", "cpu architecture for which the environment is executing against")
|
|
|
|
var fTags = flag.String("env.tags", "", "comma delimited list of tags associated with the environment")
|
|
|
|
var fLocal = flag.Bool("local", false, "denotes execution is against a local environment")
|
|
|
|
var fSlow = flag.Bool("slow", false, "toggles execution of slow test suites")
|
|
|
|
var fForceRun = flag.Bool("forceRun", false, "if set, skips all environment checks when filtering test suites")
|
2018-06-28 17:29:26 +00:00
|
|
|
|
|
|
|
var pkgFramework = New()
|
|
|
|
|
|
|
|
type Framework struct {
|
|
|
|
suites []*TestSuite
|
|
|
|
provisioner Provisioner
|
|
|
|
env Environment
|
|
|
|
|
|
|
|
isLocalRun bool
|
|
|
|
slow bool
|
|
|
|
force bool
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// Environment includes information about the target environment the test
|
|
|
|
// framework is targeting. During 'go run', these fields are populated by
|
|
|
|
// the following flags:
|
|
|
|
//
|
2018-07-05 20:01:30 +00:00
|
|
|
// -env <string> "name of the environment executing against"
|
|
|
|
// -env.provider <string> "cloud provider for which the environment is executing against"
|
|
|
|
// -env.os <string> "operating system of environment"
|
|
|
|
// -env.arch <string> "cpu architecture of environment"
|
|
|
|
// -env.tags <string> "comma delimited list of environment tags"
|
2018-06-28 19:55:53 +00:00
|
|
|
//
|
|
|
|
// These flags are not needed when executing locally
|
2018-06-28 17:29:26 +00:00
|
|
|
type Environment struct {
|
|
|
|
Name string
|
|
|
|
Provider string
|
|
|
|
OS string
|
|
|
|
Arch string
|
|
|
|
Tags map[string]struct{}
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// New creates a Framework
|
2018-06-28 17:29:26 +00:00
|
|
|
func New() *Framework {
|
|
|
|
env := Environment{
|
|
|
|
Name: *fEnv,
|
|
|
|
Provider: *fProvider,
|
|
|
|
OS: *fOS,
|
|
|
|
Arch: *fArch,
|
|
|
|
Tags: map[string]struct{}{},
|
|
|
|
}
|
|
|
|
for _, tag := range strings.Split(*fTags, ",") {
|
|
|
|
env.Tags[tag] = struct{}{}
|
|
|
|
}
|
|
|
|
return &Framework{
|
|
|
|
provisioner: DefaultProvisioner,
|
|
|
|
env: env,
|
|
|
|
isLocalRun: *fLocal,
|
|
|
|
slow: *fSlow,
|
2018-07-05 20:01:30 +00:00
|
|
|
force: *fForceRun,
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// AddSuites adds a set of test suites to a Framework
|
2018-06-28 17:29:26 +00:00
|
|
|
func (f *Framework) AddSuites(s ...*TestSuite) *Framework {
|
|
|
|
f.suites = append(f.suites, s...)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// AddSuites adds a set of test suites to the package scoped Framework
|
2018-06-28 17:29:26 +00:00
|
|
|
func AddSuites(s ...*TestSuite) *Framework {
|
|
|
|
pkgFramework.AddSuites(s...)
|
|
|
|
return pkgFramework
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// Run starts the test framework, running each TestSuite
|
2018-06-28 17:29:26 +00:00
|
|
|
func (f *Framework) Run(t *testing.T) {
|
|
|
|
for _, s := range f.suites {
|
|
|
|
t.Run(s.Component, func(t *testing.T) {
|
|
|
|
skip, err := f.runSuite(t, s)
|
|
|
|
if skip {
|
|
|
|
t.Skipf("skipping suite '%s': %v", s.Component, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error starting suite '%s': %v", s.Component, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// Run starts the package scoped Framework, running each TestSuite
|
2018-06-28 17:29:26 +00:00
|
|
|
func Run(t *testing.T) {
|
|
|
|
pkgFramework.Run(t)
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// runSuite is called from Framework.Run inside of a sub test for each TestSuite.
|
|
|
|
// If skip is returned as true, the test suite is skipped with the error text added
|
|
|
|
// to the Skip reason
|
|
|
|
// If skip is false and an error is returned, the test suite is failed.
|
2018-06-28 17:29:26 +00:00
|
|
|
func (f *Framework) runSuite(t *testing.T, s *TestSuite) (skip bool, err error) {
|
|
|
|
|
2018-07-05 20:01:30 +00:00
|
|
|
// If -forceRun is set, skip all constraint checks
|
2018-06-28 17:29:26 +00:00
|
|
|
if !f.force {
|
|
|
|
// If this is a local run, check that the suite supports running locally
|
|
|
|
if !s.CanRunLocal && f.isLocalRun {
|
|
|
|
return true, fmt.Errorf("local run detected and suite cannot run locally")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that constraints are met
|
|
|
|
if err := s.Constraints.matches(f.env); err != nil {
|
|
|
|
return true, fmt.Errorf("constraint failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the slow toggle and if the suite's slow flag is that same
|
|
|
|
if f.slow != s.Slow {
|
|
|
|
return true, fmt.Errorf("framework slow suite configuration is %v but suite is %v", f.slow, s.Slow)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range s.Cases {
|
2018-06-28 19:55:53 +00:00
|
|
|
// The test name is set to the name of the implementing type, including package
|
2018-06-28 17:29:26 +00:00
|
|
|
name := fmt.Sprintf("%T", c)
|
2018-06-28 19:55:53 +00:00
|
|
|
|
2018-06-28 17:29:26 +00:00
|
|
|
// Each TestCase is provisioned a nomad cluster
|
2018-06-30 02:53:59 +00:00
|
|
|
info, err := f.provisioner.ProvisionCluster(ProvisionerOptions{
|
|
|
|
Name: name,
|
|
|
|
ExpectConsul: s.Consul,
|
|
|
|
ExpectVault: s.Vault,
|
|
|
|
})
|
2018-06-28 17:29:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("could not provision cluster for case: %v", err)
|
|
|
|
}
|
|
|
|
defer f.provisioner.DestroyCluster(info.ID)
|
|
|
|
c.setClusterInfo(info)
|
2018-06-28 19:55:53 +00:00
|
|
|
|
2018-06-28 17:29:26 +00:00
|
|
|
// Each TestCase runs as a subtest of the TestSuite
|
|
|
|
t.Run(c.Name(), func(t *testing.T) {
|
|
|
|
// If the TestSuite has Parallel set, all cases run in parallel
|
|
|
|
if s.Parallel {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
|
2018-07-10 19:57:02 +00:00
|
|
|
f := newF(t)
|
|
|
|
|
2018-06-28 17:29:26 +00:00
|
|
|
// Check if the case includes a before all function
|
2018-07-03 18:26:05 +00:00
|
|
|
if beforeAllTests, ok := c.(BeforeAllTests); ok {
|
2018-07-10 19:57:02 +00:00
|
|
|
beforeAllTests.BeforeAll(f)
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the case includes an after all function at the end
|
|
|
|
defer func() {
|
2018-07-03 18:26:05 +00:00
|
|
|
if afterAllTests, ok := c.(AfterAllTests); ok {
|
2018-07-10 19:57:02 +00:00
|
|
|
afterAllTests.AfterAll(f)
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Here we need to iterate through the methods of the case to find
|
2018-06-28 19:55:53 +00:00
|
|
|
// ones that are test functions
|
2018-06-28 17:29:26 +00:00
|
|
|
reflectC := reflect.TypeOf(c)
|
|
|
|
for i := 0; i < reflectC.NumMethod(); i++ {
|
|
|
|
method := reflectC.Method(i)
|
2018-06-28 19:55:53 +00:00
|
|
|
if ok := isTestMethod(method.Name); !ok {
|
2018-06-28 17:29:26 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-07-03 18:26:05 +00:00
|
|
|
// Each test is run as its own sub test of the case
|
2018-06-28 19:55:53 +00:00
|
|
|
// Test cases are never parallel
|
2018-06-28 17:29:26 +00:00
|
|
|
t.Run(method.Name, func(t *testing.T) {
|
|
|
|
|
2018-07-10 19:57:02 +00:00
|
|
|
cF := newF(t)
|
2018-07-03 18:26:05 +00:00
|
|
|
if BeforeEachTest, ok := c.(BeforeEachTest); ok {
|
2018-07-10 19:57:02 +00:00
|
|
|
BeforeEachTest.BeforeEach(cF)
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
2018-07-03 18:26:05 +00:00
|
|
|
if afterEachTest, ok := c.(AfterEachTest); ok {
|
2018-07-10 19:57:02 +00:00
|
|
|
afterEachTest.AfterEach(cF)
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
}()
|
2018-06-28 19:55:53 +00:00
|
|
|
|
2018-06-28 17:29:26 +00:00
|
|
|
//Call the method
|
2018-07-10 19:57:02 +00:00
|
|
|
method.Func.Call([]reflect.Value{reflect.ValueOf(c), reflect.ValueOf(cF)})
|
2018-06-28 17:29:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
func isTestMethod(m string) bool {
|
2018-06-28 17:29:26 +00:00
|
|
|
if !strings.HasPrefix(m, "Test") {
|
2018-06-28 19:55:53 +00:00
|
|
|
return false
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
2018-07-03 18:26:05 +00:00
|
|
|
// THINKING: adding flag to target a specific test or test regex?
|
2018-06-28 19:55:53 +00:00
|
|
|
return true
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|