service_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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 versioned
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/intstr"
  20. "k8s.io/kubernetes/pkg/kubectl/generate"
  21. )
  22. func TestGenerateService(t *testing.T) {
  23. tests := []struct {
  24. name string
  25. generator generate.Generator
  26. params map[string]interface{}
  27. expected v1.Service
  28. }{
  29. {
  30. name: "test1",
  31. generator: ServiceGeneratorV2{},
  32. params: map[string]interface{}{
  33. "selector": "foo=bar,baz=blah",
  34. "name": "test",
  35. "port": "80",
  36. "protocol": "TCP",
  37. "container-port": "1234",
  38. },
  39. expected: v1.Service{
  40. ObjectMeta: metav1.ObjectMeta{
  41. Name: "test",
  42. },
  43. Spec: v1.ServiceSpec{
  44. Selector: map[string]string{
  45. "foo": "bar",
  46. "baz": "blah",
  47. },
  48. Ports: []v1.ServicePort{
  49. {
  50. Port: 80,
  51. Protocol: "TCP",
  52. TargetPort: intstr.FromInt(1234),
  53. },
  54. },
  55. },
  56. },
  57. },
  58. {
  59. name: "test2",
  60. generator: ServiceGeneratorV2{},
  61. params: map[string]interface{}{
  62. "selector": "foo=bar,baz=blah",
  63. "name": "test",
  64. "port": "80",
  65. "protocol": "UDP",
  66. "container-port": "foobar",
  67. },
  68. expected: v1.Service{
  69. ObjectMeta: metav1.ObjectMeta{
  70. Name: "test",
  71. },
  72. Spec: v1.ServiceSpec{
  73. Selector: map[string]string{
  74. "foo": "bar",
  75. "baz": "blah",
  76. },
  77. Ports: []v1.ServicePort{
  78. {
  79. Port: 80,
  80. Protocol: "UDP",
  81. TargetPort: intstr.FromString("foobar"),
  82. },
  83. },
  84. },
  85. },
  86. },
  87. {
  88. name: "test3",
  89. generator: ServiceGeneratorV2{},
  90. params: map[string]interface{}{
  91. "selector": "foo=bar,baz=blah",
  92. "labels": "key1=value1,key2=value2",
  93. "name": "test",
  94. "port": "80",
  95. "protocol": "TCP",
  96. "container-port": "1234",
  97. },
  98. expected: v1.Service{
  99. ObjectMeta: metav1.ObjectMeta{
  100. Name: "test",
  101. Labels: map[string]string{
  102. "key1": "value1",
  103. "key2": "value2",
  104. },
  105. },
  106. Spec: v1.ServiceSpec{
  107. Selector: map[string]string{
  108. "foo": "bar",
  109. "baz": "blah",
  110. },
  111. Ports: []v1.ServicePort{
  112. {
  113. Port: 80,
  114. Protocol: "TCP",
  115. TargetPort: intstr.FromInt(1234),
  116. },
  117. },
  118. },
  119. },
  120. },
  121. {
  122. name: "test4",
  123. generator: ServiceGeneratorV2{},
  124. params: map[string]interface{}{
  125. "selector": "foo=bar,baz=blah",
  126. "name": "test",
  127. "port": "80",
  128. "protocol": "UDP",
  129. "container-port": "foobar",
  130. "external-ip": "1.2.3.4",
  131. },
  132. expected: v1.Service{
  133. ObjectMeta: metav1.ObjectMeta{
  134. Name: "test",
  135. },
  136. Spec: v1.ServiceSpec{
  137. Selector: map[string]string{
  138. "foo": "bar",
  139. "baz": "blah",
  140. },
  141. Ports: []v1.ServicePort{
  142. {
  143. Port: 80,
  144. Protocol: "UDP",
  145. TargetPort: intstr.FromString("foobar"),
  146. },
  147. },
  148. ExternalIPs: []string{"1.2.3.4"},
  149. },
  150. },
  151. },
  152. {
  153. name: "test5",
  154. generator: ServiceGeneratorV2{},
  155. params: map[string]interface{}{
  156. "selector": "foo=bar,baz=blah",
  157. "name": "test",
  158. "port": "80",
  159. "protocol": "UDP",
  160. "container-port": "foobar",
  161. "external-ip": "1.2.3.4",
  162. "type": "LoadBalancer",
  163. },
  164. expected: v1.Service{
  165. ObjectMeta: metav1.ObjectMeta{
  166. Name: "test",
  167. },
  168. Spec: v1.ServiceSpec{
  169. Selector: map[string]string{
  170. "foo": "bar",
  171. "baz": "blah",
  172. },
  173. Ports: []v1.ServicePort{
  174. {
  175. Port: 80,
  176. Protocol: "UDP",
  177. TargetPort: intstr.FromString("foobar"),
  178. },
  179. },
  180. Type: v1.ServiceTypeLoadBalancer,
  181. ExternalIPs: []string{"1.2.3.4"},
  182. },
  183. },
  184. },
  185. {
  186. name: "test6",
  187. generator: ServiceGeneratorV2{},
  188. params: map[string]interface{}{
  189. "selector": "foo=bar,baz=blah",
  190. "name": "test",
  191. "port": "80",
  192. "protocol": "UDP",
  193. "container-port": "foobar",
  194. "type": string(v1.ServiceTypeNodePort),
  195. },
  196. expected: v1.Service{
  197. ObjectMeta: metav1.ObjectMeta{
  198. Name: "test",
  199. },
  200. Spec: v1.ServiceSpec{
  201. Selector: map[string]string{
  202. "foo": "bar",
  203. "baz": "blah",
  204. },
  205. Ports: []v1.ServicePort{
  206. {
  207. Port: 80,
  208. Protocol: "UDP",
  209. TargetPort: intstr.FromString("foobar"),
  210. },
  211. },
  212. Type: v1.ServiceTypeNodePort,
  213. },
  214. },
  215. },
  216. {
  217. name: "test7",
  218. generator: ServiceGeneratorV2{},
  219. params: map[string]interface{}{
  220. "selector": "foo=bar,baz=blah",
  221. "name": "test",
  222. "port": "80",
  223. "protocol": "UDP",
  224. "container-port": "foobar",
  225. "create-external-load-balancer": "true", // ignored when type is present
  226. "type": string(v1.ServiceTypeNodePort),
  227. },
  228. expected: v1.Service{
  229. ObjectMeta: metav1.ObjectMeta{
  230. Name: "test",
  231. },
  232. Spec: v1.ServiceSpec{
  233. Selector: map[string]string{
  234. "foo": "bar",
  235. "baz": "blah",
  236. },
  237. Ports: []v1.ServicePort{
  238. {
  239. Port: 80,
  240. Protocol: "UDP",
  241. TargetPort: intstr.FromString("foobar"),
  242. },
  243. },
  244. Type: v1.ServiceTypeNodePort,
  245. },
  246. },
  247. },
  248. {
  249. name: "test8",
  250. generator: ServiceGeneratorV1{},
  251. params: map[string]interface{}{
  252. "selector": "foo=bar,baz=blah",
  253. "name": "test",
  254. "port": "80",
  255. "protocol": "TCP",
  256. "container-port": "1234",
  257. },
  258. expected: v1.Service{
  259. ObjectMeta: metav1.ObjectMeta{
  260. Name: "test",
  261. },
  262. Spec: v1.ServiceSpec{
  263. Selector: map[string]string{
  264. "foo": "bar",
  265. "baz": "blah",
  266. },
  267. Ports: []v1.ServicePort{
  268. {
  269. Name: "default",
  270. Port: 80,
  271. Protocol: "TCP",
  272. TargetPort: intstr.FromInt(1234),
  273. },
  274. },
  275. },
  276. },
  277. },
  278. {
  279. name: "test9",
  280. generator: ServiceGeneratorV1{},
  281. params: map[string]interface{}{
  282. "selector": "foo=bar,baz=blah",
  283. "name": "test",
  284. "port": "80",
  285. "protocol": "TCP",
  286. "container-port": "1234",
  287. "session-affinity": "ClientIP",
  288. },
  289. expected: v1.Service{
  290. ObjectMeta: metav1.ObjectMeta{
  291. Name: "test",
  292. },
  293. Spec: v1.ServiceSpec{
  294. Selector: map[string]string{
  295. "foo": "bar",
  296. "baz": "blah",
  297. },
  298. Ports: []v1.ServicePort{
  299. {
  300. Name: "default",
  301. Port: 80,
  302. Protocol: "TCP",
  303. TargetPort: intstr.FromInt(1234),
  304. },
  305. },
  306. SessionAffinity: v1.ServiceAffinityClientIP,
  307. },
  308. },
  309. },
  310. {
  311. name: "test10",
  312. generator: ServiceGeneratorV2{},
  313. params: map[string]interface{}{
  314. "selector": "foo=bar,baz=blah",
  315. "name": "test",
  316. "port": "80",
  317. "protocol": "TCP",
  318. "container-port": "1234",
  319. "cluster-ip": "10.10.10.10",
  320. },
  321. expected: v1.Service{
  322. ObjectMeta: metav1.ObjectMeta{
  323. Name: "test",
  324. },
  325. Spec: v1.ServiceSpec{
  326. Selector: map[string]string{
  327. "foo": "bar",
  328. "baz": "blah",
  329. },
  330. Ports: []v1.ServicePort{
  331. {
  332. Port: 80,
  333. Protocol: "TCP",
  334. TargetPort: intstr.FromInt(1234),
  335. },
  336. },
  337. ClusterIP: "10.10.10.10",
  338. },
  339. },
  340. },
  341. {
  342. name: "test11",
  343. generator: ServiceGeneratorV2{},
  344. params: map[string]interface{}{
  345. "selector": "foo=bar,baz=blah",
  346. "name": "test",
  347. "port": "80",
  348. "protocol": "TCP",
  349. "container-port": "1234",
  350. "cluster-ip": "None",
  351. },
  352. expected: v1.Service{
  353. ObjectMeta: metav1.ObjectMeta{
  354. Name: "test",
  355. },
  356. Spec: v1.ServiceSpec{
  357. Selector: map[string]string{
  358. "foo": "bar",
  359. "baz": "blah",
  360. },
  361. Ports: []v1.ServicePort{
  362. {
  363. Port: 80,
  364. Protocol: "TCP",
  365. TargetPort: intstr.FromInt(1234),
  366. },
  367. },
  368. ClusterIP: v1.ClusterIPNone,
  369. },
  370. },
  371. },
  372. {
  373. name: "test12",
  374. generator: ServiceGeneratorV1{},
  375. params: map[string]interface{}{
  376. "selector": "foo=bar",
  377. "name": "test",
  378. "ports": "80,443",
  379. "protocol": "TCP",
  380. "container-port": "foobar",
  381. },
  382. expected: v1.Service{
  383. ObjectMeta: metav1.ObjectMeta{
  384. Name: "test",
  385. },
  386. Spec: v1.ServiceSpec{
  387. Selector: map[string]string{
  388. "foo": "bar",
  389. },
  390. Ports: []v1.ServicePort{
  391. {
  392. Name: "port-1",
  393. Port: 80,
  394. Protocol: v1.ProtocolTCP,
  395. TargetPort: intstr.FromString("foobar"),
  396. },
  397. {
  398. Name: "port-2",
  399. Port: 443,
  400. Protocol: v1.ProtocolTCP,
  401. TargetPort: intstr.FromString("foobar"),
  402. },
  403. },
  404. },
  405. },
  406. },
  407. {
  408. name: "test13",
  409. generator: ServiceGeneratorV2{},
  410. params: map[string]interface{}{
  411. "selector": "foo=bar",
  412. "name": "test",
  413. "ports": "80,443",
  414. "protocol": "UDP",
  415. "target-port": "1234",
  416. },
  417. expected: v1.Service{
  418. ObjectMeta: metav1.ObjectMeta{
  419. Name: "test",
  420. },
  421. Spec: v1.ServiceSpec{
  422. Selector: map[string]string{
  423. "foo": "bar",
  424. },
  425. Ports: []v1.ServicePort{
  426. {
  427. Name: "port-1",
  428. Port: 80,
  429. Protocol: v1.ProtocolUDP,
  430. TargetPort: intstr.FromInt(1234),
  431. },
  432. {
  433. Name: "port-2",
  434. Port: 443,
  435. Protocol: v1.ProtocolUDP,
  436. TargetPort: intstr.FromInt(1234),
  437. },
  438. },
  439. },
  440. },
  441. },
  442. {
  443. name: "test14",
  444. generator: ServiceGeneratorV2{},
  445. params: map[string]interface{}{
  446. "selector": "foo=bar",
  447. "name": "test",
  448. "ports": "80,443",
  449. "protocol": "TCP",
  450. },
  451. expected: v1.Service{
  452. ObjectMeta: metav1.ObjectMeta{
  453. Name: "test",
  454. },
  455. Spec: v1.ServiceSpec{
  456. Selector: map[string]string{
  457. "foo": "bar",
  458. },
  459. Ports: []v1.ServicePort{
  460. {
  461. Name: "port-1",
  462. Port: 80,
  463. Protocol: v1.ProtocolTCP,
  464. TargetPort: intstr.FromInt(80),
  465. },
  466. {
  467. Name: "port-2",
  468. Port: 443,
  469. Protocol: v1.ProtocolTCP,
  470. TargetPort: intstr.FromInt(443),
  471. },
  472. },
  473. },
  474. },
  475. },
  476. {
  477. name: "test15",
  478. generator: ServiceGeneratorV2{},
  479. params: map[string]interface{}{
  480. "selector": "foo=bar",
  481. "name": "test",
  482. "ports": "80,8080",
  483. "protocols": "8080/UDP",
  484. },
  485. expected: v1.Service{
  486. ObjectMeta: metav1.ObjectMeta{
  487. Name: "test",
  488. },
  489. Spec: v1.ServiceSpec{
  490. Selector: map[string]string{
  491. "foo": "bar",
  492. },
  493. Ports: []v1.ServicePort{
  494. {
  495. Name: "port-1",
  496. Port: 80,
  497. Protocol: v1.ProtocolTCP,
  498. TargetPort: intstr.FromInt(80),
  499. },
  500. {
  501. Name: "port-2",
  502. Port: 8080,
  503. Protocol: v1.ProtocolUDP,
  504. TargetPort: intstr.FromInt(8080),
  505. },
  506. },
  507. },
  508. },
  509. },
  510. {
  511. name: "test16",
  512. generator: ServiceGeneratorV2{},
  513. params: map[string]interface{}{
  514. "selector": "foo=bar",
  515. "name": "test",
  516. "ports": "80,8080,8081",
  517. "protocols": "8080/UDP,8081/TCP",
  518. },
  519. expected: v1.Service{
  520. ObjectMeta: metav1.ObjectMeta{
  521. Name: "test",
  522. },
  523. Spec: v1.ServiceSpec{
  524. Selector: map[string]string{
  525. "foo": "bar",
  526. },
  527. Ports: []v1.ServicePort{
  528. {
  529. Name: "port-1",
  530. Port: 80,
  531. Protocol: v1.ProtocolTCP,
  532. TargetPort: intstr.FromInt(80),
  533. },
  534. {
  535. Name: "port-2",
  536. Port: 8080,
  537. Protocol: v1.ProtocolUDP,
  538. TargetPort: intstr.FromInt(8080),
  539. },
  540. {
  541. Name: "port-3",
  542. Port: 8081,
  543. Protocol: v1.ProtocolTCP,
  544. TargetPort: intstr.FromInt(8081),
  545. },
  546. },
  547. },
  548. },
  549. },
  550. {
  551. name: "test17",
  552. generator: ServiceGeneratorV2{},
  553. params: map[string]interface{}{
  554. "selector": "foo=bar,baz=blah",
  555. "name": "test",
  556. "protocol": "TCP",
  557. "container-port": "1234",
  558. "cluster-ip": "None",
  559. },
  560. expected: v1.Service{
  561. ObjectMeta: metav1.ObjectMeta{
  562. Name: "test",
  563. },
  564. Spec: v1.ServiceSpec{
  565. Selector: map[string]string{
  566. "foo": "bar",
  567. "baz": "blah",
  568. },
  569. Ports: []v1.ServicePort{},
  570. ClusterIP: v1.ClusterIPNone,
  571. },
  572. },
  573. },
  574. {
  575. name: "test18",
  576. generator: ServiceGeneratorV2{},
  577. params: map[string]interface{}{
  578. "selector": "foo=bar",
  579. "name": "test",
  580. "cluster-ip": "None",
  581. },
  582. expected: v1.Service{
  583. ObjectMeta: metav1.ObjectMeta{
  584. Name: "test",
  585. },
  586. Spec: v1.ServiceSpec{
  587. Selector: map[string]string{
  588. "foo": "bar",
  589. },
  590. Ports: []v1.ServicePort{},
  591. ClusterIP: v1.ClusterIPNone,
  592. },
  593. },
  594. },
  595. {
  596. generator: ServiceGeneratorV2{},
  597. params: map[string]interface{}{
  598. "selector": "foo=bar,baz=blah",
  599. "name": "test",
  600. "port": "80",
  601. "protocol": "SCTP",
  602. "container-port": "1234",
  603. },
  604. expected: v1.Service{
  605. ObjectMeta: metav1.ObjectMeta{
  606. Name: "test",
  607. },
  608. Spec: v1.ServiceSpec{
  609. Selector: map[string]string{
  610. "foo": "bar",
  611. "baz": "blah",
  612. },
  613. Ports: []v1.ServicePort{
  614. {
  615. Port: 80,
  616. Protocol: "SCTP",
  617. TargetPort: intstr.FromInt(1234),
  618. },
  619. },
  620. },
  621. },
  622. },
  623. {
  624. generator: ServiceGeneratorV2{},
  625. params: map[string]interface{}{
  626. "selector": "foo=bar,baz=blah",
  627. "labels": "key1=value1,key2=value2",
  628. "name": "test",
  629. "port": "80",
  630. "protocol": "SCTP",
  631. "container-port": "1234",
  632. },
  633. expected: v1.Service{
  634. ObjectMeta: metav1.ObjectMeta{
  635. Name: "test",
  636. Labels: map[string]string{
  637. "key1": "value1",
  638. "key2": "value2",
  639. },
  640. },
  641. Spec: v1.ServiceSpec{
  642. Selector: map[string]string{
  643. "foo": "bar",
  644. "baz": "blah",
  645. },
  646. Ports: []v1.ServicePort{
  647. {
  648. Port: 80,
  649. Protocol: "SCTP",
  650. TargetPort: intstr.FromInt(1234),
  651. },
  652. },
  653. },
  654. },
  655. },
  656. {
  657. generator: ServiceGeneratorV1{},
  658. params: map[string]interface{}{
  659. "selector": "foo=bar,baz=blah",
  660. "name": "test",
  661. "port": "80",
  662. "protocol": "SCTP",
  663. "container-port": "1234",
  664. },
  665. expected: v1.Service{
  666. ObjectMeta: metav1.ObjectMeta{
  667. Name: "test",
  668. },
  669. Spec: v1.ServiceSpec{
  670. Selector: map[string]string{
  671. "foo": "bar",
  672. "baz": "blah",
  673. },
  674. Ports: []v1.ServicePort{
  675. {
  676. Name: "default",
  677. Port: 80,
  678. Protocol: "SCTP",
  679. TargetPort: intstr.FromInt(1234),
  680. },
  681. },
  682. },
  683. },
  684. },
  685. {
  686. generator: ServiceGeneratorV1{},
  687. params: map[string]interface{}{
  688. "selector": "foo=bar,baz=blah",
  689. "name": "test",
  690. "port": "80",
  691. "protocol": "SCTP",
  692. "container-port": "1234",
  693. "session-affinity": "ClientIP",
  694. },
  695. expected: v1.Service{
  696. ObjectMeta: metav1.ObjectMeta{
  697. Name: "test",
  698. },
  699. Spec: v1.ServiceSpec{
  700. Selector: map[string]string{
  701. "foo": "bar",
  702. "baz": "blah",
  703. },
  704. Ports: []v1.ServicePort{
  705. {
  706. Name: "default",
  707. Port: 80,
  708. Protocol: "SCTP",
  709. TargetPort: intstr.FromInt(1234),
  710. },
  711. },
  712. SessionAffinity: v1.ServiceAffinityClientIP,
  713. },
  714. },
  715. },
  716. {
  717. generator: ServiceGeneratorV2{},
  718. params: map[string]interface{}{
  719. "selector": "foo=bar,baz=blah",
  720. "name": "test",
  721. "port": "80",
  722. "protocol": "SCTP",
  723. "container-port": "1234",
  724. "cluster-ip": "10.10.10.10",
  725. },
  726. expected: v1.Service{
  727. ObjectMeta: metav1.ObjectMeta{
  728. Name: "test",
  729. },
  730. Spec: v1.ServiceSpec{
  731. Selector: map[string]string{
  732. "foo": "bar",
  733. "baz": "blah",
  734. },
  735. Ports: []v1.ServicePort{
  736. {
  737. Port: 80,
  738. Protocol: "SCTP",
  739. TargetPort: intstr.FromInt(1234),
  740. },
  741. },
  742. ClusterIP: "10.10.10.10",
  743. },
  744. },
  745. },
  746. {
  747. generator: ServiceGeneratorV2{},
  748. params: map[string]interface{}{
  749. "selector": "foo=bar,baz=blah",
  750. "name": "test",
  751. "port": "80",
  752. "protocol": "SCTP",
  753. "container-port": "1234",
  754. "cluster-ip": "None",
  755. },
  756. expected: v1.Service{
  757. ObjectMeta: metav1.ObjectMeta{
  758. Name: "test",
  759. },
  760. Spec: v1.ServiceSpec{
  761. Selector: map[string]string{
  762. "foo": "bar",
  763. "baz": "blah",
  764. },
  765. Ports: []v1.ServicePort{
  766. {
  767. Port: 80,
  768. Protocol: "SCTP",
  769. TargetPort: intstr.FromInt(1234),
  770. },
  771. },
  772. ClusterIP: v1.ClusterIPNone,
  773. },
  774. },
  775. },
  776. {
  777. generator: ServiceGeneratorV1{},
  778. params: map[string]interface{}{
  779. "selector": "foo=bar",
  780. "name": "test",
  781. "ports": "80,443",
  782. "protocol": "SCTP",
  783. "container-port": "foobar",
  784. },
  785. expected: v1.Service{
  786. ObjectMeta: metav1.ObjectMeta{
  787. Name: "test",
  788. },
  789. Spec: v1.ServiceSpec{
  790. Selector: map[string]string{
  791. "foo": "bar",
  792. },
  793. Ports: []v1.ServicePort{
  794. {
  795. Name: "port-1",
  796. Port: 80,
  797. Protocol: v1.ProtocolSCTP,
  798. TargetPort: intstr.FromString("foobar"),
  799. },
  800. {
  801. Name: "port-2",
  802. Port: 443,
  803. Protocol: v1.ProtocolSCTP,
  804. TargetPort: intstr.FromString("foobar"),
  805. },
  806. },
  807. },
  808. },
  809. },
  810. {
  811. generator: ServiceGeneratorV2{},
  812. params: map[string]interface{}{
  813. "selector": "foo=bar",
  814. "name": "test",
  815. "ports": "80,443",
  816. "protocol": "SCTP",
  817. },
  818. expected: v1.Service{
  819. ObjectMeta: metav1.ObjectMeta{
  820. Name: "test",
  821. },
  822. Spec: v1.ServiceSpec{
  823. Selector: map[string]string{
  824. "foo": "bar",
  825. },
  826. Ports: []v1.ServicePort{
  827. {
  828. Name: "port-1",
  829. Port: 80,
  830. Protocol: v1.ProtocolSCTP,
  831. TargetPort: intstr.FromInt(80),
  832. },
  833. {
  834. Name: "port-2",
  835. Port: 443,
  836. Protocol: v1.ProtocolSCTP,
  837. TargetPort: intstr.FromInt(443),
  838. },
  839. },
  840. },
  841. },
  842. },
  843. {
  844. generator: ServiceGeneratorV2{},
  845. params: map[string]interface{}{
  846. "selector": "foo=bar",
  847. "name": "test",
  848. "ports": "80,8080",
  849. "protocols": "8080/SCTP",
  850. },
  851. expected: v1.Service{
  852. ObjectMeta: metav1.ObjectMeta{
  853. Name: "test",
  854. },
  855. Spec: v1.ServiceSpec{
  856. Selector: map[string]string{
  857. "foo": "bar",
  858. },
  859. Ports: []v1.ServicePort{
  860. {
  861. Name: "port-1",
  862. Port: 80,
  863. Protocol: v1.ProtocolTCP,
  864. TargetPort: intstr.FromInt(80),
  865. },
  866. {
  867. Name: "port-2",
  868. Port: 8080,
  869. Protocol: v1.ProtocolSCTP,
  870. TargetPort: intstr.FromInt(8080),
  871. },
  872. },
  873. },
  874. },
  875. },
  876. {
  877. generator: ServiceGeneratorV2{},
  878. params: map[string]interface{}{
  879. "selector": "foo=bar",
  880. "name": "test",
  881. "ports": "80,8080,8081,8082",
  882. "protocols": "8080/UDP,8081/TCP,8082/SCTP",
  883. },
  884. expected: v1.Service{
  885. ObjectMeta: metav1.ObjectMeta{
  886. Name: "test",
  887. },
  888. Spec: v1.ServiceSpec{
  889. Selector: map[string]string{
  890. "foo": "bar",
  891. },
  892. Ports: []v1.ServicePort{
  893. {
  894. Name: "port-1",
  895. Port: 80,
  896. Protocol: v1.ProtocolTCP,
  897. TargetPort: intstr.FromInt(80),
  898. },
  899. {
  900. Name: "port-2",
  901. Port: 8080,
  902. Protocol: v1.ProtocolUDP,
  903. TargetPort: intstr.FromInt(8080),
  904. },
  905. {
  906. Name: "port-3",
  907. Port: 8081,
  908. Protocol: v1.ProtocolTCP,
  909. TargetPort: intstr.FromInt(8081),
  910. },
  911. {
  912. Name: "port-4",
  913. Port: 8082,
  914. Protocol: v1.ProtocolSCTP,
  915. TargetPort: intstr.FromInt(8082),
  916. },
  917. },
  918. },
  919. },
  920. },
  921. {
  922. generator: ServiceGeneratorV2{},
  923. params: map[string]interface{}{
  924. "selector": "foo=bar,baz=blah",
  925. "name": "test",
  926. "protocol": "SCTP",
  927. "container-port": "1234",
  928. "cluster-ip": "None",
  929. },
  930. expected: v1.Service{
  931. ObjectMeta: metav1.ObjectMeta{
  932. Name: "test",
  933. },
  934. Spec: v1.ServiceSpec{
  935. Selector: map[string]string{
  936. "foo": "bar",
  937. "baz": "blah",
  938. },
  939. Ports: []v1.ServicePort{},
  940. ClusterIP: v1.ClusterIPNone,
  941. },
  942. },
  943. },
  944. }
  945. for _, tt := range tests {
  946. t.Run(tt.name, func(t *testing.T) {
  947. obj, err := tt.generator.Generate(tt.params)
  948. if !reflect.DeepEqual(obj, &tt.expected) {
  949. t.Errorf("expected:\n%#v\ngot\n%#v\n", &tt.expected, obj)
  950. }
  951. if err != nil {
  952. t.Errorf("unexpected error: %v", err)
  953. }
  954. })
  955. }
  956. }