Skip to content

Commit

Permalink
fix(tests): properly silence log output
Browse files Browse the repository at this point in the history
Using `init` allows it to also work for benchmarks.
And `log.Silence` was sometimes getting overridden by `log.init`.
  • Loading branch information
ThinkChaos committed Nov 18, 2023
1 parent 94663ee commit c03ab78
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 32 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ issues:
linters:
- dupl
- funlen
- gochecknoinits
- gochecknoglobals
- gosec
5 changes: 4 additions & 1 deletion api/api_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 4 additions & 1 deletion cache/expirationcache/expiration_cache_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 4 additions & 1 deletion cache/stringcache/string_cache_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 4 additions & 1 deletion cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 4 additions & 1 deletion config/config_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
5 changes: 4 additions & 1 deletion e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
5 changes: 4 additions & 1 deletion lists/list_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 4 additions & 1 deletion lists/parsers/parsers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
17 changes: 14 additions & 3 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package log

import (
"fmt"
"io"
"strings"
"sync"
"sync/atomic"

"github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"golang.org/x/exp/maps"
)
Expand All @@ -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
Expand Down Expand Up @@ -47,6 +51,10 @@ type Config struct {

//nolint:gochecknoinits
func init() {
if !initDone.CompareAndSwap(false, true) {
return
}

logger = logrus.New()

defaultConfig := &Config{
Expand Down Expand Up @@ -122,7 +130,10 @@ func ConfigureLogger(cfg *Config) {

// Silence disables the logger output
func Silence() {
logger.Out = io.Discard
initDone.Store(true)

logger, _ = test.NewNullLogger()
logger.Hooks = nil // don't store log entries when not needed
}

func WithIndent(log *logrus.Entry, prefix string, callback func(*logrus.Entry)) {
Expand Down
6 changes: 2 additions & 4 deletions log/mock_entry.go
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
5 changes: 4 additions & 1 deletion querylog/querylog_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 4 additions & 1 deletion redis/redis_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
5 changes: 4 additions & 1 deletion resolver/resolver_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
15 changes: 4 additions & 11 deletions resolver/upstream_tree_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -21,8 +20,6 @@ var _ = Describe("UpstreamTreeResolver", Label("upstreamTreeResolver"), func() {
sutConfig config.UpstreamsConfig
branches map[string]Resolver

loggerHook *test.Hook

err error
)

Expand Down Expand Up @@ -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{}},
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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(
Expand All @@ -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")))
})
})
})
Expand Down
5 changes: 4 additions & 1 deletion server/server_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 4 additions & 1 deletion trie/trie_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 4 additions & 1 deletion util/util_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit c03ab78

Please sign in to comment.