komega

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package komega is a modified version of sigs.k8s.io/controller-runtime/pkg/envtest/komega. Instead of requiring users to set a global context, the returned functions accept a context to integrate nicely with ginkgo's/gomega's timeout/interrupt handling and polling. For this, users must pass a spec-specific context, e.g.:

It("...", func(ctx SpecContext) {
	Eventually(ctx, Get(...)).Should(Succeed())
})

Index

Constants

This section is empty.

Variables

View Source
var (
	// IgnoreAutogeneratedMetadata contains the paths for all the metadata fields that are commonly set by the
	// client and APIServer. This is used as a MatchOption for situations when only user-provided metadata is relevant.
	IgnoreAutogeneratedMetadata = IgnorePaths{
		"metadata.uid",
		"metadata.generation",
		"metadata.creationTimestamp",
		"metadata.resourceVersion",
		"metadata.managedFields",
		"metadata.deletionGracePeriodSeconds",
		"metadata.deletionTimestamp",
		"metadata.selfLink",
		"metadata.generateName",
	}
)

These package variables hold pre-created commonly used options that can be used to reduce the manual work involved in identifying the paths that need to be compared for testing equality between objects.

Functions

func EqualObject

func EqualObject(original runtime.Object, opts ...EqualObjectOption) types.GomegaMatcher

EqualObject returns a Matcher for the passed Kubernetes runtime.Object with the passed Options. This function can be used as a Gomega Matcher in Gomega Assertions.

func Get

func Get(obj client.Object) func(context.Context) error

Get returns a function that fetches a resource and returns the occurring error. It can be used with gomega.Eventually() like this

deployment := appsv1.Deployment{ ... }
gomega.Eventually(komega.Get(&deployment)).To(gomega.Succeed())

By calling the returned function directly it can also be used with gomega.Expect(komega.Get(...)(ctx)).To(...)

func List

func List(list client.ObjectList, opts ...client.ListOption) func(context.Context) error

List returns a function that lists resources and returns the occurring error. It can be used with gomega.Eventually() like this

deployments := v1.DeploymentList{ ... }
gomega.Eventually(k.List(&deployments)).To(gomega.Succeed())

By calling the returned function directly it can also be used as gomega.Expect(k.List(...)(ctx)).To(...)

func Object

func Object(obj client.Object) func(context.Context) (client.Object, error)

Object returns a function that fetches a resource and returns the object. It can be used with gomega.Eventually() like this:

deployment := appsv1.Deployment{ ... }
gomega.Eventually(k.Object(&deployment)).To(HaveField("Spec.Replicas", gomega.Equal(ptr.To(3))))

By calling the returned function directly it can also be used as gomega.Expect(k.Object(...)(ctx)).To(...)

func ObjectList

func ObjectList(list client.ObjectList, opts ...client.ListOption) func(context.Context) (client.ObjectList, error)

ObjectList returns a function that fetches a resource and returns the object. It can be used with gomega.Eventually() like this:

deployments := appsv1.DeploymentList{ ... }
gomega.Eventually(k.ObjectList(&deployments)).To(HaveField("Items", HaveLen(1)))

By calling the returned function directly it can also be used as gomega.Expect(k.ObjectList(...)(ctx)).To(...)

func SetClient

func SetClient(c client.Client)

SetClient sets the client used by the package global functions.

func Update

func Update(obj client.Object, f func(), opts ...client.UpdateOption) func(context.Context) error

Update returns a function that fetches a resource, applies the provided update function and then updates the resource. It can be used with gomega.Eventually() like this:

deployment := appsv1.Deployment{ ... }
gomega.Eventually(k.Update(&deployment, func() {
  deployment.Spec.Replicas = 3
})).To(gomega.Succeed())

By calling the returned function directly it can also be used as gomega.Expect(k.Update(...)(ctx)).To(...)

func UpdateStatus

func UpdateStatus(obj client.Object, f func(), opts ...client.SubResourceUpdateOption) func(context.Context) error

UpdateStatus returns a function that fetches a resource, applies the provided update function and then updates the resource's status. It can be used with gomega.Eventually() like this:

deployment := appsv1.Deployment{ ... }
gomega.Eventually(k.UpdateStatus(&deployment, func() {
  deployment.Status.AvailableReplicas = 1
})).To(gomega.Succeed())

By calling the returned function directly it can also be used as gomega.Expect(k.UpdateStatus(...)(ctx)).To(...)

Types

type EqualObjectOption

type EqualObjectOption interface {
	// ApplyToEqualObjectMatcher applies this configuration to the given MatchOption.
	ApplyToEqualObjectMatcher(options *EqualObjectOptions)
}

EqualObjectOption describes an Option that can be applied to a Matcher.

type EqualObjectOptions

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

EqualObjectOptions holds the available types of EqualObjectOptions that can be applied to a Matcher.

func (*EqualObjectOptions) ApplyOptions

func (o *EqualObjectOptions) ApplyOptions(opts []EqualObjectOption) *EqualObjectOptions

ApplyOptions adds the passed MatchOptions to the MatchOptions struct.

type IgnorePaths

type IgnorePaths []string

IgnorePaths instructs the Matcher to ignore given paths when computing a diff. Paths are written in a syntax similar to Go with a few special cases. Both types and json/yaml field names are supported.

Regular Paths: * "ObjectMeta.Name" * "metadata.name" Arrays: * "metadata.ownerReferences[0].name" Maps, if they do not contain any of .[]/\: * "metadata.labels.something" Maps, if they contain any of .[]/\: * "metadata.labels[kubernetes.io/something]"

func (IgnorePaths) ApplyToEqualObjectMatcher

func (i IgnorePaths) ApplyToEqualObjectMatcher(opts *EqualObjectOptions)

ApplyToEqualObjectMatcher applies this configuration to the given MatchOptions.

type Komega

type Komega interface {
	// Get returns a function that fetches a resource and returns the occurring error.
	// It can be used with gomega.Eventually() like this
	//   deployment := appsv1.Deployment{ ... }
	//   gomega.Eventually(k.Get(&deployment)).To(gomega.Succeed())
	// By calling the returned function directly it can also be used with gomega.Expect(k.Get(...)(ctx)).To(...)
	Get(client.Object) func(context.Context) error

	// List returns a function that lists resources and returns the occurring error.
	// It can be used with gomega.Eventually() like this
	//   deployments := v1.DeploymentList{ ... }
	//   gomega.Eventually(k.List(&deployments)).To(gomega.Succeed())
	// By calling the returned function directly it can also be used as gomega.Expect(k.List(...)(ctx)).To(...)
	List(client.ObjectList, ...client.ListOption) func(context.Context) error

	// Update returns a function that fetches a resource, applies the provided update function and then updates the resource.
	// It can be used with gomega.Eventually() like this:
	//   deployment := appsv1.Deployment{ ... }
	//   gomega.Eventually(k.Update(&deployment, func() {
	//     deployment.Spec.Replicas = 3
	//   })).To(gomega.Succeed())
	// By calling the returned function directly it can also be used as gomega.Expect(k.Update(...)(ctx)).To(...)
	Update(client.Object, func(), ...client.UpdateOption) func(context.Context) error

	// UpdateStatus returns a function that fetches a resource, applies the provided update function and then updates the resource's status.
	// It can be used with gomega.Eventually() like this:
	//   deployment := appsv1.Deployment{ ... }
	//   gomega.Eventually(k.Update(&deployment, func() {
	//     deployment.Status.AvailableReplicas = 1
	//   })).To(gomega.Succeed())
	// By calling the returned function directly it can also be used as gomega.Expect(k.UpdateStatus(...)(ctx)).To(...)
	UpdateStatus(client.Object, func(), ...client.SubResourceUpdateOption) func(context.Context) error

	// Object returns a function that fetches a resource and returns the object.
	// It can be used with gomega.Eventually() like this:
	//   deployment := appsv1.Deployment{ ... }
	//   gomega.Eventually(k.Object(&deployment)).To(HaveField("Spec.Replicas", gomega.Equal(ptr.To(int32(3)))))
	// By calling the returned function directly it can also be used as gomega.Expect(k.Object(...)(ctx)).To(...)
	Object(client.Object) func(context.Context) (client.Object, error)

	// ObjectList returns a function that fetches a resource and returns the object.
	// It can be used with gomega.Eventually() like this:
	//   deployments := appsv1.DeploymentList{ ... }
	//   gomega.Eventually(k.ObjectList(&deployments)).To(HaveField("Items", HaveLen(1)))
	// By calling the returned function directly it can also be used as gomega.Expect(k.ObjectList(...)(ctx)).To(...)
	ObjectList(client.ObjectList, ...client.ListOption) func(context.Context) (client.ObjectList, error)
}

Komega is a collection of utilites for writing tests involving a mocked Kubernetes API.

func New

func New(c client.Client) Komega

New creates a new Komega instance with the given client.

type MatchPaths

type MatchPaths []string

MatchPaths instructs the Matcher to restrict its diff to the given paths. If empty the Matcher will look at all paths. Paths are written in a syntax similar to Go with a few special cases. Both types and json/yaml field names are supported.

Regular Paths: * "ObjectMeta.Name" * "metadata.name" Arrays: * "metadata.ownerReferences[0].name" Maps, if they do not contain any of .[]/\: * "metadata.labels.something" Maps, if they contain any of .[]/\: * "metadata.labels[kubernetes.io/something]"

func (MatchPaths) ApplyToEqualObjectMatcher

func (i MatchPaths) ApplyToEqualObjectMatcher(opts *EqualObjectOptions)

ApplyToEqualObjectMatcher applies this configuration to the given MatchOptions.

Jump to

Keyboard shortcuts

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