-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The test toolkit and courtesies are things that other policies could use in their tests. The remaining functions in the utils.go file are more specifically just for fakepolicy. In preparation for more tests, the existing tests have been moved into a separate package/suite. In particular, since some of them require very specific details in the cluster (exact namespaces, configmaps, and no extras), this lets them be run in a more isolated way. Signed-off-by: Justin Kulikauskas <[email protected]>
- Loading branch information
1 parent
ac65408
commit 343128b
Showing
17 changed files
with
160 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright Contributors to the Open Cluster Management project | ||
|
||
package testutils | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func ObjNN(obj client.Object) types.NamespacedName { | ||
return types.NamespacedName{ | ||
Namespace: obj.GetNamespace(), | ||
Name: obj.GetName(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright Contributors to the Open Cluster Management project | ||
|
||
package testutils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
gomegaTypes "github.com/onsi/gomega/types" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type Toolkit struct { | ||
client.Client | ||
EventuallyPoll string | ||
EventuallyTimeout string | ||
ConsistentlyPoll string | ||
ConsistentallyTimeout string | ||
BackgroundCtx context.Context //nolint:containedctx | ||
} | ||
|
||
func NewToolkit(client client.Client) Toolkit { | ||
return Toolkit{ | ||
Client: client, | ||
EventuallyPoll: "100ms", | ||
EventuallyTimeout: "1s", | ||
ConsistentlyPoll: "100ms", | ||
ConsistentallyTimeout: "1s", | ||
BackgroundCtx: context.Background(), | ||
} | ||
} | ||
|
||
// cleanlyCreate creates the given object, and registers a callback to delete the object which | ||
// Ginkgo will call at the appropriate time. The error from the `Create` call is returned (so it | ||
// can be checked) and the `Delete` callback handles 'NotFound' errors as a success. | ||
func (tk Toolkit) CleanlyCreate(ctx context.Context, obj client.Object) error { | ||
// Save and then re-set the GVK because the API call removes it | ||
savedGVK := obj.GetObjectKind().GroupVersionKind() | ||
createErr := tk.Create(ctx, obj) | ||
obj.GetObjectKind().SetGroupVersionKind(savedGVK) | ||
|
||
if createErr == nil { | ||
ginkgo.DeferCleanup(func() { | ||
ginkgo.GinkgoWriter.Printf("Deleting %v %v/%v\n", | ||
obj.GetObjectKind().GroupVersionKind().Kind, obj.GetNamespace(), obj.GetName()) | ||
|
||
if err := tk.Delete(tk.BackgroundCtx, obj); err != nil { | ||
if !errors.IsNotFound(err) { | ||
// Use Fail in order to provide a custom message with useful information | ||
ginkgo.Fail(fmt.Sprintf("Expected success or 'NotFound' error, got %v", err), 1) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
return createErr | ||
} | ||
|
||
// EC runs assertions on asynchronous behavior, both *E*ventually and *C*onsistently, | ||
// using the polling and timeout settings of the toolkit. Its usage should feel familiar | ||
// to gomega users, simply skip the `.Should(...)` call and put your matcher as the second | ||
// parameter here. | ||
func (tk Toolkit) EC( | ||
actualOrCtx interface{}, matcher gomegaTypes.GomegaMatcher, optionalDescription ...interface{}, | ||
) bool { | ||
gomega.Eventually( | ||
actualOrCtx, tk.EventuallyTimeout, tk.EventuallyPoll, | ||
).Should(matcher, optionalDescription...) | ||
|
||
return gomega.Consistently( | ||
actualOrCtx, tk.ConsistentallyTimeout, tk.ConsistentlyPoll, | ||
).Should(matcher, optionalDescription...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.