run_test.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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. appsv1beta1 "k8s.io/api/apps/v1beta1"
  18. batchv1 "k8s.io/api/batch/v1"
  19. batchv1beta1 "k8s.io/api/batch/v1beta1"
  20. batchv2alpha1 "k8s.io/api/batch/v2alpha1"
  21. "k8s.io/api/core/v1"
  22. extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
  23. "k8s.io/apimachinery/pkg/api/resource"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. )
  26. func TestGenerate(t *testing.T) {
  27. one := int32(1)
  28. tests := []struct {
  29. name string
  30. params map[string]interface{}
  31. expected *v1.ReplicationController
  32. expectErr bool
  33. }{
  34. {
  35. name: "test1",
  36. params: map[string]interface{}{
  37. "name": "foo",
  38. "image": "someimage",
  39. "image-pull-policy": "Always",
  40. "replicas": "1",
  41. "port": "",
  42. },
  43. expected: &v1.ReplicationController{
  44. ObjectMeta: metav1.ObjectMeta{
  45. Name: "foo",
  46. Labels: map[string]string{"run": "foo"},
  47. },
  48. Spec: v1.ReplicationControllerSpec{
  49. Replicas: &one,
  50. Selector: map[string]string{"run": "foo"},
  51. Template: &v1.PodTemplateSpec{
  52. ObjectMeta: metav1.ObjectMeta{
  53. Labels: map[string]string{"run": "foo"},
  54. },
  55. Spec: v1.PodSpec{
  56. Containers: []v1.Container{
  57. {
  58. Name: "foo",
  59. Image: "someimage",
  60. ImagePullPolicy: v1.PullAlways,
  61. },
  62. },
  63. },
  64. },
  65. },
  66. },
  67. },
  68. {
  69. name: "test2",
  70. params: map[string]interface{}{
  71. "name": "foo",
  72. "image": "someimage",
  73. "replicas": "1",
  74. "port": "",
  75. "env": []string{"a=b", "c=d"},
  76. },
  77. expected: &v1.ReplicationController{
  78. ObjectMeta: metav1.ObjectMeta{
  79. Name: "foo",
  80. Labels: map[string]string{"run": "foo"},
  81. },
  82. Spec: v1.ReplicationControllerSpec{
  83. Replicas: &one,
  84. Selector: map[string]string{"run": "foo"},
  85. Template: &v1.PodTemplateSpec{
  86. ObjectMeta: metav1.ObjectMeta{
  87. Labels: map[string]string{"run": "foo"},
  88. },
  89. Spec: v1.PodSpec{
  90. Containers: []v1.Container{
  91. {
  92. Name: "foo",
  93. Image: "someimage",
  94. Env: []v1.EnvVar{
  95. {
  96. Name: "a",
  97. Value: "b",
  98. },
  99. {
  100. Name: "c",
  101. Value: "d",
  102. },
  103. },
  104. },
  105. },
  106. },
  107. },
  108. },
  109. },
  110. },
  111. {
  112. name: "test3",
  113. params: map[string]interface{}{
  114. "name": "foo",
  115. "image": "someimage",
  116. "image-pull-policy": "Never",
  117. "replicas": "1",
  118. "port": "",
  119. "args": []string{"bar", "baz", "blah"},
  120. },
  121. expected: &v1.ReplicationController{
  122. ObjectMeta: metav1.ObjectMeta{
  123. Name: "foo",
  124. Labels: map[string]string{"run": "foo"},
  125. },
  126. Spec: v1.ReplicationControllerSpec{
  127. Replicas: &one,
  128. Selector: map[string]string{"run": "foo"},
  129. Template: &v1.PodTemplateSpec{
  130. ObjectMeta: metav1.ObjectMeta{
  131. Labels: map[string]string{"run": "foo"},
  132. },
  133. Spec: v1.PodSpec{
  134. Containers: []v1.Container{
  135. {
  136. Name: "foo",
  137. Image: "someimage",
  138. ImagePullPolicy: v1.PullNever,
  139. Args: []string{"bar", "baz", "blah"},
  140. },
  141. },
  142. },
  143. },
  144. },
  145. },
  146. },
  147. {
  148. name: "test3",
  149. params: map[string]interface{}{
  150. "name": "foo",
  151. "image": "someimage",
  152. "replicas": "1",
  153. "port": "",
  154. "args": []string{"bar", "baz", "blah"},
  155. "command": "true",
  156. },
  157. expected: &v1.ReplicationController{
  158. ObjectMeta: metav1.ObjectMeta{
  159. Name: "foo",
  160. Labels: map[string]string{"run": "foo"},
  161. },
  162. Spec: v1.ReplicationControllerSpec{
  163. Replicas: &one,
  164. Selector: map[string]string{"run": "foo"},
  165. Template: &v1.PodTemplateSpec{
  166. ObjectMeta: metav1.ObjectMeta{
  167. Labels: map[string]string{"run": "foo"},
  168. },
  169. Spec: v1.PodSpec{
  170. Containers: []v1.Container{
  171. {
  172. Name: "foo",
  173. Image: "someimage",
  174. Command: []string{"bar", "baz", "blah"},
  175. },
  176. },
  177. },
  178. },
  179. },
  180. },
  181. },
  182. {
  183. name: "test4",
  184. params: map[string]interface{}{
  185. "name": "foo",
  186. "image": "someimage",
  187. "replicas": "1",
  188. "port": "80",
  189. },
  190. expected: &v1.ReplicationController{
  191. ObjectMeta: metav1.ObjectMeta{
  192. Name: "foo",
  193. Labels: map[string]string{"run": "foo"},
  194. },
  195. Spec: v1.ReplicationControllerSpec{
  196. Replicas: &one,
  197. Selector: map[string]string{"run": "foo"},
  198. Template: &v1.PodTemplateSpec{
  199. ObjectMeta: metav1.ObjectMeta{
  200. Labels: map[string]string{"run": "foo"},
  201. },
  202. Spec: v1.PodSpec{
  203. Containers: []v1.Container{
  204. {
  205. Name: "foo",
  206. Image: "someimage",
  207. Ports: []v1.ContainerPort{
  208. {
  209. ContainerPort: 80,
  210. },
  211. },
  212. },
  213. },
  214. },
  215. },
  216. },
  217. },
  218. },
  219. {
  220. name: "test5",
  221. params: map[string]interface{}{
  222. "name": "foo",
  223. "image": "someimage",
  224. "image-pull-policy": "IfNotPresent",
  225. "replicas": "1",
  226. "port": "80",
  227. "hostport": "80",
  228. },
  229. expected: &v1.ReplicationController{
  230. ObjectMeta: metav1.ObjectMeta{
  231. Name: "foo",
  232. Labels: map[string]string{"run": "foo"},
  233. },
  234. Spec: v1.ReplicationControllerSpec{
  235. Replicas: &one,
  236. Selector: map[string]string{"run": "foo"},
  237. Template: &v1.PodTemplateSpec{
  238. ObjectMeta: metav1.ObjectMeta{
  239. Labels: map[string]string{"run": "foo"},
  240. },
  241. Spec: v1.PodSpec{
  242. Containers: []v1.Container{
  243. {
  244. Name: "foo",
  245. Image: "someimage",
  246. ImagePullPolicy: v1.PullIfNotPresent,
  247. Ports: []v1.ContainerPort{
  248. {
  249. ContainerPort: 80,
  250. HostPort: 80,
  251. },
  252. },
  253. },
  254. },
  255. },
  256. },
  257. },
  258. },
  259. },
  260. {
  261. name: "test6",
  262. params: map[string]interface{}{
  263. "name": "foo",
  264. "image": "someimage",
  265. "replicas": "1",
  266. "hostport": "80",
  267. },
  268. expected: nil,
  269. expectErr: true,
  270. },
  271. {
  272. name: "test7",
  273. params: map[string]interface{}{
  274. "name": "foo",
  275. "image": "someimage",
  276. "replicas": "1",
  277. "labels": "foo=bar,baz=blah",
  278. },
  279. expected: &v1.ReplicationController{
  280. ObjectMeta: metav1.ObjectMeta{
  281. Name: "foo",
  282. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  283. },
  284. Spec: v1.ReplicationControllerSpec{
  285. Replicas: &one,
  286. Selector: map[string]string{"foo": "bar", "baz": "blah"},
  287. Template: &v1.PodTemplateSpec{
  288. ObjectMeta: metav1.ObjectMeta{
  289. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  290. },
  291. Spec: v1.PodSpec{
  292. Containers: []v1.Container{
  293. {
  294. Name: "foo",
  295. Image: "someimage",
  296. },
  297. },
  298. },
  299. },
  300. },
  301. },
  302. },
  303. {
  304. name: "test8",
  305. params: map[string]interface{}{
  306. "name": "foo",
  307. "image": "someimage",
  308. "replicas": "1",
  309. "hostport": "80",
  310. },
  311. expected: nil,
  312. expectErr: true,
  313. },
  314. {
  315. name: "test9",
  316. params: map[string]interface{}{
  317. "name": "foo",
  318. "image": "someimage",
  319. "replicas": "1",
  320. "labels": "foo=bar,baz=blah",
  321. "requests": "cpu100m,memory=100Mi",
  322. },
  323. expected: nil,
  324. expectErr: true,
  325. },
  326. {
  327. name: "test10",
  328. params: map[string]interface{}{
  329. "name": "foo",
  330. "image": "someimage",
  331. "replicas": "1",
  332. "labels": "foo=bar,baz=blah",
  333. "requests": "cpu=100m&memory=100Mi",
  334. },
  335. expected: nil,
  336. expectErr: true,
  337. },
  338. {
  339. name: "test11",
  340. params: map[string]interface{}{
  341. "name": "foo",
  342. "image": "someimage",
  343. "replicas": "1",
  344. "labels": "foo=bar,baz=blah",
  345. "requests": "cpu=",
  346. },
  347. expected: nil,
  348. expectErr: true,
  349. },
  350. {
  351. name: "test12",
  352. params: map[string]interface{}{
  353. "name": "foo",
  354. "image": "someimage",
  355. "replicas": "1",
  356. "labels": "foo=bar,baz=blah",
  357. "requests": "cpu=100m,memory=100Mi",
  358. "limits": "cpu=400m,memory=200Mi",
  359. },
  360. expected: &v1.ReplicationController{
  361. ObjectMeta: metav1.ObjectMeta{
  362. Name: "foo",
  363. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  364. },
  365. Spec: v1.ReplicationControllerSpec{
  366. Replicas: &one,
  367. Selector: map[string]string{"foo": "bar", "baz": "blah"},
  368. Template: &v1.PodTemplateSpec{
  369. ObjectMeta: metav1.ObjectMeta{
  370. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  371. },
  372. Spec: v1.PodSpec{
  373. Containers: []v1.Container{
  374. {
  375. Name: "foo",
  376. Image: "someimage",
  377. Resources: v1.ResourceRequirements{
  378. Requests: v1.ResourceList{
  379. v1.ResourceCPU: resource.MustParse("100m"),
  380. v1.ResourceMemory: resource.MustParse("100Mi"),
  381. },
  382. Limits: v1.ResourceList{
  383. v1.ResourceCPU: resource.MustParse("400m"),
  384. v1.ResourceMemory: resource.MustParse("200Mi"),
  385. },
  386. },
  387. },
  388. },
  389. },
  390. },
  391. },
  392. },
  393. },
  394. }
  395. generator := BasicReplicationController{}
  396. for i, tt := range tests {
  397. t.Run(tt.name, func(t *testing.T) {
  398. obj, err := generator.Generate(tt.params)
  399. t.Logf("%d: %#v", i, obj)
  400. if !tt.expectErr && err != nil {
  401. t.Errorf("unexpected error: %v", err)
  402. return
  403. }
  404. if tt.expectErr && err != nil {
  405. return
  406. }
  407. if !reflect.DeepEqual(obj.(*v1.ReplicationController).Spec.Template, tt.expected.Spec.Template) {
  408. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected.Spec.Template, obj.(*v1.ReplicationController).Spec.Template)
  409. }
  410. })
  411. }
  412. }
  413. func TestGeneratePod(t *testing.T) {
  414. tests := []struct {
  415. name string
  416. params map[string]interface{}
  417. expected *v1.Pod
  418. expectErr bool
  419. }{
  420. {
  421. name: "test1",
  422. params: map[string]interface{}{
  423. "name": "foo",
  424. "image": "someimage",
  425. "port": "",
  426. },
  427. expected: &v1.Pod{
  428. ObjectMeta: metav1.ObjectMeta{
  429. Name: "foo",
  430. Labels: map[string]string{"run": "foo"},
  431. },
  432. Spec: v1.PodSpec{
  433. Containers: []v1.Container{
  434. {
  435. Name: "foo",
  436. Image: "someimage",
  437. },
  438. },
  439. DNSPolicy: v1.DNSClusterFirst,
  440. RestartPolicy: v1.RestartPolicyAlways,
  441. },
  442. },
  443. },
  444. {
  445. name: "test2",
  446. params: map[string]interface{}{
  447. "name": "foo",
  448. "image": "someimage",
  449. "env": []string{"a", "c"},
  450. },
  451. expected: nil,
  452. expectErr: true,
  453. },
  454. {
  455. name: "test3",
  456. params: map[string]interface{}{
  457. "name": "foo",
  458. "image": "someimage",
  459. "image-pull-policy": "Always",
  460. "env": []string{"a=b", "c=d"},
  461. },
  462. expected: &v1.Pod{
  463. ObjectMeta: metav1.ObjectMeta{
  464. Name: "foo",
  465. Labels: map[string]string{"run": "foo"},
  466. },
  467. Spec: v1.PodSpec{
  468. Containers: []v1.Container{
  469. {
  470. Name: "foo",
  471. Image: "someimage",
  472. ImagePullPolicy: v1.PullAlways,
  473. Env: []v1.EnvVar{
  474. {
  475. Name: "a",
  476. Value: "b",
  477. },
  478. {
  479. Name: "c",
  480. Value: "d",
  481. },
  482. },
  483. },
  484. },
  485. DNSPolicy: v1.DNSClusterFirst,
  486. RestartPolicy: v1.RestartPolicyAlways,
  487. },
  488. },
  489. },
  490. {
  491. name: "test4",
  492. params: map[string]interface{}{
  493. "name": "foo",
  494. "image": "someimage",
  495. "port": "80",
  496. },
  497. expected: &v1.Pod{
  498. ObjectMeta: metav1.ObjectMeta{
  499. Name: "foo",
  500. Labels: map[string]string{"run": "foo"},
  501. },
  502. Spec: v1.PodSpec{
  503. Containers: []v1.Container{
  504. {
  505. Name: "foo",
  506. Image: "someimage",
  507. Ports: []v1.ContainerPort{
  508. {
  509. ContainerPort: 80,
  510. },
  511. },
  512. },
  513. },
  514. DNSPolicy: v1.DNSClusterFirst,
  515. RestartPolicy: v1.RestartPolicyAlways,
  516. },
  517. },
  518. },
  519. {
  520. name: "test5",
  521. params: map[string]interface{}{
  522. "name": "foo",
  523. "image": "someimage",
  524. "port": "80",
  525. "hostport": "80",
  526. },
  527. expected: &v1.Pod{
  528. ObjectMeta: metav1.ObjectMeta{
  529. Name: "foo",
  530. Labels: map[string]string{"run": "foo"},
  531. },
  532. Spec: v1.PodSpec{
  533. Containers: []v1.Container{
  534. {
  535. Name: "foo",
  536. Image: "someimage",
  537. Ports: []v1.ContainerPort{
  538. {
  539. ContainerPort: 80,
  540. HostPort: 80,
  541. },
  542. },
  543. },
  544. },
  545. DNSPolicy: v1.DNSClusterFirst,
  546. RestartPolicy: v1.RestartPolicyAlways,
  547. },
  548. },
  549. },
  550. {
  551. name: "test6",
  552. params: map[string]interface{}{
  553. "name": "foo",
  554. "image": "someimage",
  555. "hostport": "80",
  556. },
  557. expected: nil,
  558. expectErr: true,
  559. },
  560. {
  561. name: "test7",
  562. params: map[string]interface{}{
  563. "name": "foo",
  564. "image": "someimage",
  565. "replicas": "1",
  566. "labels": "foo=bar,baz=blah",
  567. },
  568. expected: &v1.Pod{
  569. ObjectMeta: metav1.ObjectMeta{
  570. Name: "foo",
  571. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  572. },
  573. Spec: v1.PodSpec{
  574. Containers: []v1.Container{
  575. {
  576. Name: "foo",
  577. Image: "someimage",
  578. },
  579. },
  580. DNSPolicy: v1.DNSClusterFirst,
  581. RestartPolicy: v1.RestartPolicyAlways,
  582. },
  583. },
  584. },
  585. {
  586. name: "test8",
  587. params: map[string]interface{}{
  588. "name": "foo",
  589. "image": "someimage",
  590. "replicas": "1",
  591. "labels": "foo=bar,baz=blah",
  592. "stdin": "true",
  593. },
  594. expected: &v1.Pod{
  595. ObjectMeta: metav1.ObjectMeta{
  596. Name: "foo",
  597. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  598. },
  599. Spec: v1.PodSpec{
  600. Containers: []v1.Container{
  601. {
  602. Name: "foo",
  603. Image: "someimage",
  604. Stdin: true,
  605. StdinOnce: true,
  606. },
  607. },
  608. DNSPolicy: v1.DNSClusterFirst,
  609. RestartPolicy: v1.RestartPolicyAlways,
  610. },
  611. },
  612. },
  613. {
  614. name: "test9",
  615. params: map[string]interface{}{
  616. "name": "foo",
  617. "image": "someimage",
  618. "replicas": "1",
  619. "labels": "foo=bar,baz=blah",
  620. "stdin": "true",
  621. "leave-stdin-open": "true",
  622. },
  623. expected: &v1.Pod{
  624. ObjectMeta: metav1.ObjectMeta{
  625. Name: "foo",
  626. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  627. },
  628. Spec: v1.PodSpec{
  629. Containers: []v1.Container{
  630. {
  631. Name: "foo",
  632. Image: "someimage",
  633. Stdin: true,
  634. StdinOnce: false,
  635. },
  636. },
  637. DNSPolicy: v1.DNSClusterFirst,
  638. RestartPolicy: v1.RestartPolicyAlways,
  639. },
  640. },
  641. },
  642. }
  643. generator := BasicPod{}
  644. for _, tt := range tests {
  645. t.Run(tt.name, func(t *testing.T) {
  646. obj, err := generator.Generate(tt.params)
  647. if !tt.expectErr && err != nil {
  648. t.Errorf("unexpected error: %v", err)
  649. }
  650. if tt.expectErr && err != nil {
  651. return
  652. }
  653. if !reflect.DeepEqual(obj.(*v1.Pod), tt.expected) {
  654. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*v1.Pod))
  655. }
  656. })
  657. }
  658. }
  659. func TestGenerateDeployment(t *testing.T) {
  660. three := int32(3)
  661. tests := []struct {
  662. name string
  663. params map[string]interface{}
  664. expected *extensionsv1beta1.Deployment
  665. expectErr bool
  666. }{
  667. {
  668. name: "test1",
  669. params: map[string]interface{}{
  670. "labels": "foo=bar,baz=blah",
  671. "name": "foo",
  672. "replicas": "3",
  673. "image": "someimage",
  674. "image-pull-policy": "Always",
  675. "port": "80",
  676. "hostport": "80",
  677. "stdin": "true",
  678. "command": "true",
  679. "args": []string{"bar", "baz", "blah"},
  680. "env": []string{"a=b", "c=d"},
  681. "requests": "cpu=100m,memory=100Mi",
  682. "limits": "cpu=400m,memory=200Mi",
  683. },
  684. expected: &extensionsv1beta1.Deployment{
  685. ObjectMeta: metav1.ObjectMeta{
  686. Name: "foo",
  687. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  688. },
  689. Spec: extensionsv1beta1.DeploymentSpec{
  690. Replicas: &three,
  691. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar", "baz": "blah"}},
  692. Template: v1.PodTemplateSpec{
  693. ObjectMeta: metav1.ObjectMeta{
  694. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  695. },
  696. Spec: v1.PodSpec{
  697. Containers: []v1.Container{
  698. {
  699. Name: "foo",
  700. Image: "someimage",
  701. ImagePullPolicy: v1.PullAlways,
  702. Stdin: true,
  703. Ports: []v1.ContainerPort{
  704. {
  705. ContainerPort: 80,
  706. HostPort: 80,
  707. },
  708. },
  709. Command: []string{"bar", "baz", "blah"},
  710. Env: []v1.EnvVar{
  711. {
  712. Name: "a",
  713. Value: "b",
  714. },
  715. {
  716. Name: "c",
  717. Value: "d",
  718. },
  719. },
  720. Resources: v1.ResourceRequirements{
  721. Requests: v1.ResourceList{
  722. v1.ResourceCPU: resource.MustParse("100m"),
  723. v1.ResourceMemory: resource.MustParse("100Mi"),
  724. },
  725. Limits: v1.ResourceList{
  726. v1.ResourceCPU: resource.MustParse("400m"),
  727. v1.ResourceMemory: resource.MustParse("200Mi"),
  728. },
  729. },
  730. },
  731. },
  732. },
  733. },
  734. },
  735. },
  736. },
  737. }
  738. generator := DeploymentV1Beta1{}
  739. for _, tt := range tests {
  740. t.Run(tt.name, func(t *testing.T) {
  741. obj, err := generator.Generate(tt.params)
  742. if !tt.expectErr && err != nil {
  743. t.Errorf("unexpected error: %v", err)
  744. }
  745. if tt.expectErr && err != nil {
  746. return
  747. }
  748. if !reflect.DeepEqual(obj.(*extensionsv1beta1.Deployment), tt.expected) {
  749. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*extensionsv1beta1.Deployment))
  750. }
  751. })
  752. }
  753. }
  754. func TestGenerateAppsDeployment(t *testing.T) {
  755. three := int32(3)
  756. tests := []struct {
  757. name string
  758. params map[string]interface{}
  759. expected *appsv1beta1.Deployment
  760. expectErr bool
  761. }{
  762. {
  763. name: "test1",
  764. params: map[string]interface{}{
  765. "labels": "foo=bar,baz=blah",
  766. "name": "foo",
  767. "replicas": "3",
  768. "image": "someimage",
  769. "image-pull-policy": "Always",
  770. "port": "80",
  771. "hostport": "80",
  772. "stdin": "true",
  773. "command": "true",
  774. "args": []string{"bar", "baz", "blah"},
  775. "env": []string{"a=b", "c=d"},
  776. "requests": "cpu=100m,memory=100Mi",
  777. "limits": "cpu=400m,memory=200Mi",
  778. },
  779. expected: &appsv1beta1.Deployment{
  780. ObjectMeta: metav1.ObjectMeta{
  781. Name: "foo",
  782. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  783. },
  784. Spec: appsv1beta1.DeploymentSpec{
  785. Replicas: &three,
  786. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar", "baz": "blah"}},
  787. Template: v1.PodTemplateSpec{
  788. ObjectMeta: metav1.ObjectMeta{
  789. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  790. },
  791. Spec: v1.PodSpec{
  792. Containers: []v1.Container{
  793. {
  794. Name: "foo",
  795. Image: "someimage",
  796. ImagePullPolicy: v1.PullAlways,
  797. Stdin: true,
  798. Ports: []v1.ContainerPort{
  799. {
  800. ContainerPort: 80,
  801. HostPort: 80,
  802. },
  803. },
  804. Command: []string{"bar", "baz", "blah"},
  805. Env: []v1.EnvVar{
  806. {
  807. Name: "a",
  808. Value: "b",
  809. },
  810. {
  811. Name: "c",
  812. Value: "d",
  813. },
  814. },
  815. Resources: v1.ResourceRequirements{
  816. Requests: v1.ResourceList{
  817. v1.ResourceCPU: resource.MustParse("100m"),
  818. v1.ResourceMemory: resource.MustParse("100Mi"),
  819. },
  820. Limits: v1.ResourceList{
  821. v1.ResourceCPU: resource.MustParse("400m"),
  822. v1.ResourceMemory: resource.MustParse("200Mi"),
  823. },
  824. },
  825. },
  826. },
  827. },
  828. },
  829. },
  830. },
  831. },
  832. }
  833. generator := DeploymentAppsV1Beta1{}
  834. for _, tt := range tests {
  835. t.Run(tt.name, func(t *testing.T) {
  836. obj, err := generator.Generate(tt.params)
  837. if !tt.expectErr && err != nil {
  838. t.Errorf("unexpected error: %v", err)
  839. }
  840. if tt.expectErr && err != nil {
  841. return
  842. }
  843. if !reflect.DeepEqual(obj.(*appsv1beta1.Deployment), tt.expected) {
  844. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*appsv1beta1.Deployment))
  845. }
  846. })
  847. }
  848. }
  849. func TestGenerateJob(t *testing.T) {
  850. tests := []struct {
  851. name string
  852. params map[string]interface{}
  853. expected *batchv1.Job
  854. expectErr bool
  855. }{
  856. {
  857. name: "test1",
  858. params: map[string]interface{}{
  859. "labels": "foo=bar,baz=blah",
  860. "name": "foo",
  861. "image": "someimage",
  862. "port": "80",
  863. "hostport": "80",
  864. "stdin": "true",
  865. "leave-stdin-open": "true",
  866. "command": "true",
  867. "args": []string{"bar", "baz", "blah"},
  868. "env": []string{"a=b", "c=d"},
  869. "requests": "cpu=100m,memory=100Mi",
  870. "limits": "cpu=400m,memory=200Mi",
  871. "restart": "OnFailure",
  872. },
  873. expected: &batchv1.Job{
  874. ObjectMeta: metav1.ObjectMeta{
  875. Name: "foo",
  876. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  877. },
  878. Spec: batchv1.JobSpec{
  879. Template: v1.PodTemplateSpec{
  880. ObjectMeta: metav1.ObjectMeta{
  881. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  882. },
  883. Spec: v1.PodSpec{
  884. RestartPolicy: v1.RestartPolicyOnFailure,
  885. Containers: []v1.Container{
  886. {
  887. Name: "foo",
  888. Image: "someimage",
  889. Stdin: true,
  890. StdinOnce: false,
  891. Ports: []v1.ContainerPort{
  892. {
  893. ContainerPort: 80,
  894. HostPort: 80,
  895. },
  896. },
  897. Command: []string{"bar", "baz", "blah"},
  898. Env: []v1.EnvVar{
  899. {
  900. Name: "a",
  901. Value: "b",
  902. },
  903. {
  904. Name: "c",
  905. Value: "d",
  906. },
  907. },
  908. Resources: v1.ResourceRequirements{
  909. Requests: v1.ResourceList{
  910. v1.ResourceCPU: resource.MustParse("100m"),
  911. v1.ResourceMemory: resource.MustParse("100Mi"),
  912. },
  913. Limits: v1.ResourceList{
  914. v1.ResourceCPU: resource.MustParse("400m"),
  915. v1.ResourceMemory: resource.MustParse("200Mi"),
  916. },
  917. },
  918. },
  919. },
  920. },
  921. },
  922. },
  923. },
  924. },
  925. }
  926. generator := JobV1{}
  927. for _, tt := range tests {
  928. t.Run(tt.name, func(t *testing.T) {
  929. obj, err := generator.Generate(tt.params)
  930. if !tt.expectErr && err != nil {
  931. t.Errorf("unexpected error: %v", err)
  932. }
  933. if tt.expectErr && err != nil {
  934. return
  935. }
  936. if !reflect.DeepEqual(obj.(*batchv1.Job), tt.expected) {
  937. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*batchv1.Job))
  938. }
  939. })
  940. }
  941. }
  942. func TestGenerateCronJobAlpha(t *testing.T) {
  943. tests := []struct {
  944. name string
  945. params map[string]interface{}
  946. expected *batchv2alpha1.CronJob
  947. expectErr bool
  948. }{
  949. {
  950. name: "test1",
  951. params: map[string]interface{}{
  952. "labels": "foo=bar,baz=blah",
  953. "name": "foo",
  954. "image": "someimage",
  955. "port": "80",
  956. "hostport": "80",
  957. "stdin": "true",
  958. "leave-stdin-open": "true",
  959. "command": "true",
  960. "args": []string{"bar", "baz", "blah"},
  961. "env": []string{"a=b", "c=d"},
  962. "requests": "cpu=100m,memory=100Mi",
  963. "limits": "cpu=400m,memory=200Mi",
  964. "restart": "OnFailure",
  965. "schedule": "0/5 * * * ?",
  966. },
  967. expected: &batchv2alpha1.CronJob{
  968. ObjectMeta: metav1.ObjectMeta{
  969. Name: "foo",
  970. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  971. },
  972. Spec: batchv2alpha1.CronJobSpec{
  973. Schedule: "0/5 * * * ?",
  974. ConcurrencyPolicy: batchv2alpha1.AllowConcurrent,
  975. JobTemplate: batchv2alpha1.JobTemplateSpec{
  976. Spec: batchv1.JobSpec{
  977. Template: v1.PodTemplateSpec{
  978. ObjectMeta: metav1.ObjectMeta{
  979. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  980. },
  981. Spec: v1.PodSpec{
  982. RestartPolicy: v1.RestartPolicyOnFailure,
  983. Containers: []v1.Container{
  984. {
  985. Name: "foo",
  986. Image: "someimage",
  987. Stdin: true,
  988. StdinOnce: false,
  989. Ports: []v1.ContainerPort{
  990. {
  991. ContainerPort: 80,
  992. HostPort: 80,
  993. },
  994. },
  995. Command: []string{"bar", "baz", "blah"},
  996. Env: []v1.EnvVar{
  997. {
  998. Name: "a",
  999. Value: "b",
  1000. },
  1001. {
  1002. Name: "c",
  1003. Value: "d",
  1004. },
  1005. },
  1006. Resources: v1.ResourceRequirements{
  1007. Requests: v1.ResourceList{
  1008. v1.ResourceCPU: resource.MustParse("100m"),
  1009. v1.ResourceMemory: resource.MustParse("100Mi"),
  1010. },
  1011. Limits: v1.ResourceList{
  1012. v1.ResourceCPU: resource.MustParse("400m"),
  1013. v1.ResourceMemory: resource.MustParse("200Mi"),
  1014. },
  1015. },
  1016. },
  1017. },
  1018. },
  1019. },
  1020. },
  1021. },
  1022. },
  1023. },
  1024. },
  1025. }
  1026. generator := CronJobV2Alpha1{}
  1027. for _, tt := range tests {
  1028. t.Run(tt.name, func(t *testing.T) {
  1029. obj, err := generator.Generate(tt.params)
  1030. if !tt.expectErr && err != nil {
  1031. t.Errorf("unexpected error: %v", err)
  1032. }
  1033. if tt.expectErr && err != nil {
  1034. return
  1035. }
  1036. if !reflect.DeepEqual(obj.(*batchv2alpha1.CronJob), tt.expected) {
  1037. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*batchv2alpha1.CronJob))
  1038. }
  1039. })
  1040. }
  1041. }
  1042. func TestGenerateCronJobBeta(t *testing.T) {
  1043. tests := []struct {
  1044. name string
  1045. params map[string]interface{}
  1046. expected *batchv1beta1.CronJob
  1047. expectErr bool
  1048. }{
  1049. {
  1050. name: "test1",
  1051. params: map[string]interface{}{
  1052. "labels": "foo=bar,baz=blah",
  1053. "name": "foo",
  1054. "image": "someimage",
  1055. "port": "80",
  1056. "hostport": "80",
  1057. "stdin": "true",
  1058. "leave-stdin-open": "true",
  1059. "command": "true",
  1060. "args": []string{"bar", "baz", "blah"},
  1061. "env": []string{"a=b", "c=d"},
  1062. "requests": "cpu=100m,memory=100Mi",
  1063. "limits": "cpu=400m,memory=200Mi",
  1064. "restart": "OnFailure",
  1065. "schedule": "0/5 * * * ?",
  1066. },
  1067. expected: &batchv1beta1.CronJob{
  1068. ObjectMeta: metav1.ObjectMeta{
  1069. Name: "foo",
  1070. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  1071. },
  1072. Spec: batchv1beta1.CronJobSpec{
  1073. Schedule: "0/5 * * * ?",
  1074. ConcurrencyPolicy: batchv1beta1.AllowConcurrent,
  1075. JobTemplate: batchv1beta1.JobTemplateSpec{
  1076. Spec: batchv1.JobSpec{
  1077. Template: v1.PodTemplateSpec{
  1078. ObjectMeta: metav1.ObjectMeta{
  1079. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  1080. },
  1081. Spec: v1.PodSpec{
  1082. RestartPolicy: v1.RestartPolicyOnFailure,
  1083. Containers: []v1.Container{
  1084. {
  1085. Name: "foo",
  1086. Image: "someimage",
  1087. Stdin: true,
  1088. StdinOnce: false,
  1089. Ports: []v1.ContainerPort{
  1090. {
  1091. ContainerPort: 80,
  1092. HostPort: 80,
  1093. },
  1094. },
  1095. Command: []string{"bar", "baz", "blah"},
  1096. Env: []v1.EnvVar{
  1097. {
  1098. Name: "a",
  1099. Value: "b",
  1100. },
  1101. {
  1102. Name: "c",
  1103. Value: "d",
  1104. },
  1105. },
  1106. Resources: v1.ResourceRequirements{
  1107. Requests: v1.ResourceList{
  1108. v1.ResourceCPU: resource.MustParse("100m"),
  1109. v1.ResourceMemory: resource.MustParse("100Mi"),
  1110. },
  1111. Limits: v1.ResourceList{
  1112. v1.ResourceCPU: resource.MustParse("400m"),
  1113. v1.ResourceMemory: resource.MustParse("200Mi"),
  1114. },
  1115. },
  1116. },
  1117. },
  1118. },
  1119. },
  1120. },
  1121. },
  1122. },
  1123. },
  1124. },
  1125. }
  1126. generator := CronJobV1Beta1{}
  1127. for _, tt := range tests {
  1128. t.Run(tt.name, func(t *testing.T) {
  1129. obj, err := generator.Generate(tt.params)
  1130. if !tt.expectErr && err != nil {
  1131. t.Errorf("unexpected error: %v", err)
  1132. }
  1133. if tt.expectErr && err != nil {
  1134. return
  1135. }
  1136. if !reflect.DeepEqual(obj.(*batchv1beta1.CronJob), tt.expected) {
  1137. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*batchv1beta1.CronJob))
  1138. }
  1139. })
  1140. }
  1141. }
  1142. func TestParseEnv(t *testing.T) {
  1143. tests := []struct {
  1144. name string
  1145. envArray []string
  1146. expected []v1.EnvVar
  1147. expectErr bool
  1148. test string
  1149. }{
  1150. {
  1151. name: "test1",
  1152. envArray: []string{
  1153. "THIS_ENV=isOK",
  1154. "this.dotted.env=isOKToo",
  1155. "HAS_COMMAS=foo,bar",
  1156. "HAS_EQUALS=jJnro54iUu75xNy==",
  1157. },
  1158. expected: []v1.EnvVar{
  1159. {
  1160. Name: "THIS_ENV",
  1161. Value: "isOK",
  1162. },
  1163. {
  1164. Name: "this.dotted.env",
  1165. Value: "isOKToo",
  1166. },
  1167. {
  1168. Name: "HAS_COMMAS",
  1169. Value: "foo,bar",
  1170. },
  1171. {
  1172. Name: "HAS_EQUALS",
  1173. Value: "jJnro54iUu75xNy==",
  1174. },
  1175. },
  1176. expectErr: false,
  1177. test: "test case 1",
  1178. },
  1179. {
  1180. name: "test2",
  1181. envArray: []string{
  1182. "WITH_OUT_EQUALS",
  1183. },
  1184. expected: []v1.EnvVar{},
  1185. expectErr: true,
  1186. test: "test case 2",
  1187. },
  1188. {
  1189. name: "test3",
  1190. envArray: []string{
  1191. "WITH_OUT_VALUES=",
  1192. },
  1193. expected: []v1.EnvVar{
  1194. {
  1195. Name: "WITH_OUT_VALUES",
  1196. Value: "",
  1197. },
  1198. },
  1199. expectErr: false,
  1200. test: "test case 3",
  1201. },
  1202. {
  1203. name: "test4",
  1204. envArray: []string{
  1205. "=WITH_OUT_NAME",
  1206. },
  1207. expected: []v1.EnvVar{},
  1208. expectErr: true,
  1209. test: "test case 4",
  1210. },
  1211. }
  1212. for _, tt := range tests {
  1213. t.Run(tt.name, func(t *testing.T) {
  1214. envs, err := parseEnvs(tt.envArray)
  1215. if !tt.expectErr && err != nil {
  1216. t.Errorf("unexpected error: %v (%s)", err, tt.test)
  1217. }
  1218. if tt.expectErr && err != nil {
  1219. return
  1220. }
  1221. if !reflect.DeepEqual(envs, tt.expected) {
  1222. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v (%s)", tt.expected, envs, tt.test)
  1223. }
  1224. })
  1225. }
  1226. }