Add logbridge for testing

This commit is contained in:
Jeff Mitchell 2017-11-07 15:49:58 -05:00
parent 0773031985
commit a2db5671c5
2 changed files with 132 additions and 1 deletions

122
helper/logbridge/logger.go Normal file
View File

@ -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)
}

View File

@ -34,6 +34,7 @@ import (
"github.com/hashicorp/go-uuid" "github.com/hashicorp/go-uuid"
"github.com/hashicorp/vault/api" "github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/audit" "github.com/hashicorp/vault/audit"
"github.com/hashicorp/vault/helper/logbridge"
"github.com/hashicorp/vault/helper/logformat" "github.com/hashicorp/vault/helper/logformat"
"github.com/hashicorp/vault/helper/reload" "github.com/hashicorp/vault/helper/reload"
"github.com/hashicorp/vault/helper/salt" "github.com/hashicorp/vault/helper/salt"
@ -788,6 +789,7 @@ type TestClusterOptions struct {
BaseListenAddress string BaseListenAddress string
NumCores int NumCores int
SealFunc func() Seal SealFunc func() Seal
RawLogger interface{}
} }
var DefaultNumCores = 3 var DefaultNumCores = 3
@ -1117,6 +1119,12 @@ func NewTestCluster(t testing.T, base *CoreConfig, opts *TestClusterOptions) *Te
coreConfig.Seal = opts.SealFunc() 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) c, err := NewCore(coreConfig)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -1260,7 +1268,8 @@ func NewTestCluster(t testing.T, base *CoreConfig, opts *TestClusterOptions) *Te
Transport: transport, Transport: transport,
CheckRedirect: func(*http.Request, []*http.Request) error { CheckRedirect: func(*http.Request, []*http.Request) error {
// This can of course be overridden per-test by using its own client // 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() config := api.DefaultConfig()