client-go icon indicating copy to clipboard operation
client-go copied to clipboard

How To Test Code That Calls leaderelection.RunOrDie

Open d80tb7 opened this issue 9 months ago • 2 comments

I have an application that uses K8s leader election via leaderelection.RunOrDie(). Unfoertunately I've have greate difficulty writing a unit test for my code becuase of the way that leaderelection.RunOrDie() works. Specifically:

  • I can inject a coordinationv1client.LeasesGetter via the lock in the Config I pass to RunOrDie(). This allows me to control the leader election results.
  • Unfortunately this only solves part of the problem becuase leaderelection.RunOrDie is controlled via an internal clock. Without being able to control this clock it is almost impossible to control the behaviour of the function in the unit test. I think this was realised because this clock has been made configurable for test purposes, but because the clock is packag-private this approach isn't available outside of client-go.

It's therefore not clear to me how I can test my code. Options I have considered are:

  • Changing my code so that instead of calling leaderelection.RunOrDie() I just call a generic func(ctx context.Context, lec LeaderElectionConfig) which I inject at runtime. I dislike this becuase the config you pass in to leaderelection.RunOrDie() is relatively complex and this would give me no real verfication that I'm passing in arguments that make sense.
  • Accepting that I can't unit test this code and try to deal with this via integration tests. This again isn't an attractive option as the integration tests would be relatively complex to set up and race conditions (which are inherant in leder election scenarios) are extremely hard to test in this way.

I think the most practical solution here would be if you made the clock settable outside the packge. Alternatively I wonder if it would be possible to write some simplified facade ovet the leader election client such that it would be so easy to reason about what was going to happen that users would be happy to simply abstract this into an interface and not worry about needing to test integration.

d80tb7 avatar Mar 16 '25 12:03 d80tb7