Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Fix errors/panics associated with empty config sections; add warnings…
Browse files Browse the repository at this point in the history
… when empty sections are found/skipped
  • Loading branch information
cce committed Nov 13, 2017
1 parent 44825e6 commit ab70083
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
13 changes: 12 additions & 1 deletion control/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down
5 changes: 5 additions & 0 deletions examples/configs/snap-config-empty.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
2 changes: 1 addition & 1 deletion snapteld_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ab70083

Please sign in to comment.