generate_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. Copyright 2014 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 generate
  14. import (
  15. "reflect"
  16. "testing"
  17. "github.com/spf13/cobra"
  18. )
  19. type TestStruct struct {
  20. val int
  21. }
  22. func TestIsZero(t *testing.T) {
  23. tests := []struct {
  24. name string
  25. val interface{}
  26. expectZero bool
  27. }{
  28. {
  29. name: "test1",
  30. val: "",
  31. expectZero: true,
  32. },
  33. {
  34. name: "test2",
  35. val: nil,
  36. expectZero: true,
  37. },
  38. {
  39. name: "test3",
  40. val: 0,
  41. expectZero: true,
  42. },
  43. {
  44. name: "test4",
  45. val: TestStruct{},
  46. expectZero: true,
  47. },
  48. {
  49. name: "test5",
  50. val: "foo",
  51. expectZero: false,
  52. },
  53. {
  54. name: "test6",
  55. val: 1,
  56. expectZero: false,
  57. },
  58. {
  59. name: "test7",
  60. val: TestStruct{val: 2},
  61. expectZero: false,
  62. },
  63. }
  64. for _, tt := range tests {
  65. t.Run(tt.name, func(t *testing.T) {
  66. output := IsZero(tt.val)
  67. if output != tt.expectZero {
  68. t.Errorf("expected: %v, saw %v", tt.expectZero, output)
  69. }
  70. })
  71. }
  72. }
  73. func TestValidateParams(t *testing.T) {
  74. tests := []struct {
  75. name string
  76. paramSpec []GeneratorParam
  77. params map[string]interface{}
  78. valid bool
  79. }{
  80. {
  81. name: "test1",
  82. paramSpec: []GeneratorParam{},
  83. params: map[string]interface{}{},
  84. valid: true,
  85. },
  86. {
  87. name: "test2",
  88. paramSpec: []GeneratorParam{
  89. {Name: "foo"},
  90. },
  91. params: map[string]interface{}{},
  92. valid: true,
  93. },
  94. {
  95. name: "test3",
  96. paramSpec: []GeneratorParam{
  97. {Name: "foo", Required: true},
  98. },
  99. params: map[string]interface{}{
  100. "foo": "bar",
  101. },
  102. valid: true,
  103. },
  104. {
  105. name: "test4",
  106. paramSpec: []GeneratorParam{
  107. {Name: "foo", Required: true},
  108. },
  109. params: map[string]interface{}{
  110. "baz": "blah",
  111. "foo": "bar",
  112. },
  113. valid: true,
  114. },
  115. {
  116. name: "test5",
  117. paramSpec: []GeneratorParam{
  118. {Name: "foo", Required: true},
  119. {Name: "baz", Required: true},
  120. },
  121. params: map[string]interface{}{
  122. "baz": "blah",
  123. "foo": "bar",
  124. },
  125. valid: true,
  126. },
  127. {
  128. name: "test6",
  129. paramSpec: []GeneratorParam{
  130. {Name: "foo", Required: true},
  131. {Name: "baz", Required: true},
  132. },
  133. params: map[string]interface{}{
  134. "foo": "bar",
  135. },
  136. valid: false,
  137. },
  138. }
  139. for _, tt := range tests {
  140. t.Run(tt.name, func(t *testing.T) {
  141. err := ValidateParams(tt.paramSpec, tt.params)
  142. if tt.valid && err != nil {
  143. t.Errorf("unexpected error: %v", err)
  144. }
  145. if !tt.valid && err == nil {
  146. t.Errorf("unexpected non-error")
  147. }
  148. })
  149. }
  150. }
  151. func TestMakeParams(t *testing.T) {
  152. cmd := &cobra.Command{}
  153. cmd.Flags().String("foo", "bar", "")
  154. cmd.Flags().String("baz", "", "")
  155. cmd.Flags().Set("baz", "blah")
  156. paramSpec := []GeneratorParam{
  157. {Name: "foo", Required: true},
  158. {Name: "baz", Required: true},
  159. }
  160. expected := map[string]interface{}{
  161. "foo": "bar",
  162. "baz": "blah",
  163. }
  164. params := MakeParams(cmd, paramSpec)
  165. if !reflect.DeepEqual(params, expected) {
  166. t.Errorf("\nexpected:\n%v\nsaw:\n%v", expected, params)
  167. }
  168. }
  169. func TestGetBool(t *testing.T) {
  170. testCases := []struct {
  171. name string
  172. parameters map[string]string
  173. key string
  174. defaultValue bool
  175. expected bool
  176. expectError bool
  177. }{
  178. {
  179. name: "found key in parameters, default value is different from key value",
  180. parameters: map[string]string{
  181. "foo": "false",
  182. },
  183. key: "foo",
  184. defaultValue: false,
  185. expected: false,
  186. expectError: false,
  187. },
  188. {
  189. name: "found key in parameters, default value is same with key value",
  190. parameters: map[string]string{
  191. "foo": "true",
  192. },
  193. key: "foo",
  194. defaultValue: true,
  195. expected: true,
  196. expectError: false,
  197. },
  198. {
  199. name: "key not found in parameters, default value is true",
  200. parameters: map[string]string{
  201. "foo": "true",
  202. "far": "false",
  203. },
  204. key: "bar",
  205. defaultValue: true,
  206. expected: true,
  207. expectError: false,
  208. },
  209. {
  210. name: "key not found in parameters, default value is false",
  211. parameters: map[string]string{
  212. "foo": "true",
  213. "far": "false",
  214. },
  215. key: "bar",
  216. defaultValue: false,
  217. expected: false,
  218. expectError: false,
  219. },
  220. {
  221. name: "parameters is empty",
  222. parameters: map[string]string{},
  223. key: "foo",
  224. defaultValue: true,
  225. expected: true,
  226. expectError: false,
  227. },
  228. {
  229. name: "parameters key is not a valid bool value",
  230. parameters: map[string]string{
  231. "foo": "error",
  232. },
  233. key: "foo",
  234. defaultValue: true,
  235. expected: false,
  236. expectError: true,
  237. },
  238. }
  239. for _, tt := range testCases {
  240. t.Run(tt.name, func(t *testing.T) {
  241. got, err := GetBool(tt.parameters, tt.key, tt.defaultValue)
  242. if err != nil && tt.expectError == false {
  243. t.Errorf("%s: unexpected error: %v", tt.name, err)
  244. }
  245. if err == nil && tt.expectError == true {
  246. t.Errorf("%s: expect error, got nil", tt.name)
  247. }
  248. if got != tt.expected {
  249. t.Errorf("%s: expect %v, got %v", tt.name, tt.expected, got)
  250. }
  251. })
  252. }
  253. }
  254. func TestMakeParseLabels(t *testing.T) {
  255. successCases := []struct {
  256. name string
  257. labels map[string]string
  258. expected map[string]string
  259. }{
  260. {
  261. name: "test1",
  262. labels: map[string]string{
  263. "foo": "false",
  264. },
  265. expected: map[string]string{
  266. "foo": "false",
  267. },
  268. },
  269. {
  270. name: "test2",
  271. labels: map[string]string{
  272. "foo": "true",
  273. "bar": "123",
  274. },
  275. expected: map[string]string{
  276. "foo": "true",
  277. "bar": "123",
  278. },
  279. },
  280. }
  281. for _, tt := range successCases {
  282. t.Run(tt.name, func(t *testing.T) {
  283. labelString := MakeLabels(tt.labels)
  284. got, err := ParseLabels(labelString)
  285. if err != nil {
  286. t.Errorf("unexpected error :%v", err)
  287. }
  288. if !reflect.DeepEqual(tt.expected, got) {
  289. t.Errorf("\nexpected:\n%v\ngot:\n%v", tt.expected, got)
  290. }
  291. })
  292. }
  293. errorCases := []struct {
  294. name string
  295. labels interface{}
  296. }{
  297. {
  298. name: "non-string",
  299. labels: 123,
  300. },
  301. {
  302. name: "empty string",
  303. labels: "",
  304. },
  305. {
  306. name: "error format",
  307. labels: "abc=456;bcd=789",
  308. },
  309. {
  310. name: "error format",
  311. labels: "abc=456.bcd=789",
  312. },
  313. {
  314. name: "error format",
  315. labels: "abc,789",
  316. },
  317. {
  318. name: "error format",
  319. labels: "abc",
  320. },
  321. {
  322. name: "error format",
  323. labels: "=abc",
  324. },
  325. }
  326. for _, test := range errorCases {
  327. _, err := ParseLabels(test.labels)
  328. if err == nil {
  329. t.Errorf("labels %s expect error, reason: %s, got nil", test.labels, test.name)
  330. }
  331. }
  332. }
  333. func TestMakeParseProtocols(t *testing.T) {
  334. successCases := []struct {
  335. name string
  336. protocols map[string]string
  337. expected map[string]string
  338. }{
  339. {
  340. name: "test1",
  341. protocols: map[string]string{
  342. "101": "TCP",
  343. },
  344. expected: map[string]string{
  345. "101": "TCP",
  346. },
  347. },
  348. {
  349. name: "test2",
  350. protocols: map[string]string{
  351. "102": "UDP",
  352. "101": "TCP",
  353. "103": "SCTP",
  354. },
  355. expected: map[string]string{
  356. "102": "UDP",
  357. "101": "TCP",
  358. "103": "SCTP",
  359. },
  360. },
  361. }
  362. for _, tt := range successCases {
  363. t.Run(tt.name, func(t *testing.T) {
  364. protocolString := MakeProtocols(tt.protocols)
  365. got, err := ParseProtocols(protocolString)
  366. if err != nil {
  367. t.Errorf("unexpected error :%v", err)
  368. }
  369. if !reflect.DeepEqual(tt.expected, got) {
  370. t.Errorf("\nexpected:\n%v\ngot:\n%v", tt.expected, got)
  371. }
  372. })
  373. }
  374. errorCases := []struct {
  375. name string
  376. protocols interface{}
  377. }{
  378. {
  379. name: "non-string",
  380. protocols: 123,
  381. },
  382. {
  383. name: "empty string",
  384. protocols: "",
  385. },
  386. {
  387. name: "error format",
  388. protocols: "123/TCP;456/UDP",
  389. },
  390. {
  391. name: "error format",
  392. protocols: "123/TCP.456/UDP",
  393. },
  394. {
  395. name: "error format",
  396. protocols: "123=456",
  397. },
  398. {
  399. name: "error format",
  400. protocols: "123",
  401. },
  402. {
  403. name: "error format",
  404. protocols: "123=",
  405. },
  406. {
  407. name: "error format",
  408. protocols: "=TCP",
  409. },
  410. }
  411. for _, test := range errorCases {
  412. _, err := ParseProtocols(test.protocols)
  413. if err == nil {
  414. t.Errorf("protocols %s expect error, reason: %s, got nil", test.protocols, test.name)
  415. }
  416. }
  417. }