Merge pull request #283 from crawford/validate

validate: empty user_data is also valid
This commit is contained in:
Alex Crawford 2014-12-12 15:05:51 -08:00
commit 565a9540c9
2 changed files with 25 additions and 0 deletions

View File

@ -38,6 +38,8 @@ var (
// can be validated. // can be validated.
func Validate(userdataBytes []byte) (Report, error) { func Validate(userdataBytes []byte) (Report, error) {
switch { switch {
case len(userdataBytes) == 0:
return Report{}, nil
case config.IsScript(string(userdataBytes)): case config.IsScript(string(userdataBytes)):
return Report{}, nil return Report{}, nil
case config.IsCloudConfig(string(userdataBytes)): 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) { func BenchmarkValidate(b *testing.B) {
config := `#cloud-config config := `#cloud-config
hostname: test hostname: test