-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests to improve code coverage for logging
- Loading branch information
1 parent
49801f4
commit 650fcd3
Showing
1 changed file
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package logger | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type loggerTest struct { | ||
name string | ||
logLevel string | ||
testFunction func(t *testing.T, log *Logger, buf *bytes.Buffer) | ||
} | ||
|
||
func TestLogger(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []loggerTest{ | ||
{ | ||
name: "Debug level logging", | ||
logLevel: "debug", | ||
testFunction: func(t *testing.T, log *Logger, buf *bytes.Buffer) { | ||
t.Helper() | ||
log.Debug("debug message") | ||
assert.Contains(t, buf.String(), "debug message") | ||
}, | ||
}, | ||
{ | ||
name: "Info level logging", | ||
logLevel: "info", | ||
testFunction: func(t *testing.T, log *Logger, buf *bytes.Buffer) { | ||
t.Helper() | ||
log.Info("info message") | ||
assert.Contains(t, buf.String(), "info message") | ||
}, | ||
}, | ||
{ | ||
name: "Warn level logging", | ||
logLevel: "warn", | ||
testFunction: func(t *testing.T, log *Logger, buf *bytes.Buffer) { | ||
t.Helper() | ||
log.Warn("warn message") | ||
assert.Contains(t, buf.String(), "warn message") | ||
}, | ||
}, | ||
{ | ||
name: "Error level logging", | ||
logLevel: "error", | ||
testFunction: func(t *testing.T, log *Logger, buf *bytes.Buffer) { | ||
t.Helper() | ||
log.Error("error message") | ||
assert.Contains(t, buf.String(), "error message") | ||
}, | ||
}, | ||
{ | ||
name: "Fatal level logging", | ||
logLevel: "fatal", | ||
testFunction: func(t *testing.T, log *Logger, buf *bytes.Buffer) { | ||
Check failure on line 64 in pkg/logger/logger_test.go GitHub Actions / runner / golangci-lint
|
||
t.Helper() | ||
if os.Getenv("BE_CRASHER") == "1" { | ||
log.Fatal("fatal message") | ||
|
||
return | ||
} | ||
cmd := exec.Command(os.Args[0], "-test.run=TestLogger") | ||
Check failure on line 71 in pkg/logger/logger_test.go GitHub Actions / runner / golangci-lint
|
||
cmd.Env = append(os.Environ(), "BE_CRASHER=1") | ||
err := cmd.Run() | ||
if e, ok := err.(*exec.ExitError); ok && !e.Success() { | ||
|
||
Check failure on line 75 in pkg/logger/logger_test.go GitHub Actions / runner / golangci-lint
|
||
return | ||
|
||
Check failure on line 77 in pkg/logger/logger_test.go GitHub Actions / runner / golangci-lint
|
||
} | ||
t.Fatalf("process ran with err %v, want exit status 1", err) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc // capture range variable | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var buf bytes.Buffer | ||
logger := zerolog.New(&buf).With().Timestamp().Logger() | ||
log := &Logger{logger: &logger} | ||
|
||
tc.testFunction(t, log, &buf) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewLogger(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
level string | ||
expectedLevel zerolog.Level | ||
}{ | ||
{"debug", zerolog.DebugLevel}, | ||
{"info", zerolog.InfoLevel}, | ||
{"warn", zerolog.WarnLevel}, | ||
{"error", zerolog.ErrorLevel}, | ||
{"invalid", zerolog.InfoLevel}, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
|
||
t.Run(fmt.Sprintf("LogLevel_%s", tc.level), func(t *testing.T) { | ||
t.Parallel() | ||
log := New(tc.level) | ||
Check failure on line 118 in pkg/logger/logger_test.go GitHub Actions / runner / golangci-lint
|
||
require.NotNil(t, log) | ||
assert.Equal(t, tc.expectedLevel, zerolog.GlobalLevel()) | ||
}) | ||
} | ||
} |