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

Add unit tests to nats reconciler #13315

Merged

Conversation

vpaskar
Copy link
Contributor

@vpaskar vpaskar commented Feb 8, 2022

Description

Changes proposed in this pull request:

  • add unit tests for nats subscription controller
  • move some reconciler login into separate function
  • mock MessagingBackend

Related issue(s)
#13161

@vpaskar vpaskar requested a review from a user February 8, 2022 11:54
@kyma-bot kyma-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 8, 2022
@vpaskar vpaskar linked an issue Feb 8, 2022 that may be closed by this pull request
1 task
@vpaskar vpaskar self-assigned this Feb 8, 2022
@vpaskar vpaskar added the area/eventing Issues or PRs related to eventing label Feb 8, 2022
@kyma-project kyma-project deleted a comment from kyma-bot Feb 8, 2022
@nachtmaar nachtmaar self-assigned this Feb 8, 2022
// Stop reconciliation as the object is being deleted
return ctrl.Result{}, nil
err := r.handleSubscriptionDeletion(ctx, subscription, log)
return ctrl.Result{}, err
Copy link
Member

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)

Copy link
Contributor Author

@vpaskar vpaskar Feb 8, 2022

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)

Copy link
Contributor

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

Copy link
Member

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

Copy link
Contributor Author

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.

* refactor the reconciler code,
* mock the messaging backend
@vpaskar vpaskar force-pushed the add-unit-tests-to-nats-reconciler branch from 2095f35 to 60bb8ba Compare February 8, 2022 19:11
Copy link
Contributor

@nachtmaar nachtmaar left a 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 💪 👍

// Stop reconciliation as the object is being deleted
return ctrl.Result{}, nil
err := r.handleSubscriptionDeletion(ctx, subscription, log)
return ctrl.Result{}, err
Copy link
Contributor

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

@kyma-bot kyma-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 9, 2022
@kyma-project kyma-project deleted a comment from kyma-bot Feb 9, 2022
Copy link
Contributor

@nachtmaar nachtmaar left a 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) {
Copy link
Contributor

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),
	})))
}

@kyma-bot kyma-bot added the lgtm Looks good to me! label Feb 9, 2022
@kyma-bot
Copy link
Contributor

kyma-bot commented Feb 9, 2022

@VladislavPaskar: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pre-main-kyma-skr-eventing 4a15f73 link false /test pre-main-kyma-skr-eventing

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.

@kyma-bot kyma-bot merged commit 5cb888c into kyma-project:main Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/eventing Issues or PRs related to eventing lgtm Looks good to me! size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add unit tests to NATS eventing controller
5 participants