forked from kubeflow/kubeflow
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #502 from jiridanek/jiridanek-patch-2
RHOAIENG-17634: ref(odh-nbc/tests): create Gomega custom matcher for comparing CRs in kubeflow notebooks tests
- Loading branch information
Showing
2 changed files
with
71 additions
and
28 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
components/odh-notebook-controller/controllers/matchers_test.go
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,52 @@ | ||
package controllers | ||
|
||
import ( | ||
"fmt" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/onsi/gomega/types" | ||
) | ||
|
||
// See https://onsi.github.io/gomega/#adding-your-own-matchers | ||
|
||
// BeMatchingK8sResource is a custom Gomega matcher that compares using `comparator` function and reports differences using | ||
// [cmp.Diff]. It attempts to minimize the diff (TODO(jdanek): not yet implemented) to only include those entries that cause `comparator` to fail. | ||
// | ||
// Use this to replace assertions such as | ||
// > Expect(CompareNotebookRoutes(*route, expectedRoute)).Should(BeTrueBecause(cmp.Diff(*route, expectedRoute))) | ||
// with | ||
// > Expect(*route).To(BeMatchingK8sResource(expectedRoute, CompareNotebookRoutes)) | ||
// | ||
// NOTE: The diff minimization functionality (TODO(jdanek): not yet implemented) is best-effort. It is designed to never under-approximate, but over-approximation is possible. | ||
// NOTE2: Using [gcustom.MakeMatcher] is not possible because it does not conveniently allow running [cmp.Diff] at the time of failure message generation. | ||
func BeMatchingK8sResource[T any](expected T, comparator func(T, T) bool) types.GomegaMatcher { | ||
return &beMatchingK8sResource[T]{ | ||
expected: expected, | ||
comparator: comparator, | ||
} | ||
} | ||
|
||
type beMatchingK8sResource[T any] struct { | ||
expected T | ||
comparator func(r1 T, r2 T) bool | ||
} | ||
|
||
var _ types.GomegaMatcher = &beMatchingK8sResource[any]{} | ||
|
||
func (m *beMatchingK8sResource[T]) Match(actual interface{}) (success bool, err error) { | ||
actualT, ok := actual.(T) | ||
if !ok { | ||
return false, fmt.Errorf("BeMatchingK8sResource matcher expects two objects of the same type") | ||
} | ||
|
||
return m.comparator(m.expected, actualT), nil | ||
} | ||
|
||
func (m *beMatchingK8sResource[T]) FailureMessage(actual interface{}) (message string) { | ||
diff := cmp.Diff(actual, m.expected) | ||
return fmt.Sprintf("Expected\n\t%#v\nto compare identical to\n\t%#v\nbut it differs in\n%s", actual, m.expected, diff) | ||
} | ||
|
||
func (m *beMatchingK8sResource[T]) NegatedFailureMessage(actual interface{}) (message string) { | ||
diff := cmp.Diff(actual, m.expected) | ||
return fmt.Sprintf("Expected\n\t%#v\nto not compare identical to\n\t%#v\nit differs in\n%s", actual, m.expected, diff) | ||
} |
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