tailscale/util/syspolicy/internal/loggerx/logger_test.go
Nick Khyl da40609abd util/syspolicy, ipn: add "tailscale debug component-logs" support
Fixes #13313
Fixes #12687

Signed-off-by: Nick Khyl <nickk@tailscale.com>
2024-10-08 18:11:23 -05:00

54 lines
1.4 KiB
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package loggerx
import (
"fmt"
"io"
"strings"
"testing"
"tailscale.com/types/logger"
)
func TestDebugLogging(t *testing.T) {
var normal, verbose strings.Builder
SetForTest(t, logfTo(&normal), logfTo(&verbose))
checkOutput := func(wantNormal, wantVerbose string) {
t.Helper()
if gotNormal := normal.String(); gotNormal != wantNormal {
t.Errorf("Unexpected normal output: got %q; want %q", gotNormal, wantNormal)
}
if gotVerbose := verbose.String(); gotVerbose != wantVerbose {
t.Errorf("Unexpected verbose output: got %q; want %q", gotVerbose, wantVerbose)
}
normal.Reset()
verbose.Reset()
}
Errorf("This is an error message: %v", 42)
checkOutput("This is an error message: 42", "")
Verbosef("This is a verbose message: %v", 17)
checkOutput("", "This is a verbose message: 17")
SetDebugLoggingEnabled(true)
Errorf("This is an error message: %v", 42)
checkOutput("This is an error message: 42", "")
Verbosef("This is a verbose message: %v", 17)
checkOutput("This is a verbose message: 17", "")
SetDebugLoggingEnabled(false)
Errorf("This is an error message: %v", 42)
checkOutput("This is an error message: 42", "")
Verbosef("This is a verbose message: %v", 17)
checkOutput("", "This is a verbose message: 17")
}
func logfTo(w io.Writer) logger.Logf {
return func(format string, args ...any) {
fmt.Fprintf(w, format, args...)
}
}