Permalink
Browse files

Fix errors/panics associated with empty config sections; add warnings…

… when empty sections are found/skipped
  • Loading branch information...
cce committed Nov 8, 2017
1 parent 44825e6 commit ab70083972ea48bfa2a271c11a44e0a6ef44d329
Showing with 18 additions and 2 deletions.
  1. +12 −1 control/config.go
  2. +5 −0 examples/configs/snap-config-empty.yaml
  3. +1 −1 snapteld_test.go
View
@@ -465,6 +465,10 @@ func unmarshalPluginConfig(typ string, p *pluginConfig, t map[string]interface{}
switch col := c.(type) {
case map[string]interface{}:
if v, ok := col["all"]; ok {
if v == nil { // handle empty config section by converting nil value to empty map
log.WithFields(log.Fields{"name": name, "type": typ}).Warning("Ignoring empty plugin configuration all: section")
v = map[string]interface{}{}
}
jv, err := json.Marshal(v)
if err != nil {
return err
@@ -489,7 +493,11 @@ func unmarshalPluginConfig(typ string, p *pluginConfig, t map[string]interface{}
case map[string]interface{}:
for ver, version := range versions {
switch v := version.(type) {
case map[string]interface{}:
case map[string]interface{}, nil:
if v == nil { // handle empty config section by converting nil value to empty map
log.WithFields(log.Fields{"name": name, "type": typ, "version": ver}).Warning("Ignoring empty plugin configuration version section")
v = map[string]interface{}{}
}
jv, err := json.Marshal(v)
if err != nil {
return err
@@ -521,6 +529,9 @@ func unmarshalPluginConfig(typ string, p *pluginConfig, t map[string]interface{}
return fmt.Errorf("Error unmarshalling %v '%v' expected '%v' got '%v'", typ, name, map[string]interface{}{}, reflect.TypeOf(versions))
}
}
case nil: // ignore empty config section
log.WithFields(log.Fields{"name": name, "type": typ}).Warning("Ignoring empty plugin configuration section")
continue
default:
return fmt.Errorf("Error unmarshalling %v '%v' expected '%v' got '%v'", typ, name, map[string]interface{}{}, reflect.TypeOf(col))
}
@@ -10,3 +10,8 @@ control:
psutil:
all:
path: /usr/local/bin/psutil
# this should not cause a fatal error
versions:
1:
# this should also not cause a fatal error
myplugin:
View
@@ -133,7 +133,7 @@ func TestSnapConfig(t *testing.T) {
func TestSnapConfigEmpty(t *testing.T) {
Convey("Test Config", t, func() {
Convey("with empty all: plugin config section", func() {
Convey("with empty plugin config sections", func() {
cfg := getDefaultConfig()
readConfig(cfg, "./examples/configs/snap-config-empty.yaml")
jb, _ := json.Marshal(cfg)

0 comments on commit ab70083

Please sign in to comment.