2020-01-28 23:50:41 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
2020-06-10 20:47:35 +00:00
|
|
|
"bytes"
|
2020-01-28 23:50:41 +00:00
|
|
|
"encoding/json"
|
2020-06-10 20:47:35 +00:00
|
|
|
"errors"
|
2020-04-19 09:14:54 +00:00
|
|
|
"os"
|
2020-01-28 23:50:41 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-04-19 09:14:54 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogger_SetupBasic(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
2020-08-19 16:09:35 +00:00
|
|
|
cfg := Config{LogLevel: "INFO"}
|
2020-01-28 23:50:41 +00:00
|
|
|
|
2020-07-29 20:18:54 +00:00
|
|
|
logger, err := Setup(cfg, nil)
|
2020-06-10 20:47:35 +00:00
|
|
|
require.NoError(err)
|
2020-01-28 23:50:41 +00:00
|
|
|
require.NotNil(logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogger_SetupInvalidLogLevel(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-08-19 16:09:35 +00:00
|
|
|
cfg := Config{}
|
2020-01-28 23:50:41 +00:00
|
|
|
|
2020-07-29 20:18:54 +00:00
|
|
|
_, err := Setup(cfg, nil)
|
2020-06-10 20:47:35 +00:00
|
|
|
testutil.RequireErrorContains(t, err, "Invalid log level")
|
2020-01-28 23:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogger_SetupLoggerErrorLevel(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
before func(*Config)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "ERR log level",
|
|
|
|
before: func(cfg *Config) {
|
|
|
|
cfg.LogLevel = "ERR"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ERROR log level",
|
|
|
|
before: func(cfg *Config) {
|
|
|
|
cfg.LogLevel = "ERROR"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.desc, func(t *testing.T) {
|
|
|
|
var cfg Config
|
|
|
|
|
|
|
|
c.before(&cfg)
|
|
|
|
require := require.New(t)
|
2020-06-10 20:47:35 +00:00
|
|
|
var buf bytes.Buffer
|
2020-01-28 23:50:41 +00:00
|
|
|
|
2020-08-19 16:09:35 +00:00
|
|
|
logger, err := Setup(cfg, &buf)
|
2020-06-10 20:47:35 +00:00
|
|
|
require.NoError(err)
|
2020-01-28 23:50:41 +00:00
|
|
|
require.NotNil(logger)
|
|
|
|
|
|
|
|
logger.Error("test error msg")
|
|
|
|
logger.Info("test info msg")
|
|
|
|
|
2020-06-10 20:47:35 +00:00
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
require.Contains(output, "[ERROR] test error msg")
|
|
|
|
require.NotContains(output, "[INFO] test info msg")
|
2020-01-28 23:50:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogger_SetupLoggerDebugLevel(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
2020-08-19 16:09:35 +00:00
|
|
|
cfg := Config{LogLevel: "DEBUG"}
|
2020-06-10 20:47:35 +00:00
|
|
|
var buf bytes.Buffer
|
2020-01-28 23:50:41 +00:00
|
|
|
|
2020-08-19 16:09:35 +00:00
|
|
|
logger, err := Setup(cfg, &buf)
|
2020-06-10 20:47:35 +00:00
|
|
|
require.NoError(err)
|
2020-01-28 23:50:41 +00:00
|
|
|
require.NotNil(logger)
|
|
|
|
|
|
|
|
logger.Info("test info msg")
|
|
|
|
logger.Debug("test debug msg")
|
|
|
|
|
2020-06-10 20:47:35 +00:00
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
require.Contains(output, "[INFO] test info msg")
|
|
|
|
require.Contains(output, "[DEBUG] test debug msg")
|
2020-01-28 23:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogger_SetupLoggerWithName(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
2020-08-19 16:09:35 +00:00
|
|
|
cfg := Config{
|
2020-01-28 23:50:41 +00:00
|
|
|
LogLevel: "DEBUG",
|
|
|
|
Name: "test-system",
|
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
var buf bytes.Buffer
|
2020-01-28 23:50:41 +00:00
|
|
|
|
2020-08-19 16:09:35 +00:00
|
|
|
logger, err := Setup(cfg, &buf)
|
2020-06-10 20:47:35 +00:00
|
|
|
require.NoError(err)
|
2020-01-28 23:50:41 +00:00
|
|
|
require.NotNil(logger)
|
|
|
|
|
|
|
|
logger.Warn("test warn msg")
|
|
|
|
|
2020-06-10 20:47:35 +00:00
|
|
|
require.Contains(buf.String(), "[WARN] test-system: test warn msg")
|
2020-01-28 23:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogger_SetupLoggerWithJSON(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
2020-08-19 16:09:35 +00:00
|
|
|
cfg := Config{
|
2020-01-28 23:50:41 +00:00
|
|
|
LogLevel: "DEBUG",
|
|
|
|
LogJSON: true,
|
|
|
|
Name: "test-system",
|
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
var buf bytes.Buffer
|
2020-01-28 23:50:41 +00:00
|
|
|
|
2020-08-19 16:09:35 +00:00
|
|
|
logger, err := Setup(cfg, &buf)
|
2020-06-10 20:47:35 +00:00
|
|
|
require.NoError(err)
|
2020-01-28 23:50:41 +00:00
|
|
|
require.NotNil(logger)
|
|
|
|
|
|
|
|
logger.Warn("test warn msg")
|
|
|
|
|
|
|
|
var jsonOutput map[string]string
|
2020-06-10 20:47:35 +00:00
|
|
|
err = json.Unmarshal(buf.Bytes(), &jsonOutput)
|
2020-01-28 23:50:41 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Contains(jsonOutput, "@level")
|
|
|
|
require.Equal(jsonOutput["@level"], "warn")
|
|
|
|
require.Contains(jsonOutput, "@message")
|
|
|
|
require.Equal(jsonOutput["@message"], "test warn msg")
|
|
|
|
}
|
2020-04-19 09:14:54 +00:00
|
|
|
|
|
|
|
func TestLogger_SetupLoggerWithValidLogPath(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
tmpDir := testutil.TempDir(t, t.Name())
|
|
|
|
|
2020-08-19 16:09:35 +00:00
|
|
|
cfg := Config{
|
2020-04-19 09:14:54 +00:00
|
|
|
LogLevel: "INFO",
|
|
|
|
LogFilePath: tmpDir + "/",
|
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
var buf bytes.Buffer
|
2020-04-19 09:14:54 +00:00
|
|
|
|
2020-08-19 16:09:35 +00:00
|
|
|
logger, err := Setup(cfg, &buf)
|
2020-06-10 20:47:35 +00:00
|
|
|
require.NoError(err)
|
2020-04-19 09:14:54 +00:00
|
|
|
require.NotNil(logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogger_SetupLoggerWithInValidLogPath(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2020-08-19 16:09:35 +00:00
|
|
|
cfg := Config{
|
2020-04-19 09:14:54 +00:00
|
|
|
LogLevel: "INFO",
|
|
|
|
LogFilePath: "nonexistentdir/",
|
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
var buf bytes.Buffer
|
2020-04-19 09:14:54 +00:00
|
|
|
|
2020-08-19 16:09:35 +00:00
|
|
|
logger, err := Setup(cfg, &buf)
|
2020-06-10 20:47:35 +00:00
|
|
|
require.Error(err)
|
|
|
|
require.True(errors.Is(err, os.ErrNotExist))
|
2020-04-19 09:14:54 +00:00
|
|
|
require.Nil(logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogger_SetupLoggerWithInValidLogPathPermission(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
tmpDir := "/tmp/" + t.Name()
|
|
|
|
|
|
|
|
os.Mkdir(tmpDir, 0000)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2020-08-19 16:09:35 +00:00
|
|
|
cfg := Config{
|
2020-04-19 09:14:54 +00:00
|
|
|
LogLevel: "INFO",
|
|
|
|
LogFilePath: tmpDir + "/",
|
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
var buf bytes.Buffer
|
2020-04-19 09:14:54 +00:00
|
|
|
|
2020-08-19 16:09:35 +00:00
|
|
|
logger, err := Setup(cfg, &buf)
|
2020-06-10 20:47:35 +00:00
|
|
|
require.Error(err)
|
|
|
|
require.True(errors.Is(err, os.ErrPermission))
|
2020-04-19 09:14:54 +00:00
|
|
|
require.Nil(logger)
|
|
|
|
}
|