assertions

package
v0.0.0-...-1ceadb6 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertThat

func AssertThat(t *testing.T, object client.Object, predicates ...Predicate[client.Object])

AssertThat is a helper function that tests that the provided object satisfies given predicate. It is a exactly implemented as:

assert.True(t, predicate.Matches(object))

but provides better readability. Instead of:

assert.True(t, Is(Named("asdf")).Matches(object))

one can write:

AssertThat(t, object, Is(Named("asdf")))

Note that this method accepts multiple predicates and reports any failures in them using the Explain function.

func Explain

func Explain[T client.Object](predicate Predicate[client.Object], actual T) string

Explain produces a textual explanation for why the provided predicate didn't match. The explanation contains the type name of the predicate, the type of the object and, if the predicate implements PredicateMatchFixer interface, a diff between what the object looks like and should have looked like to match the predicate. This is best used for logging the explanation of test failures in the end to end tests.

The lines beginning with "-" are what was expected, "+" marks the actual values.

Note that this function doesn't actually check if the predicate matches the object so it can produce slightly misleading output if called with a predicate that matches given object.

Types

type Predicate

type Predicate[T client.Object] interface {
	Matches(obj T) bool
}

Predicate is a generic predicate for testing whether some object of type T has some quality. It is best used with the `AssertThat` function or with the `wait.For(...).FirstThat(...)` function in end to end tests using the Is function as a helper to satisfy the method signatures and help Go's type inference.

Note that if you're implementing your own predicates, it is helpful for the constructor function to not return a concrete type but the generic Predicate[YourObjectType]. This helps the Go compiler to be able to infer and match up the types correctly.

E.g. if one would want to implement a predicate checking that a ToolchainCluster CR has the ready condition checked, one might implement a constructor function for that predicate like this:

type toochainClusterReady struct {}

func (t *toolchainClusterReady) Matches(c *toolchainv1alpha1.ToolchainCluster) bool {
  return condition.IsTrue(c.Status.Conditions, toolchainv1alpha1.ConditionReady)
}

func Ready() predicates.Predicate[*toolchainv1alpha1.ToolchainCluster] {
  return &toolchainClusterReady{}
}

Such predicate can then easily be used with the `AssertThat` function (or `wait.For(...).FirstThat(...)` from toolchain-e2e which does something very similar but waits for an object that satisfies the predicates to appear in the cluster).

assertions.AssertThat(t, toolchainCluster, assertions.Is(Ready()))

If you're implementing your own predicate, consider implementing the PredicateMatchFixer, too, so that you can benefit from improved failure diagnostics offered by Explain function.

func Annotations

func Annotations(requiredAnnotations map[string]string) Predicate[client.Object]

Annotations returns a predicate checking that an Object has provided annotations and their values.

func Has

Has is just an alias of Is. It is provided for better readability with certain predicate names.

func InNamespace

func InNamespace(name string) Predicate[client.Object]

InNamespace returns a predicate checking that an Object is in the given namespace.

func Is

Is merely casts the generic predicate on type T to a predicate on client.Object. This is always valid because T is required to implement client.Object. Using this function helps readability of the code by being able to construct expressions like:

predicates.Is(predicates.Named("whatevs"))

func Labels

func Labels(requiredLabels map[string]string) Predicate[client.Object]

Labels returns a predicate checking that an Object has provided labels and their values.

func Name

func Name(name string) Predicate[client.Object]

Name returns a predicate checking that an Object has given name.

func ObjectKey

func ObjectKey(key types.NamespacedName) Predicate[client.Object]

ObjectKey returns a predicate checking that an Object has given NamespacedName (aka client.ObjectKey).

type PredicateMatchFixer

type PredicateMatchFixer[T client.Object] interface {
	FixToMatch(obj T) T
}

PredicateMatchFixer is an optional interface that the predicate implementations can also implement. If so, the FixToMatch method is used to obtain an object that WOULD match the predicate. This would-be-matching object is then used to produce a diff between it and the non-matching object of the predicate in case of a test failure for logging purposes.

There is no need to copy the provided object.

Jump to

Keyboard shortcuts

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