1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

add validating configs from configuration file

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud 2016-03-11 16:50:49 +08:00
parent 9ad946eded
commit 825b582207
2 changed files with 104 additions and 0 deletions

View file

@ -164,6 +164,11 @@ func ReloadConfiguration(configFile string, flags *flag.FlagSet, reload func(*Co
if err != nil { if err != nil {
return err return err
} }
if err := validateConfiguration(newConfig); err != nil {
return fmt.Errorf("file configuration validation failed (%v)", err)
}
reload(newConfig) reload(newConfig)
return nil return nil
} }
@ -184,6 +189,10 @@ func MergeDaemonConfigurations(flagsConfig *Config, flags *flag.FlagSet, configF
return nil, err return nil, err
} }
if err := validateConfiguration(fileConfig); err != nil {
return nil, fmt.Errorf("file configuration validation failed (%v)", err)
}
// merge flags configuration on top of the file configuration // merge flags configuration on top of the file configuration
if err := mergo.Merge(fileConfig, flagsConfig); err != nil { if err := mergo.Merge(fileConfig, flagsConfig); err != nil {
return nil, err return nil, err
@ -336,3 +345,30 @@ func findConfigurationConflicts(config map[string]interface{}, flags *flag.FlagS
} }
return nil return nil
} }
// validateConfiguration validates some specific configs.
// such as config.DNS, config.Labels, config.DNSSearch
func validateConfiguration(config *Config) error {
// validate DNS
for _, dns := range config.DNS {
if _, err := opts.ValidateIPAddress(dns); err != nil {
return err
}
}
// validate DNSSearch
for _, dnsSearch := range config.DNSSearch {
if _, err := opts.ValidateDNSSearch(dnsSearch); err != nil {
return err
}
}
// validate Labels
for _, label := range config.Labels {
if _, err := opts.ValidateLabel(label); err != nil {
return err
}
}
return nil
}

View file

@ -208,3 +208,71 @@ func TestFindConfigurationConflictsWithMergedValues(t *testing.T) {
t.Fatalf("expected hosts conflict, got %v", err) t.Fatalf("expected hosts conflict, got %v", err)
} }
} }
func TestValidateConfiguration(t *testing.T) {
c1 := &Config{
CommonConfig: CommonConfig{
Labels: []string{"one"},
},
}
err := validateConfiguration(c1)
if err == nil {
t.Fatal("expected error, got nil")
}
c2 := &Config{
CommonConfig: CommonConfig{
Labels: []string{"one=two"},
},
}
err = validateConfiguration(c2)
if err != nil {
t.Fatalf("expected no error, got error %v", err)
}
c3 := &Config{
CommonConfig: CommonConfig{
DNS: []string{"1.1.1.1"},
},
}
err = validateConfiguration(c3)
if err != nil {
t.Fatalf("expected no error, got error %v", err)
}
c4 := &Config{
CommonConfig: CommonConfig{
DNS: []string{"1.1.1.1o"},
},
}
err = validateConfiguration(c4)
if err == nil {
t.Fatal("expected error, got nil")
}
c5 := &Config{
CommonConfig: CommonConfig{
DNSSearch: []string{"a.b.c"},
},
}
err = validateConfiguration(c5)
if err != nil {
t.Fatalf("expected no error, got error %v", err)
}
c6 := &Config{
CommonConfig: CommonConfig{
DNSSearch: []string{"123456"},
},
}
err = validateConfiguration(c6)
if err == nil {
t.Fatal("expected error, got nil")
}
}