diff --git a/pkg/api/validation/validation_test.go b/pkg/api/validation/validation_test.go index d0d51354ae0..f89aa652cff 100644 --- a/pkg/api/validation/validation_test.go +++ b/pkg/api/validation/validation_test.go @@ -3059,12 +3059,12 @@ func TestValidatePorts(t *testing.T) { "invalid protocol case": { []api.ContainerPort{{ContainerPort: 80, Protocol: "tcp"}}, field.ErrorTypeNotSupported, - "protocol", "supported values: TCP, UDP", + "protocol", `supported values: "TCP", "UDP"`, }, "invalid protocol": { []api.ContainerPort{{ContainerPort: 80, Protocol: "ICMP"}}, field.ErrorTypeNotSupported, - "protocol", "supported values: TCP, UDP", + "protocol", `supported values: "TCP", "UDP"`, }, "protocol required": { []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", @@ -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", @@ -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 { diff --git a/pkg/apis/admissionregistration/validation/validation_test.go b/pkg/apis/admissionregistration/validation/validation_test.go index 6d41c28be87..de36f18065c 100644 --- a/pkg/apis/admissionregistration/validation/validation_test.go +++ b/pkg/apis/admissionregistration/validation/validation_test.go @@ -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 { diff --git a/pkg/apis/extensions/validation/validation_test.go b/pkg/apis/extensions/validation/validation_test.go index 2bd0d5a051d..ace5732e033 100644 --- a/pkg/apis/extensions/validation/validation_test.go +++ b/pkg/apis/extensions/validation/validation_test.go @@ -1169,7 +1169,7 @@ func TestValidateDeployment(t *testing.T) { // must have valid strategy type invalidStrategyDeployment := validDeployment() 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. invalidRecreateDeployment := validDeployment() @@ -2524,42 +2524,42 @@ func TestValidatePodSecurityPolicy(t *testing.T) { "no user options": { psp: noUserOptions, errorType: field.ErrorTypeNotSupported, - errorDetail: "supported values: MustRunAs, MustRunAsNonRoot, RunAsAny", + errorDetail: `supported values: "MustRunAs", "MustRunAsNonRoot", "RunAsAny"`, }, "no selinux options": { psp: noSELinuxOptions, errorType: field.ErrorTypeNotSupported, - errorDetail: "supported values: MustRunAs, RunAsAny", + errorDetail: `supported values: "MustRunAs", "RunAsAny"`, }, "no fsgroup options": { psp: noFSGroupOptions, errorType: field.ErrorTypeNotSupported, - errorDetail: "supported values: MustRunAs, RunAsAny", + errorDetail: `supported values: "MustRunAs", "RunAsAny"`, }, "no sup group options": { psp: noSupplementalGroupsOptions, errorType: field.ErrorTypeNotSupported, - errorDetail: "supported values: MustRunAs, RunAsAny", + errorDetail: `supported values: "MustRunAs", "RunAsAny"`, }, "invalid user strategy type": { psp: invalidUserStratType, errorType: field.ErrorTypeNotSupported, - errorDetail: "supported values: MustRunAs, MustRunAsNonRoot, RunAsAny", + errorDetail: `supported values: "MustRunAs", "MustRunAsNonRoot", "RunAsAny"`, }, "invalid selinux strategy type": { psp: invalidSELinuxStratType, errorType: field.ErrorTypeNotSupported, - errorDetail: "supported values: MustRunAs, RunAsAny", + errorDetail: `supported values: "MustRunAs", "RunAsAny"`, }, "invalid sup group strategy type": { psp: invalidSupGroupStratType, errorType: field.ErrorTypeNotSupported, - errorDetail: "supported values: MustRunAs, RunAsAny", + errorDetail: `supported values: "MustRunAs", "RunAsAny"`, }, "invalid fs group strategy type": { psp: invalidFSGroupStratType, errorType: field.ErrorTypeNotSupported, - errorDetail: "supported values: MustRunAs, RunAsAny", + errorDetail: `supported values: "MustRunAs", "RunAsAny"`, }, "invalid uid": { psp: invalidUIDPSP, diff --git a/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go b/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go index 43c779a11b7..31705dee386 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go @@ -19,6 +19,7 @@ package field import ( "fmt" "reflect" + "strconv" "strings" 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 { detail := "" 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} } diff --git a/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors_test.go b/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors_test.go index 26612017ff9..bff0d88e9c4 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors_test.go @@ -165,3 +165,11 @@ func TestErrListFilter(t *testing.T) { 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()) + } +}