2020-03-10 02:57:53 +07:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-01 20:14:15 +07:00
|
|
|
"io"
|
2020-03-10 02:57:53 +07:00
|
|
|
"log"
|
|
|
|
"os"
|
2022-03-03 18:20:43 +07:00
|
|
|
"runtime"
|
2020-03-10 02:57:53 +07:00
|
|
|
"testing"
|
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-03-10 02:57:53 +07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-08-03 16:55:21 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2020-03-10 02:57:53 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldWriteLogsToFile(t *testing.T) {
|
2021-12-01 20:14:15 +07:00
|
|
|
dir, err := os.MkdirTemp("/tmp", "logs-dir")
|
2020-03-10 02:57:53 +07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-03-10 02:57:53 +07:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
path := fmt.Sprintf("%s/authelia.log", dir)
|
2021-08-03 16:55:21 +07:00
|
|
|
err = InitializeLogger(schema.LogConfiguration{Format: "text", FilePath: path, KeepStdout: false}, false)
|
2021-05-31 11:02:54 +07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
Logger().Info("This is a test")
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-12-01 20:14:15 +07:00
|
|
|
b, err := io.ReadAll(f)
|
2021-05-31 11:02:54 +07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Contains(t, string(b), "level=info msg=\"This is a test\"\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldWriteLogsToFileAndStdout(t *testing.T) {
|
2021-12-01 20:14:15 +07:00
|
|
|
dir, err := os.MkdirTemp("/tmp", "logs-dir")
|
2021-05-31 11:02:54 +07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
path := fmt.Sprintf("%s/authelia.log", dir)
|
2021-08-03 16:55:21 +07:00
|
|
|
err = InitializeLogger(schema.LogConfiguration{Format: "text", FilePath: path, KeepStdout: true}, false)
|
2020-03-10 02:57:53 +07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
Logger().Info("This is a test")
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-12-01 20:14:15 +07:00
|
|
|
b, err := io.ReadAll(f)
|
2020-03-10 02:57:53 +07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Contains(t, string(b), "level=info msg=\"This is a test\"\n")
|
|
|
|
}
|
2020-11-25 06:46:41 +07:00
|
|
|
|
|
|
|
func TestShouldFormatLogsAsJSON(t *testing.T) {
|
2021-12-01 20:14:15 +07:00
|
|
|
dir, err := os.MkdirTemp("/tmp", "logs-dir")
|
2020-11-25 06:46:41 +07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
path := fmt.Sprintf("%s/authelia.log", dir)
|
2021-08-03 16:55:21 +07:00
|
|
|
err = InitializeLogger(schema.LogConfiguration{Format: "json", FilePath: path, KeepStdout: false}, false)
|
2020-11-25 06:46:41 +07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
Logger().Info("This is a test")
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-12-01 20:14:15 +07:00
|
|
|
b, err := io.ReadAll(f)
|
2020-11-25 06:46:41 +07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Contains(t, string(b), "{\"level\":\"info\",\"msg\":\"This is a test\",")
|
|
|
|
}
|
2022-03-03 18:20:43 +07:00
|
|
|
|
|
|
|
func TestShouldRaiseErrorOnInvalidFile(t *testing.T) {
|
|
|
|
err := InitializeLogger(schema.LogConfiguration{FilePath: "/not/a/valid/path/to.log"}, false)
|
|
|
|
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
assert.EqualError(t, err, "open /not/a/valid/path/to.log: The system cannot find the path specified.")
|
|
|
|
default:
|
|
|
|
assert.EqualError(t, err, "open /not/a/valid/path/to.log: no such file or directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetLevels(t *testing.T) {
|
|
|
|
assert.Equal(t, logrus.InfoLevel, logrus.GetLevel())
|
|
|
|
|
|
|
|
setLevelStr("error", false)
|
|
|
|
assert.Equal(t, logrus.ErrorLevel, logrus.GetLevel())
|
|
|
|
|
|
|
|
setLevelStr("warn", false)
|
|
|
|
assert.Equal(t, logrus.WarnLevel, logrus.GetLevel())
|
|
|
|
|
|
|
|
setLevelStr("info", false)
|
|
|
|
assert.Equal(t, logrus.InfoLevel, logrus.GetLevel())
|
|
|
|
|
|
|
|
setLevelStr("debug", false)
|
|
|
|
assert.Equal(t, logrus.DebugLevel, logrus.GetLevel())
|
|
|
|
|
|
|
|
setLevelStr("trace", false)
|
|
|
|
assert.Equal(t, logrus.TraceLevel, logrus.GetLevel())
|
|
|
|
}
|