testutil

package
v0.36.0-flux.11 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const OwningInventoryKey = "config.k8s.io/owning-inventory"

OwningInventoryKey is the annotation key indicating the inventory owning an object. This is a copy of inventory.OwningInventoryKey to avoid dependency cycle.

Variables

View Source
var DefaultAsserter = NewAsserter(cmpopts.EquateErrors())

DefaultAsserter is a global Asserter with default comparison options: - EquateErrors (compare with "Is(T) bool" method)

Functions

func AssertEqual

func AssertEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{})

AssertEqual fails the test if the actual value does not deeply equal the expected value. Prints a diff on failure.

func AssertNotEqual

func AssertNotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{})

AssertNotEqual fails the test if the actual value deeply equals the expected value. Prints a diff on failure.

func EqualError

func EqualError(err error) error

EqualError returns an error with an Is(error)bool function that matches any error with the same type and string value as the supplied error.

Use with testutil.Equal to handle error comparisons.

func EqualErrorString

func EqualErrorString(err string) error

EqualErrorString returns an error with an Is(error)bool function that matches any error with the same Error() as the supplied string value.

Use with testutil.Equal to handle error comparisons.

func EqualErrorType

func EqualErrorType(err error) error

EqualErrorType returns an error with an Is(error)bool function that matches any error with the same type as the supplied error.

Use with testutil.Equal to handle error comparisons.

func Mutate

Mutate executes the specified Mutators on the specified object.

func NewFakeRESTMapper

func NewFakeRESTMapper(gvks ...schema.GroupVersionKind) meta.RESTMapper

func SortExpEvents

func SortExpEvents(events []ExpEvent)

SortExpEvents sorts a list of ExpEvents so they can be compared for equality.

This is a stable sort which only sorts nearly identical contiguous events by object identifier, to make the full list easier to validate.

You may need to remove StatusEvents from the list before comparing, because these events are fully asynchronous and non-contiguous.

Comparison Options: A) Expect(received).To(testutil.Equal(expected)) B) testutil.assertEqual(t, expected, received)

func ToIdentifier

func ToIdentifier(t *testing.T, manifest string) object.ObjMetadata

ToIdentifier translates object yaml config into ObjMetadata.

func Unstructured

func Unstructured(t *testing.T, manifest string, mutators ...Mutator) *unstructured.Unstructured

Unstructured translates the passed object config string into an object in Unstructured format. The mutators modify the config yaml before returning the object.

func VerifyEvents

func VerifyEvents(expEvents []ExpEvent, events []event.Event) error

Types

type Asserter

type Asserter struct {
	Options cmp.Options
}

Asserter provides a set of assertion methods that use a shared set of comparison options.

func NewAsserter

func NewAsserter(opts ...cmp.Option) *Asserter

Returns a new Asserter with the specified options.

func (*Asserter) Equal

func (a *Asserter) Equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{})

Equal fails the test if the actual value does not deeply equal the expected value. Prints a diff on failure.

func (*Asserter) EqualMatcher

func (a *Asserter) EqualMatcher(expected interface{}) *EqualMatcher

EqualMatcher returns a new EqualMatcher with the Asserter's options and the specified expected value.

func (*Asserter) NotEqual

func (a *Asserter) NotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{})

NotEqual fails the test if the actual value deeply equals the expected value. Prints a diff on failure.

type EqualMatcher

type EqualMatcher struct {
	Expected interface{}
	Options  cmp.Options
	// contains filtered or unexported fields
}

func Equal

func Equal(expected interface{}) *EqualMatcher

Equal returns a matcher for use with Gomega that uses go-cmp's cmp.Equal to compare and cmp.Diff to show the difference, if there is one.

Example Usage: Expect(receivedEvents).To(testutil.Equal(expectedEvents))

func (*EqualMatcher) FailureMessage

func (cm *EqualMatcher) FailureMessage(actual interface{}) string

func (*EqualMatcher) Match

func (cm *EqualMatcher) Match(actual interface{}) (bool, error)

func (*EqualMatcher) NegatedFailureMessage

func (cm *EqualMatcher) NegatedFailureMessage(actual interface{}) string

type ExpActionGroupEvent

type ExpActionGroupEvent struct {
	GroupName string
	Action    event.ResourceAction
	Type      event.ActionGroupEventStatus
}

