fix(initialize): also check for unknown coreos keys

This commit is contained in:
Jonathan Boulle 2014-05-12 15:46:38 -07:00
parent e852be65f7
commit 81fe0dc9e0
2 changed files with 29 additions and 6 deletions

View File

@ -45,20 +45,38 @@ type CloudConfig struct {
type warner func(format string, v ...interface{})
// warnOnUnrecognizedKeys parses the contents of a cloud-config file and calls
// warn(msg, key) for every unrecognized key (i.e. those not present in CloudConfig)
func warnOnUnrecognizedKeys(contents string, warn warner) {
// Generate a map of all understood cloud config options
var cc map[string]interface{}
b, _ := goyaml.Marshal(&CloudConfig{})
goyaml.Unmarshal(b, &cc)
// Now unmarshal the entire provided contents
var c map[string]interface{}
goyaml.Unmarshal([]byte(contents), &c)
// Check that every key in the contents exists in the cloud config
for k, _ := range c {
if _, ok := cc[k]; !ok {
warn("Warning: unrecognized key %q in provided cloud config - ignoring section", k)
}
}
// Finally, check for unrecognized coreos options, if any are set
coreos, ok := c["coreos"]
if !ok {
return
}
set := coreos.(map[interface{}]interface{})
known := cc["coreos"].(map[interface{}]interface{})
for k, _ := range set {
key := k.(string)
if _, ok := known[key]; !ok {
warn("Warning: unrecognized key %q in coreos section of provided cloud config - ignoring", key)
}
}
}
// NewCloudConfig instantiates a new CloudConfig from the given contents (a

View File

@ -11,10 +11,12 @@ func TestCloudConfigUnknownKeys(t *testing.T) {
coreos:
etcd:
discovery: "https://discovery.etcd.io/827c73219eeb2fa5530027c37bf18877"
unknown:
coreos_unknown:
foo: "bar"
section_unknown:
dunno:
something
foo:
bare_unknown:
bar
hostname:
foo
@ -37,11 +39,14 @@ hostname:
warnOnUnrecognizedKeys(contents, catchWarn)
if !strings.Contains(warnings, "foo") {
t.Errorf("warnings did not catch unrecognized key foo")
if !strings.Contains(warnings, "coreos_unknown") {
t.Errorf("warnings did not catch unrecognized coreos option coreos_unknown")
}
if !strings.Contains(warnings, "unknown") {
t.Errorf("warnings did not catch unrecognized key unknown")
if !strings.Contains(warnings, "bare_unknown") {
t.Errorf("warnings did not catch unrecognized key bare_unknown")
}
if !strings.Contains(warnings, "section_unknown") {
t.Errorf("warnings did not catch unrecognized key section_unknown")
}
}