assert

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package assert provides a function to compare the object used in the test. Use google/go-cmp to compare objects, and output diffs when there are differences.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(t TestingT, manifests *helmut.Manifests, contains runtime.Object, options ...Option) bool

Contains asserts that the specified manifests contains the specified object. If there is a difference in object, fail the test and output diffs.

func ContainsWithRawManifest

func ContainsWithRawManifest(t TestingT, manifests *helmut.Manifests, contains []byte, options ...Option) bool

ContainsWithRawManifest asserts that the specified manifests contains the specified manifest raw data. If there is a difference in object, fail the test and output diffs.

func SortObjectsByNameField added in v0.3.0

func SortObjectsByNameField(x, y interface{}) bool

SortObjectsByNameField is the function used for cmpopts.SortSlices. If the struct slice has a "Name" field, compare and sort in lexicographic order.

Types

type Option

type Option func(*option)

Option is the option used when asserting.

func WithAdditionalKeys added in v0.0.5

func WithAdditionalKeys(fn ...func(helmut.ObjectKey) helmut.ObjectKey) Option

WithAdditionalKeys you can specify a function to generate additional search keys that will be used if the object is not found. The original search key is passed as an argument to the function, you can be overwritten.

When an object is found by the generated key, the group, version, kind, name, namespace of the object are rewritten based on the key. This will prevent the above fields from diffs.

You can pass multiple functions as arguments, but when the generated key finds the object, the execution of the remaining functions will be interrupted and the object comparison process will be performed.

For example, it can be used to ignore the release name given by Helm.

Example of removing prefix from name:

removePrefix := func(key helmut.ObjectKey) helmut.ObjectKey {
	if strings.HasPrefix(key.GetName(), "prefix") {
		key.Name = strings.TrimPrefix(key.GetName(), "prefix-")
	}
	return key
}

assert.Contains(t, manifests, obj, assert.WithAdditionalKeys(removePrefix))

func WithCmpOptions

func WithCmpOptions(opts ...cmp.Option) Option

WithCmpOptions specifies the options to be used when comparing objects with google/go-cmp.

func WithIgnoreAnnotationKeys added in v0.0.2

func WithIgnoreAnnotationKeys(annotations ...string) Option

WithIgnoreAnnotationKeys is an option to ignore diffs for the specified annotations. Annotations will be ignored regardless of the value of value if the key matches.

func WithIgnoreHelmManagedLabels

func WithIgnoreHelmManagedLabels() Option

WithIgnoreHelmManagedLabels is an option to ignore diffs in labels that Helm is supposed to use in general. Labels will be ignored regardless of the value of value if the key matches. If you want to ignore individual labels, please use the WithIgnoreLabelKeys option.

The labels that are ignored are:

app.kubernetes.io/name
app.kubernetes.io/managed-by
app.kubernetes.io/instance
app.kubernetes.io/version
app.kubernetes.io/component
app.kubernetes.io/part-of
helm.sh/chart

see: https://helm.sh/docs/chart_best_practices/labels/

func WithIgnoreLabelKeys added in v0.0.2

func WithIgnoreLabelKeys(labels ...string) Option

WithIgnoreLabelKeys is an option to ignore diffs for the specified labels. Labels will be ignored regardless of the value of value if the key matches.

func WithSortObjectsByNameField added in v0.3.0

func WithSortObjectsByNameField() Option

WithSortObjectsByNameField sorts and compares the "Name" field of an object slice, if it exists.

func WithTransformer added in v0.0.3

func WithTransformer(fn ...func(runtime.Object) runtime.Object) Option

WithTransformer is an option to provide a function to freely transform the object to be compared. For example, you can use it to omit or edit a particular field. The function passed here will be executed just before the comparison and will be applied to both of the two Objects being compared.

Example of omitting the securityContext of a Pod:

omitSecurityContext := func(obj runtime.Object) runtime.Object {
	pod, ok := obj.(*corev1.Pod)
	if !ok {
		return obj
	}

	pod.Spec.SecurityContext = nil

	return pod
}

assert.Contains(t, manifests, obj, assert.WithTransformer(omitSecurityContext))

type TestingT

type TestingT interface {
	Helper()
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
}

TestingT is an interface wrapper around *testing.T.

Jump to

Keyboard shortcuts

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