mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
quote valid strings for field validation
This commit is contained in:
parent
01a4a600ac
commit
7508afedd3
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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,
|
||||
|
@ -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}
|
||||
}
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user