Files
virtual-kubelet/test/e2e/suite.go
Ernest Wong f10a16aed7 Importable End-To-End Test Suite (#758)
* Rename VK to chewong for development purpose

* Rename basic_test.go to basic.go

* Add e2e.go and suite.go

* Disable tests in node.go

* End to end tests are now importable as a testing suite

* Remove 'test' from test files

* Add documentations

* Rename chewong back to virtual-kubelet

* Change 'Testing Suite' to 'Test Suite'

* Add the ability to skip certain testss

* Add unit tests for suite.go

* Add README.md for importable e2e test suite

* VK implementation has to be based on VK v1.0.0

* Stricter checks on validating test functions

* Move certain files back to internal folder

* Add WatchTimeout as a config field

* Add slight modifications
2019-09-04 22:25:43 +01:00

105 lines
3.3 KiB
Go

package e2e
import (
"testing"
"time"
"github.com/virtual-kubelet/virtual-kubelet/internal/test/e2e/framework"
"github.com/virtual-kubelet/virtual-kubelet/internal/test/suite"
)
const defaultWatchTimeout = 2 * time.Minute
// f is a testing framework that is accessible across the e2e package
var f *framework.Framework
// EndToEndTestSuite holds the setup, teardown, and shouldSkipTest functions for a specific provider
type EndToEndTestSuite struct {
setup suite.SetUpFunc
teardown suite.TeardownFunc
shouldSkipTest suite.ShouldSkipTestFunc
}
// EndToEndTestSuiteConfig is the config passed to initialize the testing framework and test suite.
type EndToEndTestSuiteConfig struct {
// Kubeconfig is the path to the kubeconfig file to use when running the test suite outside a Kubernetes cluster.
Kubeconfig string
// Namespace is the name of the Kubernetes namespace to use for running the test suite (i.e. where to create pods).
Namespace string
// NodeName is the name of the virtual-kubelet node to test.
NodeName string
// WatchTimeout is the duration for which the framework watch a particular condition to be satisfied (e.g. watches a pod becoming ready)
WatchTimeout time.Duration
// Setup is a function that sets up provider-specific resource in the test suite
Setup suite.SetUpFunc
// Teardown is a function that tears down provider-specific resources from the test suite
Teardown suite.TeardownFunc
// ShouldSkipTest is a function that determines whether the test suite should skip certain tests
ShouldSkipTest suite.ShouldSkipTestFunc
}
// Setup runs the setup function from the provider and other
// procedures before running the test suite
func (ts *EndToEndTestSuite) Setup() {
if err := ts.setup(); err != nil {
panic(err)
}
// Wait for the virtual kubelet (deployed as a pod) to become fully ready
if _, err := f.WaitUntilPodReady(f.Namespace, f.NodeName); err != nil {
panic(err)
}
}
// Teardown runs the teardown function from the provider and other
// procedures after running the test suite
func (ts *EndToEndTestSuite) Teardown() {
if err := ts.teardown(); err != nil {
panic(err)
}
}
// ShouldSkipTest returns true if a provider wants to skip running a particular test
func (ts *EndToEndTestSuite) ShouldSkipTest(testName string) bool {
return ts.shouldSkipTest(testName)
}
// Run runs tests registered in the test suite
func (ts *EndToEndTestSuite) Run(t *testing.T) {
suite.Run(t, ts)
}
// NewEndToEndTestSuite returns a new EndToEndTestSuite given a test suite configuration,
// setup, and teardown functions from provider
func NewEndToEndTestSuite(cfg EndToEndTestSuiteConfig) *EndToEndTestSuite {
if cfg.Namespace == "" {
panic("Empty namespace")
} else if cfg.NodeName == "" {
panic("Empty node name")
}
if cfg.WatchTimeout == time.Duration(0) {
cfg.WatchTimeout = defaultWatchTimeout
}
f = framework.NewTestingFramework(cfg.Kubeconfig, cfg.Namespace, cfg.NodeName, cfg.WatchTimeout)
emptyFunc := func() error { return nil }
if cfg.Setup == nil {
cfg.Setup = emptyFunc
}
if cfg.Teardown == nil {
cfg.Teardown = emptyFunc
}
if cfg.ShouldSkipTest == nil {
// This will not skip any test in the test suite
cfg.ShouldSkipTest = func(_ string) bool { return false }
}
return &EndToEndTestSuite{
setup: cfg.Setup,
teardown: cfg.Teardown,
shouldSkipTest: cfg.ShouldSkipTest,
}
}