Skip to content

Commit

Permalink
RSDK-7917: Only output TestReconfigureParity logs on failure. (viam…
Browse files Browse the repository at this point in the history
  • Loading branch information
dgottlieb authored Jun 13, 2024
1 parent 5d1125c commit 7d0ed8e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
33 changes: 33 additions & 0 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
"go.viam.com/utils"
)

var (
Expand Down Expand Up @@ -110,3 +111,35 @@ func NewObservedTestLogger(tb testing.TB) (Logger, *observer.ObservedLogs) {

return logger, observedLogs
}

// MemLogger stores test logs in memory. And can write them on request with `OutputLogs`.
type MemLogger struct {
Logger

tb testing.TB
observer *observer.ObservedLogs
}

// OutputLogs writes in-memory logs to the test object MemLogger was constructed with.
func (memLogger *MemLogger) OutputLogs() {
appender := NewTestAppender(memLogger.tb)
for _, loggedEntry := range memLogger.observer.All() {
utils.UncheckedError(appender.Write(loggedEntry.Entry, loggedEntry.Context))
}
}

// NewInMemoryLogger creates a MemLogger that can be used to buffer test logs and output them on
// command. This is handy if a test is noisy, but the output is useful when the test fails.
func NewInMemoryLogger(tb testing.TB) *MemLogger {
observerCore, observedLogs := observer.New(zap.LevelEnablerFunc(zapcore.DebugLevel.Enabled))
logger := &impl{
name: "",
level: NewAtomicLevelAt(DEBUG),
appenders: []Appender{
observerCore,
},
testHelper: tb.Helper,
}

return &MemLogger{logger, tb, observedLogs}
}
8 changes: 7 additions & 1 deletion robot/impl/resource_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1838,13 +1838,19 @@ func TestReconfigureParity(t *testing.T) {
"data/diff_config_deps11.json",
"data/diff_config_deps12.json",
}
logger := logging.NewTestLogger(t)
ctx := context.Background()

testReconfigureParity := func(t *testing.T, initCfg, updateCfg string) {
name := fmt.Sprintf("%s -> %s", initCfg, updateCfg)
t.Run(name, func(t *testing.T) {
t.Parallel()
// Capture logs for this sub-test run. Only output the logs if the test fails.
logger := logging.NewInMemoryLogger(t)
defer func() {
if t.Failed() {
logger.OutputLogs()
}
}()

// Configuration may mutate `*config.Config`, so we read it from
// file each time.
Expand Down

0 comments on commit 7d0ed8e

Please sign in to comment.