validation_test.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package validation
  14. import (
  15. "strings"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/util/validation/field"
  18. )
  19. func TestValidateLabels(t *testing.T) {
  20. successCases := []map[string]string{
  21. {"simple": "bar"},
  22. {"now-with-dashes": "bar"},
  23. {"1-starts-with-num": "bar"},
  24. {"1234": "bar"},
  25. {"simple/simple": "bar"},
  26. {"now-with-dashes/simple": "bar"},
  27. {"now-with-dashes/now-with-dashes": "bar"},
  28. {"now.with.dots/simple": "bar"},
  29. {"now-with.dashes-and.dots/simple": "bar"},
  30. {"1-num.2-num/3-num": "bar"},
  31. {"1234/5678": "bar"},
  32. {"1.2.3.4/5678": "bar"},
  33. {"UpperCaseAreOK123": "bar"},
  34. {"goodvalue": "123_-.BaR"},
  35. }
  36. for i := range successCases {
  37. errs := ValidateLabels(successCases[i], field.NewPath("field"))
  38. if len(errs) != 0 {
  39. t.Errorf("case[%d] expected success, got %#v", i, errs)
  40. }
  41. }
  42. labelNameErrorCases := []struct {
  43. labels map[string]string
  44. expect string
  45. }{
  46. {map[string]string{"nospecialchars^=@": "bar"}, "must match the regex"},
  47. {map[string]string{"cantendwithadash-": "bar"}, "must match the regex"},
  48. {map[string]string{"only/one/slash": "bar"}, "must match the regex"},
  49. {map[string]string{strings.Repeat("a", 254): "bar"}, "must be no more than"},
  50. }
  51. for i := range labelNameErrorCases {
  52. errs := ValidateLabels(labelNameErrorCases[i].labels, field.NewPath("field"))
  53. if len(errs) != 1 {
  54. t.Errorf("case[%d]: expected failure", i)
  55. } else {
  56. if !strings.Contains(errs[0].Detail, labelNameErrorCases[i].expect) {
  57. t.Errorf("case[%d]: error details do not include %q: %q", i, labelNameErrorCases[i].expect, errs[0].Detail)
  58. }
  59. }
  60. }
  61. labelValueErrorCases := []struct {
  62. labels map[string]string
  63. expect string
  64. }{
  65. {map[string]string{"toolongvalue": strings.Repeat("a", 64)}, "must be no more than"},
  66. {map[string]string{"backslashesinvalue": "some\\bad\\value"}, "must match the regex"},
  67. {map[string]string{"nocommasallowed": "bad,value"}, "must match the regex"},
  68. {map[string]string{"strangecharsinvalue": "?#$notsogood"}, "must match the regex"},
  69. }
  70. for i := range labelValueErrorCases {
  71. errs := ValidateLabels(labelValueErrorCases[i].labels, field.NewPath("field"))
  72. if len(errs) != 1 {
  73. t.Errorf("case[%d]: expected failure", i)
  74. } else {
  75. if !strings.Contains(errs[0].Detail, labelValueErrorCases[i].expect) {
  76. t.Errorf("case[%d]: error details do not include %q: %q", i, labelValueErrorCases[i].expect, errs[0].Detail)
  77. }
  78. }
  79. }
  80. }