daemon/config: Validate(): validate log-level

Log-level validation was previously performed when configuring the daemon-logs;
this moves the validation to config.Validate() so that we can catch invalid
settings when running dockerd --validate.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-04-22 15:59:23 +02:00
parent 787257f767
commit 390c7d6871
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
4 changed files with 34 additions and 18 deletions

View file

@ -91,10 +91,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
}
configureProxyEnv(cli.Config)
if err := configureDaemonLogs(cli.Config); err != nil {
return err
}
configureDaemonLogs(cli.Config)
logrus.Info("Starting up")
@ -764,14 +761,14 @@ func systemContainerdRunning(honorXDG bool) (string, bool, error) {
return addr, err == nil, nil
}
// configureDaemonLogs sets the logrus logging level and formatting
func configureDaemonLogs(conf *config.Config) error {
// configureDaemonLogs sets the logrus logging level and formatting. It expects
// the passed configuration to already be validated, and ignores invalid options.
func configureDaemonLogs(conf *config.Config) {
if conf.LogLevel != "" {
lvl, err := logrus.ParseLevel(conf.LogLevel)
if err != nil {
return fmt.Errorf("unable to parse logging level: %s", conf.LogLevel)
if err == nil {
logrus.SetLevel(lvl)
}
logrus.SetLevel(lvl)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
@ -780,7 +777,6 @@ func configureDaemonLogs(conf *config.Config) error {
DisableColors: conf.RawLogs,
FullTimestamp: true,
})
return nil
}
func configureProxyEnv(conf *config.Config) {

View file

@ -186,19 +186,15 @@ func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) {
func TestConfigureDaemonLogs(t *testing.T) {
conf := &config.Config{}
err := configureDaemonLogs(conf)
assert.NilError(t, err)
configureDaemonLogs(conf)
assert.Check(t, is.Equal(logrus.InfoLevel, logrus.GetLevel()))
conf.LogLevel = "warn"
err = configureDaemonLogs(conf)
assert.NilError(t, err)
configureDaemonLogs(conf)
assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
// log level should not be changed when passing an invalid value
conf.LogLevel = "foobar"
err = configureDaemonLogs(conf)
assert.Error(t, err, "unable to parse logging level: foobar")
// log level should not be changed after a failure
configureDaemonLogs(conf)
assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
}

View file

@ -558,6 +558,13 @@ func findConfigurationConflicts(config map[string]interface{}, flags *pflag.Flag
// such as config.DNS, config.Labels, config.DNSSearch,
// as well as config.MaxConcurrentDownloads, config.MaxConcurrentUploads and config.MaxDownloadAttempts.
func Validate(config *Config) error {
// validate log-level
if config.LogLevel != "" {
if _, err := logrus.ParseLevel(config.LogLevel); err != nil {
return fmt.Errorf("invalid logging level: %s", config.LogLevel)
}
}
// validate DNS
for _, dns := range config.DNS {
if _, err := opts.ValidateIPAddress(dns); err != nil {

View file

@ -345,6 +345,15 @@ func TestValidateConfigurationErrors(t *testing.T) {
},
expectedErr: "invalid bind address (127.0.0.1:2375/path): should not contain a path element",
},
{
name: "with invalid log-level",
config: &Config{
CommonConfig: CommonConfig{
LogLevel: "foobar",
},
},
expectedErr: "invalid logging level: foobar",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
@ -437,6 +446,14 @@ func TestValidateConfiguration(t *testing.T) {
},
},
},
{
name: "with log-level warn",
config: &Config{
CommonConfig: CommonConfig{
LogLevel: "warn",
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {