feature

package
v0.0.0-...-6231fe6 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 19 Imported by: 85

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var LevelMapping = map[Levels]string{
	Must:      "MUST",
	MustNot:   "MUST NOT",
	Should:    "SHOULD",
	ShouldNot: "SHOULD NOT",
	May:       "MAY",
}
View Source
var StatesMapping = map[States]string{
	Alpha:  "Alpha",
	Beta:   "Beta",
	Stable: "Stable",
}

Functions

func AppendRandomString

func AppendRandomString(prefix string) string

AppendRandomString will generate a random string that begins with prefix. This is useful if you want to make sure that your tests can run at the same time against the same environment without conflicting. This method will use "-" as the separator between the prefix and the random suffix.

func ContextWith

func ContextWith(ctx context.Context, f *Feature) context.Context

ContextWith decorates the given context with the provided Feature, and returns the resulting context.

func DeleteResources

func DeleteResources(ctx context.Context, t T, refs []corev1.ObjectReference) error

func GetBaseFuncName

func GetBaseFuncName(fullFuncName string) string

GetBaseFuncName returns the baseFuncName parsed from the fullFuncName. eg. test/e2e.TestMain will return TestMain.

func MakeK8sNamePrefix

func MakeK8sNamePrefix(s string) string

MakeK8sNamePrefix converts each chunk of non-alphanumeric character into a single dash and also convert camelcase tokens into dash-delimited lowercase tokens.

func MakeRandomK8sName

func MakeRandomK8sName(prefix string) string

MakeRandomK8sName

func ObjectNameForTest

func ObjectNameForTest(t Namer) string

ObjectNameForTest generates a random object Name based on the test Name.

func ObjectPrefixForTest

func ObjectPrefixForTest(t Namer) string

ObjectPrefixForTest returns the Name prefix for this test's random names.

func RandomString

func RandomString() string

RandomString will generate a random string.

Types

type Assertable

type Assertable interface {
	Must(name string, fn StepFn) Assertable
	Should(name string, fn StepFn) Assertable
	May(name string, fn StepFn) Assertable
	MustNot(name string, fn StepFn) Assertable
	ShouldNot(name string, fn StepFn) Assertable
}

Assertable is a fluent interface based on Levels for creating an Assert step.

type Asserter

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

func (*Asserter) Assert

func (a *Asserter) Assert(l Levels, name string, fn StepFn)

Assert adds a step function to the feature set at the Assert timing phase.

func (*Asserter) May

func (a *Asserter) May(name string, fn StepFn) Assertable

func (*Asserter) Must

func (a *Asserter) Must(name string, fn StepFn) Assertable

func (*Asserter) MustNot

func (a *Asserter) MustNot(name string, fn StepFn) Assertable

func (*Asserter) Should

func (a *Asserter) Should(name string, fn StepFn) Assertable

func (*Asserter) ShouldNot

func (a *Asserter) ShouldNot(name string, fn StepFn) Assertable

type Feature

type Feature struct {
	Name  string
	Steps []Step
	State state.Store
	// contains filtered or unexported fields
}

Feature is a list of steps and feature name.

func FromContext

func FromContext(ctx context.Context) *Feature

FromContext returns the Feature from Context, if not found FromContext will return nil.

func NewFeature

func NewFeature() *Feature

NewFeature creates a new feature with name taken from the caller name

Example
f := NewFeature()
f.State = &state.KVStore{}
f.Reference(corev1.ObjectReference{
	Kind:       "Pod",
	Namespace:  "ns",
	Name:       "name",
	APIVersion: "v1",
})

f.Setup("step 1", func(ctx context.Context, t T) {})
_ = f.State.Set(context.Background(), "key", "value")

b, err := f.MarshalJSON()
if err != nil {
	panic(err)
}
fmt.Println(string(b))
Output:

{
 "name": "ExampleNewFeature",
 "steps": [
  {
   "name": "step 1",
   "states": "Any",
   "levels": "All",
   "timing": "Setup"
  }
 ],
 "state": {
  "key": "\"value\""
 },
 "refs": [
  {
   "kind": "Pod",
   "namespace": "ns",
   "name": "name",
   "apiVersion": "v1"
  }
 ]
}

func NewFeatureNamed

func NewFeatureNamed(name string) *Feature

NewFeatureNamed creates a new feature with the provided name

func (*Feature) AddStep

func (f *Feature) AddStep(step ...Step)

AddStep appends one or more steps to the Feature.

func (*Feature) Alpha

func (f *Feature) Alpha(name string) Assertable

Alpha is a fluent style method for creating an Assert step in Alpha State.

func (*Feature) Assert

func (f *Feature) Assert(name string, fn StepFn)

Assert is a shortcut for Stable().Must(name, fn), useful for developing integration tests that don't require assertion levels.

func (*Feature) Beta

func (f *Feature) Beta(name string) Assertable

Beta is a fluent style method for creating an Assert step in Beta State.

func (*Feature) DeleteResources

func (f *Feature) DeleteResources(ctx context.Context, t T)

DeleteResources delete all known resources to the Feature registered via `Reference`.

Expected to be used as a StepFn.

func (*Feature) DumpWith

func (f *Feature) DumpWith(log func(args ...interface{}))

DumpWith calls the provided log function with a nicely formatted string that represents the Feature.

