238 lines
5.6 KiB
Go
238 lines
5.6 KiB
Go
package logger
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestMaskPasswords(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
command []string
|
|
expected []string
|
|
}{
|
|
{
|
|
name: "mask cluster password with equals",
|
|
command: []string{
|
|
"rac", "localhost:1545", "infobase", "update",
|
|
"--cluster-pwd=secret123",
|
|
"--other-flag=value",
|
|
},
|
|
expected: []string{
|
|
"rac", "localhost:1545", "infobase", "update",
|
|
"--cluster-pwd=***",
|
|
"--other-flag=value",
|
|
},
|
|
},
|
|
{
|
|
name: "mask infobase password with equals",
|
|
command: []string{
|
|
"rac", "localhost:1545", "infobase", "update",
|
|
"--infobase-pwd=secret456",
|
|
},
|
|
expected: []string{
|
|
"rac", "localhost:1545", "infobase", "update",
|
|
"--infobase-pwd=***",
|
|
},
|
|
},
|
|
{
|
|
name: "mask password with space separator",
|
|
command: []string{
|
|
"rac", "localhost:1545", "infobase", "update",
|
|
"--cluster-pwd", "secret789",
|
|
"--other-flag", "value",
|
|
},
|
|
expected: []string{
|
|
"rac", "localhost:1545", "infobase", "update",
|
|
"--cluster-pwd", "***",
|
|
"--other-flag", "value",
|
|
},
|
|
},
|
|
{
|
|
name: "mask both passwords",
|
|
command: []string{
|
|
"rac", "localhost:1545", "infobase", "update",
|
|
"--cluster-pwd=cluster_secret",
|
|
"--infobase-pwd=infobase_secret",
|
|
},
|
|
expected: []string{
|
|
"rac", "localhost:1545", "infobase", "update",
|
|
"--cluster-pwd=***",
|
|
"--infobase-pwd=***",
|
|
},
|
|
},
|
|
{
|
|
name: "no passwords to mask",
|
|
command: []string{
|
|
"rac", "localhost:1545", "cluster", "list",
|
|
"--some-flag=value",
|
|
},
|
|
expected: []string{
|
|
"rac", "localhost:1545", "cluster", "list",
|
|
"--some-flag=value",
|
|
},
|
|
},
|
|
{
|
|
name: "mixed format passwords",
|
|
command: []string{
|
|
"rac", "localhost:1545", "infobase", "update",
|
|
"--cluster-pwd=secret1",
|
|
"--infobase-pwd", "secret2",
|
|
},
|
|
expected: []string{
|
|
"rac", "localhost:1545", "infobase", "update",
|
|
"--cluster-pwd=***",
|
|
"--infobase-pwd", "***",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
actual := maskPasswords(tt.command)
|
|
|
|
if len(actual) != len(tt.expected) {
|
|
t.Errorf("Expected length %d, got %d", len(tt.expected), len(actual))
|
|
return
|
|
}
|
|
|
|
for i, expected := range tt.expected {
|
|
if actual[i] != expected {
|
|
t.Errorf("At index %d: expected '%s', got '%s'", i, expected, actual[i])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewLogger(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
level string
|
|
expected string // Мы не можем напрямую проверить уровень, но можем проверить что логгер создается
|
|
}{
|
|
{"debug level", "debug", "debug"},
|
|
{"info level", "info", "info"},
|
|
{"warn level", "warn", "warn"},
|
|
{"error level", "error", "error"},
|
|
{"unknown level defaults to info", "unknown", "info"},
|
|
{"empty level defaults to info", "", "info"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
logger := NewLogger(tt.level)
|
|
if logger == nil {
|
|
t.Error("Expected logger to be created, got nil")
|
|
}
|
|
|
|
// Проверяем что логгер реализует интерфейс Logger
|
|
var _ Logger = logger
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSlogLoggerMethods(t *testing.T) {
|
|
// Создаем логгер для тестирования
|
|
logger := NewLogger("debug")
|
|
|
|
// Тестируем что методы не паникуют
|
|
t.Run("debug method", func(t *testing.T) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Errorf("Debug method panicked: %v", r)
|
|
}
|
|
}()
|
|
logger.Debug("test debug message", "key", "value")
|
|
})
|
|
|
|
t.Run("info method", func(t *testing.T) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Errorf("Info method panicked: %v", r)
|
|
}
|
|
}()
|
|
logger.Info("test info message", "key", "value")
|
|
})
|
|
|
|
t.Run("warn method", func(t *testing.T) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Errorf("Warn method panicked: %v", r)
|
|
}
|
|
}()
|
|
logger.Warn("test warn message", "key", "value")
|
|
})
|
|
|
|
t.Run("error method", func(t *testing.T) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Errorf("Error method panicked: %v", r)
|
|
}
|
|
}()
|
|
logger.Error("test error message", "key", "value")
|
|
})
|
|
|
|
t.Run("debug command method", func(t *testing.T) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Errorf("DebugCommand method panicked: %v", r)
|
|
}
|
|
}()
|
|
command := []string{"rac", "--cluster-pwd=secret", "command"}
|
|
logger.DebugCommand("test command", command)
|
|
})
|
|
}
|
|
|
|
func TestMaskPasswordsEdgeCases(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
command []string
|
|
expected []string
|
|
}{
|
|
{
|
|
name: "empty command",
|
|
command: []string{},
|
|
expected: []string{},
|
|
},
|
|
{
|
|
name: "single element",
|
|
command: []string{"rac"},
|
|
expected: []string{"rac"},
|
|
},
|
|
{
|
|
name: "password flag at end without value",
|
|
command: []string{
|
|
"rac", "localhost:1545", "--cluster-pwd",
|
|
},
|
|
expected: []string{
|
|
"rac", "localhost:1545", "--cluster-pwd",
|
|
},
|
|
},
|
|
{
|
|
name: "password flag with empty value",
|
|
command: []string{
|
|
"rac", "localhost:1545", "--cluster-pwd=",
|
|
},
|
|
expected: []string{
|
|
"rac", "localhost:1545", "--cluster-pwd=***",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
actual := maskPasswords(tt.command)
|
|
|
|
if len(actual) != len(tt.expected) {
|
|
t.Errorf("Expected length %d, got %d", len(tt.expected), len(actual))
|
|
return
|
|
}
|
|
|
|
for i, expected := range tt.expected {
|
|
if actual[i] != expected {
|
|
t.Errorf("At index %d: expected '%s', got '%s'", i, expected, actual[i])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
} |