mirror of
https://github.com/juanfont/headscale.git
synced 2026-09-12 19:42:02 +09:00
3fa4e2dafe
Running go test ./... produced an extreme amount of zerolog output because
test binaries inherited the package default of DebugLevel. Per-test mitigations
were scattered across roughly fifty test functions and most of them were
subtly broken: they restored to DebugLevel instead of the prior level, leaking
state into subsequent tests in the same binary.
Add a single init() in hscontrol/types/testlog.go that detects test mode via
testing.Testing() and lowers the global zerolog level to ErrorLevel. The types
package is transitively imported by every test binary that emits zerolog
output, so this one insertion point silences go test ./... without any
per-package boilerplate. Production binaries are unaffected because
testing.Testing() returns false outside of test execution; the same pattern
already exists in hscontrol/db/{users,node}.go.
Set HEADSCALE_TEST_LOG_LEVEL=trace|debug|info|warn|error|disabled to override
when debugging a specific test.
hscontrol/util/zlog/zlog_test.go asserts on Info-level output from a
zerolog.New(&buf) logger, which is also gated by the global level. Add a
defensive init_test.go in that package pinning the global level to TraceLevel
so the assertions stay reliable if a future import chain pulls
hscontrol/types into the zlog test binary.
Document the mechanism, the env var override, the save/restore idiom for
per-test overrides, and the relevant pitfalls under Testing Guidelines in
AGENTS.md.
Updates #3157
Updates #3169
49 lines
1.6 KiB
Go
49 lines
1.6 KiB
Go
package types
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
// EnvTestLogLevel overrides the default test log level. Accepts any zerolog
|
|
// level string: trace, debug, info, warn, error, fatal, panic, disabled.
|
|
const EnvTestLogLevel = "HEADSCALE_TEST_LOG_LEVEL"
|
|
|
|
// init quiets zerolog when this package is loaded inside a test binary.
|
|
//
|
|
// hscontrol/types is transitively imported by every test in the repo that
|
|
// emits zerolog output, so this init() runs once per test binary and is
|
|
// the only place that needs to know about test logging configuration.
|
|
//
|
|
// Default: ErrorLevel (silent in green-path runs, real errors still surface).
|
|
// Override: HEADSCALE_TEST_LOG_LEVEL=debug (or trace, info, warn, disabled).
|
|
//
|
|
// Production binaries are unaffected because testing.Testing() returns false
|
|
// outside of test execution. The same testing.Testing() pattern is already
|
|
// used in hscontrol/db/users.go and hscontrol/db/node.go, so importing the
|
|
// testing package here is consistent with existing project conventions.
|
|
//
|
|
// Pitfalls:
|
|
// - log.Fatal still calls os.Exit and log.Panic still panics regardless of
|
|
// level — only the rendered message is suppressed.
|
|
// - Local buffer loggers (zerolog.New(&buf)) are also gated by the global
|
|
// level. Tests that assert on log output (currently only
|
|
// hscontrol/util/zlog) re-enable trace level via their own init_test.go.
|
|
func init() {
|
|
if !testing.Testing() {
|
|
return
|
|
}
|
|
|
|
if raw := os.Getenv(EnvTestLogLevel); raw != "" {
|
|
lvl, err := zerolog.ParseLevel(raw)
|
|
if err == nil {
|
|
zerolog.SetGlobalLevel(lvl)
|
|
return
|
|
}
|
|
}
|
|
|
|
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
|
|
}
|