summaryrefslogtreecommitdiff
path: root/internal/configuration/validator/logging_test.go
blob: 49ba9c715c810e947204a279d5cff41aef700223 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package validator

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/authelia/authelia/internal/configuration/schema"
)

func TestShouldSetDefaultLoggingValues(t *testing.T) {
	config := &schema.Configuration{}

	validator := schema.NewStructValidator()

	ValidateLogging(config, validator)

	assert.Len(t, validator.Warnings(), 0)
	assert.Len(t, validator.Errors(), 0)

	require.NotNil(t, config.Logging.KeepStdout)

	assert.Equal(t, "", config.LogLevel)
	assert.Equal(t, "", config.LogFormat)
	assert.Equal(t, "", config.LogFilePath)

	assert.Equal(t, "info", config.Logging.Level)
	assert.Equal(t, "text", config.Logging.Format)
	assert.Equal(t, "", config.Logging.FilePath)
}

func TestShouldRaiseErrorOnInvalidLoggingLevel(t *testing.T) {
	config := &schema.Configuration{
		Logging: schema.LogConfiguration{
			Level: "TRACE",
		},
	}

	validator := schema.NewStructValidator()

	ValidateLogging(config, validator)

	assert.Len(t, validator.Warnings(), 0)
	require.Len(t, validator.Errors(), 1)

	assert.EqualError(t, validator.Errors()[0], "the log level 'TRACE' is invalid, must be one of: trace, debug, info, warn, error")
}

// TODO: DEPRECATED TEST. Remove in 4.33.0.
func TestShouldMigrateDeprecatedLoggingConfig(t *testing.T) {
	config := &schema.Configuration{
		LogLevel:    "trace",
		LogFormat:   "json",
		LogFilePath: "/a/b/c",
	}

	validator := schema.NewStructValidator()

	ValidateLogging(config, validator)

	assert.Len(t, validator.Errors(), 0)
	require.Len(t, validator.Warnings(), 3)

	require.NotNil(t, config.Logging.KeepStdout)

	assert.Equal(t, "trace", config.LogLevel)
	assert.Equal(t, "json", config.LogFormat)
	assert.Equal(t, "/a/b/c", config.LogFilePath)

	assert.Equal(t, "trace", config.Logging.Level)
	assert.Equal(t, "json", config.Logging.Format)
	assert.Equal(t, "/a/b/c", config.Logging.FilePath)

	assert.EqualError(t, validator.Warnings()[0], fmt.Sprintf(errFmtDeprecatedConfigurationKey, "log_level", "4.33.0", "log.level"))
	assert.EqualError(t, validator.Warnings()[1], fmt.Sprintf(errFmtDeprecatedConfigurationKey, "log_format", "4.33.0", "log.format"))
	assert.EqualError(t, validator.Warnings()[2], fmt.Sprintf(errFmtDeprecatedConfigurationKey, "log_file_path", "4.33.0", "log.file_path"))
}

func TestShouldRaiseErrorsAndNotOverwriteConfigurationWhenUsingDeprecatedLoggingConfig(t *testing.T) {
	config := &schema.Configuration{
		Logging: schema.LogConfiguration{
			Level:      "info",
			Format:     "text",
			FilePath:   "/x/y/z",
			KeepStdout: true,
		},
		LogLevel:    "debug",
		LogFormat:   "json",
		LogFilePath: "/a/b/c",
	}

	validator := schema.NewStructValidator()

	ValidateLogging(config, validator)

	require.NotNil(t, config.Logging.KeepStdout)

	assert.Equal(t, "info", config.Logging.Level)
	assert.Equal(t, "text", config.Logging.Format)
	assert.True(t, config.Logging.KeepStdout)
	assert.Equal(t, "/x/y/z", config.Logging.FilePath)

	assert.Len(t, validator.Errors(), 0)
	require.Len(t, validator.Warnings(), 3)

	assert.EqualError(t, validator.Warnings()[0], fmt.Sprintf(errFmtDeprecatedConfigurationKey, "log_level", "4.33.0", "log.level"))
	assert.EqualError(t, validator.Warnings()[1], fmt.Sprintf(errFmtDeprecatedConfigurationKey, "log_format", "4.33.0", "log.format"))
	assert.EqualError(t, validator.Warnings()[2], fmt.Sprintf(errFmtDeprecatedConfigurationKey, "log_file_path", "4.33.0", "log.file_path"))
}