fix(tests): properly silence log output (#1259)

* fix(tests): properly silence log output

Using `init` allows it to also work for benchmarks.
And `log.Silence` was sometimes getting overridden by `log.init`.

* squash: fix(server): don't setup the logger again
This commit is contained in:
ThinkChaos 2023-11-19 17:51:49 -05:00 committed by GitHub
parent 94663eeaeb
commit d52c598546
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 88 additions and 35 deletions

View File

@ -105,5 +105,6 @@ issues:
linters:
- dupl
- funlen
- gochecknoinits
- gochecknoglobals
- gosec

View File

@ -8,8 +8,11 @@ import (
. "github.com/onsi/gomega"
)
func TestResolver(t *testing.T) {
func init() {
log.Silence()
}
func TestResolver(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "API Suite")
}

View File

@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)
func TestCache(t *testing.T) {
func init() {
log.Silence()
}
func TestCache(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Expiration cache suite")
}

View File

@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)
func TestCache(t *testing.T) {
func init() {
log.Silence()
}
func TestCache(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "String cache suite")
}

View File

@ -33,7 +33,7 @@ var (
baseMemStats runtime.MemStats
)
func init() { //nolint:gochecknoinits
func init() {
// If you update either list, make sure both are the list version (see file header).
stringTestData = loadTestdata("../../helpertest/data/oisd-big-plain.txt")

View File

@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)
func TestCmd(t *testing.T) {
func init() {
log.Silence()
}
func TestCmd(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Command Suite")
}

View File

@ -14,8 +14,11 @@ var (
hook *log.MockLoggerHook
)
func TestConfig(t *testing.T) {
func init() {
log.Silence()
}
func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Config Suite")
}

View File

@ -14,8 +14,11 @@ import (
"github.com/testcontainers/testcontainers-go"
)
func TestLists(t *testing.T) {
func init() {
log.Silence()
}
func TestLists(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "e2e Suite", Label("e2e"))
}

View File

@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)
func TestLists(t *testing.T) {
func init() {
log.Silence()
}
func TestLists(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Lists Suite")
}

View File

@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)
func TestLists(t *testing.T) {
func init() {
log.Silence()
}
func TestLists(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Parsers Suite")
}

View File

@ -7,6 +7,7 @@ import (
"io"
"strings"
"sync"
"sync/atomic"
"github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"
@ -19,7 +20,10 @@ const prefixField = "prefix"
// Logger is the global logging instance
//
//nolint:gochecknoglobals
var logger *logrus.Logger
var (
logger *logrus.Logger
initDone atomic.Bool
)
// FormatType format for logging ENUM(
// text // logging as text
@ -47,6 +51,10 @@ type Config struct {
//nolint:gochecknoinits
func init() {
if !initDone.CompareAndSwap(false, true) {
return
}
logger = logrus.New()
defaultConfig := &Config{
@ -122,7 +130,20 @@ func ConfigureLogger(cfg *Config) {
// Silence disables the logger output
func Silence() {
logger.Out = io.Discard
initDone.Store(true)
logger = logrus.New()
logger.SetFormatter(nopFormatter{}) // skip expensive formatting
// not actually needed but doesn't hurt
logger.SetOutput(io.Discard)
}
type nopFormatter struct{}
func (f nopFormatter) Format(*logrus.Entry) ([]byte, error) {
return nil, nil
}
func WithIndent(log *logrus.Entry, prefix string, callback func(*logrus.Entry)) {

View File

@ -1,15 +1,13 @@
package log
import (
"io"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/mock"
)
func NewMockEntry() (*logrus.Entry, *MockLoggerHook) {
logger := logrus.New()
logger.Out = io.Discard
logger, _ := test.NewNullLogger()
logger.Level = logrus.TraceLevel
entry := logrus.Entry{Logger: logger}

View File

@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)
func TestResolver(t *testing.T) {
func init() {
log.Silence()
}
func TestResolver(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Querylog Suite")
}

View File

@ -10,9 +10,12 @@ import (
. "github.com/onsi/gomega"
)
func TestRedisClient(t *testing.T) {
func init() {
log.Silence()
redis.SetLogger(NoLogs{})
}
func TestRedisClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Redis Suite")
}

View File

@ -12,9 +12,12 @@ import (
. "github.com/onsi/gomega"
)
func TestResolver(t *testing.T) {
func init() {
log.Silence()
redis.SetLogger(NoLogs{})
}
func TestResolver(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Resolver Suite")
}

View File

@ -104,7 +104,7 @@ func (r *UpstreamTreeResolver) upstreamGroupByClient(request *model.Request) str
if len(groups) > 0 {
if len(groups) > 1 {
r.log().WithFields(logrus.Fields{
request.Log.WithFields(logrus.Fields{
"clientNames": request.ClientNames,
"clientIP": clientIP,
"groups": groups,

View File

@ -9,7 +9,6 @@ import (
"github.com/miekg/dns"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/mock"
)
@ -21,8 +20,6 @@ var _ = Describe("UpstreamTreeResolver", Label("upstreamTreeResolver"), func() {
sutConfig config.UpstreamsConfig
branches map[string]Resolver
loggerHook *test.Hook
err error
)
@ -143,9 +140,6 @@ var _ = Describe("UpstreamTreeResolver", Label("upstreamTreeResolver"), func() {
When("client specific resolvers are defined", func() {
BeforeEach(func() {
loggerHook = test.NewGlobal()
log.Log().AddHook(loggerHook)
sutConfig = config.UpstreamsConfig{Groups: config.UpstreamGroups{
upstreamDefaultCfgName: {config.Upstream{}},
"laptop": {config.Upstream{}},
@ -194,10 +188,6 @@ var _ = Describe("UpstreamTreeResolver", Label("upstreamTreeResolver"), func() {
Expect(branches).To(HaveLen(8))
})
AfterEach(func() {
loggerHook.Reset()
})
It("Should use default if client name or IP don't match", func() {
request := newRequestWithClient("example.com.", A, "192.168.178.55", "test")
@ -298,7 +288,10 @@ var _ = Describe("UpstreamTreeResolver", Label("upstreamTreeResolver"), func() {
))
})
It("Should use one of the matching resolvers & log warning", func() {
logger, hook := log.NewMockEntry()
request := newRequestWithClient("example.com.", A, "0.0.0.0", "name-matches1")
request.Log = logger
Expect(sut.Resolve(request)).
Should(
@ -311,7 +304,7 @@ var _ = Describe("UpstreamTreeResolver", Label("upstreamTreeResolver"), func() {
HaveReturnCode(dns.RcodeSuccess),
))
Expect(loggerHook.LastEntry().Message).Should(ContainSubstring("client matches multiple groups"))
Expect(hook.Messages).Should(ContainElement(ContainSubstring("client matches multiple groups")))
})
})
})

View File

@ -115,8 +115,6 @@ func retrieveCertificate(cfg *config.Config) (cert tls.Certificate, err error) {
//
//nolint:funlen
func NewServer(ctx context.Context, cfg *config.Config) (server *Server, err error) {
log.ConfigureLogger(&cfg.Log)
var cert tls.Certificate
if len(cfg.Ports.HTTPS) > 0 || len(cfg.Ports.TLS) > 0 {

View File

@ -8,8 +8,11 @@ import (
. "github.com/onsi/gomega"
)
func TestDNSServer(t *testing.T) {
func init() {
log.Silence()
}
func TestDNSServer(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Server Suite")
}

View File

@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)
func TestTrie(t *testing.T) {
func init() {
log.Silence()
}
func TestTrie(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Trie Suite")
}

View File

@ -8,8 +8,11 @@ import (
. "github.com/onsi/gomega"
)
func TestLists(t *testing.T) {
func init() {
log.Silence()
}
func TestLists(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Util Suite")
}