03cf302e9a
* logbridge with hclog and identical output * Initial search & replace This compiles, but there is a fair amount of TODO and commented out code, especially around the plugin logclient/logserver code. * strip logbridge * fix majority of tests * update logxi aliases * WIP fixing tests * more test fixes * Update test to hclog * Fix format * Rename hclog -> log * WIP making hclog and logxi love each other * update logger_test.go * clean up merged comments * Replace RawLogger interface with a Logger * Add some logger names * Replace Trace with Debug * update builtin logical logging patterns * Fix build errors * More log updates * update log approach in command and builtin * More log updates * update helper, http, and logical directories * Update loggers * Log updates * Update logging * Update logging * Update logging * Update logging * update logging in physical * prefixing and lowercase * Update logging * Move phyisical logging name to server command * Fix som tests * address jims feedback so far * incorporate brians feedback so far * strip comments * move vault.go to logging package * update Debug to Trace * Update go-plugin deps * Update logging based on review comments * Updates from review * Unvendor logxi * Remove null_logger.go
263 lines
5.7 KiB
Go
263 lines
5.7 KiB
Go
package plugin
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"io/ioutil"
|
|
"net/rpc"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
|
"github.com/hashicorp/vault/helper/logging"
|
|
)
|
|
|
|
func TestLogger_levels(t *testing.T) {
|
|
client, server := plugin.TestRPCConn(t)
|
|
defer client.Close()
|
|
|
|
var buf bytes.Buffer
|
|
writer := bufio.NewWriter(&buf)
|
|
|
|
l := logging.NewVaultLoggerWithWriter(writer, hclog.Trace)
|
|
|
|
server.RegisterName("Plugin", &LoggerServer{
|
|
logger: l,
|
|
})
|
|
|
|
expected := "foobar"
|
|
testLogger := &deprecatedLoggerClient{client: client}
|
|
|
|
// Test trace
|
|
testLogger.Trace(expected)
|
|
if err := writer.Flush(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result := buf.String()
|
|
buf.Reset()
|
|
if !strings.Contains(result, expected) {
|
|
t.Fatalf("expected log to contain %s, got %s", expected, result)
|
|
}
|
|
|
|
// Test debug
|
|
testLogger.Debug(expected)
|
|
if err := writer.Flush(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result = buf.String()
|
|
buf.Reset()
|
|
if !strings.Contains(result, expected) {
|
|
t.Fatalf("expected log to contain %s, got %s", expected, result)
|
|
}
|
|
|
|
// Test debug
|
|
testLogger.Info(expected)
|
|
if err := writer.Flush(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result = buf.String()
|
|
buf.Reset()
|
|
if !strings.Contains(result, expected) {
|
|
t.Fatalf("expected log to contain %s, got %s", expected, result)
|
|
}
|
|
|
|
// Test warn
|
|
testLogger.Warn(expected)
|
|
if err := writer.Flush(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result = buf.String()
|
|
buf.Reset()
|
|
if !strings.Contains(result, expected) {
|
|
t.Fatalf("expected log to contain %s, got %s", expected, result)
|
|
}
|
|
|
|
// Test error
|
|
testLogger.Error(expected)
|
|
if err := writer.Flush(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result = buf.String()
|
|
buf.Reset()
|
|
if !strings.Contains(result, expected) {
|
|
t.Fatalf("expected log to contain %s, got %s", expected, result)
|
|
}
|
|
|
|
// Test fatal
|
|
testLogger.Fatal(expected)
|
|
if err := writer.Flush(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result = buf.String()
|
|
buf.Reset()
|
|
if result != "" {
|
|
t.Fatalf("expected log Fatal() to be no-op, got %s", result)
|
|
}
|
|
}
|
|
|
|
func TestLogger_isLevels(t *testing.T) {
|
|
client, server := plugin.TestRPCConn(t)
|
|
defer client.Close()
|
|
|
|
l := logging.NewVaultLoggerWithWriter(ioutil.Discard, hclog.Trace)
|
|
|
|
server.RegisterName("Plugin", &LoggerServer{
|
|
logger: l,
|
|
})
|
|
|
|
testLogger := &deprecatedLoggerClient{client: client}
|
|
|
|
if !testLogger.IsDebug() || !testLogger.IsInfo() || !testLogger.IsTrace() || !testLogger.IsWarn() {
|
|
t.Fatal("expected logger to return true for all logger level checks")
|
|
}
|
|
}
|
|
|
|
func TestLogger_log(t *testing.T) {
|
|
client, server := plugin.TestRPCConn(t)
|
|
defer client.Close()
|
|
|
|
var buf bytes.Buffer
|
|
writer := bufio.NewWriter(&buf)
|
|
|
|
l := logging.NewVaultLoggerWithWriter(writer, hclog.Trace)
|
|
|
|
server.RegisterName("Plugin", &LoggerServer{
|
|
logger: l,
|
|
})
|
|
|
|
expected := "foobar"
|
|
testLogger := &deprecatedLoggerClient{client: client}
|
|
|
|
// Test trace 6 = logxi.LevelInfo
|
|
testLogger.Log(6, expected, nil)
|
|
if err := writer.Flush(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result := buf.String()
|
|
if !strings.Contains(result, expected) {
|
|
t.Fatalf("expected log to contain %s, got %s", expected, result)
|
|
}
|
|
|
|
}
|
|
|
|
func TestLogger_setLevel(t *testing.T) {
|
|
client, server := plugin.TestRPCConn(t)
|
|
defer client.Close()
|
|
|
|
l := hclog.New(&hclog.LoggerOptions{Output: ioutil.Discard})
|
|
|
|
server.RegisterName("Plugin", &LoggerServer{
|
|
logger: l,
|
|
})
|
|
|
|
testLogger := &deprecatedLoggerClient{client: client}
|
|
testLogger.SetLevel(4) // 4 == logxi.LevelWarn
|
|
|
|
if !testLogger.IsWarn() {
|
|
t.Fatal("expected logger to support warn level")
|
|
}
|
|
}
|
|
|
|
type deprecatedLoggerClient struct {
|
|
client *rpc.Client
|
|
}
|
|
|
|
func (l *deprecatedLoggerClient) Trace(msg string, args ...interface{}) {
|
|
cArgs := &LoggerArgs{
|
|
Msg: msg,
|
|
Args: args,
|
|
}
|
|
l.client.Call("Plugin.Trace", cArgs, &struct{}{})
|
|
}
|
|
|
|
func (l *deprecatedLoggerClient) Debug(msg string, args ...interface{}) {
|
|
cArgs := &LoggerArgs{
|
|
Msg: msg,
|
|
Args: args,
|
|
}
|
|
l.client.Call("Plugin.Debug", cArgs, &struct{}{})
|
|
}
|
|
|
|
func (l *deprecatedLoggerClient) Info(msg string, args ...interface{}) {
|
|
cArgs := &LoggerArgs{
|
|
Msg: msg,
|
|
Args: args,
|
|
}
|
|
l.client.Call("Plugin.Info", cArgs, &struct{}{})
|
|
}
|
|
func (l *deprecatedLoggerClient) Warn(msg string, args ...interface{}) error {
|
|
var reply LoggerReply
|
|
cArgs := &LoggerArgs{
|
|
Msg: msg,
|
|
Args: args,
|
|
}
|
|
err := l.client.Call("Plugin.Warn", cArgs, &reply)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if reply.Error != nil {
|
|
return reply.Error
|
|
}
|
|
|
|
return nil
|
|
}
|
|
func (l *deprecatedLoggerClient) Error(msg string, args ...interface{}) error {
|
|
var reply LoggerReply
|
|
cArgs := &LoggerArgs{
|
|
Msg: msg,
|
|
Args: args,
|
|
}
|
|
err := l.client.Call("Plugin.Error", cArgs, &reply)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if reply.Error != nil {
|
|
return reply.Error
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l *deprecatedLoggerClient) Fatal(msg string, args ...interface{}) {
|
|
// NOOP since it's not actually used within vault
|
|
return
|
|
}
|
|
|
|
func (l *deprecatedLoggerClient) Log(level int, msg string, args []interface{}) {
|
|
cArgs := &LoggerArgs{
|
|
Level: level,
|
|
Msg: msg,
|
|
Args: args,
|
|
}
|
|
l.client.Call("Plugin.Log", cArgs, &struct{}{})
|
|
}
|
|
|
|
func (l *deprecatedLoggerClient) SetLevel(level int) {
|
|
l.client.Call("Plugin.SetLevel", level, &struct{}{})
|
|
}
|
|
|
|
func (l *deprecatedLoggerClient) IsTrace() bool {
|
|
var reply LoggerReply
|
|
l.client.Call("Plugin.IsTrace", new(interface{}), &reply)
|
|
return reply.IsTrue
|
|
}
|
|
func (l *deprecatedLoggerClient) IsDebug() bool {
|
|
var reply LoggerReply
|
|
l.client.Call("Plugin.IsDebug", new(interface{}), &reply)
|
|
return reply.IsTrue
|
|
}
|
|
|
|
func (l *deprecatedLoggerClient) IsInfo() bool {
|
|
var reply LoggerReply
|
|
l.client.Call("Plugin.IsInfo", new(interface{}), &reply)
|
|
return reply.IsTrue
|
|
}
|
|
|
|
func (l *deprecatedLoggerClient) IsWarn() bool {
|
|
var reply LoggerReply
|
|
l.client.Call("Plugin.IsWarn", new(interface{}), &reply)
|
|
return reply.IsTrue
|
|
}
|