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

Merge pull request #21112 from allencloud/validate-configs-from-file

add validating configs from configuration file
This commit is contained in:
David Calavera 2016-03-16 10:42:25 -07:00
commit f81754811f
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 {
return err
}
if err := validateConfiguration(newConfig); err != nil {
return fmt.Errorf("file configuration validation failed (%v)", err)
}
reload(newConfig)
return nil
}
@ -184,6 +189,10 @@ func MergeDaemonConfigurations(flagsConfig *Config, flags *flag.FlagSet, configF
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
if err := mergo.Merge(fileConfig, flagsConfig); err != nil {
return nil, err
@ -336,3 +345,30 @@ func findConfigurationConflicts(config map[string]interface{}, flags *flag.FlagS
}
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)
}
}
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")
}
}