framework

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

README

e2e framework

Enables to easily build and start a number of automated test cases. Depends on ginkgo to describe test suites.

A sample usage is found in examples

Framework

Holds the global configurations for the framework. It mainly:

  1. *rest.Config Kubernetes client configuration
  2. context.Context a common context
  3. *zap.SugaredLogger a logger for test cases

etc.



import (
	"testing"

    // Adds test cases from other packages here
	_ "github.com/katanomi/pkg/examples/sample-e2e/another"
	"github.com/katanomi/pkg/testing/framework"
)

var fmw = framework.New("sample-e2e")

func TestMain(m *testing.M) {
	fmw.SynchronizedBeforeSuite(nil).
		SynchronizedAfterSuite(nil).
		MRun(m)
}

func TestE2E(t *testing.T) {
	// start step to run e2e
	fmw.Run(t)
}

TestCases

Most test cases can be written in a ginkgo fashion with a few helper methods to speedup construction and common logic. Cases can be started with:

  1. TestCase: constructor that receives an Options struct with all options for test case.
  2. P0Case: constructor method to set a name and a priority. Other levels are also available: P1Case, P2Case, P3Case.

After constructing a few more methods:

  1. WithFunc: takes a TestFunction that is given a context in which the test case executes.
  2. Do: finilizes the test case construction
package another

import (
	. "github.com/katanomi/pkg/testing/framework"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)



var _ = TestCase(Options{Name: "Testinge2e", Priority: P0, Scope: NamespaceScoped}).WithFunc(func(ctx TestContext) {
	BeforeEach(func() {
		ctx.Debugw("some debug message")
		// fmt.Println("TestCase BeforeEach", ctx.Config)
	})
	It("should succeed", func() {
		Expect(ctx.Config).ToNot(BeNil())
	})
}).Do()

var _ = P1Case("another-test").Cluster().WithFunc(func(ctx TestContext) {
	// test case
	BeforeEach(func() {
		ctx.Debugw("before each in another pkg")
	})
	AfterEach(func() {
		ctx.Debugw("after each in another pkg")
	})
	Context("With a cluster scoped test case", func() {
		JustBeforeEach(func() {
			ctx.Infow("just before each in another pkg")
		})
		JustAfterEach(func() {
			ctx.Infow("just after each in another pkg")
		})
		It("it", func() {
			Expect(ctx.Config).ToNot(BeNil())
		})
	})
}).Do()

TODO:

  • Add support for traits/external dependencies: Add functions to declare dependencies of other systems/toolings and having independent implementations for each of them.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DurationToFloat

func DurationToFloat(dur time.Duration) float64

DurationToFloat converts a duration into a float64 seconds, useful for Ginkgo methods

Types

type Framework

type Framework struct {
	Name string

	Config *rest.Config

	Scheme *runtime.Scheme

	Context context.Context

	*zap.SugaredLogger

	Output io.Writer

	InitTimeout time.Duration

	sync.Once
}

Framework base framework for running automated test cases

func New

func New(name string) *Framework

New sets a name to framework

func (*Framework) MRun

func (f *Framework) MRun(m *testing.M)

MRun main testing.M run

func (*Framework) Run

func (f *Framework) Run(t *testing.T)

Run start tests

func (*Framework) SynchronizedAfterSuite

func (f *Framework) SynchronizedAfterSuite(destroyFunc func()) *Framework

SynchronizedAfterSuite destroys the whole environment

func (*Framework) SynchronizedBeforeSuite

func (f *Framework) SynchronizedBeforeSuite(initFunc func()) *Framework

SynchronizedBeforeSuite basic before suite initialization

func (*Framework) WithScheme

func (f *Framework) WithScheme(scheme *runtime.Scheme) *Framework

WithScheme adds a scheme object to the framework

type Options

type Options struct {
	// Name oof the test case
	Name string
	// Priority of the test case
	Priority TestCasePriority
	// Scope defines what kind of permissions this test case needs
	Scope TestCaseScope
}

Options options for TestCase

type TestCaseBuilder

type TestCaseBuilder struct {
	// contains filtered or unexported fields
}

TestCaseBuilder builder for TestCases helps provide methods to construct

func P0Case

func P0Case(name string) *TestCaseBuilder

P0Case builds a P0 case

func P1Case

func P1Case(name string) *TestCaseBuilder

P1Case builds a P1 case

func P2Case

func P2Case(name string) *TestCaseBuilder

P2Case builds a P1 case

func P3Case

func P3Case(name string) *TestCaseBuilder

P3Case builds a P1 case

func TestCase

func TestCase(opts Options) *TestCaseBuilder

TestCase constructor for test cases

func (*TestCaseBuilder) Cluster

func (b *TestCaseBuilder) Cluster() *TestCaseBuilder

Cluster set the scope of the testcase as a cluster scoped

func (*TestCaseBuilder) Do

func (b *TestCaseBuilder) Do() bool

Do builds and returns the test case

func (*TestCaseBuilder) Namespaced

func (b *TestCaseBuilder) Namespaced() *TestCaseBuilder

Namespaced set the scope of the testcase as namespaced

func (*TestCaseBuilder) P0

P0 sets as P0

func (*TestCaseBuilder) P1

P1 sets as P1

func (*TestCaseBuilder) P2

P2 sets as P2

func (*TestCaseBuilder) P3

P3 sets as P3

func (*TestCaseBuilder) WithFunc

func (b *TestCaseBuilder) WithFunc(tc TestFunction) *TestCaseBuilder

WithFunc replaces the function with another given function

func (*TestCaseBuilder) WithPriority

func (b *TestCaseBuilder) WithPriority(prior TestCasePriority) *TestCaseBuilder

WithPriority sets priorities

type TestCasePriority

type TestCasePriority uint16

TestCasePriority priority for the testcase

const (
	// P0 critical priority test case
	P0 TestCasePriority = 0
	// P1 high priority test case
	P1 TestCasePriority = 1
	// P2 medium priority test case
	P2 TestCasePriority = 2
	// P3 low priority test case
	P3 TestCasePriority = 3
)

type TestCaseScope

type TestCaseScope string

TestCaseScope scope for test case

const (
	// ClusterScoped cluster test case scope
	ClusterScoped TestCaseScope = "Cluster"
	//NamespaceScoped test case scoped for a namespace
	NamespaceScoped TestCaseScope = "Namespaced"
)

type TestContext

type TestContext struct {
	Context context.Context
	Config  *rest.Config
	Opts    Options
	*zap.SugaredLogger

	Scheme *runtime.Scheme
}

TestContext a test context

type TestFunction

type TestFunction func(TestContext)

TestFunction function used as describe

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL