Skip to content

Commit 0f80469

Browse files
committed
komega: add package global functions
allows to use it in a similar way to Gomega
1 parent ccd8ded commit 0f80469

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

pkg/envtest/komega/default.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package komega
2+
3+
import (
4+
"github.com/onsi/gomega"
5+
"k8s.io/apimachinery/pkg/runtime"
6+
"sigs.k8s.io/controller-runtime/pkg/client"
7+
)
8+
9+
// Default is the Komega used by the package global functions.
10+
// NOTE: Replace it with a custom instance or use WithDefaultClient.
11+
// Otherwise the package global functions will panic.
12+
var Default Komega = &komega{}
13+
14+
// WithDefaultClient replaces the default Komega with a new one that uses the given client.
15+
func WithDefaultClient(c client.Client) {
16+
Default = &komega{client: c}
17+
}
18+
19+
func checkDefaultClient() {
20+
if Default.(*komega).client == nil {
21+
panic("Default Komega's client is not set. Use WithDefaultClient to set it.")
22+
}
23+
}
24+
25+
// CreateObject creates a
26+
func CreateObject(obj client.Object, opts ...client.CreateOption) gomega.GomegaAssertion {
27+
checkDefaultClient()
28+
return Default.Create(obj, opts...)
29+
}
30+
31+
// GetObject fetches an object until the forwarded error matches.
32+
func GetObject(obj client.Object) gomega.AsyncAssertion {
33+
checkDefaultClient()
34+
return Default.Get(obj)
35+
}
36+
37+
// ListObject fetches a list until the forwarded error matches.
38+
func ListObject(obj client.ObjectList, opts ...client.ListOption) gomega.AsyncAssertion {
39+
checkDefaultClient()
40+
return Default.List(obj, opts...)
41+
}
42+
43+
// UpdateObject tries to update an object by applying the updateFunc until the forwarded error matches.
44+
func UpdateObject(obj client.Object, f UpdateFunc, opts ...client.UpdateOption) gomega.AsyncAssertion {
45+
checkDefaultClient()
46+
return Default.Update(obj, f, opts...)
47+
}
48+
49+
// UpdateObjectStatus tries to update an object's status by applying the updateFunc until the forwarded error matches.
50+
func UpdateObjectStatus(obj client.Object, f UpdateFunc, opts ...client.UpdateOption) gomega.AsyncAssertion {
51+
checkDefaultClient()
52+
return Default.UpdateStatus(obj, f, opts...)
53+
}
54+
55+
// DeleteObject deletes an object and forwards the error for matching.
56+
func DeleteObject(obj client.Object, opts ...client.DeleteOption) gomega.GomegaAssertion {
57+
checkDefaultClient()
58+
return Default.Delete(obj, opts...)
59+
}
60+
61+
// ConsistentlyObject gets an object using Gomega's Consistently.
62+
// See https://onsi.github.io/gomega/#consistently for how it works.
63+
// It supports listing objects as well.
64+
func ConsistentlyObject(obj runtime.Object, opts ...client.ListOption) gomega.AsyncAssertion {
65+
checkDefaultClient()
66+
return Default.Consistently(obj, opts...)
67+
}
68+
69+
// EventuallyObject gets an object repeatedly until it matches.
70+
// See https://onsi.github.io/gomega/#eventually for how it works.
71+
// It supports listing objects as well.
72+
func EventuallyObject(obj runtime.Object, opts ...client.ListOption) gomega.AsyncAssertion {
73+
checkDefaultClient()
74+
return Default.Eventually(obj, opts...)
75+
}

0 commit comments

Comments
 (0)