test(config,git,logger): add some test functions

This commit is contained in:
Tom Neuber 2024-12-06 23:56:18 +01:00
parent 316e5de757
commit 1356128a7b
Signed by: tom
GPG key ID: F17EFE4272D89FF6
16 changed files with 387 additions and 25 deletions

View file

@ -0,0 +1,49 @@
package logger_test
import (
"bytes"
"testing"
"git.ar21.de/yolokube/grafana-backuper/internal/logger"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)
func TestCliLoggerLayout_DebugLevel(t *testing.T) {
var buf bytes.Buffer
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log := logger.CliLoggerLayout(&buf)
log.Debug().Msg("test message")
output := buf.String()
assert.Contains(t, output, "test message")
assert.Contains(t, output, "DBG") // Assuming zerolog adds a short level indicator like "DBG"
assert.Contains(t, output, ".go:") // Ensures the caller file and line are included
}
func TestCliLoggerLayout_NonDebugLevel(t *testing.T) {
var buf bytes.Buffer
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log := logger.CliLoggerLayout(&buf)
log.Info().Msg("test message")
output := buf.String()
assert.Contains(t, output, "test message")
assert.NotContains(t, output, "DBG") // Since we're at Info level
assert.NotContains(t, output, ".go:") // Caller information should not be included
}
func TestJSONLoggerLayout(t *testing.T) {
var buf bytes.Buffer
log := logger.JSONLoggerLayout(&buf)
log.Info().Msg("test message")
output := buf.String()
assert.Contains(t, output, `"message":"test message"`)
assert.Contains(t, output, `"level":"info"`)
assert.Contains(t, output, `"time":`) // Timestamp should be included
assert.Contains(t, output, `"caller":`) // Caller should be included
}