apply comments

This commit is contained in:
gmarek
2015-03-02 14:41:13 +01:00
parent 726a5af075
commit bb8a4f5ed3
6 changed files with 32 additions and 24 deletions

View File

@@ -30,7 +30,6 @@ import (
"github.com/golang/glog"
)
const invalidAnnotationValueErrorMsg string = "must match regex " + util.AnnotationValueFmt
const cIdentifierErrorMsg string = "must match regex " + util.CIdentifierFmt
const isNegativeErrorMsg string = "value must not be negative"
@@ -46,6 +45,8 @@ var dns952LabelErrorMsg string = fmt.Sprintf("must have at most %d characters an
var pdPartitionErrorMsg string = intervalErrorMsg(0, 255)
var portRangeErrorMsg string = intervalErrorMsg(0, 65536)
const totalAnnotationSizeLimitB int = 64 * (1 << 10) // 64 kB
// ValidateLabels validates that a set of labels are correctly defined.
func ValidateLabels(labels map[string]string, field string) errs.ValidationErrorList {
allErrs := errs.ValidationErrorList{}
@@ -69,11 +70,11 @@ func ValidateAnnotations(annotations map[string]string, field string) errs.Valid
allErrs = append(allErrs, errs.NewFieldInvalid(field, k, qualifiedNameErrorMsg))
}
if !util.IsValidAnnotationValue(v) {
allErrs = append(allErrs, errs.NewFieldInvalid(field, k, invalidAnnotationValueErrorMsg))
allErrs = append(allErrs, errs.NewFieldInvalid(field, k, ""))
}
totalSize += (int64)(len(k)) + (int64)(len(v))
}
if totalSize > (int64)(util.TotalAnnotationSizeB) {
if totalSize > (int64)(totalAnnotationSizeLimitB) {
allErrs = append(allErrs, errs.NewFieldTooLong("annotations", ""))
}
return allErrs
@@ -881,7 +882,7 @@ func validateResourceName(value string, field string) errs.ValidationErrorList {
if len(strings.Split(value, "/")) == 1 {
if !api.IsStandardResourceName(value) {
return append(allErrs, errs.NewFieldInvalid(field, value, "is neither a standard resource type nor is fully qualified")
return append(allErrs, errs.NewFieldInvalid(field, value, "is neither a standard resource type nor is fully qualified"))
}
}

View File

@@ -76,7 +76,7 @@ func TestValidateLabels(t *testing.T) {
{"1234/5678": "bar"},
{"1.2.3.4/5678": "bar"},
{"UpperCaseAreOK123": "bar"},
{"goodvalue": "123_-.BaR,"},
{"goodvalue": "123_-.BaR"},
}
for i := range successCases {
errs := ValidateLabels(successCases[i], "field")
@@ -106,6 +106,7 @@ func TestValidateLabels(t *testing.T) {
labelValueErrorCases := []map[string]string{
{"toolongvalue": strings.Repeat("a", 64)},
{"backslashesinvalue": "some\\bad\\value"},
{"nocommasallowed": "bad,value"},
{"strangecharsinvalue": "?#$notsogood"},
}
for i := range labelValueErrorCases {
@@ -136,7 +137,11 @@ func TestValidateAnnotations(t *testing.T) {
{"1234/5678": "bar"},
{"1.2.3.4/5678": "bar"},
{"UpperCase123": "bar"},
{"a": strings.Repeat("b", 64*(1<<10)-1)},
{"a": strings.Repeat("b", (64*1024)-1)},
{
"a": strings.Repeat("b", (32*1024)-1),
"c": strings.Repeat("d", (32*1024)-1),
},
}
for i := range successCases {
errs := ValidateAnnotations(successCases[i], "field")
@@ -144,7 +149,7 @@ func TestValidateAnnotations(t *testing.T) {
t.Errorf("case[%d] expected success, got %#v", i, errs)
}
}
nameErrorCases := []map[string]string{
{"nospecialchars^=@": "bar"},
{"cantendwithadash-": "bar"},
@@ -161,11 +166,15 @@ func TestValidateAnnotations(t *testing.T) {
t.Errorf("error detail %s should be equal %s", detail, qualifiedNameErrorMsg)
}
}
errorCases := []map[string]string{
{"a": strings.Repeat("b", 64*(1<<10))},
totalSizeErrorCases := []map[string]string{
{"a": strings.Repeat("b", 64*1024)},
{
"a": strings.Repeat("b", 32*1024),
"c": strings.Repeat("d", 32*1024),
},
}
for i := range errorCases {
errs := ValidateAnnotations(errorCases[i], "field")
for i := range totalSizeErrorCases {
errs := ValidateAnnotations(totalSizeErrorCases[i], "field")
if len(errs) != 1 {
t.Errorf("case[%d] expected failure", i)
}
@@ -2274,7 +2283,7 @@ func TestValidateResourceNames(t *testing.T) {
{".", false},
{"..", false},
{"my.favorite.app.co/12345", true},
{"my.favorite.app.co/_12345", true},
{"my.favorite.app.co/_12345", false},
{"my.favorite.app.co/12345_", false},
{"kubernetes.io/..", false},
{"kubernetes.io/" + longString, true},