Repository navigation
Stop the runner from silently dropping some of its logs #53
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "log" | ||
| "log/slog" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
| ) | ||
|
|
||
| // lockedBuffer is a bytes.Buffer safe for the concurrent writes a process-wide | ||
| // log destination can receive. | ||
| type lockedBuffer struct { | ||
| mu sync.Mutex | ||
| buf bytes.Buffer | ||
| } | ||
|
|
||
| func (b *lockedBuffer) Write(p []byte) (int, error) { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| return b.buf.Write(p) | ||
| } | ||
|
|
||
| func (b *lockedBuffer) String() string { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| return b.buf.String() | ||
| } | ||
|
|
||
| func (b *lockedBuffer) Reset() { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| b.buf.Reset() | ||
| } | ||
|
|
||
| // installedOutput backs installedHandler. controller-runtime's SetLogger is | ||
| // first-write-wins and cannot be undone, so the handler installLogging is | ||
| // tested with must be the same one on every run in this process (-count=2 | ||
| // included); only its buffer is reset. | ||
| var ( | ||
| installedOutput = &lockedBuffer{} | ||
| installedHandler = slog.NewTextHandler(installedOutput, nil) | ||
| ) | ||
|
|
||
| // These tests cover the two helpers main() and run() call. They cannot prove | ||
| // that main() calls installLogging or that run() builds its root context with | ||
| // newRootContext — neither is reachable from a unit test. | ||
|
|
||
| // Not parallel: installLogging mutates slog's default logger, the stdlib log | ||
| // package's output, and controller-runtime's global logger. | ||
| func TestInstallLogging(t *testing.T) { | ||
| prevWriter, prevFlags := log.Writer(), log.Flags() | ||
|
|
||
| t.Run("slog default and controller-runtime's global logger reach the handler", func(t *testing.T) { | ||
| prevDefault := slog.Default() | ||
| t.Cleanup(func() { | ||
| slog.SetDefault(prevDefault) | ||
| // SetDefault with a non-default handler also redirects the stdlib | ||
| // log package; restoring the previous default does not undo that. | ||
| log.SetOutput(prevWriter) | ||
| log.SetFlags(prevFlags) | ||
| }) | ||
| installedOutput.Reset() | ||
|
|
||
| installLogging(installedHandler) | ||
|
|
||
| slog.Info("via slog default") | ||
| logf.Log.Info("fallback notice publish failed", "session", "ns/demo-agent") | ||
| logf.Log.Error(errors.New("bus closed"), "publish errored") | ||
|
|
||
| out := installedOutput.String() | ||
| assert.Contains(t, out, "via slog default") | ||
| assert.Contains(t, out, "fallback notice publish failed") | ||
| assert.Contains(t, out, "session=ns/demo-agent") | ||
| assert.Contains(t, out, `level=ERROR msg="publish errored" err="bus closed"`) | ||
| }) | ||
|
|
||
| assert.Same(t, prevWriter, log.Writer(), "stdlib log output restored after cleanup") | ||
| assert.Equal(t, prevFlags, log.Flags(), "stdlib log flags restored after cleanup") | ||
| } | ||
|
|
||
| // log.FromContext is how respond_to_user, the dispatch loop and authz slot | ||
| // promotion log. | ||
| func TestNewRootContext_FromContextReachesHandler(t *testing.T) { | ||
| var buf bytes.Buffer | ||
| ctx, cancel := newRootContext(slog.NewTextHandler(&buf, nil)) | ||
|
|
||
| logf.FromContext(ctx).Info("fallback notice publish failed", "session", "ns/demo-agent") | ||
|
|
||
| assert.Contains(t, buf.String(), "fallback notice publish failed") | ||
| assert.Contains(t, buf.String(), "session=ns/demo-agent") | ||
|
|
||
| assert.NoError(t, ctx.Err()) | ||
| cancel() | ||
| assert.ErrorIs(t, ctx.Err(), context.Canceled) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] TestInstallLogging can fail for an unrelated reason if controller-runtime's global logger was already claimed
Problem: controller-runtime's
SetLoggeris first-write-wins. If anything in this test process callslogf.Log/log.FromContext(context.Background())more than 30s after package init withoutSetLoggerhaving run,eventuallyFulfillRoot(controller-runtimepkg/log/log.go:54-70) installs aNullLogSinkpermanently.installLoggingthen becomes a no-op for the global and the threelogf.Logassertions at lines 78-80 fail with no hint why. Not triggered today (package has noTestMain, runs in a few seconds), but it grows with the suite.Verify: optional. Add a throwaway test in a file that sorts before
logging_test.gothat sleeps 31s and then callslogf.Log.Info(...); run it together withTestInstallLogging. controller-runtime prints "log.SetLogger(...) was never called" andTestInstallLoggingfails. The reviewer reproduced this withgo test -overlay.Fix: add a
TestMainininternal/cmd/runnerthat callsinstallLogging(installedHandler)once beforem.Run(), and keep the assertions in the test (resetinstalledOutputfirst). Alternative: leave as is and add a comment here naming the dependency. Either is acceptable; low priority.Test:
go test -race -count=2 -run 'TestInstallLogging|TestNewRootContext' ./internal/cmd/runner/must still pass.