ParseUserData: return nil on empty input string

This commit is contained in:
Jonathan Boulle 2014-06-27 23:58:27 -07:00
parent 231c0fa20b
commit d4e048a1f4
3 changed files with 15 additions and 3 deletions

View File

@ -115,7 +115,7 @@ func main() {
env := initialize.NewEnvironment("/", ds.ConfigRoot(), workspace, convertNetconf, sshKeyName, subs) env := initialize.NewEnvironment("/", ds.ConfigRoot(), workspace, convertNetconf, sshKeyName, subs)
var ccm, ccu *initialize.CloudConfig var ccm, ccu *initialize.CloudConfig
var script system.Script var script *system.Script
if ccm, err = initialize.ParseMetaData(string(metadataBytes)); err != nil { if ccm, err = initialize.ParseMetaData(string(metadataBytes)); err != nil {
fmt.Printf("Failed to parse meta-data: %v\n", err) fmt.Printf("Failed to parse meta-data: %v\n", err)
die() die()
@ -128,7 +128,7 @@ func main() {
case *initialize.CloudConfig: case *initialize.CloudConfig:
ccu = t ccu = t
case system.Script: case system.Script:
script = t script = &t
} }
} }
@ -155,7 +155,7 @@ func main() {
} }
if script != nil { if script != nil {
if err = runScript(script, env); err != nil { if err = runScript(*script, env); err != nil {
fmt.Printf("Failed to run script: %v\n", err) fmt.Printf("Failed to run script: %v\n", err)
die() die()
} }

View File

@ -9,6 +9,9 @@ import (
) )
func ParseUserData(contents string) (interface{}, error) { func ParseUserData(contents string) (interface{}, error) {
if len(contents) == 0 {
return nil, nil
}
header := strings.SplitN(contents, "\n", 2)[0] header := strings.SplitN(contents, "\n", 2)[0]
// Explicitly trim the header so we can handle user-data from // Explicitly trim the header so we can handle user-data from

View File

@ -47,3 +47,12 @@ func TestParseConfigCRLF(t *testing.T) {
t.Error("Parsed incorrect number of SSH keys") t.Error("Parsed incorrect number of SSH keys")
} }
} }
func TestParseConfigEmpty(t *testing.T) {
i, e := ParseUserData(``)
if i != nil {
t.Error("ParseUserData of empty string returned non-nil unexpectedly")
} else if e != nil {
t.Error("ParseUserData of empty string returned error unexpectedly")
}
}