2018-06-28 17:29:26 +00:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2020-01-22 13:48:52 +00:00
|
|
|
"log"
|
2018-06-28 17:29:26 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2021-01-26 14:14:47 +00:00
|
|
|
|
2022-03-17 13:37:34 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2021-01-26 14:14:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-28 17:29:26 +00:00
|
|
|
)
|
|
|
|
|
2020-01-22 13:48:52 +00:00
|
|
|
const frameworkHelp = `
|
|
|
|
Usage: go test -v ./e2e [options]
|
|
|
|
|
|
|
|
These flags are coarse overrides for the test environment.
|
|
|
|
|
|
|
|
-forceRun skip all environment checks when filtering test suites
|
2020-09-18 15:46:47 +00:00
|
|
|
-local denotes execution is against a local environment
|
2020-01-22 13:48:52 +00:00
|
|
|
-slow include execution of slow test suites
|
2020-01-29 20:52:34 +00:00
|
|
|
-suite run specified test suite
|
2020-01-22 13:48:52 +00:00
|
|
|
-showHelp shows this help text
|
|
|
|
|
|
|
|
TestSuites can request Constraints on the Framework.Environment so that tests
|
|
|
|
are only run in the appropriate conditions. These environment flags provide
|
|
|
|
the information for those constraints.
|
|
|
|
|
|
|
|
-env=string name of the environment
|
|
|
|
-env.arch=string cpu architecture of the targets
|
|
|
|
-env.os=string operating system of the targets
|
|
|
|
-env.provider=string cloud provider of the environment
|
|
|
|
-env.tags=string comma delimited list of tags for the environment
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
var fHelp = flag.Bool("showHelp", false, "print the help screen")
|
|
|
|
var fLocal = flag.Bool("local", false,
|
2020-09-18 15:46:47 +00:00
|
|
|
"denotes execution is against a local environment")
|
2018-07-05 20:01:30 +00:00
|
|
|
var fSlow = flag.Bool("slow", false, "toggles execution of slow test suites")
|
2020-01-22 13:48:52 +00:00
|
|
|
var fForceRun = flag.Bool("forceRun", false,
|
|
|
|
"if set, skips all environment checks when filtering test suites")
|
2020-01-29 20:52:34 +00:00
|
|
|
var fSuite = flag.String("suite", "", "run specified test suite")
|
2020-01-22 13:48:52 +00:00
|
|
|
|
|
|
|
// Environment flags
|
|
|
|
// TODO:
|
2020-09-18 15:46:47 +00:00
|
|
|
// It would be nice if we could match the target environment against
|
|
|
|
// the tests automatically so that we always avoid running tests that
|
|
|
|
// don't apply, and then have these flags override that behavior.
|
2020-01-22 13:48:52 +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")
|
2018-06-28 17:29:26 +00:00
|
|
|
|
2023-02-07 14:10:40 +00:00
|
|
|
// Deprecated: no longer use e2e/framework for new tests; see TestExample for new e2e test structure.
|
2018-06-28 17:29:26 +00:00
|
|
|
type Framework struct {
|
|
|
|
suites []*TestSuite
|
2020-09-18 15:46:47 +00:00
|
|
|
provisioner Provisioner
|
2018-06-28 17:29:26 +00:00
|
|
|
env Environment
|
|
|
|
|
|
|
|
isLocalRun bool
|
|
|
|
slow bool
|
|
|
|
force bool
|
2020-01-29 20:52:34 +00:00
|
|
|
suite string
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:48:52 +00:00
|
|
|
// Environment contains information about the test target environment, used
|
|
|
|
// to constrain the set of tests run. See the environment flags above.
|
2023-02-07 14:10:40 +00:00
|
|
|
//
|
|
|
|
// Deprecated: no longer use e2e/framework for new tests; see TestExample for new e2e test structure.
|
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
|
2023-02-07 14:10:40 +00:00
|
|
|
//
|
|
|
|
// Deprecated: no longer use e2e/framework for new tests; see TestExample for new e2e test structure.
|
2018-06-28 17:29:26 +00:00
|
|
|
func New() *Framework {
|
2020-01-22 13:48:52 +00:00
|
|
|
flag.Parse()
|
|
|
|
if *fHelp {
|
|
|
|
log.Fatal(frameworkHelp)
|
|
|
|
}
|
2018-06-28 17:29:26 +00:00
|
|
|
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{
|
2020-09-18 15:46:47 +00:00
|
|
|
provisioner: DefaultProvisioner,
|
|
|
|
env: env,
|
|
|
|
isLocalRun: *fLocal,
|
|
|
|
slow: *fSlow,
|
|
|
|
force: *fForceRun,
|
|
|
|
suite: *fSuite,
|
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
|
2023-02-07 14:10:40 +00:00
|
|
|
//
|
|
|
|
// Deprecated: no longer use e2e/framework for new tests; see TestExample for new e2e test structure.
|
2018-06-28 17:29:26 +00:00
|
|
|
func (f *Framework) AddSuites(s ...*TestSuite) *Framework {
|
|
|
|
f.suites = append(f.suites, s...)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-03-02 19:13:54 +00:00
|
|
|
var pkgSuites []*TestSuite
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
// AddSuites adds a set of test suites to the package scoped Framework
|
2023-02-07 14:10:40 +00:00
|
|
|
//
|
|
|
|
// Deprecated: no longer use e2e/framework for new tests; see TestExample for new e2e test structure.
|
2020-03-02 19:13:54 +00:00
|
|
|
func AddSuites(s ...*TestSuite) {
|
|
|
|
pkgSuites = append(pkgSuites, s...)
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-09-18 15:46:47 +00:00
|
|
|
info, err := f.provisioner.SetupTestRun(t, SetupOptions{})
|
2020-01-22 13:48:52 +00:00
|
|
|
if err != nil {
|
2021-01-26 14:14:47 +00:00
|
|
|
require.NoError(t, err, "could not provision cluster")
|
2020-01-22 13:48:52 +00:00
|
|
|
}
|
|
|
|
defer f.provisioner.TearDownTestRun(t, info.ID)
|
|
|
|
|
2018-06-28 17:29:26 +00:00
|
|
|
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
|
2023-02-07 14:10:40 +00:00
|
|
|
//
|
|
|
|
// Deprecated: no longer use e2e/framework for new tests; see TestExample for new e2e test structure.
|
2018-06-28 17:29:26 +00:00
|
|
|
func Run(t *testing.T) {
|
2020-03-02 19:13:54 +00:00
|
|
|
f := New()
|
|
|
|
f.AddSuites(pkgSuites...)
|
|
|
|
f.Run(t)
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 20:52:34 +00:00
|
|
|
// If -suite is set, skip any suite that is not the one specified.
|
|
|
|
if f.suite != "" && f.suite != s.Component {
|
|
|
|
return true, fmt.Errorf("only running suite %q", f.suite)
|
|
|
|
}
|
|
|
|
|
2020-09-18 15:46:47 +00:00
|
|
|
info, err := f.provisioner.SetupTestSuite(t, SetupOptions{
|
2020-01-22 13:48:52 +00:00
|
|
|
Name: s.Component,
|
|
|
|
ExpectConsul: s.Consul,
|
|
|
|
ExpectVault: s.Vault,
|
|
|
|
})
|
2021-01-26 14:14:47 +00:00
|
|
|
require.NoError(t, err, "could not provision cluster")
|
2020-01-22 13:48:52 +00:00
|
|
|
defer f.provisioner.TearDownTestSuite(t, info.ID)
|
|
|
|
|
2018-06-28 17:29:26 +00:00
|
|
|
for _, c := range s.Cases {
|
2020-01-22 13:48:52 +00:00
|
|
|
f.runCase(t, s, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Framework) runCase(t *testing.T, s *TestSuite, c TestCase) {
|
|
|
|
|
|
|
|
// The test name is set to the name of the implementing type, including package
|
|
|
|
name := fmt.Sprintf("%T", c)
|
|
|
|
|
|
|
|
// The ClusterInfo handle should be used by each TestCase to isolate
|
|
|
|
// job/task state created during the test.
|
2020-09-18 15:46:47 +00:00
|
|
|
info, err := f.provisioner.SetupTestCase(t, SetupOptions{
|
2020-01-22 13:48:52 +00:00
|
|
|
Name: name,
|
|
|
|
ExpectConsul: s.Consul,
|
|
|
|
ExpectVault: s.Vault,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not provision cluster for case: %v", err)
|
|
|
|
}
|
|
|
|
defer f.provisioner.TearDownTestCase(t, info.ID)
|
|
|
|
c.setClusterInfo(info)
|
|
|
|
|
|
|
|
// 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 {
|
2022-03-17 13:37:34 +00:00
|
|
|
ci.Parallel(t)
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 13:48:52 +00:00
|
|
|
f := newF(t)
|
2018-07-10 19:57:02 +00:00
|
|
|
|
2020-01-22 13:48:52 +00:00
|
|
|
// Check if the case includes a before all function
|
|
|
|
if beforeAllTests, ok := c.(BeforeAllTests); ok {
|
|
|
|
beforeAllTests.BeforeAll(f)
|
|
|
|
}
|
2018-06-28 17:29:26 +00:00
|
|
|
|
2020-01-22 13:48:52 +00:00
|
|
|
// Check if the case includes an after all function at the end
|
|
|
|
defer func() {
|
|
|
|
if afterAllTests, ok := c.(AfterAllTests); ok {
|
|
|
|
afterAllTests.AfterAll(f)
|
|
|
|
}
|
|
|
|
}()
|
2018-06-28 17:29:26 +00:00
|
|
|
|
2020-01-22 13:48:52 +00:00
|
|
|
// Here we need to iterate through the methods of the case to find
|
|
|
|
// ones that are test functions
|
|
|
|
reflectC := reflect.TypeOf(c)
|
|
|
|
for i := 0; i < reflectC.NumMethod(); i++ {
|
|
|
|
method := reflectC.Method(i)
|
|
|
|
if ok := isTestMethod(method.Name); !ok {
|
|
|
|
continue
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
2020-01-22 13:48:52 +00:00
|
|
|
// Each test is run as its own sub test of the case
|
|
|
|
// Test cases are never parallel
|
|
|
|
t.Run(method.Name, func(t *testing.T) {
|
2018-06-28 17:29:26 +00:00
|
|
|
|
2020-01-22 13:48:52 +00:00
|
|
|
cF := newFFromParent(f, t)
|
|
|
|
if BeforeEachTest, ok := c.(BeforeEachTest); ok {
|
|
|
|
BeforeEachTest.BeforeEach(cF)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if afterEachTest, ok := c.(AfterEachTest); ok {
|
|
|
|
afterEachTest.AfterEach(cF)
|
|
|
|
}
|
|
|
|
}()
|
2018-06-28 17:29:26 +00:00
|
|
|
|
2020-01-22 13:48:52 +00:00
|
|
|
//Call the method
|
|
|
|
method.Func.Call([]reflect.Value{reflect.ValueOf(c), reflect.ValueOf(cF)})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 19:55:53 +00:00
|
|
|
func isTestMethod(m string) bool {
|
2020-12-09 19:05:18 +00:00
|
|
|
return strings.HasPrefix(m, "Test")
|
2018-06-28 17:29:26 +00:00
|
|
|
}
|