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

restore state in unit tests #318

Closed
pohly opened this issue Mar 28, 2022 · 1 comment · Fixed by #320
Closed

restore state in unit tests #318

pohly opened this issue Mar 28, 2022 · 1 comment · Fixed by #320
Assignees
Labels
kind/feature Categorizes issue or PR as related to a new feature.

Comments

@pohly
Copy link

pohly commented Mar 28, 2022

/kind feature

Describe the solution you'd like

There are several unit tests which temporarily modify global klog state, for example to capture log output. There have been several cases where this led to race conditions:

Besides the race conditions related to byte.Buffer there's also the question on how to restore state after a test. In some recent tests I added defer klog.StopFlushDaemon, but that is a new call and many tests don't have it.

I think we should add a klog.CaptureState than can be used like this:

func TestSomething(t *testing.T) {
    defer klog.CaptureState(t)()
}

CaptureState captures the current state and returns a function that can restore that state:

  • all flags
  • output buffer
  • flush daemon

Anything else you would like to add:

Many different tests uses a custom struct with mutex locking around byte.Buffer. This is unnecessarily complex and can be simplified as in kubernetes/kubernetes#108553 by setting a second byte.Buffer before reading the first one.

Long-term, contextual logging can completely replace SetBuffer: kubernetes/kubernetes#98944

@k8s-ci-robot k8s-ci-robot added the kind/feature Categorizes issue or PR as related to a new feature. label Mar 28, 2022
@pohly
Copy link
Author

pohly commented Apr 6, 2022

/assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants