framework

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 14 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)
}

Configuration

Each project e2e test may depend on some configurations. By default, if these configurations are missing, the test will not execute normally.

The framework has a built-in NewConfigCondition tool method to help us load and manage configurations. By default configuration files are stored in the katanomi-e2e namespace of the target cluster, and the name of the configmap will be prefixed with e2e-config.

func NewConfigCondition(configName string, obj interface{}) *configCondition {
	c := &configCondition{
		name: configName,
		obj:  obj,
	}

	return c
}

Of course, you can use environment variables to change these defaults:

E2E_CONFIG_NAMESPACE
E2E_CONFIG_NAME_PREFIX

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

Examples

Test k8s cluster related features such as controller.

package another

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

var _ = P1Case("test controller").
	WithLabels(ControllerLabel).
	Cluster().
	DoFunc(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())
            })
        })
    })

Test normal features without deploy component to the cluster.

package another

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



var _ = P0Case("normal case").DoFunc(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())
	})
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CaseBuilder added in v0.10.0

type CaseBuilder struct {
	TestCaseBuilder
}

CaseBuilder builder for TestCases

func P0Case

func P0Case(name string) *CaseBuilder

P0Case builds a P0 case

func P1Case

func P1Case(name string) *CaseBuilder

P1Case builds a P1 case

func P2Case

func P2Case(name string) *CaseBuilder

P2Case builds a P1 case

func P3Case

func P3Case(name string) *CaseBuilder

P3Case builds a P1 case

func (*CaseBuilder) AllowSkip added in v0.10.0

func (b *CaseBuilder) AllowSkip() *CaseBuilder

AllowSkip set the test case to skip when condition check failed

func (*CaseBuilder) Cluster added in v0.10.0

func (b *CaseBuilder) Cluster() *cluster.TestCaseBuilder

Cluster convert to cluster test case builder

func (*CaseBuilder) Do added in v0.10.0

func (b *CaseBuilder) Do() bool

Do build and return the test case

func (*CaseBuilder) DoFunc added in v0.10.0

func (b *CaseBuilder) DoFunc(f TestSpecFunc) bool

DoFunc build and return the test case, just like the Do function

func (*CaseBuilder) DoNotSkip added in v0.10.0

func (b *CaseBuilder) DoNotSkip() *CaseBuilder

DoNotSkip set the test case to not skip when condition check failed

func (*CaseBuilder) DoWithContext added in v0.11.0

func (b *CaseBuilder) DoWithContext(ctx context.Context) bool

DoWithContext run a test case with special context The context can be used for construct case layouts

func (*CaseBuilder) P0 added in v0.10.0

func (b *CaseBuilder) P0() *CaseBuilder

P0 sets as P0

func (*CaseBuilder) P1 added in v0.10.0

func (b *CaseBuilder) P1() *CaseBuilder

P1 sets as P1

func (*CaseBuilder) P2 added in v0.10.0

func (b *CaseBuilder) P2() *CaseBuilder

P2 sets as P2

func (*CaseBuilder) P3 added in v0.10.0

func (b *CaseBuilder) P3() *CaseBuilder

P3 sets as P3

func (*CaseBuilder) WithCondition added in v0.10.0

func (b *CaseBuilder) WithCondition(funcs ...Condition) *CaseBuilder

WithCondition sets conditions

func (*CaseBuilder) WithFunc added in v0.10.0

func (b *CaseBuilder) WithFunc(tc TestSpecFunc) *CaseBuilder

WithFunc replaces the function with another given function

func (*CaseBuilder) WithLabels added in v0.10.0

func (b *CaseBuilder) WithLabels(labels ...interface{}) *CaseBuilder

WithLabels sets labels

func (*CaseBuilder) WithPriority added in v0.10.0

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

WithPriority sets priorities

type Framework

type Framework struct {
	Name string

	TestContext
	// contains filtered or unexported fields
}

Framework base framework for running automated test cases

func New

func New(name string) *Framework

New sets a name to framework

func (*Framework) Config

func (f *Framework) Config(configures ...Configure) *Framework

Config register configuration mutators which executed before case running

func (*Framework) Extensions added in v0.10.0

func (f *Framework) Extensions(extensions ...SharedExtension) *Framework

Run start tests

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) WithContext added in v0.10.0

func (f *Framework) WithContext(ctx context.Context) *Framework

Directories

Path Synopsis
Package conformance is a framework for conformance test
Package conformance is a framework for conformance test

Jump to

Keyboard shortcuts

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