Skip to content

Commit

Permalink
Add validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmorie committed Apr 14, 2015
1 parent 6e55b79 commit 471a41b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
3 changes: 1 addition & 2 deletions pkg/api/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,7 @@ func validateEnv(vars []api.EnvVar) errs.ValidationErrorList {
vErrs := errs.ValidationErrorList{}
if len(ev.Name) == 0 {
vErrs = append(vErrs, errs.NewFieldRequired("name"))
}
if !util.IsCIdentifier(ev.Name) {
} else if !util.IsCIdentifier(ev.Name) {
vErrs = append(vErrs, errs.NewFieldInvalid("name", ev.Name, cIdentifierErrorMsg))
}
vErrs = append(vErrs, validateEnvVarSources(ev)...)
Expand Down
98 changes: 89 additions & 9 deletions pkg/api/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,23 +633,103 @@ func TestValidateEnv(t *testing.T) {
{Name: "ABC", Value: "value"},
{Name: "AbC_123", Value: "value"},
{Name: "abc", Value: ""},
{
Name: "abc",
EnvVarSource: &api.EnvVarSource{
ObjectInfo: &api.ObjectReference{
APIVersion: "v1beta3",
FieldPath: "metadata.name",
},
},
},
{
Name: "abc",
EnvVarSource: &api.EnvVarSource{
HostInfo: &api.HostInfoSource{
HostName: true,
},
},
},
}
if errs := validateEnv(successCase); len(errs) != 0 {
t.Errorf("expected success: %v", errs)
}

errorCases := map[string][]api.EnvVar{
"zero-length name": {{Name: ""}},
"name not a C identifier": {{Name: "a.b.c"}},
errorCases := []struct {
name string
envs []api.EnvVar
expectedError string
}{
{
name: "zero-length name",
envs: []api.EnvVar{{Name: ""}},
expectedError: "[0].name: required value",
},
{
name: "name not a C identifier",
envs: []api.EnvVar{{Name: "a.b.c"}},
expectedError: "[0].name: invalid value 'a.b.c': must match regex [A-Za-z_][A-Za-z0-9_]*",
},
{
name: "value and source specified",
envs: []api.EnvVar{{
Name: "abc",
Value: "foo",
EnvVarSource: &api.EnvVarSource{
HostInfo: &api.HostInfoSource{
HostName: true,
},
},
}},
expectedError: "[0]: invalid value '': sources cannot be specified when value is set",
},
{
name: "missing FieldPath on ObjectInfo",
envs: []api.EnvVar{{
Name: "abc",
EnvVarSource: &api.EnvVarSource{
ObjectInfo: &api.ObjectReference{
APIVersion: "v1beta3",
},
},
}},
expectedError: "[0].fieldPath: required value",
},
{
name: "missing APIVersion on ObjectInfo",
envs: []api.EnvVar{{
Name: "abc",
EnvVarSource: &api.EnvVarSource{
ObjectInfo: &api.ObjectReference{
FieldPath: "metadata.name",
},
},
}},
expectedError: "[0].apiVersion: required value",
},
{
name: "forbidden fields on ObjectInfo",
envs: []api.EnvVar{{
Name: "abc",
EnvVarSource: &api.EnvVarSource{
ObjectInfo: &api.ObjectReference{
APIVersion: "v1beta3",
FieldPath: "metadata.name",
UID: "uhoh",
},
},
}},
expectedError: "[0].uid: forbidden 'uhoh'",
},
}
for k, v := range errorCases {
if errs := validateEnv(v); len(errs) == 0 {
t.Errorf("expected failure for %s", k)
for _, tc := range errorCases {
if errs := validateEnv(tc.envs); len(errs) == 0 {
t.Errorf("expected failure for %s", tc.name)
} else {
for i := range errs {
detail := errs[i].(*errors.ValidationError).Detail
if detail != "" && detail != cIdentifierErrorMsg {
t.Errorf("%s: expected error detail either empty or %s, got %s", k, cIdentifierErrorMsg, detail)
str := errs[i].(*errors.ValidationError).Error()
if str != "" && str != tc.expectedError {
t.Errorf("%s: expected error detail either empty or %s, got %s", tc.name, tc.expectedError, str)
}
}
}
Expand Down

0 comments on commit 471a41b

Please sign in to comment.