From a2db5671c5686e6cb0443405cd86922dd59e2987 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Tue, 7 Nov 2017 15:49:58 -0500 Subject: [PATCH] Add logbridge for testing --- helper/logbridge/logger.go | 122 +++++++++++++++++++++++++++++++++++++ vault/testing.go | 11 +++- 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 helper/logbridge/logger.go diff --git a/helper/logbridge/logger.go b/helper/logbridge/logger.go new file mode 100644 index 000000000..2626f1e65 --- /dev/null +++ b/helper/logbridge/logger.go @@ -0,0 +1,122 @@ +package logbridge + +import ( + "log" + + hclog "github.com/hashicorp/go-hclog" +) + +type Logger struct { + hclogger hclog.Logger +} + +func NewLogger(hclogger hclog.Logger) *Logger { + return &Logger{hclogger: hclogger} +} +func (l *Logger) Trace(msg string, args ...interface{}) { + l.hclogger.Trace(msg, args...) +} +func (l *Logger) Debug(msg string, args ...interface{}) { + l.hclogger.Debug(msg, args...) +} +func (l *Logger) Info(msg string, args ...interface{}) { + l.hclogger.Info(msg, args...) +} +func (l *Logger) Warn(msg string, args ...interface{}) { + l.hclogger.Warn(msg, args...) +} +func (l *Logger) Error(msg string, args ...interface{}) { + l.hclogger.Error(msg, args...) +} +func (l *Logger) IsTrace() bool { + return l.hclogger.IsTrace() +} +func (l *Logger) IsDebug() bool { + return l.hclogger.IsDebug() +} +func (l *Logger) IsInfo() bool { + return l.hclogger.IsInfo() +} +func (l *Logger) IsWarn() bool { + return l.hclogger.IsWarn() +} +func (l *Logger) With(args ...interface{}) *Logger { + return &Logger{ + hclogger: l.hclogger.With(args...), + } +} +func (l *Logger) Named(name string) *Logger { + return &Logger{ + hclogger: l.hclogger.Named(name), + } +} +func (l *Logger) ResetNamed(name string) *Logger { + return &Logger{ + hclogger: l.hclogger.ResetNamed(name), + } +} +func (l *Logger) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger { + return l.hclogger.StandardLogger(opts) +} +func (l *Logger) LogxiLogger() *LogxiLogger { + return &LogxiLogger{ + l: l, + } +} + +// This is only for compatibility with whatever the fuck is up with the errors +// coming back from warn/error in Logxi's API. Don't use this directly. +type LogxiLogger struct { + l *Logger +} + +func (l *LogxiLogger) Trace(msg string, args ...interface{}) { + l.l.Trace(msg, args...) +} +func (l *LogxiLogger) Debug(msg string, args ...interface{}) { + l.l.Debug(msg, args...) +} +func (l *LogxiLogger) Info(msg string, args ...interface{}) { + l.l.Info(msg, args...) +} +func (l *LogxiLogger) Warn(msg string, args ...interface{}) error { + l.l.Warn(msg, args...) + return nil +} +func (l *LogxiLogger) Error(msg string, args ...interface{}) error { + l.l.Error(msg, args...) + return nil +} +func (l *LogxiLogger) Fatal(msg string, args ...interface{}) { + panic(msg) +} +func (l *LogxiLogger) Log(level int, msg string, args []interface{}) { + panic(msg) +} +func (l *LogxiLogger) IsTrace() bool { + return l.l.IsTrace() +} +func (l *LogxiLogger) IsDebug() bool { + return l.l.IsDebug() +} +func (l *LogxiLogger) IsInfo() bool { + return l.l.IsInfo() +} +func (l *LogxiLogger) IsWarn() bool { + return l.l.IsWarn() +} +func (l *LogxiLogger) SetLevel(level int) { + panic("set level") +} +func (l *LogxiLogger) With(args ...interface{}) *LogxiLogger { + return l.l.With(args...).LogxiLogger() +} +func (l *LogxiLogger) Named(name string) *LogxiLogger { + return l.l.Named(name).LogxiLogger() +} +func (l *LogxiLogger) ResetNamed(name string) *LogxiLogger { + return l.l.ResetNamed(name).LogxiLogger() +} +func (l *LogxiLogger) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger { + return l.l.StandardLogger(opts) +} diff --git a/vault/testing.go b/vault/testing.go index 66951f63c..e3dca06d6 100644 --- a/vault/testing.go +++ b/vault/testing.go @@ -34,6 +34,7 @@ import ( "github.com/hashicorp/go-uuid" "github.com/hashicorp/vault/api" "github.com/hashicorp/vault/audit" + "github.com/hashicorp/vault/helper/logbridge" "github.com/hashicorp/vault/helper/logformat" "github.com/hashicorp/vault/helper/reload" "github.com/hashicorp/vault/helper/salt" @@ -788,6 +789,7 @@ type TestClusterOptions struct { BaseListenAddress string NumCores int SealFunc func() Seal + RawLogger interface{} } var DefaultNumCores = 3 @@ -1117,6 +1119,12 @@ func NewTestCluster(t testing.T, base *CoreConfig, opts *TestClusterOptions) *Te coreConfig.Seal = opts.SealFunc() } + if opts != nil && opts.RawLogger != nil { + if hclogger, ok := opts.RawLogger.(*logbridge.Logger); ok { + coreConfig.Logger = hclogger.Named(fmt.Sprintf("core%d", i)).LogxiLogger() + } + } + c, err := NewCore(coreConfig) if err != nil { t.Fatalf("err: %v", err) @@ -1260,7 +1268,8 @@ func NewTestCluster(t testing.T, base *CoreConfig, opts *TestClusterOptions) *Te Transport: transport, CheckRedirect: func(*http.Request, []*http.Request) error { // This can of course be overridden per-test by using its own client - return fmt.Errorf("redirects not allowed in these tests") + //return fmt.Errorf("redirects not allowed in these tests") + return http.ErrUseLastResponse }, } config := api.DefaultConfig()