build/log_test: add tests for parsing log levels

This commit is contained in:
Johan T. Halseth 2020-11-16 11:18:45 +01:00
parent 17976df658
commit fcf2e7f687
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

104
build/log_test.go Normal file

@ -0,0 +1,104 @@
package build_test
import (
"testing"
"github.com/btcsuite/btclog"
"github.com/lightningnetwork/lnd/build"
"github.com/stretchr/testify/require"
)
type mockSubLogger struct {
globalLogLevel string
subLogLevels map[string]string
}
func (m *mockSubLogger) SubLoggers() build.SubLoggers {
return build.SubLoggers{
"PEER": btclog.Disabled,
"SRVR": btclog.Disabled,
}
}
func (m *mockSubLogger) SupportedSubsystems() []string {
return nil
}
func (m *mockSubLogger) SetLogLevel(subsystemID string, logLevel string) {
m.subLogLevels[subsystemID] = logLevel
}
func (m *mockSubLogger) SetLogLevels(logLevel string) {
m.globalLogLevel = logLevel
}
// TestParseAndSetDebugLevels tests tha we can properly set the log levels for
// all andspecified subsystems.
func TestParseAndSetDebugLevels(t *testing.T) {
testCases := []struct {
name string
debugLevel string
expErr string
expGlobal string
expSubLevels map[string]string
}{
{
name: "empty log level",
debugLevel: "",
expErr: "invalid",
},
{
name: "invalid global debug level",
debugLevel: "ddddddebug",
expErr: "invalid",
},
{
name: "global debug level",
debugLevel: "debug",
expGlobal: "debug",
},
{
name: "invalid global debug level#2",
debugLevel: "debug,info",
expErr: "invalid",
},
{
name: "invalid subsystem debug level",
debugLevel: "AAAA=debug",
expErr: "invalid",
},
{
name: "valid subsystem debug level",
debugLevel: "PEER=info,SRVR=debug",
expSubLevels: map[string]string{
"PEER": "info",
"SRVR": "debug",
},
},
}
for _, test := range testCases {
test := test
t.Run(test.name, func(t *testing.T) {
m := &mockSubLogger{
subLogLevels: make(map[string]string),
}
// If the subsystem map is empty, make and empty one to ensure
// the equal test later succeeds.
if len(test.expSubLevels) == 0 {
test.expSubLevels = make(map[string]string)
}
err := build.ParseAndSetDebugLevels(test.debugLevel, m)
if test.expErr != "" {
require.Contains(t, err.Error(), test.expErr)
return
}
require.NoError(t, err)
require.Equal(t, test.expGlobal, m.globalLogLevel)
require.Equal(t, test.expSubLevels, m.subLogLevels)
})
}
}