quote valid strings for field validation

This commit is contained in:
Di Xu 2017-09-13 12:03:37 +08:00
parent 01a4a600ac
commit 7508afedd3
5 changed files with 29 additions and 16 deletions

View File

@ -3059,12 +3059,12 @@ func TestValidatePorts(t *testing.T) {
"invalid protocol case": { "invalid protocol case": {
[]api.ContainerPort{{ContainerPort: 80, Protocol: "tcp"}}, []api.ContainerPort{{ContainerPort: 80, Protocol: "tcp"}},
field.ErrorTypeNotSupported, field.ErrorTypeNotSupported,
"protocol", "supported values: TCP, UDP", "protocol", `supported values: "TCP", "UDP"`,
}, },
"invalid protocol": { "invalid protocol": {
[]api.ContainerPort{{ContainerPort: 80, Protocol: "ICMP"}}, []api.ContainerPort{{ContainerPort: 80, Protocol: "ICMP"}},
field.ErrorTypeNotSupported, field.ErrorTypeNotSupported,
"protocol", "supported values: TCP, UDP", "protocol", `supported values: "TCP", "UDP"`,
}, },
"protocol required": { "protocol required": {
[]api.ContainerPort{{Name: "abc", ContainerPort: 80}}, []api.ContainerPort{{Name: "abc", ContainerPort: 80}},
@ -3426,7 +3426,7 @@ func TestValidateEnv(t *testing.T) {
}, },
}, },
}}, }},
expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.labels": supported values: metadata.name, metadata.namespace, metadata.uid, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP`, expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.labels": supported values: "metadata.name", "metadata.namespace", "metadata.uid", "spec.nodeName", "spec.serviceAccountName", "status.hostIP", "status.podIP"`,
}, },
{ {
name: "invalid fieldPath annotations", name: "invalid fieldPath annotations",
@ -3439,7 +3439,7 @@ func TestValidateEnv(t *testing.T) {
}, },
}, },
}}, }},
expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.annotations": supported values: metadata.name, metadata.namespace, metadata.uid, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP`, expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.annotations": supported values: "metadata.name", "metadata.namespace", "metadata.uid", "spec.nodeName", "spec.serviceAccountName", "status.hostIP", "status.podIP"`,
}, },
{ {
name: "unsupported fieldPath", name: "unsupported fieldPath",
@ -3452,7 +3452,7 @@ func TestValidateEnv(t *testing.T) {
}, },
}, },
}}, }},
expectedError: `valueFrom.fieldRef.fieldPath: Unsupported value: "status.phase": supported values: metadata.name, metadata.namespace, metadata.uid, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP`, expectedError: `valueFrom.fieldRef.fieldPath: Unsupported value: "status.phase": supported values: "metadata.name", "metadata.namespace", "metadata.uid", "spec.nodeName", "spec.serviceAccountName", "status.hostIP", "status.podIP"`,
}, },
} }
for _, tc := range errorCases { for _, tc := range errorCases {

View File

@ -480,7 +480,7 @@ func TestValidateExternalAdmissionHookConfiguration(t *testing.T) {
}(), }(),
}, },
}), }),
expectedError: `failurePolicy: Unsupported value: "Fail": supported values: Ignore`, expectedError: `failurePolicy: Unsupported value: "Fail": supported values: "Ignore"`,
}, },
} }
for _, test := range tests { for _, test := range tests {

View File

@ -1169,7 +1169,7 @@ func TestValidateDeployment(t *testing.T) {
// must have valid strategy type // must have valid strategy type
invalidStrategyDeployment := validDeployment() invalidStrategyDeployment := validDeployment()
invalidStrategyDeployment.Spec.Strategy.Type = extensions.DeploymentStrategyType("randomType") invalidStrategyDeployment.Spec.Strategy.Type = extensions.DeploymentStrategyType("randomType")
errorCases["supported values: Recreate, RollingUpdate"] = invalidStrategyDeployment errorCases[`supported values: "Recreate", "RollingUpdate"`] = invalidStrategyDeployment
// rollingUpdate should be nil for recreate. // rollingUpdate should be nil for recreate.
invalidRecreateDeployment := validDeployment() invalidRecreateDeployment := validDeployment()
@ -2524,42 +2524,42 @@ func TestValidatePodSecurityPolicy(t *testing.T) {
"no user options": { "no user options": {
psp: noUserOptions, psp: noUserOptions,
errorType: field.ErrorTypeNotSupported, errorType: field.ErrorTypeNotSupported,
errorDetail: "supported values: MustRunAs, MustRunAsNonRoot, RunAsAny", errorDetail: `supported values: "MustRunAs", "MustRunAsNonRoot", "RunAsAny"`,
}, },
"no selinux options": { "no selinux options": {
psp: noSELinuxOptions, psp: noSELinuxOptions,
errorType: field.ErrorTypeNotSupported, errorType: field.ErrorTypeNotSupported,
errorDetail: "supported values: MustRunAs, RunAsAny", errorDetail: `supported values: "MustRunAs", "RunAsAny"`,
}, },
"no fsgroup options": { "no fsgroup options": {
psp: noFSGroupOptions, psp: noFSGroupOptions,
errorType: field.ErrorTypeNotSupported, errorType: field.ErrorTypeNotSupported,
errorDetail: "supported values: MustRunAs, RunAsAny", errorDetail: `supported values: "MustRunAs", "RunAsAny"`,
}, },
"no sup group options": { "no sup group options": {
psp: noSupplementalGroupsOptions, psp: noSupplementalGroupsOptions,
errorType: field.ErrorTypeNotSupported, errorType: field.ErrorTypeNotSupported,
errorDetail: "supported values: MustRunAs, RunAsAny", errorDetail: `supported values: "MustRunAs", "RunAsAny"`,
}, },
"invalid user strategy type": { "invalid user strategy type": {
psp: invalidUserStratType, psp: invalidUserStratType,
errorType: field.ErrorTypeNotSupported, errorType: field.ErrorTypeNotSupported,
errorDetail: "supported values: MustRunAs, MustRunAsNonRoot, RunAsAny", errorDetail: `supported values: "MustRunAs", "MustRunAsNonRoot", "RunAsAny"`,
}, },
"invalid selinux strategy type": { "invalid selinux strategy type": {
psp: invalidSELinuxStratType, psp: invalidSELinuxStratType,
errorType: field.ErrorTypeNotSupported, errorType: field.ErrorTypeNotSupported,
errorDetail: "supported values: MustRunAs, RunAsAny", errorDetail: `supported values: "MustRunAs", "RunAsAny"`,
}, },
"invalid sup group strategy type": { "invalid sup group strategy type": {
psp: invalidSupGroupStratType, psp: invalidSupGroupStratType,
errorType: field.ErrorTypeNotSupported, errorType: field.ErrorTypeNotSupported,
errorDetail: "supported values: MustRunAs, RunAsAny", errorDetail: `supported values: "MustRunAs", "RunAsAny"`,
}, },
"invalid fs group strategy type": { "invalid fs group strategy type": {
psp: invalidFSGroupStratType, psp: invalidFSGroupStratType,
errorType: field.ErrorTypeNotSupported, errorType: field.ErrorTypeNotSupported,
errorDetail: "supported values: MustRunAs, RunAsAny", errorDetail: `supported values: "MustRunAs", "RunAsAny"`,
}, },
"invalid uid": { "invalid uid": {
psp: invalidUIDPSP, psp: invalidUIDPSP,

View File

@ -19,6 +19,7 @@ package field
import ( import (
"fmt" "fmt"
"reflect" "reflect"
"strconv"
"strings" "strings"
utilerrors "k8s.io/apimachinery/pkg/util/errors" utilerrors "k8s.io/apimachinery/pkg/util/errors"
@ -175,7 +176,11 @@ func Invalid(field *Path, value interface{}, detail string) *Error {
func NotSupported(field *Path, value interface{}, validValues []string) *Error { func NotSupported(field *Path, value interface{}, validValues []string) *Error {
detail := "" detail := ""
if validValues != nil && len(validValues) > 0 { if validValues != nil && len(validValues) > 0 {
detail = "supported values: " + strings.Join(validValues, ", ") quotedValues := make([]string, len(validValues))
for i, v := range validValues {
quotedValues[i] = strconv.Quote(v)
}
detail = "supported values: " + strings.Join(quotedValues, ", ")
} }
return &Error{ErrorTypeNotSupported, field.String(), value, detail} return &Error{ErrorTypeNotSupported, field.String(), value, detail}
} }

View File

@ -165,3 +165,11 @@ func TestErrListFilter(t *testing.T) {
t.Errorf("should filter") t.Errorf("should filter")
} }
} }
func TestNotSupported(t *testing.T) {
notSupported := NotSupported(NewPath("f"), "v", []string{"a", "b", "c"})
expected := `Unsupported value: "v": supported values: "a", "b", "c"`
if notSupported.ErrorBody() != expected {
t.Errorf("Expected: %s\n, but got: %s\n", expected, notSupported.ErrorBody())
}
}