type ExpApplyEvent

type ExpApplyEvent struct {
	GroupName  string
	Status     event.ApplyEventStatus
	Identifier object.ObjMetadata
	Error      error
}

type ExpDeleteEvent

type ExpDeleteEvent struct {
	GroupName  string
	Status     event.DeleteEventStatus
	Identifier object.ObjMetadata
	Error      error
}

type ExpErrorEvent

type ExpErrorEvent struct {
	Err error
}

type ExpEvent

type ExpEvent struct {
	EventType event.Type

	InitEvent        *ExpInitEvent
	ErrorEvent       *ExpErrorEvent
	ActionGroupEvent *ExpActionGroupEvent
	ApplyEvent       *ExpApplyEvent
	StatusEvent      *ExpStatusEvent
	PruneEvent       *ExpPruneEvent
	DeleteEvent      *ExpDeleteEvent
	WaitEvent        *ExpWaitEvent
	ValidationEvent  *ExpValidationEvent
}

func EventToExpEvent

func EventToExpEvent(e event.Event) ExpEvent

func EventsToExpEvents

func EventsToExpEvents(events []event.Event) []ExpEvent

func RemoveEqualEvents

func RemoveEqualEvents(in []ExpEvent, expected ExpEvent) ([]ExpEvent, int)

type ExpInitEvent

type ExpInitEvent struct {
}

type ExpPruneEvent

type ExpPruneEvent struct {
	GroupName  string
	Status     event.PruneEventStatus
	Identifier object.ObjMetadata
	Error      error
}

type ExpStatusEvent

type ExpStatusEvent struct {
	Status     status.Status
	Identifier object.ObjMetadata
	Error      error
}

type ExpValidationEvent

type ExpValidationEvent struct {
	Identifiers object.ObjMetadataSet
	Error       error
}

type ExpWaitEvent

type ExpWaitEvent struct {
	GroupName  string
	Status     event.WaitEventStatus
	Identifier object.ObjMetadata
}

type GroupedEventsByID

type GroupedEventsByID []ExpEvent

GroupedEventsByID implements sort.Interface for []ExpEvent based on the serialized ObjMetadata of Apply, Prune, and Delete events within the same task group. This makes testing events easier, because apply/prune/delete order is non-deterministic within each task group. This is only needed if you expect to have multiple apply/prune/delete events in the same task group.

func (GroupedEventsByID) Len

func (ape GroupedEventsByID) Len() int

func (GroupedEventsByID) Less

func (ape GroupedEventsByID) Less(i, j int) bool

func (GroupedEventsByID) Swap

func (ape GroupedEventsByID) Swap(i, j int)

type Mutator

type Mutator interface {
	Mutate(u *unstructured.Unstructured)
}

Mutator inteface defines a function to update an object while translating it unto Unstructured format from yaml config.

func AddDependsOn

func AddDependsOn(t *testing.T, deps ...object.ObjMetadata) Mutator

AddDependsOn returns a testutil.Mutator which adds the passed objects as a depends-on annotation to the object which is mutated. Multiple objects passed in means multiple depends on objects in the annotation separated by a comma.

func AddOwningInv

func AddOwningInv(t *testing.T, inv string) Mutator

AddOwningInv returns a Mutator which adds the passed inv string as the owning inventory annotation.

func DeleteOwningInv

func DeleteOwningInv(t *testing.T, inv string) Mutator

DeleteOwningInv returns a Mutator which deletes the passed inv string from the owning inventory annotation.

type TestFilesystem

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

TestFilesystem creates directories and files for testing

func Setup

func Setup(t *testing.T, dirs ...string) TestFilesystem

Setupd creates directories in the test filesystem, returning the TestFilesystem.

func (TestFilesystem) Clean

func (tf TestFilesystem) Clean()

Clean deletes the test filesystem.

func (TestFilesystem) GetRootDir

func (tf TestFilesystem) GetRootDir() string

GetRootDir returns the path to the root of the test filesystem.

func (TestFilesystem) WriteFile

func (tf TestFilesystem) WriteFile(t *testing.T, path string, value []byte)

WriteFile writes a file in the test filesystem at relative "path" containing the bytes "value".

Jump to

Keyboard shortcuts

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