validate: empty user_data is also valid

This commit is contained in:
Alex Crawford 2014-12-12 14:01:27 -08:00
parent 685d8317bc
commit fd10e27b99
2 changed files with 25 additions and 0 deletions

View File

@ -38,6 +38,8 @@ var (
// can be validated.
func Validate(userdataBytes []byte) (Report, error) {
switch {
case len(userdataBytes) == 0:
return Report{}, nil
case config.IsScript(string(userdataBytes)):
return Report{}, nil
case config.IsCloudConfig(string(userdataBytes)):

View File

@ -78,6 +78,29 @@ func TestValidateCloudConfig(t *testing.T) {
}
}
func TestValidate(t *testing.T) {
tests := []struct {
config string
report Report
}{
{},
{
config: "#!/bin/bash\necho hey",
},
}
for i, tt := range tests {
r, err := Validate([]byte(tt.config))
if err != nil {
t.Errorf("bad error (case #%d): want %v, got %v", i, nil, err)
}
if !reflect.DeepEqual(tt.report, r) {
t.Errorf("bad report (case #%d): want %+v, got %+v", i, tt.report, r)
}
}
}
func BenchmarkValidate(b *testing.B) {
config := `#cloud-config
hostname: test