controller-runtime icon indicating copy to clipboard operation
controller-runtime copied to clipboard

Allow unittesting indexes on the cache-backed reader

Open alvaroaleman opened this issue 5 years ago • 9 comments

Currently, the only way of testing if:

  • An index actually gets added to the cache
  • Using the index yields the expected result

is to use envtest, which spins up an etcd and apiserver. Doing so works fine, but requires more setup (the binaries must be available) and has a decent chance of flaking in CI, e.G. due to CPU starvation.

It would be nice if there was a way to test the two cases above via pure unittests.

Discussion originally started here: https://github.com/kubernetes/test-infra/pull/14830#discussion_r336232996 /kind feature

alvaroaleman avatar Oct 23 '19 19:10 alvaroaleman

I'm all ears for a good way to do this

DirectXMan12 avatar Nov 05 '19 23:11 DirectXMan12

@DirectXMan12 So what I had in mind would be something like:

  • Add a new pkg/manager/fake that exposes a NewFakeManager(obj ...runtime.Object)
  • All methods on that manager except for GetClient and GetFieldIndexer will error or panic
  • GetClient and GetFieldIndexer can be used to create and use an index, something along the lines of https://github.com/kubernetes/test-infra/blob/f0e45d49993ce6aec651c981bbed9805d016ccc3/prow/tide/tide_test.go#L3192-L3252

WDYT?

alvaroaleman avatar Nov 06 '19 19:11 alvaroaleman

All methods on that manager except for GetClient and GetFieldIndexer will error or panic

:-/ I'm not a huge fan of this, but if we can't figure out a better method, it might be a viable way forward. Usually, this means that there's an overbroad interface, but in this case it's "set of available dependencies", so it might be fine.

DirectXMan12 avatar Dec 03 '19 21:12 DirectXMan12

/help /priority backlog

vincepri avatar Feb 21 '20 17:02 vincepri

@vincepri: This request has been marked as needing help from a contributor.

Please ensure the request meets the requirements listed here.

If this request no longer meets these requirements, the label can be removed by commenting with the /remove-help command.

In response to this:

/help /priority backlog

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.

k8s-ci-robot avatar Feb 21 '20 17:02 k8s-ci-robot

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle stale

fejta-bot avatar May 21 '20 17:05 fejta-bot

/lifecycle frozen

vincepri avatar May 21 '20 18:05 vincepri

For posterity, I did built something for this out-of tree some time ago that might be used as inspiration to add this to controller-runtime directly: https://github.com/kubernetes/test-infra/blob/682727078f0c86e556d9d766bd4ffbf3e2980a56/prow/tide/tide_test.go#L3836-L3923

alvaroaleman avatar Sep 06 '22 17:09 alvaroaleman

It should also be mentioned that the "interact with cluster" part has been decoupled form the "run stuff" part and moved into pkg/cluster from pkg/manager in the meantime. So having something like a pkg/cluster/fake might be a good way forward here

alvaroaleman avatar Sep 06 '22 17:09 alvaroaleman