kubectl_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 kubectl
  14. import (
  15. "testing"
  16. )
  17. func TestParseFileSource(t *testing.T) {
  18. cases := []struct {
  19. name string
  20. input string
  21. key string
  22. filepath string
  23. err bool
  24. }{
  25. {
  26. name: "success 1",
  27. input: "boo=zoo",
  28. key: "boo",
  29. filepath: "zoo",
  30. err: false,
  31. },
  32. {
  33. name: "success 2",
  34. input: "boo=/path/to/zoo",
  35. key: "boo",
  36. filepath: "/path/to/zoo",
  37. err: false,
  38. },
  39. {
  40. name: "success 3",
  41. input: "boo-2=/1/2/3/4/5/zab.txt",
  42. key: "boo-2",
  43. filepath: "/1/2/3/4/5/zab.txt",
  44. err: false,
  45. },
  46. {
  47. name: "success 4",
  48. input: "boo-=this/seems/weird.txt",
  49. key: "boo-",
  50. filepath: "this/seems/weird.txt",
  51. err: false,
  52. },
  53. {
  54. name: "success 5",
  55. input: "-key=some/path",
  56. key: "-key",
  57. filepath: "some/path",
  58. err: false,
  59. },
  60. {
  61. name: "invalid 1",
  62. input: "key==some/path",
  63. err: true,
  64. },
  65. {
  66. name: "invalid 2",
  67. input: "=key=some/path",
  68. err: true,
  69. },
  70. {
  71. name: "invalid 3",
  72. input: "==key=/some/other/path",
  73. err: true,
  74. },
  75. {
  76. name: "invalid 4",
  77. input: "=key",
  78. err: true,
  79. },
  80. {
  81. name: "invalid 5",
  82. input: "key=",
  83. err: true,
  84. },
  85. }
  86. for _, tc := range cases {
  87. key, filepath, err := parseFileSource(tc.input)
  88. if err != nil {
  89. if tc.err {
  90. continue
  91. }
  92. t.Errorf("%v: unexpected error: %v", tc.name, err)
  93. continue
  94. }
  95. if tc.err {
  96. t.Errorf("%v: unexpected success", tc.name)
  97. continue
  98. }
  99. if e, a := tc.key, key; e != a {
  100. t.Errorf("%v: expected key %v; got %v", tc.name, e, a)
  101. continue
  102. }
  103. if e, a := tc.filepath, filepath; e != a {
  104. t.Errorf("%v: expected filepath %v; got %v", tc.name, e, a)
  105. }
  106. }
  107. }
  108. func TestParseLiteralSource(t *testing.T) {
  109. cases := []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 _, tc := range cases {
  163. key, value, err := parseLiteralSource(tc.input)
  164. if err != nil {
  165. if tc.err {
  166. continue
  167. }
  168. t.Errorf("%v: unexpected error: %v", tc.name, err)
  169. continue
  170. }
  171. if tc.err {
  172. t.Errorf("%v: unexpected success", tc.name)
  173. continue
  174. }
  175. if e, a := tc.key, key; e != a {
  176. t.Errorf("%v: expected key %v; got %v", tc.name, e, a)
  177. continue
  178. }
  179. if e, a := tc.value, value; e != a {
  180. t.Errorf("%v: expected value %v; got %v", tc.name, e, a)
  181. }
  182. }
  183. }