2021-01-26 22:34:27 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-11-10 14:57:29 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package plog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"k8s.io/component-base/logs"
|
|
|
|
"k8s.io/klog/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateAndSetLogLevelGlobally(t *testing.T) {
|
2021-01-26 22:34:27 +00:00
|
|
|
originalLogLevel := getKlogLevel()
|
|
|
|
require.GreaterOrEqual(t, int(originalLogLevel), int(klog.Level(0)), "cannot get klog level")
|
2020-11-10 14:57:29 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-26 22:34:27 +00:00
|
|
|
name string
|
|
|
|
level LogLevel
|
|
|
|
wantLevel klog.Level
|
|
|
|
wantEnabled []LogLevel
|
|
|
|
wantErr string
|
2020-11-10 14:57:29 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-01-26 22:34:27 +00:00
|
|
|
name: "unset",
|
|
|
|
wantLevel: 0,
|
|
|
|
wantEnabled: []LogLevel{LevelWarning},
|
2020-11-10 14:57:29 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-26 22:34:27 +00:00
|
|
|
name: "warning",
|
|
|
|
level: LevelWarning,
|
|
|
|
wantLevel: 0,
|
|
|
|
wantEnabled: []LogLevel{LevelWarning},
|
2020-11-10 14:57:29 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-26 22:34:27 +00:00
|
|
|
name: "info",
|
|
|
|
level: LevelInfo,
|
|
|
|
wantLevel: 2,
|
|
|
|
wantEnabled: []LogLevel{LevelWarning, LevelInfo},
|
2020-11-10 14:57:29 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-26 22:34:27 +00:00
|
|
|
name: "debug",
|
|
|
|
level: LevelDebug,
|
|
|
|
wantLevel: 4,
|
|
|
|
wantEnabled: []LogLevel{LevelWarning, LevelInfo, LevelDebug},
|
2020-11-10 14:57:29 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-26 22:34:27 +00:00
|
|
|
name: "trace",
|
|
|
|
level: LevelTrace,
|
|
|
|
wantLevel: 6,
|
|
|
|
wantEnabled: []LogLevel{LevelWarning, LevelInfo, LevelDebug, LevelTrace},
|
2020-11-10 14:57:29 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-26 22:34:27 +00:00
|
|
|
name: "all",
|
|
|
|
level: LevelAll,
|
|
|
|
wantLevel: 108,
|
|
|
|
wantEnabled: []LogLevel{LevelWarning, LevelInfo, LevelDebug, LevelTrace, LevelAll},
|
2020-11-10 14:57:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid level",
|
|
|
|
level: "panda",
|
|
|
|
wantLevel: originalLogLevel,
|
|
|
|
wantErr: errInvalidLogLevel.Error(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt // capture range variable
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
undoGlobalLogLevelChanges(t, originalLogLevel)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := ValidateAndSetLogLevelGlobally(tt.level)
|
|
|
|
require.Equal(t, tt.wantErr, errString(err))
|
2021-01-26 22:34:27 +00:00
|
|
|
require.Equal(t, tt.wantLevel, getKlogLevel())
|
|
|
|
|
|
|
|
if tt.wantEnabled != nil {
|
|
|
|
allLevels := []LogLevel{LevelWarning, LevelInfo, LevelDebug, LevelTrace, LevelAll}
|
|
|
|
for _, level := range allLevels {
|
|
|
|
if contains(tt.wantEnabled, level) {
|
|
|
|
require.Truef(t, Enabled(level), "wanted %q to be enabled", level)
|
|
|
|
} else {
|
|
|
|
require.False(t, Enabled(level), "did not want %q to be enabled", level)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-10 14:57:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-26 22:34:27 +00:00
|
|
|
require.Equal(t, originalLogLevel, getKlogLevel())
|
2020-11-10 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 22:34:27 +00:00
|
|
|
func contains(haystack []LogLevel, needle LogLevel) bool {
|
|
|
|
for _, hay := range haystack {
|
|
|
|
if hay == needle {
|
|
|
|
return true
|
2020-11-10 14:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-26 22:34:27 +00:00
|
|
|
return false
|
2020-11-10 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func errString(err error) string {
|
|
|
|
if err == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func undoGlobalLogLevelChanges(t *testing.T, originalLogLevel klog.Level) {
|
|
|
|
t.Helper()
|
|
|
|
_, err := logs.GlogSetter(strconv.Itoa(int(originalLogLevel)))
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|