Context is the public instance of our private context struct. All framework instances and specific suite logic should interact with this object when reading or updating test context
var Context testContext
func HandleFlags()
HandleFlags is the key entrypoint for test configuration for the framework. It sets up flags for providing test configuration files, reading flags from environment variables (flag separators are turned into `_` by ff), registering common framework flags, and copying all of the flags registered via `config.AddOptions` to the flag set. NOTE: Currently must be called from TestMain so that flags are handled by
the time the first test begins.
func RandGKEName(basename string) (string, error)
RandGKEName generates a max length (40) random string that conform to GKE cluster naming requirements. It appends random characters to a base name, e.g. "my-gke-cluster-{24 character random string}". Base name must start with a lowercase letter and has a max length of 38 characters. UTF details are blissfully ignored.
func RegisterCommonFlags(flags *flag.FlagSet)
RegisterCommonFlags registers flags for the framework that are common to all suites (e.g., parameters for Ginkgo or other config that effects how the tests are ran) and sets some defaults for Ginkgo
func ResolvePath(p ...string) string
ResolvePath resolves the provided path relative to the repository root. If a repository root isnt provided via flag, it panics.
Framework represents the internal state of the test runner and provides utilities for tests that leverage the global test context (defined in context.go and populated via flags.go as well as test/framework/config).
The test context is not embedded in the Framework to avoid requiring a global framework: each suite or describe block can initialize its own framework without worrying about binding additional flags or causing the configuration to be re-parsed.
type Framework struct { suite.Suite // A distinct name for the test(s) the framework will be running. It should be // descriptive enough to set it apart from other framework instances. BaseName string // A randomly generated unique name for this specific instance, based on the // BaseName. This allows suites and specs to be ran in parallel (even the same // one). UniqueName string // contains filtered or unexported fields }
func New(baseName string) *Framework
New creates a base framework using a base name provided (typically this is unique per describe block or suite). It instantiates a logger using the io.Writer that Ginkgo provides and registers universal BeforeEach hooks for each spec.
func (f *Framework) AfterEachTest(fns ...Step) *Framework
AfterEach adds a function to the framework instance that is executed after each test in a suite, similar to ginkgo.AfterEach.
func (f *Framework) BeforeEachTest(fns ...Step) *Framework
BeforeEach adds a function to the framework instance that is executed before each test in a suite, similar to ginkgo.BeforeEach. The functions are passed a copy of the framework so that they can set up randomized values based on framework.BaseName, framework.UniqueName, and use framework methods
func (f *Framework) Component(c string) *Framework
Component is syntactic sugar for adding a component label to the test block. Returns a pointer to itself for function chaining. TODO: allow more than one component
func (f *Framework) Disruptive() *Framework
Disruptive is a label that should be applied to all test suites that are likely to disrupt other test cases being ran at the same time.
func (f *Framework) Flaky() *Framework
Flaky is a label that should be applied to tests with inconsistent results.
func (f *Framework) Label(key, value string) *Framework
Label adds a label and a value to the framework to describe the block of tests the framework instance will be used for. Returns a pointer to itself so it can be chained together to add multiple labels:
f := framework.New("my-test-suite") .Label("serial", "true") .Label("foo", "bar")
func (f *Framework) Log(args ...interface{})
Log is syntactic sugar for framework.T().Log, so that it can be used naturally in test suites.
func (f *Framework) Logf(format string, args ...interface{})
Logf is syntactic sugar for framework.T().Logf, so that it can be used naturally in test suites.
func (f *Framework) Register(subs ...SubFramework) *Framework
Register calls `SetupWithFramework()`, allowing a `SubFramework` to add lifecycle steps (e.g., BeforeEachTest), labels, or other modifications to the registering `Framework` instance. This is the main way for additional framework components to integrate lifecycle hooks for a specific suite automatically.
func (f *Framework) Serial() *Framework
Serial is a label that should be applied to all test suites that can't be run in parallel.
func (f *Framework) Setup(fns ...Step) *Framework
Setup adds a lifecycle function that is ran once, before the suite is executed. Similar to `ginkgo.BeforeSuite`.
func (f *Framework) SetupSuite()
SetupSuite runs the added Setup functions once per suite. This is when framework.UniqueName is populated.
It is the function that testify/suite executes to drive the suite: https://pkg.go.dev/github.com/stretchr/testify/suite#SetupAllSuite
func (f *Framework) SetupTest()
SetupTest reasserts its own internal state or creates new randomized values for each test spec to ensure that we can run tests (even the same tests) in parallel repeatedly and handles skipping by labels.
SetupTest is the function that testify/suite executes before each test in a suite: https://pkg.go.dev/github.com/stretchr/testify/suite#SetupTestSuite
func (f *Framework) Skip(ctx string, msg string)
Skip is syntactic sugar for framework.T().Skip, with an extra parameter to make it easier to create contextual skip messages with a uniform format, e.g., Context skipping: Message [KonfigKonnector] skipping: misisng key.json for non-GKE runtime f.Skip("KonfigKonnector", "missing key.json for non-GKE runtime")
func (f *Framework) SkipBasedOnLabels(labels map[string]string)
SkipBasedOnLabels will conditionally call t.Skip() if the test suite should be skipped based on the -labels and -skip-labels flags, by calling skipper.SkipBasedOnLabel.
func (f *Framework) Slow() *Framework
Slow is a label that should be applied to all test suites which take longer than 5 minutes to execute.
func (f *Framework) TearDownSuite()
TearDownSuite runs the added Teardown functions once per suite.
It is the function that testify/suite executes to drive the suite: https://pkg.go.dev/github.com/stretchr/testify/suite#TearDownAllSuite
func (f *Framework) TearDownTest()
TearDownTest runs the added AfterEachTest functions after each test in the suite.
TearDownTest is the function that testify/suite executes after each test in a suite: https://pkg.go.dev/github.com/stretchr/testify/suite#TearDownTestSuite
func (f *Framework) Teardown(fns ...Step) *Framework
Teardown adds a lifecycle function that is ran once, after the suite is executed. Similar to `ginkgo.AfterSuite`
Step is a test lifecycle step function that is passed the Freamwork instance. The framework can be extended by implementing the `SubFramework` interface, allowing life cycle steps to be registered with the framework via `Register`. A typical use case for a Step function is setting up randomized values based on framework.BaseName, framework.UniqueName, and taking advantage of framework.Assertions (e.g., `framework.True()`, `framework.NoError()`)
type Step func(*Framework)
SubFramework
type SubFramework interface { SetupWithFramework(*Framework) }
Name | Synopsis |
---|---|
.. | |
config | Package config simplifies the declaration of configuration options. |
gcp | Package gcp provides test fraemwork utilities and configuration for integration tests against GCP. |
pubsub | |
integration | |
k8s | Package k8s provides test framework utilities for K8s-based unit and integration tests, supporting the ability to do both with the same test suite. |
envtest | Package envtest helps to set up various pieces of sigs.k8s.io/controller-runtime/pkg/envtest framework to simplify writing K8s controller tests |
skipper |