|
@ -1,41 +1,63 @@ |
|
|
package flags_test |
|
|
package flags_test |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"bytes" |
|
|
|
|
|
"strings" |
|
|
"testing" |
|
|
"testing" |
|
|
|
|
|
|
|
|
tmflags "github.com/tendermint/tendermint/cmd/tendermint/commands/flags" |
|
|
tmflags "github.com/tendermint/tendermint/cmd/tendermint/commands/flags" |
|
|
"github.com/tendermint/tmlibs/log" |
|
|
"github.com/tendermint/tmlibs/log" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func TestIsLogLevelSimple(t *testing.T) { |
|
|
|
|
|
simpleFlags := []string{"info", "debug", "some"} |
|
|
|
|
|
for _, f := range simpleFlags { |
|
|
|
|
|
if !tmflags.IsLogLevelSimple(f) { |
|
|
|
|
|
t.Fatalf("%s is a simple flag", f) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func TestParseLogLevel(t *testing.T) { |
|
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
jsonLogger := log.NewTMJSONLogger(&buf) |
|
|
|
|
|
|
|
|
|
|
|
correctLogLevels := []struct { |
|
|
|
|
|
lvl string |
|
|
|
|
|
expectedLogLines []string |
|
|
|
|
|
}{ |
|
|
|
|
|
{"mempool:error", []string{``, ``, `{"_msg":"Mesmero","level":"error","module":"mempool"}`}}, |
|
|
|
|
|
{"mempool:error,*:debug", []string{``, ``, `{"_msg":"Mesmero","level":"error","module":"mempool"}`}}, |
|
|
|
|
|
{"*:debug,wire:none", []string{ |
|
|
|
|
|
`{"_msg":"Kingpin","level":"debug","module":"mempool"}`, |
|
|
|
|
|
`{"_msg":"Kitty Pryde","level":"info","module":"mempool"}`, |
|
|
|
|
|
`{"_msg":"Mesmero","level":"error","module":"mempool"}`}}, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
complexFlags := []string{"mempool:error", "mempool:error,*:debug"} |
|
|
|
|
|
for _, f := range complexFlags { |
|
|
|
|
|
if tmflags.IsLogLevelSimple(f) { |
|
|
|
|
|
t.Fatalf("%s is a complex flag", f) |
|
|
|
|
|
|
|
|
for _, c := range correctLogLevels { |
|
|
|
|
|
logger, err := tmflags.ParseLogLevel(c.lvl, jsonLogger) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Fatal(err) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestParseComplexLogLevel(t *testing.T) { |
|
|
|
|
|
logger := log.TestingLogger() |
|
|
|
|
|
|
|
|
logger = logger.With("module", "mempool") |
|
|
|
|
|
|
|
|
correctLogLevels := []string{"mempool:error", "mempool:error,*:debug", "*:debug,wire:none"} |
|
|
|
|
|
for _, lvl := range correctLogLevels { |
|
|
|
|
|
if _, err := tmflags.ParseComplexLogLevel(lvl, logger); err != nil { |
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
|
buf.Reset() |
|
|
|
|
|
|
|
|
|
|
|
logger.Debug("Kingpin") |
|
|
|
|
|
if have := strings.TrimSpace(buf.String()); c.expectedLogLines[0] != have { |
|
|
|
|
|
t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[0], have, c.lvl) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
buf.Reset() |
|
|
|
|
|
|
|
|
|
|
|
logger.Info("Kitty Pryde") |
|
|
|
|
|
if have := strings.TrimSpace(buf.String()); c.expectedLogLines[1] != have { |
|
|
|
|
|
t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[1], have, c.lvl) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
buf.Reset() |
|
|
|
|
|
|
|
|
|
|
|
logger.Error("Mesmero") |
|
|
|
|
|
if have := strings.TrimSpace(buf.String()); c.expectedLogLines[2] != have { |
|
|
|
|
|
t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[2], have, c.lvl) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
incorrectLogLevel := []string{"some", "mempool:some", "*:some,mempool:error"} |
|
|
incorrectLogLevel := []string{"some", "mempool:some", "*:some,mempool:error"} |
|
|
for _, lvl := range incorrectLogLevel { |
|
|
for _, lvl := range incorrectLogLevel { |
|
|
if _, err := tmflags.ParseComplexLogLevel(lvl, logger); err == nil { |
|
|
|
|
|
|
|
|
if _, err := tmflags.ParseLogLevel(lvl, jsonLogger); err == nil { |
|
|
t.Fatalf("Expected %s to produce error", lvl) |
|
|
t.Fatalf("Expected %s to produce error", lvl) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|