service_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 kubectl
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/util/intstr"
  19. )
  20. func TestGenerateService(t *testing.T) {
  21. tests := []struct {
  22. generator Generator
  23. params map[string]interface{}
  24. expected api.Service
  25. }{
  26. {
  27. generator: ServiceGeneratorV2{},
  28. params: map[string]interface{}{
  29. "selector": "foo=bar,baz=blah",
  30. "name": "test",
  31. "port": "80",
  32. "protocol": "TCP",
  33. "container-port": "1234",
  34. },
  35. expected: api.Service{
  36. ObjectMeta: api.ObjectMeta{
  37. Name: "test",
  38. },
  39. Spec: api.ServiceSpec{
  40. Selector: map[string]string{
  41. "foo": "bar",
  42. "baz": "blah",
  43. },
  44. Ports: []api.ServicePort{
  45. {
  46. Port: 80,
  47. Protocol: "TCP",
  48. TargetPort: intstr.FromInt(1234),
  49. },
  50. },
  51. },
  52. },
  53. },
  54. {
  55. generator: ServiceGeneratorV2{},
  56. params: map[string]interface{}{
  57. "selector": "foo=bar,baz=blah",
  58. "name": "test",
  59. "port": "80",
  60. "protocol": "UDP",
  61. "container-port": "foobar",
  62. },
  63. expected: api.Service{
  64. ObjectMeta: api.ObjectMeta{
  65. Name: "test",
  66. },
  67. Spec: api.ServiceSpec{
  68. Selector: map[string]string{
  69. "foo": "bar",
  70. "baz": "blah",
  71. },
  72. Ports: []api.ServicePort{
  73. {
  74. Port: 80,
  75. Protocol: "UDP",
  76. TargetPort: intstr.FromString("foobar"),
  77. },
  78. },
  79. },
  80. },
  81. },
  82. {
  83. generator: ServiceGeneratorV2{},
  84. params: map[string]interface{}{
  85. "selector": "foo=bar,baz=blah",
  86. "labels": "key1=value1,key2=value2",
  87. "name": "test",
  88. "port": "80",
  89. "protocol": "TCP",
  90. "container-port": "1234",
  91. },
  92. expected: api.Service{
  93. ObjectMeta: api.ObjectMeta{
  94. Name: "test",
  95. Labels: map[string]string{
  96. "key1": "value1",
  97. "key2": "value2",
  98. },
  99. },
  100. Spec: api.ServiceSpec{
  101. Selector: map[string]string{
  102. "foo": "bar",
  103. "baz": "blah",
  104. },
  105. Ports: []api.ServicePort{
  106. {
  107. Port: 80,
  108. Protocol: "TCP",
  109. TargetPort: intstr.FromInt(1234),
  110. },
  111. },
  112. },
  113. },
  114. },
  115. {
  116. generator: ServiceGeneratorV2{},
  117. params: map[string]interface{}{
  118. "selector": "foo=bar,baz=blah",
  119. "name": "test",
  120. "port": "80",
  121. "protocol": "UDP",
  122. "container-port": "foobar",
  123. "external-ip": "1.2.3.4",
  124. },
  125. expected: api.Service{
  126. ObjectMeta: api.ObjectMeta{
  127. Name: "test",
  128. },
  129. Spec: api.ServiceSpec{
  130. Selector: map[string]string{
  131. "foo": "bar",
  132. "baz": "blah",
  133. },
  134. Ports: []api.ServicePort{
  135. {
  136. Port: 80,
  137. Protocol: "UDP",
  138. TargetPort: intstr.FromString("foobar"),
  139. },
  140. },
  141. ExternalIPs: []string{"1.2.3.4"},
  142. },
  143. },
  144. },
  145. {
  146. generator: ServiceGeneratorV2{},
  147. params: map[string]interface{}{
  148. "selector": "foo=bar,baz=blah",
  149. "name": "test",
  150. "port": "80",
  151. "protocol": "UDP",
  152. "container-port": "foobar",
  153. "external-ip": "1.2.3.4",
  154. "create-external-load-balancer": "true",
  155. },
  156. expected: api.Service{
  157. ObjectMeta: api.ObjectMeta{
  158. Name: "test",
  159. },
  160. Spec: api.ServiceSpec{
  161. Selector: map[string]string{
  162. "foo": "bar",
  163. "baz": "blah",
  164. },
  165. Ports: []api.ServicePort{
  166. {
  167. Port: 80,
  168. Protocol: "UDP",
  169. TargetPort: intstr.FromString("foobar"),
  170. },
  171. },
  172. Type: api.ServiceTypeLoadBalancer,
  173. ExternalIPs: []string{"1.2.3.4"},
  174. },
  175. },
  176. },
  177. {
  178. generator: ServiceGeneratorV2{},
  179. params: map[string]interface{}{
  180. "selector": "foo=bar,baz=blah",
  181. "name": "test",
  182. "port": "80",
  183. "protocol": "UDP",
  184. "container-port": "foobar",
  185. "type": string(api.ServiceTypeNodePort),
  186. },
  187. expected: api.Service{
  188. ObjectMeta: api.ObjectMeta{
  189. Name: "test",
  190. },
  191. Spec: api.ServiceSpec{
  192. Selector: map[string]string{
  193. "foo": "bar",
  194. "baz": "blah",
  195. },
  196. Ports: []api.ServicePort{
  197. {
  198. Port: 80,
  199. Protocol: "UDP",
  200. TargetPort: intstr.FromString("foobar"),
  201. },
  202. },
  203. Type: api.ServiceTypeNodePort,
  204. },
  205. },
  206. },
  207. {
  208. generator: ServiceGeneratorV2{},
  209. params: map[string]interface{}{
  210. "selector": "foo=bar,baz=blah",
  211. "name": "test",
  212. "port": "80",
  213. "protocol": "UDP",
  214. "container-port": "foobar",
  215. "create-external-load-balancer": "true", // ignored when type is present
  216. "type": string(api.ServiceTypeNodePort),
  217. },
  218. expected: api.Service{
  219. ObjectMeta: api.ObjectMeta{
  220. Name: "test",
  221. },
  222. Spec: api.ServiceSpec{
  223. Selector: map[string]string{
  224. "foo": "bar",
  225. "baz": "blah",
  226. },
  227. Ports: []api.ServicePort{
  228. {
  229. Port: 80,
  230. Protocol: "UDP",
  231. TargetPort: intstr.FromString("foobar"),
  232. },
  233. },
  234. Type: api.ServiceTypeNodePort,
  235. },
  236. },
  237. },
  238. {
  239. generator: ServiceGeneratorV1{},
  240. params: map[string]interface{}{
  241. "selector": "foo=bar,baz=blah",
  242. "name": "test",
  243. "port": "80",
  244. "protocol": "TCP",
  245. "container-port": "1234",
  246. },
  247. expected: api.Service{
  248. ObjectMeta: api.ObjectMeta{
  249. Name: "test",
  250. },
  251. Spec: api.ServiceSpec{
  252. Selector: map[string]string{
  253. "foo": "bar",
  254. "baz": "blah",
  255. },
  256. Ports: []api.ServicePort{
  257. {
  258. Name: "default",
  259. Port: 80,
  260. Protocol: "TCP",
  261. TargetPort: intstr.FromInt(1234),
  262. },
  263. },
  264. },
  265. },
  266. },
  267. {
  268. generator: ServiceGeneratorV1{},
  269. params: map[string]interface{}{
  270. "selector": "foo=bar,baz=blah",
  271. "name": "test",
  272. "port": "80",
  273. "protocol": "TCP",
  274. "container-port": "1234",
  275. "session-affinity": "ClientIP",
  276. },
  277. expected: api.Service{
  278. ObjectMeta: api.ObjectMeta{
  279. Name: "test",
  280. },
  281. Spec: api.ServiceSpec{
  282. Selector: map[string]string{
  283. "foo": "bar",
  284. "baz": "blah",
  285. },
  286. Ports: []api.ServicePort{
  287. {
  288. Name: "default",
  289. Port: 80,
  290. Protocol: "TCP",
  291. TargetPort: intstr.FromInt(1234),
  292. },
  293. },
  294. SessionAffinity: api.ServiceAffinityClientIP,
  295. },
  296. },
  297. },
  298. {
  299. generator: ServiceGeneratorV2{},
  300. params: map[string]interface{}{
  301. "selector": "foo=bar,baz=blah",
  302. "name": "test",
  303. "port": "80",
  304. "protocol": "TCP",
  305. "container-port": "1234",
  306. "cluster-ip": "10.10.10.10",
  307. },
  308. expected: api.Service{
  309. ObjectMeta: api.ObjectMeta{
  310. Name: "test",
  311. },
  312. Spec: api.ServiceSpec{
  313. Selector: map[string]string{
  314. "foo": "bar",
  315. "baz": "blah",
  316. },
  317. Ports: []api.ServicePort{
  318. {
  319. Port: 80,
  320. Protocol: "TCP",
  321. TargetPort: intstr.FromInt(1234),
  322. },
  323. },
  324. ClusterIP: "10.10.10.10",
  325. },
  326. },
  327. },
  328. {
  329. generator: ServiceGeneratorV2{},
  330. params: map[string]interface{}{
  331. "selector": "foo=bar,baz=blah",
  332. "name": "test",
  333. "port": "80",
  334. "protocol": "TCP",
  335. "container-port": "1234",
  336. "cluster-ip": "None",
  337. },
  338. expected: api.Service{
  339. ObjectMeta: api.ObjectMeta{
  340. Name: "test",
  341. },
  342. Spec: api.ServiceSpec{
  343. Selector: map[string]string{
  344. "foo": "bar",
  345. "baz": "blah",
  346. },
  347. Ports: []api.ServicePort{
  348. {
  349. Port: 80,
  350. Protocol: "TCP",
  351. TargetPort: intstr.FromInt(1234),
  352. },
  353. },
  354. ClusterIP: api.ClusterIPNone,
  355. },
  356. },
  357. },
  358. {
  359. generator: ServiceGeneratorV1{},
  360. params: map[string]interface{}{
  361. "selector": "foo=bar",
  362. "name": "test",
  363. "ports": "80,443",
  364. "protocol": "TCP",
  365. "container-port": "foobar",
  366. },
  367. expected: api.Service{
  368. ObjectMeta: api.ObjectMeta{
  369. Name: "test",
  370. },
  371. Spec: api.ServiceSpec{
  372. Selector: map[string]string{
  373. "foo": "bar",
  374. },
  375. Ports: []api.ServicePort{
  376. {
  377. Name: "port-1",
  378. Port: 80,
  379. Protocol: api.ProtocolTCP,
  380. TargetPort: intstr.FromString("foobar"),
  381. },
  382. {
  383. Name: "port-2",
  384. Port: 443,
  385. Protocol: api.ProtocolTCP,
  386. TargetPort: intstr.FromString("foobar"),
  387. },
  388. },
  389. },
  390. },
  391. },
  392. {
  393. generator: ServiceGeneratorV2{},
  394. params: map[string]interface{}{
  395. "selector": "foo=bar",
  396. "name": "test",
  397. "ports": "80,443",
  398. "protocol": "UDP",
  399. "target-port": "1234",
  400. },
  401. expected: api.Service{
  402. ObjectMeta: api.ObjectMeta{
  403. Name: "test",
  404. },
  405. Spec: api.ServiceSpec{
  406. Selector: map[string]string{
  407. "foo": "bar",
  408. },
  409. Ports: []api.ServicePort{
  410. {
  411. Name: "port-1",
  412. Port: 80,
  413. Protocol: api.ProtocolUDP,
  414. TargetPort: intstr.FromInt(1234),
  415. },
  416. {
  417. Name: "port-2",
  418. Port: 443,
  419. Protocol: api.ProtocolUDP,
  420. TargetPort: intstr.FromInt(1234),
  421. },
  422. },
  423. },
  424. },
  425. },
  426. {
  427. generator: ServiceGeneratorV2{},
  428. params: map[string]interface{}{
  429. "selector": "foo=bar",
  430. "name": "test",
  431. "ports": "80,443",
  432. "protocol": "TCP",
  433. },
  434. expected: api.Service{
  435. ObjectMeta: api.ObjectMeta{
  436. Name: "test",
  437. },
  438. Spec: api.ServiceSpec{
  439. Selector: map[string]string{
  440. "foo": "bar",
  441. },
  442. Ports: []api.ServicePort{
  443. {
  444. Name: "port-1",
  445. Port: 80,
  446. Protocol: api.ProtocolTCP,
  447. TargetPort: intstr.FromInt(80),
  448. },
  449. {
  450. Name: "port-2",
  451. Port: 443,
  452. Protocol: api.ProtocolTCP,
  453. TargetPort: intstr.FromInt(443),
  454. },
  455. },
  456. },
  457. },
  458. },
  459. {
  460. generator: ServiceGeneratorV2{},
  461. params: map[string]interface{}{
  462. "selector": "foo=bar",
  463. "name": "test",
  464. "ports": "80,8080",
  465. "protocols": "8080/UDP",
  466. },
  467. expected: api.Service{
  468. ObjectMeta: api.ObjectMeta{
  469. Name: "test",
  470. },
  471. Spec: api.ServiceSpec{
  472. Selector: map[string]string{
  473. "foo": "bar",
  474. },
  475. Ports: []api.ServicePort{
  476. {
  477. Name: "port-1",
  478. Port: 80,
  479. Protocol: api.ProtocolTCP,
  480. TargetPort: intstr.FromInt(80),
  481. },
  482. {
  483. Name: "port-2",
  484. Port: 8080,
  485. Protocol: api.ProtocolUDP,
  486. TargetPort: intstr.FromInt(8080),
  487. },
  488. },
  489. },
  490. },
  491. },
  492. {
  493. generator: ServiceGeneratorV2{},
  494. params: map[string]interface{}{
  495. "selector": "foo=bar",
  496. "name": "test",
  497. "ports": "80,8080,8081",
  498. "protocols": "8080/UDP,8081/TCP",
  499. },
  500. expected: api.Service{
  501. ObjectMeta: api.ObjectMeta{
  502. Name: "test",
  503. },
  504. Spec: api.ServiceSpec{
  505. Selector: map[string]string{
  506. "foo": "bar",
  507. },
  508. Ports: []api.ServicePort{
  509. {
  510. Name: "port-1",
  511. Port: 80,
  512. Protocol: api.ProtocolTCP,
  513. TargetPort: intstr.FromInt(80),
  514. },
  515. {
  516. Name: "port-2",
  517. Port: 8080,
  518. Protocol: api.ProtocolUDP,
  519. TargetPort: intstr.FromInt(8080),
  520. },
  521. {
  522. Name: "port-3",
  523. Port: 8081,
  524. Protocol: api.ProtocolTCP,
  525. TargetPort: intstr.FromInt(8081),
  526. },
  527. },
  528. },
  529. },
  530. },
  531. }
  532. for _, test := range tests {
  533. obj, err := test.generator.Generate(test.params)
  534. if !reflect.DeepEqual(obj, &test.expected) {
  535. t.Errorf("expected:\n%#v\ngot\n%#v\n", &test.expected, obj)
  536. }
  537. if err != nil {
  538. t.Errorf("unexpected error: %v", err)
  539. }
  540. }
  541. }