mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-30 22:23:53 +00:00
test: add logger settings tests (#8729)
fully cover existing behavior - ref #8667 I used Github Copilot for some code autocompletions. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8729 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Michael Kriese <michael.kriese@visualon.de> Co-committed-by: Michael Kriese <michael.kriese@visualon.de>
This commit is contained in:
parent
93b13e4d52
commit
1761dae2db
1 changed files with 177 additions and 5 deletions
|
@ -10,16 +10,13 @@ import (
|
||||||
|
|
||||||
"forgejo.org/modules/json"
|
"forgejo.org/modules/json"
|
||||||
"forgejo.org/modules/log"
|
"forgejo.org/modules/log"
|
||||||
|
"forgejo.org/modules/test"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initLoggersByConfig(t *testing.T, config string) (*log.LoggerManager, func()) {
|
func initLoggersByConfig(t *testing.T, config string) (*log.LoggerManager, func()) {
|
||||||
oldLogConfig := Log
|
defer test.MockVariableValue(&Log, LogGlobalConfig{})()
|
||||||
Log = LogGlobalConfig{}
|
|
||||||
defer func() {
|
|
||||||
Log = oldLogConfig
|
|
||||||
}()
|
|
||||||
|
|
||||||
cfg, err := NewConfigProviderFromData(config)
|
cfg, err := NewConfigProviderFromData(config)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -29,6 +26,17 @@ func initLoggersByConfig(t *testing.T, config string) (*log.LoggerManager, func(
|
||||||
return manager, manager.Close
|
return manager, manager.Close
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initLoggerConfig(t *testing.T, config string) ConfigProvider {
|
||||||
|
defer test.MockVariableValue(&Log, LogGlobalConfig{})()
|
||||||
|
|
||||||
|
cfg, err := NewConfigProviderFromData(config)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
prepareLoggerConfig(cfg)
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
func toJSON(v any) string {
|
func toJSON(v any) string {
|
||||||
b, _ := json.MarshalIndent(v, "", "\t")
|
b, _ := json.MarshalIndent(v, "", "\t")
|
||||||
return string(b)
|
return string(b)
|
||||||
|
@ -395,3 +403,167 @@ COMPRESSION_LEVEL = 4
|
||||||
expected = strings.ReplaceAll(expected, "$FILENAME-1", tempPath("file-xxx.log"))
|
expected = strings.ReplaceAll(expected, "$FILENAME-1", tempPath("file-xxx.log"))
|
||||||
require.JSONEq(t, expected, toJSON(dump))
|
require.JSONEq(t, expected, toJSON(dump))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLegacyLoggerMigrations(t *testing.T) {
|
||||||
|
type Cases = []struct {
|
||||||
|
name string
|
||||||
|
cfg string
|
||||||
|
exp string
|
||||||
|
}
|
||||||
|
|
||||||
|
runCases := func(t *testing.T, key string, cases Cases) {
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
cfg := initLoggerConfig(t, c.cfg)
|
||||||
|
require.Equal(t, c.exp, cfg.Section("log").Key(key).String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("default", func(t *testing.T) {
|
||||||
|
runCases(t, "logger.default.MODE", Cases{
|
||||||
|
{
|
||||||
|
"uses default value for default logger",
|
||||||
|
"",
|
||||||
|
",",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"uses logger.default.MODE for default logger",
|
||||||
|
`[log]
|
||||||
|
logger.default.MODE = file
|
||||||
|
`,
|
||||||
|
"file",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("access", func(t *testing.T) {
|
||||||
|
runCases(t, "logger.access.MODE", Cases{
|
||||||
|
{
|
||||||
|
"uses default value for access logger",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"uses ACCESS for access logger",
|
||||||
|
`[log]
|
||||||
|
ACCESS = file
|
||||||
|
`,
|
||||||
|
"file",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ENABLE_ACCESS_LOG=true doesn't change access logger",
|
||||||
|
`[log]
|
||||||
|
ENABLE_ACCESS_LOG = true
|
||||||
|
logger.access.MODE = console
|
||||||
|
`,
|
||||||
|
"console",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ENABLE_ACCESS_LOG=false disables access logger",
|
||||||
|
`[log]
|
||||||
|
ENABLE_ACCESS_LOG = false
|
||||||
|
logger.access.MODE = console
|
||||||
|
`,
|
||||||
|
"",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"logger.access.MODE has precedence over ACCESS for access logger",
|
||||||
|
`[log]
|
||||||
|
ACCESS = file
|
||||||
|
logger.access.MODE = console
|
||||||
|
`,
|
||||||
|
"console",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ENABLE_ACCESS_LOG doesn't enable access logger",
|
||||||
|
`[log]
|
||||||
|
ENABLE_ACCESS_LOG = true
|
||||||
|
`,
|
||||||
|
"", // should be `,`
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("router", func(t *testing.T) {
|
||||||
|
runCases(t, "logger.router.MODE", Cases{
|
||||||
|
{
|
||||||
|
"uses default value for router logger",
|
||||||
|
"",
|
||||||
|
",",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"uses ROUTER for router logger",
|
||||||
|
`[log]
|
||||||
|
ROUTER = file
|
||||||
|
`,
|
||||||
|
"file",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"DISABLE_ROUTER_LOG=false doesn't change router logger",
|
||||||
|
`[log]
|
||||||
|
ROUTER = file
|
||||||
|
DISABLE_ROUTER_LOG = false
|
||||||
|
`,
|
||||||
|
"file",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"DISABLE_ROUTER_LOG=true disables router logger",
|
||||||
|
`[log]
|
||||||
|
DISABLE_ROUTER_LOG = true
|
||||||
|
logger.router.MODE = console
|
||||||
|
`,
|
||||||
|
"",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"logger.router.MODE as precedence over ROUTER for router logger",
|
||||||
|
`[log]
|
||||||
|
ROUTER = file
|
||||||
|
logger.router.MODE = console
|
||||||
|
`,
|
||||||
|
"console",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("xorm", func(t *testing.T) {
|
||||||
|
runCases(t, "logger.xorm.MODE", Cases{
|
||||||
|
{
|
||||||
|
"uses default value for xorm logger",
|
||||||
|
"",
|
||||||
|
",",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"uses XORM for xorm logger",
|
||||||
|
`[log]
|
||||||
|
XORM = file
|
||||||
|
`,
|
||||||
|
"file",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ENABLE_XORM_LOG=true doesn't change xorm logger",
|
||||||
|
`[log]
|
||||||
|
ENABLE_XORM_LOG = true
|
||||||
|
logger.xorm.MODE = console
|
||||||
|
`,
|
||||||
|
"console",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ENABLE_XORM_LOG=false disables xorm logger",
|
||||||
|
`[log]
|
||||||
|
ENABLE_XORM_LOG = false
|
||||||
|
logger.xorm.MODE = console
|
||||||
|
`,
|
||||||
|
"",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"logger.xorm.MODE has precedence over XORM for xorm logger",
|
||||||
|
`[log]
|
||||||
|
XORM = file
|
||||||
|
logger.xorm.MODE = console
|
||||||
|
`,
|
||||||
|
"console",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue