run_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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/api/resource"
  19. "k8s.io/kubernetes/pkg/api/unversioned"
  20. "k8s.io/kubernetes/pkg/apis/batch"
  21. "k8s.io/kubernetes/pkg/apis/extensions"
  22. )
  23. func TestGenerate(t *testing.T) {
  24. tests := []struct {
  25. params map[string]interface{}
  26. expected *api.ReplicationController
  27. expectErr bool
  28. }{
  29. {
  30. params: map[string]interface{}{
  31. "name": "foo",
  32. "image": "someimage",
  33. "image-pull-policy": "Always",
  34. "replicas": "1",
  35. "port": "-1",
  36. },
  37. expected: &api.ReplicationController{
  38. ObjectMeta: api.ObjectMeta{
  39. Name: "foo",
  40. Labels: map[string]string{"run": "foo"},
  41. },
  42. Spec: api.ReplicationControllerSpec{
  43. Replicas: 1,
  44. Selector: map[string]string{"run": "foo"},
  45. Template: &api.PodTemplateSpec{
  46. ObjectMeta: api.ObjectMeta{
  47. Labels: map[string]string{"run": "foo"},
  48. },
  49. Spec: api.PodSpec{
  50. Containers: []api.Container{
  51. {
  52. Name: "foo",
  53. Image: "someimage",
  54. ImagePullPolicy: api.PullAlways,
  55. },
  56. },
  57. },
  58. },
  59. },
  60. },
  61. },
  62. {
  63. params: map[string]interface{}{
  64. "name": "foo",
  65. "image": "someimage",
  66. "replicas": "1",
  67. "port": "-1",
  68. "env": []string{"a=b", "c=d"},
  69. },
  70. expected: &api.ReplicationController{
  71. ObjectMeta: api.ObjectMeta{
  72. Name: "foo",
  73. Labels: map[string]string{"run": "foo"},
  74. },
  75. Spec: api.ReplicationControllerSpec{
  76. Replicas: 1,
  77. Selector: map[string]string{"run": "foo"},
  78. Template: &api.PodTemplateSpec{
  79. ObjectMeta: api.ObjectMeta{
  80. Labels: map[string]string{"run": "foo"},
  81. },
  82. Spec: api.PodSpec{
  83. Containers: []api.Container{
  84. {
  85. Name: "foo",
  86. Image: "someimage",
  87. Env: []api.EnvVar{
  88. {
  89. Name: "a",
  90. Value: "b",
  91. },
  92. {
  93. Name: "c",
  94. Value: "d",
  95. },
  96. },
  97. },
  98. },
  99. },
  100. },
  101. },
  102. },
  103. },
  104. {
  105. params: map[string]interface{}{
  106. "name": "foo",
  107. "image": "someimage",
  108. "image-pull-policy": "Never",
  109. "replicas": "1",
  110. "port": "-1",
  111. "args": []string{"bar", "baz", "blah"},
  112. },
  113. expected: &api.ReplicationController{
  114. ObjectMeta: api.ObjectMeta{
  115. Name: "foo",
  116. Labels: map[string]string{"run": "foo"},
  117. },
  118. Spec: api.ReplicationControllerSpec{
  119. Replicas: 1,
  120. Selector: map[string]string{"run": "foo"},
  121. Template: &api.PodTemplateSpec{
  122. ObjectMeta: api.ObjectMeta{
  123. Labels: map[string]string{"run": "foo"},
  124. },
  125. Spec: api.PodSpec{
  126. Containers: []api.Container{
  127. {
  128. Name: "foo",
  129. Image: "someimage",
  130. ImagePullPolicy: api.PullNever,
  131. Args: []string{"bar", "baz", "blah"},
  132. },
  133. },
  134. },
  135. },
  136. },
  137. },
  138. },
  139. {
  140. params: map[string]interface{}{
  141. "name": "foo",
  142. "image": "someimage",
  143. "replicas": "1",
  144. "port": "-1",
  145. "args": []string{"bar", "baz", "blah"},
  146. "command": "true",
  147. },
  148. expected: &api.ReplicationController{
  149. ObjectMeta: api.ObjectMeta{
  150. Name: "foo",
  151. Labels: map[string]string{"run": "foo"},
  152. },
  153. Spec: api.ReplicationControllerSpec{
  154. Replicas: 1,
  155. Selector: map[string]string{"run": "foo"},
  156. Template: &api.PodTemplateSpec{
  157. ObjectMeta: api.ObjectMeta{
  158. Labels: map[string]string{"run": "foo"},
  159. },
  160. Spec: api.PodSpec{
  161. Containers: []api.Container{
  162. {
  163. Name: "foo",
  164. Image: "someimage",
  165. Command: []string{"bar", "baz", "blah"},
  166. },
  167. },
  168. },
  169. },
  170. },
  171. },
  172. },
  173. {
  174. params: map[string]interface{}{
  175. "name": "foo",
  176. "image": "someimage",
  177. "replicas": "1",
  178. "port": "80",
  179. },
  180. expected: &api.ReplicationController{
  181. ObjectMeta: api.ObjectMeta{
  182. Name: "foo",
  183. Labels: map[string]string{"run": "foo"},
  184. },
  185. Spec: api.ReplicationControllerSpec{
  186. Replicas: 1,
  187. Selector: map[string]string{"run": "foo"},
  188. Template: &api.PodTemplateSpec{
  189. ObjectMeta: api.ObjectMeta{
  190. Labels: map[string]string{"run": "foo"},
  191. },
  192. Spec: api.PodSpec{
  193. Containers: []api.Container{
  194. {
  195. Name: "foo",
  196. Image: "someimage",
  197. Ports: []api.ContainerPort{
  198. {
  199. ContainerPort: 80,
  200. },
  201. },
  202. },
  203. },
  204. },
  205. },
  206. },
  207. },
  208. },
  209. {
  210. params: map[string]interface{}{
  211. "name": "foo",
  212. "image": "someimage",
  213. "image-pull-policy": "IfNotPresent",
  214. "replicas": "1",
  215. "port": "80",
  216. "hostport": "80",
  217. },
  218. expected: &api.ReplicationController{
  219. ObjectMeta: api.ObjectMeta{
  220. Name: "foo",
  221. Labels: map[string]string{"run": "foo"},
  222. },
  223. Spec: api.ReplicationControllerSpec{
  224. Replicas: 1,
  225. Selector: map[string]string{"run": "foo"},
  226. Template: &api.PodTemplateSpec{
  227. ObjectMeta: api.ObjectMeta{
  228. Labels: map[string]string{"run": "foo"},
  229. },
  230. Spec: api.PodSpec{
  231. Containers: []api.Container{
  232. {
  233. Name: "foo",
  234. Image: "someimage",
  235. ImagePullPolicy: api.PullIfNotPresent,
  236. Ports: []api.ContainerPort{
  237. {
  238. ContainerPort: 80,
  239. HostPort: 80,
  240. },
  241. },
  242. },
  243. },
  244. },
  245. },
  246. },
  247. },
  248. },
  249. {
  250. params: map[string]interface{}{
  251. "name": "foo",
  252. "image": "someimage",
  253. "replicas": "1",
  254. "hostport": "80",
  255. },
  256. expected: nil,
  257. expectErr: true,
  258. },
  259. {
  260. params: map[string]interface{}{
  261. "name": "foo",
  262. "image": "someimage",
  263. "replicas": "1",
  264. "labels": "foo=bar,baz=blah",
  265. },
  266. expected: &api.ReplicationController{
  267. ObjectMeta: api.ObjectMeta{
  268. Name: "foo",
  269. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  270. },
  271. Spec: api.ReplicationControllerSpec{
  272. Replicas: 1,
  273. Selector: map[string]string{"foo": "bar", "baz": "blah"},
  274. Template: &api.PodTemplateSpec{
  275. ObjectMeta: api.ObjectMeta{
  276. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  277. },
  278. Spec: api.PodSpec{
  279. Containers: []api.Container{
  280. {
  281. Name: "foo",
  282. Image: "someimage",
  283. },
  284. },
  285. },
  286. },
  287. },
  288. },
  289. },
  290. {
  291. params: map[string]interface{}{
  292. "name": "foo",
  293. "image": "someimage",
  294. "replicas": "1",
  295. "hostport": "80",
  296. },
  297. expected: nil,
  298. expectErr: true,
  299. },
  300. {
  301. params: map[string]interface{}{
  302. "name": "foo",
  303. "image": "someimage",
  304. "replicas": "1",
  305. "labels": "foo=bar,baz=blah",
  306. "requests": "cpu100m,memory=100Mi",
  307. },
  308. expected: nil,
  309. expectErr: true,
  310. },
  311. {
  312. params: map[string]interface{}{
  313. "name": "foo",
  314. "image": "someimage",
  315. "replicas": "1",
  316. "labels": "foo=bar,baz=blah",
  317. "requests": "cpu=100m&memory=100Mi",
  318. },
  319. expected: nil,
  320. expectErr: true,
  321. },
  322. {
  323. params: map[string]interface{}{
  324. "name": "foo",
  325. "image": "someimage",
  326. "replicas": "1",
  327. "labels": "foo=bar,baz=blah",
  328. "requests": "cpu=",
  329. },
  330. expected: nil,
  331. expectErr: true,
  332. },
  333. {
  334. params: map[string]interface{}{
  335. "name": "foo",
  336. "image": "someimage",
  337. "replicas": "1",
  338. "labels": "foo=bar,baz=blah",
  339. "requests": "cpu=100m,memory=100Mi",
  340. "limits": "cpu=400m,memory=200Mi",
  341. },
  342. expected: &api.ReplicationController{
  343. ObjectMeta: api.ObjectMeta{
  344. Name: "foo",
  345. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  346. },
  347. Spec: api.ReplicationControllerSpec{
  348. Replicas: 1,
  349. Selector: map[string]string{"foo": "bar", "baz": "blah"},
  350. Template: &api.PodTemplateSpec{
  351. ObjectMeta: api.ObjectMeta{
  352. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  353. },
  354. Spec: api.PodSpec{
  355. Containers: []api.Container{
  356. {
  357. Name: "foo",
  358. Image: "someimage",
  359. Resources: api.ResourceRequirements{
  360. Requests: api.ResourceList{
  361. api.ResourceCPU: resource.MustParse("100m"),
  362. api.ResourceMemory: resource.MustParse("100Mi"),
  363. },
  364. Limits: api.ResourceList{
  365. api.ResourceCPU: resource.MustParse("400m"),
  366. api.ResourceMemory: resource.MustParse("200Mi"),
  367. },
  368. },
  369. },
  370. },
  371. },
  372. },
  373. },
  374. },
  375. },
  376. }
  377. generator := BasicReplicationController{}
  378. for i, test := range tests {
  379. obj, err := generator.Generate(test.params)
  380. t.Logf("%d: %#v", i, obj)
  381. if !test.expectErr && err != nil {
  382. t.Errorf("unexpected error: %v", err)
  383. continue
  384. }
  385. if test.expectErr && err != nil {
  386. continue
  387. }
  388. if !reflect.DeepEqual(obj.(*api.ReplicationController).Spec.Template, test.expected.Spec.Template) {
  389. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", test.expected.Spec.Template, obj.(*api.ReplicationController).Spec.Template)
  390. }
  391. }
  392. }
  393. func TestGeneratePod(t *testing.T) {
  394. tests := []struct {
  395. params map[string]interface{}
  396. expected *api.Pod
  397. expectErr bool
  398. }{
  399. {
  400. params: map[string]interface{}{
  401. "name": "foo",
  402. "image": "someimage",
  403. "port": "-1",
  404. },
  405. expected: &api.Pod{
  406. ObjectMeta: api.ObjectMeta{
  407. Name: "foo",
  408. },
  409. Spec: api.PodSpec{
  410. Containers: []api.Container{
  411. {
  412. Name: "foo",
  413. Image: "someimage",
  414. ImagePullPolicy: api.PullIfNotPresent,
  415. },
  416. },
  417. DNSPolicy: api.DNSClusterFirst,
  418. RestartPolicy: api.RestartPolicyAlways,
  419. },
  420. },
  421. },
  422. {
  423. params: map[string]interface{}{
  424. "name": "foo",
  425. "image": "someimage",
  426. "env": []string{"a", "c"},
  427. },
  428. expected: nil,
  429. expectErr: true,
  430. },
  431. {
  432. params: map[string]interface{}{
  433. "name": "foo",
  434. "image": "someimage",
  435. "image-pull-policy": "Always",
  436. "env": []string{"a=b", "c=d"},
  437. },
  438. expected: &api.Pod{
  439. ObjectMeta: api.ObjectMeta{
  440. Name: "foo",
  441. },
  442. Spec: api.PodSpec{
  443. Containers: []api.Container{
  444. {
  445. Name: "foo",
  446. Image: "someimage",
  447. ImagePullPolicy: api.PullAlways,
  448. Env: []api.EnvVar{
  449. {
  450. Name: "a",
  451. Value: "b",
  452. },
  453. {
  454. Name: "c",
  455. Value: "d",
  456. },
  457. },
  458. },
  459. },
  460. DNSPolicy: api.DNSClusterFirst,
  461. RestartPolicy: api.RestartPolicyAlways,
  462. },
  463. },
  464. },
  465. {
  466. params: map[string]interface{}{
  467. "name": "foo",
  468. "image": "someimage",
  469. "port": "80",
  470. },
  471. expected: &api.Pod{
  472. ObjectMeta: api.ObjectMeta{
  473. Name: "foo",
  474. },
  475. Spec: api.PodSpec{
  476. Containers: []api.Container{
  477. {
  478. Name: "foo",
  479. Image: "someimage",
  480. ImagePullPolicy: api.PullIfNotPresent,
  481. Ports: []api.ContainerPort{
  482. {
  483. ContainerPort: 80,
  484. },
  485. },
  486. },
  487. },
  488. DNSPolicy: api.DNSClusterFirst,
  489. RestartPolicy: api.RestartPolicyAlways,
  490. },
  491. },
  492. },
  493. {
  494. params: map[string]interface{}{
  495. "name": "foo",
  496. "image": "someimage",
  497. "port": "80",
  498. "hostport": "80",
  499. },
  500. expected: &api.Pod{
  501. ObjectMeta: api.ObjectMeta{
  502. Name: "foo",
  503. },
  504. Spec: api.PodSpec{
  505. Containers: []api.Container{
  506. {
  507. Name: "foo",
  508. Image: "someimage",
  509. ImagePullPolicy: api.PullIfNotPresent,
  510. Ports: []api.ContainerPort{
  511. {
  512. ContainerPort: 80,
  513. HostPort: 80,
  514. },
  515. },
  516. },
  517. },
  518. DNSPolicy: api.DNSClusterFirst,
  519. RestartPolicy: api.RestartPolicyAlways,
  520. },
  521. },
  522. },
  523. {
  524. params: map[string]interface{}{
  525. "name": "foo",
  526. "image": "someimage",
  527. "hostport": "80",
  528. },
  529. expected: nil,
  530. expectErr: true,
  531. },
  532. {
  533. params: map[string]interface{}{
  534. "name": "foo",
  535. "image": "someimage",
  536. "replicas": "1",
  537. "labels": "foo=bar,baz=blah",
  538. },
  539. expected: &api.Pod{
  540. ObjectMeta: api.ObjectMeta{
  541. Name: "foo",
  542. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  543. },
  544. Spec: api.PodSpec{
  545. Containers: []api.Container{
  546. {
  547. Name: "foo",
  548. Image: "someimage",
  549. ImagePullPolicy: api.PullIfNotPresent,
  550. },
  551. },
  552. DNSPolicy: api.DNSClusterFirst,
  553. RestartPolicy: api.RestartPolicyAlways,
  554. },
  555. },
  556. },
  557. {
  558. params: map[string]interface{}{
  559. "name": "foo",
  560. "image": "someimage",
  561. "replicas": "1",
  562. "labels": "foo=bar,baz=blah",
  563. "stdin": "true",
  564. },
  565. expected: &api.Pod{
  566. ObjectMeta: api.ObjectMeta{
  567. Name: "foo",
  568. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  569. },
  570. Spec: api.PodSpec{
  571. Containers: []api.Container{
  572. {
  573. Name: "foo",
  574. Image: "someimage",
  575. ImagePullPolicy: api.PullIfNotPresent,
  576. Stdin: true,
  577. StdinOnce: true,
  578. },
  579. },
  580. DNSPolicy: api.DNSClusterFirst,
  581. RestartPolicy: api.RestartPolicyAlways,
  582. },
  583. },
  584. },
  585. {
  586. params: map[string]interface{}{
  587. "name": "foo",
  588. "image": "someimage",
  589. "replicas": "1",
  590. "labels": "foo=bar,baz=blah",
  591. "stdin": "true",
  592. "leave-stdin-open": "true",
  593. },
  594. expected: &api.Pod{
  595. ObjectMeta: api.ObjectMeta{
  596. Name: "foo",
  597. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  598. },
  599. Spec: api.PodSpec{
  600. Containers: []api.Container{
  601. {
  602. Name: "foo",
  603. Image: "someimage",
  604. ImagePullPolicy: api.PullIfNotPresent,
  605. Stdin: true,
  606. StdinOnce: false,
  607. },
  608. },
  609. DNSPolicy: api.DNSClusterFirst,
  610. RestartPolicy: api.RestartPolicyAlways,
  611. },
  612. },
  613. },
  614. }
  615. generator := BasicPod{}
  616. for _, test := range tests {
  617. obj, err := generator.Generate(test.params)
  618. if !test.expectErr && err != nil {
  619. t.Errorf("unexpected error: %v", err)
  620. }
  621. if test.expectErr && err != nil {
  622. continue
  623. }
  624. if !reflect.DeepEqual(obj.(*api.Pod), test.expected) {
  625. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", test.expected, obj.(*api.Pod))
  626. }
  627. }
  628. }
  629. func TestGenerateDeployment(t *testing.T) {
  630. tests := []struct {
  631. params map[string]interface{}
  632. expected *extensions.Deployment
  633. expectErr bool
  634. }{
  635. {
  636. params: map[string]interface{}{
  637. "labels": "foo=bar,baz=blah",
  638. "name": "foo",
  639. "replicas": "3",
  640. "image": "someimage",
  641. "image-pull-policy": "Always",
  642. "port": "80",
  643. "hostport": "80",
  644. "stdin": "true",
  645. "command": "true",
  646. "args": []string{"bar", "baz", "blah"},
  647. "env": []string{"a=b", "c=d"},
  648. "requests": "cpu=100m,memory=100Mi",
  649. "limits": "cpu=400m,memory=200Mi",
  650. },
  651. expected: &extensions.Deployment{
  652. ObjectMeta: api.ObjectMeta{
  653. Name: "foo",
  654. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  655. },
  656. Spec: extensions.DeploymentSpec{
  657. Replicas: 3,
  658. Selector: &unversioned.LabelSelector{MatchLabels: map[string]string{"foo": "bar", "baz": "blah"}},
  659. Template: api.PodTemplateSpec{
  660. ObjectMeta: api.ObjectMeta{
  661. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  662. },
  663. Spec: api.PodSpec{
  664. Containers: []api.Container{
  665. {
  666. Name: "foo",
  667. Image: "someimage",
  668. ImagePullPolicy: api.PullAlways,
  669. Stdin: true,
  670. Ports: []api.ContainerPort{
  671. {
  672. ContainerPort: 80,
  673. HostPort: 80,
  674. },
  675. },
  676. Command: []string{"bar", "baz", "blah"},
  677. Env: []api.EnvVar{
  678. {
  679. Name: "a",
  680. Value: "b",
  681. },
  682. {
  683. Name: "c",
  684. Value: "d",
  685. },
  686. },
  687. Resources: api.ResourceRequirements{
  688. Requests: api.ResourceList{
  689. api.ResourceCPU: resource.MustParse("100m"),
  690. api.ResourceMemory: resource.MustParse("100Mi"),
  691. },
  692. Limits: api.ResourceList{
  693. api.ResourceCPU: resource.MustParse("400m"),
  694. api.ResourceMemory: resource.MustParse("200Mi"),
  695. },
  696. },
  697. },
  698. },
  699. },
  700. },
  701. },
  702. },
  703. },
  704. }
  705. generator := DeploymentV1Beta1{}
  706. for _, test := range tests {
  707. obj, err := generator.Generate(test.params)
  708. if !test.expectErr && err != nil {
  709. t.Errorf("unexpected error: %v", err)
  710. }
  711. if test.expectErr && err != nil {
  712. continue
  713. }
  714. if !reflect.DeepEqual(obj.(*extensions.Deployment), test.expected) {
  715. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", test.expected, obj.(*extensions.Deployment))
  716. }
  717. }
  718. }
  719. func TestGenerateJob(t *testing.T) {
  720. tests := []struct {
  721. params map[string]interface{}
  722. expected *batch.Job
  723. expectErr bool
  724. }{
  725. {
  726. params: map[string]interface{}{
  727. "labels": "foo=bar,baz=blah",
  728. "name": "foo",
  729. "image": "someimage",
  730. "port": "80",
  731. "hostport": "80",
  732. "stdin": "true",
  733. "leave-stdin-open": "true",
  734. "command": "true",
  735. "args": []string{"bar", "baz", "blah"},
  736. "env": []string{"a=b", "c=d"},
  737. "requests": "cpu=100m,memory=100Mi",
  738. "limits": "cpu=400m,memory=200Mi",
  739. "restart": "OnFailure",
  740. },
  741. expected: &batch.Job{
  742. ObjectMeta: api.ObjectMeta{
  743. Name: "foo",
  744. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  745. },
  746. Spec: batch.JobSpec{
  747. Selector: &unversioned.LabelSelector{
  748. MatchLabels: map[string]string{"foo": "bar", "baz": "blah"},
  749. },
  750. ManualSelector: newBool(true),
  751. Template: api.PodTemplateSpec{
  752. ObjectMeta: api.ObjectMeta{
  753. Labels: map[string]string{"foo": "bar", "baz": "blah"},
  754. },
  755. Spec: api.PodSpec{
  756. RestartPolicy: api.RestartPolicyOnFailure,
  757. Containers: []api.Container{
  758. {
  759. Name: "foo",
  760. Image: "someimage",
  761. Stdin: true,
  762. StdinOnce: false,
  763. Ports: []api.ContainerPort{
  764. {
  765. ContainerPort: 80,
  766. HostPort: 80,
  767. },
  768. },
  769. Command: []string{"bar", "baz", "blah"},
  770. Env: []api.EnvVar{
  771. {
  772. Name: "a",
  773. Value: "b",
  774. },
  775. {
  776. Name: "c",
  777. Value: "d",
  778. },
  779. },
  780. Resources: api.ResourceRequirements{
  781. Requests: api.ResourceList{
  782. api.ResourceCPU: resource.MustParse("100m"),
  783. api.ResourceMemory: resource.MustParse("100Mi"),
  784. },
  785. Limits: api.ResourceList{
  786. api.ResourceCPU: resource.MustParse("400m"),
  787. api.ResourceMemory: resource.MustParse("200Mi"),
  788. },
  789. },
  790. },
  791. },
  792. },
  793. },
  794. },
  795. },
  796. },
  797. }
  798. generator := JobV1Beta1{}
  799. for _, test := range tests {
  800. obj, err := generator.Generate(test.params)
  801. if !test.expectErr && err != nil {
  802. t.Errorf("unexpected error: %v", err)
  803. }
  804. if test.expectErr && err != nil {
  805. continue
  806. }
  807. if !reflect.DeepEqual(obj.(*batch.Job), test.expected) {
  808. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", test.expected, obj.(*batch.Job))
  809. }
  810. }
  811. }
  812. func TestParseEnv(t *testing.T) {
  813. tests := []struct {
  814. envArray []string
  815. expected []api.EnvVar
  816. expectErr bool
  817. test string
  818. }{
  819. {
  820. envArray: []string{
  821. "THIS_ENV=isOK",
  822. "HAS_COMMAS=foo,bar",
  823. "HAS_EQUALS=jJnro54iUu75xNy==",
  824. },
  825. expected: []api.EnvVar{
  826. {
  827. Name: "THIS_ENV",
  828. Value: "isOK",
  829. },
  830. {
  831. Name: "HAS_COMMAS",
  832. Value: "foo,bar",
  833. },
  834. {
  835. Name: "HAS_EQUALS",
  836. Value: "jJnro54iUu75xNy==",
  837. },
  838. },
  839. expectErr: false,
  840. test: "test case 1",
  841. },
  842. {
  843. envArray: []string{
  844. "WITH_OUT_EQUALS",
  845. },
  846. expected: []api.EnvVar{},
  847. expectErr: true,
  848. test: "test case 2",
  849. },
  850. {
  851. envArray: []string{
  852. "WITH_OUT_VALUES=",
  853. },
  854. expected: []api.EnvVar{
  855. {
  856. Name: "WITH_OUT_VALUES",
  857. Value: "",
  858. },
  859. },
  860. expectErr: false,
  861. test: "test case 3",
  862. },
  863. {
  864. envArray: []string{
  865. "=WITH_OUT_NAME",
  866. },
  867. expected: []api.EnvVar{},
  868. expectErr: true,
  869. test: "test case 4",
  870. },
  871. }
  872. for _, test := range tests {
  873. envs, err := parseEnvs(test.envArray)
  874. if !test.expectErr && err != nil {
  875. t.Errorf("unexpected error: %v (%s)", err, test.test)
  876. }
  877. if test.expectErr && err != nil {
  878. continue
  879. }
  880. if !reflect.DeepEqual(envs, test.expected) {
  881. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v (%s)", test.expected, envs, test.test)
  882. }
  883. }
  884. }