2021-05-02 16:39:25 -04:00
|
|
|
package log
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var h = &Hook{}
|
|
|
|
|
|
|
|
|
|
type levelsTest struct {
|
|
|
|
|
name string
|
|
|
|
|
acceptedLevels []logrus.Level
|
|
|
|
|
expected []logrus.Level
|
|
|
|
|
description string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestLevels(t *testing.T) {
|
|
|
|
|
tests := []levelsTest{
|
|
|
|
|
{
|
|
|
|
|
name: "undefinedAcceptedLevels",
|
|
|
|
|
acceptedLevels: []logrus.Level{},
|
|
|
|
|
expected: logrus.AllLevels,
|
|
|
|
|
description: "All logrus levels expected, but did not receive them",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "definedAcceptedLevels",
|
|
|
|
|
acceptedLevels: []logrus.Level{logrus.InfoLevel},
|
|
|
|
|
expected: []logrus.Level{logrus.InfoLevel},
|
|
|
|
|
description: "Logrus Info level expected, but did not receive that.",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2026-11-16 23:38:07 -05:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2021-05-02 16:39:25 -04:00
|
|
|
h.AcceptedLevels = test.acceptedLevels
|
|
|
|
|
levels := h.Levels()
|
|
|
|
|
assert.Equal(t, test.expected, levels, test.description)
|
2026-11-16 23:38:07 -05:00
|
|
|
})
|
2021-05-02 16:39:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type levelThresholdTest struct {
|
|
|
|
|
name string
|
|
|
|
|
level logrus.Level
|
|
|
|
|
expected []logrus.Level
|
|
|
|
|
description string
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 14:43:09 -04:00
|
|
|
// levelThreshold returns a []logrus.Level including all levels
|
|
|
|
|
// above and including the level given. If the provided level does not exit,
|
|
|
|
|
// an empty slice is returned.
|
|
|
|
|
func levelThreshold(l logrus.Level) []logrus.Level {
|
2026-11-16 22:48:42 -05:00
|
|
|
if int(l) >= len(logrus.AllLevels) {
|
2021-05-12 14:43:09 -04:00
|
|
|
return []logrus.Level{}
|
|
|
|
|
}
|
|
|
|
|
return logrus.AllLevels[:l+1]
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-02 16:39:25 -04:00
|
|
|
func TestLevelThreshold(t *testing.T) {
|
|
|
|
|
tests := []levelThresholdTest{
|
|
|
|
|
{
|
|
|
|
|
name: "unknownLogLevel",
|
|
|
|
|
level: logrus.Level(100),
|
|
|
|
|
expected: []logrus.Level{},
|
|
|
|
|
description: "An empty Level slice was expected but was not returned",
|
|
|
|
|
},
|
2026-11-16 22:48:42 -05:00
|
|
|
{
|
|
|
|
|
name: "levelPastLast",
|
|
|
|
|
level: logrus.Level(len(logrus.AllLevels)),
|
|
|
|
|
expected: []logrus.Level{},
|
|
|
|
|
description: "An empty Level slice was expected for the first level past TraceLevel",
|
|
|
|
|
},
|
2021-05-02 16:39:25 -04:00
|
|
|
{
|
|
|
|
|
name: "errorLogLevel",
|
|
|
|
|
level: logrus.ErrorLevel,
|
|
|
|
|
expected: []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel},
|
|
|
|
|
description: "The panic, fatal, and error levels were expected but were not returned",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2026-11-16 23:38:07 -05:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2021-05-12 14:43:09 -04:00
|
|
|
levels := levelThreshold(test.level)
|
2021-05-02 16:39:25 -04:00
|
|
|
assert.Equal(t, test.expected, levels, test.description)
|
2026-11-16 23:38:07 -05:00
|
|
|
})
|
2021-05-02 16:39:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInvalidRegex(t *testing.T) {
|
|
|
|
|
e := &logrus.Entry{}
|
|
|
|
|
h = &Hook{RedactionList: []string{"\\"}}
|
|
|
|
|
err := h.Fire(e)
|
|
|
|
|
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type EntryDataValuesTest struct {
|
|
|
|
|
name string
|
|
|
|
|
redactionList []string
|
|
|
|
|
logFields logrus.Fields
|
|
|
|
|
expected logrus.Fields
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
description string
|
2021-05-02 16:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that any occurrence of a redaction pattern
|
|
|
|
|
// in the values of the entry's data fields is redacted.
|
|
|
|
|
func TestEntryDataValues(t *testing.T) {
|
|
|
|
|
tests := []EntryDataValuesTest{
|
|
|
|
|
{
|
|
|
|
|
name: "match on key",
|
|
|
|
|
redactionList: []string{"Password"},
|
|
|
|
|
logFields: logrus.Fields{"Password": "password123!"},
|
|
|
|
|
expected: logrus.Fields{"Password": "[REDACTED]"},
|
|
|
|
|
description: "Password value should have been redacted, but was not.",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "string value",
|
|
|
|
|
redactionList: []string{"William"},
|
|
|
|
|
logFields: logrus.Fields{"Description": "His name is William"},
|
|
|
|
|
expected: logrus.Fields{"Description": "His name is [REDACTED]"},
|
|
|
|
|
description: "William should have been redacted, but was not.",
|
|
|
|
|
},
|
2021-06-12 03:17:21 +00:00
|
|
|
{
|
|
|
|
|
name: "map value",
|
|
|
|
|
redactionList: []string{"William"},
|
|
|
|
|
logFields: logrus.Fields{"Description": map[string]string{"name": "His name is William"}},
|
|
|
|
|
expected: logrus.Fields{"Description": "map[name:His name is [REDACTED]]"},
|
|
|
|
|
description: "William should have been redacted, but was not.",
|
|
|
|
|
},
|
2021-05-02 16:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2026-11-16 23:38:07 -05:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2021-05-02 16:39:25 -04:00
|
|
|
logEntry := &logrus.Entry{
|
|
|
|
|
Data: test.logFields,
|
|
|
|
|
}
|
|
|
|
|
h = &Hook{RedactionList: test.redactionList}
|
|
|
|
|
err := h.Fire(logEntry)
|
|
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, test.expected, logEntry.Data)
|
2026-11-16 23:38:07 -05:00
|
|
|
})
|
2021-05-02 16:39:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that any occurrence of a redaction pattern
|
|
|
|
|
// in the entry's Message field is redacted.
|
|
|
|
|
func TestEntryMessage(t *testing.T) {
|
|
|
|
|
logEntry := &logrus.Entry{
|
|
|
|
|
Message: "Secret Password: password123!",
|
|
|
|
|
}
|
|
|
|
|
h = &Hook{RedactionList: []string{`(Password: ).*`}}
|
|
|
|
|
err := h.Fire(logEntry)
|
|
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, "Secret Password: [REDACTED]", logEntry.Message)
|
|
|
|
|
}
|