mirror of
https://github.com/juanfont/headscale.git
synced 2026-09-14 04:22:01 +09:00
d3a6afdbf0
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.
19 lines
732 B
Go
19 lines
732 B
Go
package zlog
|
|
|
|
import "github.com/rs/zerolog"
|
|
|
|
// init pins zerolog to TraceLevel for the zlog test binary.
|
|
//
|
|
// zlog's tests use zerolog.New(&buf) and assert on Info-level output. zerolog's
|
|
// (*Logger).should() gates emission on the global level, so any global level
|
|
// above Info would silently break the assertions.
|
|
//
|
|
// Today zlog does not transitively import hscontrol/types, so the test
|
|
// silencing init() in hscontrol/types/testlog.go does not run in this binary.
|
|
// This init defends against that changing in the future: if a future import
|
|
// chain pulls in hscontrol/types, this file will still ensure trace-level
|
|
// output is available for zlog's assertions.
|
|
func init() {
|
|
zerolog.SetGlobalLevel(zerolog.TraceLevel)
|
|
}
|