Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow rtesting to use custom diff func #590

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

scothis
Copy link
Member

@scothis scothis commented Feb 9, 2025

Test cases can now specify a custom diff function which can be used to either use custom cmp options, or replace cmp with some other differ.

The existing set of cmp options are matched using the DiffReason. Custom funcs should assume additional DiffReasons will be defined in the future. The DefaultDiff func is used if a custom Diff is not defined. Custom funcs may delegate to the default for DiffReasons they choose not to modify.

Refs #589

Test cases can now specify a custom diff function which can be used to
either use custom cmp options, or replace cmp with some other differ.

The existing set of cmp options are matched using the DiffReason. Custom
funcs should assume additional DiffReasons will be defined in the
future. The DefaultDiff func is used if a custom Diff is not defined.
Custom funcs may delegate to the default for DiffReasons they choose not
to modify.

Signed-off-by: Scott Andrews <[email protected]>
@scothis scothis requested a review from mamachanko February 9, 2025 17:39
Comment on lines +25 to +37
type DiffReason float64

const (
DiffReasonRaw DiffReason = iota
DiffReasonTrackRequest
DiffReasonDeleteCollectionRef
DiffReasonStashedValue
DiffReasonResource
DiffReasonWebhookResponse
DiffReasonResourceStatusUpdate
DiffReasonResourceUpdate
DiffReasonResourceCreate
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure this is the best approach to fully decouple the existing behavior without either coupling the public API to cmp, or introducing each as a custom func.

Copy link
Contributor

@mamachanko mamachanko Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative approach could be Differ interface with a default implementation where each "diff reason" maps to a method, e.g. Differ#DiffRaw(actual, expected) string, Differ#DiffTrackRequest(actual, expected) string etc. We'd get a bit of method sprawl, but it may be easier to provide a custom differ without accidentally missing a diff reason. At the same time, maintainers need to take care to always call the right diff method when asserting test case. To some degree that's already the case, what with the different opts / reasons.

type X509CertificateDiffer struct {
	rtesting.DefaultDiffer
}

func (d *X509CertificateDiffer) DiffStashedValue(expected, actual interface{}) string {
	return cmp.Diff(expected, actual, cmp.AllowUnexported(big.Int{}))
}

var _ rtesting.Differ = X509CertificateDiffer{}

Copy link

codecov bot commented Feb 9, 2025

Codecov Report

Attention: Patch coverage is 58.42697% with 37 lines in your changes missing coverage. Please review.

Project coverage is 58.22%. Comparing base (df0175b) to head (3544af8).

Files with missing lines Patch % Lines
testing/diff.go 65.51% 20 Missing ⚠️
testing/subreconciler.go 0.00% 7 Missing ⚠️
testing/reconciler.go 0.00% 5 Missing ⚠️
testing/webhook.go 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #590      +/-   ##
==========================================
- Coverage   58.26%   58.22%   -0.04%     
==========================================
  Files          33       34       +1     
  Lines        3800     3873      +73     
==========================================
+ Hits         2214     2255      +41     
- Misses       1492     1524      +32     
  Partials       94       94              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@mamachanko mamachanko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should work.

However, Diff is starting to compete with testing#SubReconcilerTestCase's
Verify and VerifyStashedValue and testing#ReconcilerTestCase's Verify field. Their propositions are muddling a bit, aren't they? Afaik Diff should be able to accomplish everything that Verify* is designed to do.

Maybe we should start deprecating Verify* in favour of Diff.

Comment on lines +25 to +37
type DiffReason float64

const (
DiffReasonRaw DiffReason = iota
DiffReasonTrackRequest
DiffReasonDeleteCollectionRef
DiffReasonStashedValue
DiffReasonResource
DiffReasonWebhookResponse
DiffReasonResourceStatusUpdate
DiffReasonResourceUpdate
DiffReasonResourceCreate
)
Copy link
Contributor

@mamachanko mamachanko Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative approach could be Differ interface with a default implementation where each "diff reason" maps to a method, e.g. Differ#DiffRaw(actual, expected) string, Differ#DiffTrackRequest(actual, expected) string etc. We'd get a bit of method sprawl, but it may be easier to provide a custom differ without accidentally missing a diff reason. At the same time, maintainers need to take care to always call the right diff method when asserting test case. To some degree that's already the case, what with the different opts / reasons.

type X509CertificateDiffer struct {
	rtesting.DefaultDiffer
}

func (d *X509CertificateDiffer) DiffStashedValue(expected, actual interface{}) string {
	return cmp.Diff(expected, actual, cmp.AllowUnexported(big.Int{}))
}

var _ rtesting.Differ = X509CertificateDiffer{}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants