util_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2015 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 storage
  14. import "testing"
  15. func TestEtcdParseWatchResourceVersion(t *testing.T) {
  16. testCases := []struct {
  17. Version string
  18. ExpectVersion uint64
  19. Err bool
  20. }{
  21. {Version: "", ExpectVersion: 0},
  22. {Version: "a", Err: true},
  23. {Version: " ", Err: true},
  24. {Version: "1", ExpectVersion: 1},
  25. {Version: "10", ExpectVersion: 10},
  26. }
  27. for _, testCase := range testCases {
  28. version, err := ParseWatchResourceVersion(testCase.Version)
  29. switch {
  30. case testCase.Err:
  31. if err == nil {
  32. t.Errorf("%s: unexpected non-error", testCase.Version)
  33. continue
  34. }
  35. if !IsInvalidError(err) {
  36. t.Errorf("%s: unexpected error: %v", testCase.Version, err)
  37. continue
  38. }
  39. case !testCase.Err && err != nil:
  40. t.Errorf("%s: unexpected error: %v", testCase.Version, err)
  41. continue
  42. }
  43. if version != testCase.ExpectVersion {
  44. t.Errorf("%s: expected version %d but was %d", testCase.Version, testCase.ExpectVersion, version)
  45. }
  46. }
  47. }
  48. func TestHasPathPrefix(t *testing.T) {
  49. validTestcases := []struct {
  50. s string
  51. prefix string
  52. }{
  53. // Exact matches
  54. {"", ""},
  55. {"a", "a"},
  56. {"a/", "a/"},
  57. {"a/../", "a/../"},
  58. // Path prefix matches
  59. {"a/b", "a"},
  60. {"a/b", "a/"},
  61. {"中文/", "中文"},
  62. }
  63. for i, tc := range validTestcases {
  64. if !hasPathPrefix(tc.s, tc.prefix) {
  65. t.Errorf(`%d: Expected hasPathPrefix("%s","%s") to be true`, i, tc.s, tc.prefix)
  66. }
  67. }
  68. invalidTestcases := []struct {
  69. s string
  70. prefix string
  71. }{
  72. // Mismatch
  73. {"a", "b"},
  74. // Dir requirement
  75. {"a", "a/"},
  76. // Prefix mismatch
  77. {"ns2", "ns"},
  78. {"ns2", "ns/"},
  79. {"中文文", "中文"},
  80. // Ensure no normalization is applied
  81. {"a/c/../b/", "a/b/"},
  82. {"a/", "a/b/.."},
  83. }
  84. for i, tc := range invalidTestcases {
  85. if hasPathPrefix(tc.s, tc.prefix) {
  86. t.Errorf(`%d: Expected hasPathPrefix("%s","%s") to be false`, i, tc.s, tc.prefix)
  87. }
  88. }
  89. }