util_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 util
  14. import "testing"
  15. func TestParseFileSource(t *testing.T) {
  16. tests := []struct {
  17. name string
  18. input string
  19. key string
  20. filepath string
  21. err bool
  22. }{
  23. {
  24. name: "success 1",
  25. input: "boo=zoo",
  26. key: "boo",
  27. filepath: "zoo",
  28. err: false,
  29. },
  30. {
  31. name: "success 2",
  32. input: "boo=/path/to/zoo",
  33. key: "boo",
  34. filepath: "/path/to/zoo",
  35. err: false,
  36. },
  37. {
  38. name: "success 3",
  39. input: "boo-2=/1/2/3/4/5/zab.txt",
  40. key: "boo-2",
  41. filepath: "/1/2/3/4/5/zab.txt",
  42. err: false,
  43. },
  44. {
  45. name: "success 4",
  46. input: "boo-=this/seems/weird.txt",
  47. key: "boo-",
  48. filepath: "this/seems/weird.txt",
  49. err: false,
  50. },
  51. {
  52. name: "success 5",
  53. input: "-key=some/path",
  54. key: "-key",
  55. filepath: "some/path",
  56. err: false,
  57. },
  58. {
  59. name: "invalid 1",
  60. input: "key==some/path",
  61. err: true,
  62. },
  63. {
  64. name: "invalid 2",
  65. input: "=key=some/path",
  66. err: true,
  67. },
  68. {
  69. name: "invalid 3",
  70. input: "==key=/some/other/path",
  71. err: true,
  72. },
  73. {
  74. name: "invalid 4",
  75. input: "=key",
  76. err: true,
  77. },
  78. {
  79. name: "invalid 5",
  80. input: "key=",
  81. err: true,
  82. },
  83. }
  84. for _, tt := range tests {
  85. t.Run(tt.name, func(t *testing.T) {
  86. key, filepath, err := ParseFileSource(tt.input)
  87. if err != nil {
  88. if tt.err {
  89. return
  90. }
  91. t.Errorf("%v: unexpected error: %v", tt.name, err)
  92. return
  93. }
  94. if tt.err {
  95. t.Errorf("%v: unexpected success", tt.name)
  96. return
  97. }
  98. if e, a := tt.key, key; e != a {
  99. t.Errorf("%v: expected key %v; got %v", tt.name, e, a)
  100. return
  101. }
  102. if e, a := tt.filepath, filepath; e != a {
  103. t.Errorf("%v: expected filepath %v; got %v", tt.name, e, a)
  104. }
  105. })
  106. }
  107. }
  108. func TestParseLiteralSource(t *testing.T) {
  109. tests := []struct {
  110. name string
  111. input string
  112. key string
  113. value string
  114. err bool
  115. }{
  116. {
  117. name: "success 1",
  118. input: "key=value",
  119. key: "key",
  120. value: "value",
  121. err: false,
  122. },
  123. {
  124. name: "success 2",
  125. input: "key=value/with/slashes",
  126. key: "key",
  127. value: "value/with/slashes",
  128. err: false,
  129. },
  130. {
  131. name: "err 1",
  132. input: "key==value",
  133. key: "key",
  134. value: "=value",
  135. err: false,
  136. },
  137. {
  138. name: "err 2",
  139. input: "key=value=",
  140. key: "key",
  141. value: "value=",
  142. err: false,
  143. },
  144. {
  145. name: "err 3",
  146. input: "key2=value==",
  147. key: "key2",
  148. value: "value==",
  149. err: false,
  150. },
  151. {
  152. name: "err 4",
  153. input: "==key",
  154. err: true,
  155. },
  156. {
  157. name: "err 5",
  158. input: "=key=",
  159. err: true,
  160. },
  161. }
  162. for _, tt := range tests {
  163. t.Run(tt.name, func(t *testing.T) {
  164. key, value, err := ParseLiteralSource(tt.input)
  165. if err != nil {
  166. if tt.err {
  167. return
  168. }
  169. t.Errorf("%v: unexpected error: %v", tt.name, err)
  170. return
  171. }
  172. if tt.err {
  173. t.Errorf("%v: unexpected success", tt.name)
  174. return
  175. }
  176. if e, a := tt.key, key; e != a {
  177. t.Errorf("%v: expected key %v; got %v", tt.name, e, a)
  178. return
  179. }
  180. if e, a := tt.value, value; e != a {
  181. t.Errorf("%v: expected value %v; got %v", tt.name, e, a)
  182. }
  183. })
  184. }
  185. }