Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Fix errors/panics associated with empty config sections; add warnings…
… when empty sections are found/skipped
- Loading branch information
Showing
with
18 additions
and
2 deletions.
-
+12
−1
control/config.go
-
+5
−0
examples/configs/snap-config-empty.yaml
-
+1
−1
snapteld_test.go
|
@@ -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: |
|
@@ -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) |
|
|