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

How to test SetupWithManager? #3071

Open
pbxqdown opened this issue Jan 14, 2025 · 4 comments
Open

How to test SetupWithManager? #3071

pbxqdown opened this issue Jan 14, 2025 · 4 comments

Comments

@pbxqdown
Copy link

pbxqdown commented Jan 14, 2025

When trying to test default generated controller code, I found that test doesn't cover SetupWithManager function. I tried to create/update resource, but CreateFunc/UpdateFunc defined in Predicates of SetupWithManager is not called.
Is it because envtest package doesn't emit event when there is change? Wondering how should we test CreateFunc/UpdateFunc in this case.
not sure whether this question is more appropriate here or in the kubebuilder repo.

@troy0820
Copy link
Member

It's difficult to see where your test are failing to pick up the changes. If you are using the default kubebuilder scaffold for env test, you may need to modify what you wish to test. In envtest you can

Expect(<name of controller>.SetupWithManager(ctx, mgr)).ToNot(HaveOccurred()) 

during your set up (BeforeSuite) in your suite_test.go and assert on the things that your SetUpWithManager does with predicates in your test file.

Test setup help with KubeBuilder

@pbxqdown
Copy link
Author

pbxqdown commented Jan 15, 2025

Thanks for response @troy0820. I already called SetUpWithManager function in test, would like to test following code inside SetUpWithManager.

nodePredicates := predicate.Funcs{
		CreateFunc: func(e event.CreateEvent) bool {
			return true
		},
		DeleteFunc: func(e event.DeleteEvent) bool {
			// if node is deleted we don't care if we should cordon it.
			return true
		},
                ...

In practice I found that no matter how I create/update nodes in envtest environment, above code is not executed. I suspect it is because envtest doesn't emit event such as node create/delete/update, so controller can not watch predicates, but not very confirmed.
This the code coverage, most uncovered code are in predicate.Funcs: SetupWithManager 11.8%

@troy0820
Copy link
Member

Unless you’re using an existing cluster, keep in mind that no built-in controllers are running in the test context. In some ways, the test control plane will behave differently from “real” clusters, and that might have an impact on how you write tests. One common example is garbage collection; because there are no controllers monitoring built-in resources, objects do not get deleted, even if an OwnerReference is set up.

If you need to see node events, it may make sense to try this on an existing cluster where you can see the events trigger when nodes get updated. If you are just trying to vet the functionality, you could unit test the predicates to see if what changes creates true/false which would trigger the parent object of your reconciler.

@pbxqdown
Copy link
Author

Yes. I updated my test config and it can execute CreateFunc/DeleteFunc now. still not able to execute UpdateFunc, may need to further tweak the test.

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

No branches or pull requests

2 participants