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
Add unit tests to nats reconciler #13315
Add unit tests to nats reconciler #13315
Conversation
// Stop reconciliation as the object is being deleted | ||
return ctrl.Result{}, nil | ||
err := r.handleSubscriptionDeletion(ctx, subscription, log) | ||
return ctrl.Result{}, err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) Why don't do it in oneline:
return r.handleSubscriptionDeletion(ctx, subscription, log)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO it's harder to read such oneliners if you mean something like this:
return ctrl.Result{}, r.handleSubscriptionDeletion(ctx, subscription, log)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mfaizanse handleSubscriptionDeletion
only returns an error
. We need ctrl.Result
and error
though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean you can change the return type of handleSubscriptionDeletion
to (ctrl.Result, error)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I could do that, but I think we could do it in this way two, because
- it's only one additional line
- imagine if you would want to add something after the deletion, i.e. an error log, in that case you don't need to return right away
- we already do it both ways in our code
So I would keep that.
components/eventing-controller/controllers/subscription/nats/reconciler.go
Show resolved
Hide resolved
components/eventing-controller/controllers/subscription/nats/unit_test.go
Outdated
Show resolved
Hide resolved
* refactor the reconciler code, * mock the messaging backend
2095f35
to
60bb8ba
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for your work on adding unit tests to the nats controller 💪 👍
components/eventing-controller/controllers/subscription/nats/unit_test.go
Outdated
Show resolved
Hide resolved
components/eventing-controller/controllers/subscription/nats/unit_test.go
Show resolved
Hide resolved
components/eventing-controller/controllers/subscription/nats/unit_test.go
Outdated
Show resolved
Hide resolved
components/eventing-controller/controllers/subscription/nats/unit_test.go
Outdated
Show resolved
Hide resolved
components/eventing-controller/controllers/subscription/nats/unit_test.go
Show resolved
Hide resolved
components/eventing-controller/controllers/subscription/nats/reconciler.go
Show resolved
Hide resolved
components/eventing-controller/controllers/subscription/nats/reconciler.go
Outdated
Show resolved
Hide resolved
components/eventing-controller/controllers/subscription/nats/reconciler.go
Outdated
Show resolved
Hide resolved
// Stop reconciliation as the object is being deleted | ||
return ctrl.Result{}, nil | ||
err := r.handleSubscriptionDeletion(ctx, subscription, log) | ||
return ctrl.Result{}, err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mfaizanse handleSubscriptionDeletion
only returns an error
. We need ctrl.Result
and error
though
components/eventing-controller/controllers/subscription/nats/reconciler.go
Show resolved
Hide resolved
components/eventing-controller/controllers/subscription/nats/unit_test.go
Outdated
Show resolved
Hide resolved
components/eventing-controller/controllers/subscription/nats/unit_test.go
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to merge, just left one comment which is not required for merging :)
} | ||
} | ||
|
||
func ensureSubscriptionMatchesConditionsAndStatus(g *GomegaWithT, subscription eventingv1alpha1.Subscription, expectedConditions []eventingv1alpha1.Condition, expectedStatus bool) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternative to this (which is more the gomega way I guess) would be something like this (example from the testing/matchers.go
file).
func HaveAPIRuleSpecRules(ruleMethods []string, accessStrategy, path string) gomegatypes.GomegaMatcher {
return WithTransform(func(a apigatewayv1alpha1.APIRule) []apigatewayv1alpha1.Rule {
return a.Spec.Rules
}, ContainElement(
MatchFields(IgnoreExtras|IgnoreMissing, Fields{
"Methods": ConsistOf(ruleMethods),
"AccessStrategies": ConsistOf(haveAPIRuleAccessStrategies(accessStrategy)),
"Gateway": Equal(constants.ClusterLocalAPIGateway),
"Path": Equal(path),
}),
))
}
It would read like this then (assuming the helper function is called HaveConditions
instead of HaveAPIRuleSpecRules
):
g.Expect(sub.Status.Conditions).Should(HaveConditions(testCase.expectedConditions))
Oh I just discovered there is already HaveCondition
in the same file already :)
func HaveCondition(condition eventingv1alpha1.Condition) gomegatypes.GomegaMatcher {
return WithTransform(func(s *eventingv1alpha1.Subscription) []eventingv1alpha1.Condition { return s.Status.Conditions }, ContainElement(MatchFields(IgnoreExtras|IgnoreMissing, Fields{
"Type": Equal(condition.Type),
"Reason": Equal(condition.Reason),
"Message": Equal(condition.Message),
"Status": Equal(condition.Status),
})))
}
@VladislavPaskar: The following test failed, say
Full PR test history. Your PR dashboard. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
Description
Changes proposed in this pull request:
Related issue(s)
#13161