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

Merged
merged 3 commits into from
Feb 11, 2025
Merged

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
testing/diff.go Outdated
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 a 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
Member Author

Choose a reason for hiding this comment

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

One of the things I like about using dedicated methods is that additional type safety can be introduced, or in the case of stash, additional metadata can be added like the stash key.

Copy link

codecov bot commented Feb 9, 2025

Codecov Report

Attention: Patch coverage is 57.33333% with 32 lines in your changes missing coverage. Please review.

Project coverage is 59.35%. Comparing base (df0175b) to head (655f2ce).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
testing/diff.go 64.44% 16 Missing ⚠️
testing/subreconciler.go 0.00% 7 Missing ⚠️
testing/webhook.go 0.00% 5 Missing ⚠️
testing/reconciler.go 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #590      +/-   ##
==========================================
+ Coverage   58.26%   59.35%   +1.09%     
==========================================
  Files          33       35       +2     
  Lines        3800     3981     +181     
==========================================
+ Hits         2214     2363     +149     
- Misses       1492     1522      +30     
- Partials       94       96       +2     

☔ 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.

testing/diff.go Outdated
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 a 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{}

@scothis scothis requested a review from mamachanko February 10, 2025 21:35
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.

➕1️⃣ for Differ!

LGMT

@scothis scothis merged commit 3cb9f76 into reconcilerio:main Feb 11, 2025
3 of 4 checks passed
@scothis scothis deleted the differ branch February 11, 2025 17:21
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