Skip to content

Commit

Permalink
komega: add package global functions
Browse files Browse the repository at this point in the history
allows to use it in a similar way to Gomega
  • Loading branch information
schrej committed Jan 13, 2022
1 parent c52886b commit be1fcf6
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions pkg/envtest/komega/default.go
@@ -0,0 +1,75 @@
package komega

import (
"github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// Default is the Komega used by the package global functions.
// NOTE: Replace it with a custom instance or use WithDefaultClient.
// Otherwise the package global functions will panic.
var Default Komega = &komega{}

// WithDefaultClient replaces the default Komega with a new one that uses the given client.
func WithDefaultClient(c client.Client) {
Default = &komega{client: c}
}

func checkDefaultClient() {
if Default.(*komega).client == nil {
panic("Default Komega's client is not set. Use WithDefaultClient to set it.")
}
}

// CreateObject creates a
func CreateObject(obj client.Object, opts ...client.CreateOption) gomega.GomegaAssertion {
checkDefaultClient()
return Default.Create(obj, opts...)
}

// GetObject fetches an object until the forwarded error matches.
func GetObject(obj client.Object) gomega.AsyncAssertion {
checkDefaultClient()
return Default.Get(obj)
}

// ListObject fetches a list until the forwarded error matches.
func ListObject(obj client.ObjectList, opts ...client.ListOption) gomega.AsyncAssertion {
checkDefaultClient()
return Default.List(obj, opts...)
}

// UpdateObject tries to update an object by applying the updateFunc until the forwarded error matches.
func UpdateObject(obj client.Object, f UpdateFunc, opts ...client.UpdateOption) gomega.AsyncAssertion {
checkDefaultClient()
return Default.Update(obj, f, opts...)
}

// UpdateObjectStatus tries to update an object's status by applying the updateFunc until the forwarded error matches.
func UpdateObjectStatus(obj client.Object, f UpdateFunc, opts ...client.UpdateOption) gomega.AsyncAssertion {
checkDefaultClient()
return Default.UpdateStatus(obj, f, opts...)
}

// DeleteObject deletes an object and forwards the error for matching.
func DeleteObject(obj client.Object, opts ...client.DeleteOption) gomega.GomegaAssertion {
checkDefaultClient()
return Default.Delete(obj, opts...)
}

// ConsistentlyObject gets an object using Gomega's Consistently.
// See https://onsi.github.io/gomega/#consistently for how it works.
// It supports listing objects as well.
func ConsistentlyObject(obj runtime.Object, opts ...client.ListOption) gomega.AsyncAssertion {
checkDefaultClient()
return Default.Consistently(obj, opts...)
}

// EventuallyObject gets an object repeatedly until it matches.
// See https://onsi.github.io/gomega/#eventually for how it works.
// It supports listing objects as well.
func EventuallyObject(obj runtime.Object, opts ...client.ListOption) gomega.AsyncAssertion {
checkDefaultClient()
return Default.Eventually(obj, opts...)
}

0 comments on commit be1fcf6

Please sign in to comment.