func (*Feature) MarshalJSON

func (f *Feature) MarshalJSON() ([]byte, error)

func (*Feature) Prerequisite

func (f *Feature) Prerequisite(name string, fn ShouldRun)

Prerequisite adds a step function to the feature set at the Prerequisite timing phase.

func (*Feature) Reference

func (f *Feature) Reference(ref ...corev1.ObjectReference)

Reference adds references to keep track of for example, for cleaning things after a Feature completes.

func (*Feature) References

func (f *Feature) References() []corev1.ObjectReference

References returns all known resources to the Feature registered via `Reference`.

func (*Feature) Requirement

func (f *Feature) Requirement(name string, fn StepFn)

Requirement adds a step function to the feature set at the Requirement timing phase.

func (*Feature) Setup

func (f *Feature) Setup(name string, fn StepFn)

Setup adds a step function to the feature set at the Setup timing phase.

func (*Feature) Stable

func (f *Feature) Stable(name string) Assertable

Stable is a fluent style method for creating an Assert step in Stable State.

func (*Feature) Teardown

func (f *Feature) Teardown(name string, fn StepFn)

Teardown adds a step function to the feature set at the Teardown timing phase.

type FeatureSet

type FeatureSet struct {
	Name     string
	Features []*Feature
}

FeatureSet is a list of features and feature set name.

type Levels

type Levels uint8
const (
	// Must means that the definition is an absolute requirement of the specification.
	Must Levels = 1 << iota

	// MustNot means that the definition is an absolute prohibition of the specification.
	MustNot

	// Should means that there may exist valid reasons in particular circumstances to
	// ignore a particular item
	Should

	// Should means that there may exist valid reasons in particular circumstances when the
	// particular behavior is acceptable or even useful
	ShouldNot

	// May means that an item is truly optional
	May

	// All flag enables all requirement levels
	All = Must | MustNot | Should | ShouldNot | May
)

func (Levels) MarshalJSON

func (l Levels) MarshalJSON() ([]byte, error)

func (Levels) String

func (l Levels) String() string

type Namer

type Namer interface {
	Name() string
}

type Option

type Option func(f *Feature) error

func WithName

func WithName(name string) Option

type PrerequisiteResult

type PrerequisiteResult struct {
	// ShouldRun is the flag signaling whether other timings will run or not.
	// True means other timings will run, false will skip other timings.
	ShouldRun bool
	// Reason will report why a given prerequisite is not satisfied.
	// This is used to report a clear skip reason to the user.
	Reason string
}

PrerequisiteResult is the result returned by ShouldRun.

type ShouldRun

type ShouldRun func(ctx context.Context, t T) (PrerequisiteResult, error)

ShouldRun is the function signature for Prerequisite steps.

func (ShouldRun) AsStepFn

func (sr ShouldRun) AsStepFn() StepFn

type States

type States uint8
const (
	// Alpha implies a feature is experimental and may change
	Alpha States = 1 << iota

	// Beta implies a feature is complete but may have unknown bugs or issues
	Beta

	// Stable implies a feature is ready for production
	Stable

	// Any flag enables any feature states
	Any = Alpha | Beta | Stable
)

func (States) MarshalJSON

func (s States) MarshalJSON() ([]byte, error)

func (States) String

func (s States) String() string

type Step

type Step struct {
	Name string `json:"name"`
	S    States `json:"states"`
	L    Levels `json:"levels"`
	T    Timing `json:"timing"`
	Fn   StepFn `json:"-"`
}

Step is a structure to hold the step function, step name and state, level and timing configuration.

func (*Step) TestName

func (s *Step) TestName() string

TestName returns the constructed test name based on the timing, step, state, level, and the Name provided in the step.

type StepFn

type StepFn func(ctx context.Context, t T)

StepFn is the function signature for steps.

func LogReferences

func LogReferences(refs ...corev1.ObjectReference) StepFn

func SetStateOrFail

func SetStateOrFail(key string, value interface{}) StepFn

SetStateOrFail augments the state as part of a feature step

func (StepFn) AsFeature

func (sf StepFn) AsFeature(options ...Option) *Feature

AsFeature transforms a step function into a feature running the step function at the Setup phase.

type Steps

type Steps []Step

func (Steps) String

func (ss Steps) String() string

type T

type T interface {
	Name() string

	Log(args ...interface{})
	Logf(format string, args ...interface{})

	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fail()

	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	FailNow()

	Skip(args ...interface{})
	Skipf(format string, args ...interface{})
	SkipNow()

	Failed() bool
	Skipped() bool

	// Note: these are step scoped!
	Cleanup(f func())
	Deadline() (deadline time.Time, ok bool)
}

T is an interface similar to testing.T passed to StepFn to perform logging and assertions

type Timing

type Timing uint8
const (
	// Prerequisite timing allows having steps that are asserting whether the feature should run
	// or not, a failed Prerequisite step will cause the other steps to be skipped.
	Prerequisite Timing = iota
	Setup
	Requirement
	Assert
	Teardown
)

func Timings

func Timings() []Timing

func (Timing) MarshalJSON

func (t Timing) MarshalJSON() ([]byte, error)

func (Timing) String

func (t Timing) String() string

Jump to

Keyboard shortcuts

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