defaults_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. Copyright 2015 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 v1beta1_test
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. _ "k8s.io/kubernetes/pkg/api/install"
  19. "k8s.io/kubernetes/pkg/api/resource"
  20. "k8s.io/kubernetes/pkg/api/v1"
  21. _ "k8s.io/kubernetes/pkg/apis/extensions/install"
  22. . "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
  23. "k8s.io/kubernetes/pkg/runtime"
  24. "k8s.io/kubernetes/pkg/util/intstr"
  25. )
  26. func TestSetDefaultDaemonSet(t *testing.T) {
  27. defaultLabels := map[string]string{"foo": "bar"}
  28. period := int64(v1.DefaultTerminationGracePeriodSeconds)
  29. defaultTemplate := v1.PodTemplateSpec{
  30. Spec: v1.PodSpec{
  31. DNSPolicy: v1.DNSClusterFirst,
  32. RestartPolicy: v1.RestartPolicyAlways,
  33. SecurityContext: &v1.PodSecurityContext{},
  34. TerminationGracePeriodSeconds: &period,
  35. },
  36. ObjectMeta: v1.ObjectMeta{
  37. Labels: defaultLabels,
  38. },
  39. }
  40. templateNoLabel := v1.PodTemplateSpec{
  41. Spec: v1.PodSpec{
  42. DNSPolicy: v1.DNSClusterFirst,
  43. RestartPolicy: v1.RestartPolicyAlways,
  44. SecurityContext: &v1.PodSecurityContext{},
  45. TerminationGracePeriodSeconds: &period,
  46. },
  47. }
  48. tests := []struct {
  49. original *DaemonSet
  50. expected *DaemonSet
  51. }{
  52. { // Labels change/defaulting test.
  53. original: &DaemonSet{
  54. Spec: DaemonSetSpec{
  55. Template: defaultTemplate,
  56. },
  57. },
  58. expected: &DaemonSet{
  59. ObjectMeta: v1.ObjectMeta{
  60. Labels: defaultLabels,
  61. },
  62. Spec: DaemonSetSpec{
  63. Selector: &LabelSelector{
  64. MatchLabels: defaultLabels,
  65. },
  66. Template: defaultTemplate,
  67. },
  68. },
  69. },
  70. { // Labels change/defaulting test.
  71. original: &DaemonSet{
  72. ObjectMeta: v1.ObjectMeta{
  73. Labels: map[string]string{
  74. "bar": "foo",
  75. },
  76. },
  77. Spec: DaemonSetSpec{
  78. Template: defaultTemplate,
  79. },
  80. },
  81. expected: &DaemonSet{
  82. ObjectMeta: v1.ObjectMeta{
  83. Labels: map[string]string{
  84. "bar": "foo",
  85. },
  86. },
  87. Spec: DaemonSetSpec{
  88. Selector: &LabelSelector{
  89. MatchLabels: defaultLabels,
  90. },
  91. Template: defaultTemplate,
  92. },
  93. },
  94. },
  95. { // Update strategy.
  96. original: &DaemonSet{},
  97. expected: &DaemonSet{
  98. Spec: DaemonSetSpec{
  99. Template: templateNoLabel,
  100. },
  101. },
  102. },
  103. { // Update strategy.
  104. original: &DaemonSet{
  105. Spec: DaemonSetSpec{},
  106. },
  107. expected: &DaemonSet{
  108. Spec: DaemonSetSpec{
  109. Template: templateNoLabel,
  110. },
  111. },
  112. },
  113. { // Custom unique label key.
  114. original: &DaemonSet{
  115. Spec: DaemonSetSpec{},
  116. },
  117. expected: &DaemonSet{
  118. Spec: DaemonSetSpec{
  119. Template: templateNoLabel,
  120. },
  121. },
  122. },
  123. }
  124. for i, test := range tests {
  125. original := test.original
  126. expected := test.expected
  127. obj2 := roundTrip(t, runtime.Object(original))
  128. got, ok := obj2.(*DaemonSet)
  129. if !ok {
  130. t.Errorf("(%d) unexpected object: %v", i, got)
  131. t.FailNow()
  132. }
  133. if !reflect.DeepEqual(got.Spec, expected.Spec) {
  134. t.Errorf("(%d) got different than expected\ngot:\n\t%+v\nexpected:\n\t%+v", i, got.Spec, expected.Spec)
  135. }
  136. }
  137. }
  138. func TestSetDefaultDeployment(t *testing.T) {
  139. defaultIntOrString := intstr.FromInt(1)
  140. differentIntOrString := intstr.FromInt(5)
  141. period := int64(v1.DefaultTerminationGracePeriodSeconds)
  142. defaultTemplate := v1.PodTemplateSpec{
  143. Spec: v1.PodSpec{
  144. DNSPolicy: v1.DNSClusterFirst,
  145. RestartPolicy: v1.RestartPolicyAlways,
  146. SecurityContext: &v1.PodSecurityContext{},
  147. TerminationGracePeriodSeconds: &period,
  148. },
  149. }
  150. tests := []struct {
  151. original *Deployment
  152. expected *Deployment
  153. }{
  154. {
  155. original: &Deployment{},
  156. expected: &Deployment{
  157. Spec: DeploymentSpec{
  158. Replicas: newInt32(1),
  159. Strategy: DeploymentStrategy{
  160. Type: RollingUpdateDeploymentStrategyType,
  161. RollingUpdate: &RollingUpdateDeployment{
  162. MaxSurge: &defaultIntOrString,
  163. MaxUnavailable: &defaultIntOrString,
  164. },
  165. },
  166. Template: defaultTemplate,
  167. },
  168. },
  169. },
  170. {
  171. original: &Deployment{
  172. Spec: DeploymentSpec{
  173. Replicas: newInt32(5),
  174. Strategy: DeploymentStrategy{
  175. RollingUpdate: &RollingUpdateDeployment{
  176. MaxSurge: &differentIntOrString,
  177. },
  178. },
  179. },
  180. },
  181. expected: &Deployment{
  182. Spec: DeploymentSpec{
  183. Replicas: newInt32(5),
  184. Strategy: DeploymentStrategy{
  185. Type: RollingUpdateDeploymentStrategyType,
  186. RollingUpdate: &RollingUpdateDeployment{
  187. MaxSurge: &differentIntOrString,
  188. MaxUnavailable: &defaultIntOrString,
  189. },
  190. },
  191. Template: defaultTemplate,
  192. },
  193. },
  194. },
  195. {
  196. original: &Deployment{
  197. Spec: DeploymentSpec{
  198. Replicas: newInt32(3),
  199. Strategy: DeploymentStrategy{
  200. Type: RollingUpdateDeploymentStrategyType,
  201. RollingUpdate: nil,
  202. },
  203. },
  204. },
  205. expected: &Deployment{
  206. Spec: DeploymentSpec{
  207. Replicas: newInt32(3),
  208. Strategy: DeploymentStrategy{
  209. Type: RollingUpdateDeploymentStrategyType,
  210. RollingUpdate: &RollingUpdateDeployment{
  211. MaxSurge: &defaultIntOrString,
  212. MaxUnavailable: &defaultIntOrString,
  213. },
  214. },
  215. Template: defaultTemplate,
  216. },
  217. },
  218. },
  219. {
  220. original: &Deployment{
  221. Spec: DeploymentSpec{
  222. Replicas: newInt32(5),
  223. Strategy: DeploymentStrategy{
  224. Type: RecreateDeploymentStrategyType,
  225. },
  226. },
  227. },
  228. expected: &Deployment{
  229. Spec: DeploymentSpec{
  230. Replicas: newInt32(5),
  231. Strategy: DeploymentStrategy{
  232. Type: RecreateDeploymentStrategyType,
  233. },
  234. Template: defaultTemplate,
  235. },
  236. },
  237. },
  238. {
  239. original: &Deployment{
  240. Spec: DeploymentSpec{
  241. Replicas: newInt32(5),
  242. Strategy: DeploymentStrategy{
  243. Type: RecreateDeploymentStrategyType,
  244. },
  245. },
  246. },
  247. expected: &Deployment{
  248. Spec: DeploymentSpec{
  249. Replicas: newInt32(5),
  250. Strategy: DeploymentStrategy{
  251. Type: RecreateDeploymentStrategyType,
  252. },
  253. Template: defaultTemplate,
  254. },
  255. },
  256. },
  257. }
  258. for _, test := range tests {
  259. original := test.original
  260. expected := test.expected
  261. obj2 := roundTrip(t, runtime.Object(original))
  262. got, ok := obj2.(*Deployment)
  263. if !ok {
  264. t.Errorf("unexpected object: %v", got)
  265. t.FailNow()
  266. }
  267. if !reflect.DeepEqual(got.Spec, expected.Spec) {
  268. t.Errorf("got different than expected:\n\t%+v\ngot:\n\t%+v", got.Spec, expected.Spec)
  269. }
  270. }
  271. }
  272. func TestSetDefaultJobParallelismAndCompletions(t *testing.T) {
  273. tests := []struct {
  274. original *Job
  275. expected *Job
  276. }{
  277. // both unspecified -> sets both to 1
  278. {
  279. original: &Job{
  280. Spec: JobSpec{},
  281. },
  282. expected: &Job{
  283. Spec: JobSpec{
  284. Completions: newInt32(1),
  285. Parallelism: newInt32(1),
  286. },
  287. },
  288. },
  289. // WQ: Parallelism explicitly 0 and completions unset -> no change
  290. {
  291. original: &Job{
  292. Spec: JobSpec{
  293. Parallelism: newInt32(0),
  294. },
  295. },
  296. expected: &Job{
  297. Spec: JobSpec{
  298. Parallelism: newInt32(0),
  299. },
  300. },
  301. },
  302. // WQ: Parallelism explicitly 2 and completions unset -> no change
  303. {
  304. original: &Job{
  305. Spec: JobSpec{
  306. Parallelism: newInt32(2),
  307. },
  308. },
  309. expected: &Job{
  310. Spec: JobSpec{
  311. Parallelism: newInt32(2),
  312. },
  313. },
  314. },
  315. // Completions explicitly 2 and parallelism unset -> parallelism is defaulted
  316. {
  317. original: &Job{
  318. Spec: JobSpec{
  319. Completions: newInt32(2),
  320. },
  321. },
  322. expected: &Job{
  323. Spec: JobSpec{
  324. Completions: newInt32(2),
  325. Parallelism: newInt32(1),
  326. },
  327. },
  328. },
  329. // Both set -> no change
  330. {
  331. original: &Job{
  332. Spec: JobSpec{
  333. Completions: newInt32(10),
  334. Parallelism: newInt32(11),
  335. },
  336. },
  337. expected: &Job{
  338. Spec: JobSpec{
  339. Completions: newInt32(10),
  340. Parallelism: newInt32(11),
  341. },
  342. },
  343. },
  344. // Both set, flipped -> no change
  345. {
  346. original: &Job{
  347. Spec: JobSpec{
  348. Completions: newInt32(11),
  349. Parallelism: newInt32(10),
  350. },
  351. },
  352. expected: &Job{
  353. Spec: JobSpec{
  354. Completions: newInt32(11),
  355. Parallelism: newInt32(10),
  356. },
  357. },
  358. },
  359. }
  360. for _, tc := range tests {
  361. original := tc.original
  362. expected := tc.expected
  363. obj2 := roundTrip(t, runtime.Object(original))
  364. got, ok := obj2.(*Job)
  365. if !ok {
  366. t.Errorf("unexpected object: %v", got)
  367. t.FailNow()
  368. }
  369. if (got.Spec.Completions == nil) != (expected.Spec.Completions == nil) {
  370. t.Errorf("got different *completions than expected: %v %v", got.Spec.Completions, expected.Spec.Completions)
  371. }
  372. if got.Spec.Completions != nil && expected.Spec.Completions != nil {
  373. if *got.Spec.Completions != *expected.Spec.Completions {
  374. t.Errorf("got different completions than expected: %d %d", *got.Spec.Completions, *expected.Spec.Completions)
  375. }
  376. }
  377. if (got.Spec.Parallelism == nil) != (expected.Spec.Parallelism == nil) {
  378. t.Errorf("got different *Parallelism than expected: %v %v", got.Spec.Parallelism, expected.Spec.Parallelism)
  379. }
  380. if got.Spec.Parallelism != nil && expected.Spec.Parallelism != nil {
  381. if *got.Spec.Parallelism != *expected.Spec.Parallelism {
  382. t.Errorf("got different parallelism than expected: %d %d", *got.Spec.Parallelism, *expected.Spec.Parallelism)
  383. }
  384. }
  385. }
  386. }
  387. func TestSetDefaultJobSelector(t *testing.T) {
  388. tests := []struct {
  389. original *Job
  390. expectedSelector *LabelSelector
  391. }{
  392. // selector set explicitly, nil autoSelector
  393. {
  394. original: &Job{
  395. Spec: JobSpec{
  396. Selector: &LabelSelector{
  397. MatchLabels: map[string]string{"job": "selector"},
  398. },
  399. },
  400. },
  401. expectedSelector: &LabelSelector{
  402. MatchLabels: map[string]string{"job": "selector"},
  403. },
  404. },
  405. // selector set explicitly, autoSelector=true
  406. {
  407. original: &Job{
  408. Spec: JobSpec{
  409. Selector: &LabelSelector{
  410. MatchLabels: map[string]string{"job": "selector"},
  411. },
  412. AutoSelector: newBool(true),
  413. },
  414. },
  415. expectedSelector: &LabelSelector{
  416. MatchLabels: map[string]string{"job": "selector"},
  417. },
  418. },
  419. // selector set explicitly, autoSelector=false
  420. {
  421. original: &Job{
  422. Spec: JobSpec{
  423. Selector: &LabelSelector{
  424. MatchLabels: map[string]string{"job": "selector"},
  425. },
  426. AutoSelector: newBool(false),
  427. },
  428. },
  429. expectedSelector: &LabelSelector{
  430. MatchLabels: map[string]string{"job": "selector"},
  431. },
  432. },
  433. // selector from template labels
  434. {
  435. original: &Job{
  436. Spec: JobSpec{
  437. Template: v1.PodTemplateSpec{
  438. ObjectMeta: v1.ObjectMeta{
  439. Labels: map[string]string{"job": "selector"},
  440. },
  441. },
  442. },
  443. },
  444. expectedSelector: &LabelSelector{
  445. MatchLabels: map[string]string{"job": "selector"},
  446. },
  447. },
  448. // selector from template labels, autoSelector=false
  449. {
  450. original: &Job{
  451. Spec: JobSpec{
  452. Template: v1.PodTemplateSpec{
  453. ObjectMeta: v1.ObjectMeta{
  454. Labels: map[string]string{"job": "selector"},
  455. },
  456. },
  457. AutoSelector: newBool(false),
  458. },
  459. },
  460. expectedSelector: &LabelSelector{
  461. MatchLabels: map[string]string{"job": "selector"},
  462. },
  463. },
  464. // selector not copied from template labels, autoSelector=true
  465. {
  466. original: &Job{
  467. Spec: JobSpec{
  468. Template: v1.PodTemplateSpec{
  469. ObjectMeta: v1.ObjectMeta{
  470. Labels: map[string]string{"job": "selector"},
  471. },
  472. },
  473. AutoSelector: newBool(true),
  474. },
  475. },
  476. expectedSelector: nil,
  477. },
  478. }
  479. for i, testcase := range tests {
  480. obj2 := roundTrip(t, runtime.Object(testcase.original))
  481. got, ok := obj2.(*Job)
  482. if !ok {
  483. t.Errorf("%d: unexpected object: %v", i, got)
  484. t.FailNow()
  485. }
  486. if !reflect.DeepEqual(got.Spec.Selector, testcase.expectedSelector) {
  487. t.Errorf("%d: got different selectors %#v %#v", i, got.Spec.Selector, testcase.expectedSelector)
  488. }
  489. }
  490. }
  491. func TestSetDefaultReplicaSet(t *testing.T) {
  492. tests := []struct {
  493. rs *ReplicaSet
  494. expectLabels bool
  495. expectSelector bool
  496. }{
  497. {
  498. rs: &ReplicaSet{
  499. Spec: ReplicaSetSpec{
  500. Template: v1.PodTemplateSpec{
  501. ObjectMeta: v1.ObjectMeta{
  502. Labels: map[string]string{
  503. "foo": "bar",
  504. },
  505. },
  506. },
  507. },
  508. },
  509. expectLabels: true,
  510. expectSelector: true,
  511. },
  512. {
  513. rs: &ReplicaSet{
  514. ObjectMeta: v1.ObjectMeta{
  515. Labels: map[string]string{
  516. "bar": "foo",
  517. },
  518. },
  519. Spec: ReplicaSetSpec{
  520. Template: v1.PodTemplateSpec{
  521. ObjectMeta: v1.ObjectMeta{
  522. Labels: map[string]string{
  523. "foo": "bar",
  524. },
  525. },
  526. },
  527. },
  528. },
  529. expectLabels: false,
  530. expectSelector: true,
  531. },
  532. {
  533. rs: &ReplicaSet{
  534. ObjectMeta: v1.ObjectMeta{
  535. Labels: map[string]string{
  536. "bar": "foo",
  537. },
  538. },
  539. Spec: ReplicaSetSpec{
  540. Selector: &LabelSelector{
  541. MatchLabels: map[string]string{
  542. "some": "other",
  543. },
  544. },
  545. Template: v1.PodTemplateSpec{
  546. ObjectMeta: v1.ObjectMeta{
  547. Labels: map[string]string{
  548. "foo": "bar",
  549. },
  550. },
  551. },
  552. },
  553. },
  554. expectLabels: false,
  555. expectSelector: false,
  556. },
  557. {
  558. rs: &ReplicaSet{
  559. Spec: ReplicaSetSpec{
  560. Selector: &LabelSelector{
  561. MatchLabels: map[string]string{
  562. "some": "other",
  563. },
  564. },
  565. Template: v1.PodTemplateSpec{
  566. ObjectMeta: v1.ObjectMeta{
  567. Labels: map[string]string{
  568. "foo": "bar",
  569. },
  570. },
  571. },
  572. },
  573. },
  574. expectLabels: true,
  575. expectSelector: false,
  576. },
  577. }
  578. for _, test := range tests {
  579. rs := test.rs
  580. obj2 := roundTrip(t, runtime.Object(rs))
  581. rs2, ok := obj2.(*ReplicaSet)
  582. if !ok {
  583. t.Errorf("unexpected object: %v", rs2)
  584. t.FailNow()
  585. }
  586. if test.expectSelector != reflect.DeepEqual(rs2.Spec.Selector.MatchLabels, rs2.Spec.Template.Labels) {
  587. if test.expectSelector {
  588. t.Errorf("expected: %v, got: %v", rs2.Spec.Template.Labels, rs2.Spec.Selector)
  589. } else {
  590. t.Errorf("unexpected equality: %v", rs.Spec.Selector)
  591. }
  592. }
  593. if test.expectLabels != reflect.DeepEqual(rs2.Labels, rs2.Spec.Template.Labels) {
  594. if test.expectLabels {
  595. t.Errorf("expected: %v, got: %v", rs2.Spec.Template.Labels, rs2.Labels)
  596. } else {
  597. t.Errorf("unexpected equality: %v", rs.Labels)
  598. }
  599. }
  600. }
  601. }
  602. func TestSetDefaultReplicaSetReplicas(t *testing.T) {
  603. tests := []struct {
  604. rs ReplicaSet
  605. expectReplicas int32
  606. }{
  607. {
  608. rs: ReplicaSet{
  609. Spec: ReplicaSetSpec{
  610. Template: v1.PodTemplateSpec{
  611. ObjectMeta: v1.ObjectMeta{
  612. Labels: map[string]string{
  613. "foo": "bar",
  614. },
  615. },
  616. },
  617. },
  618. },
  619. expectReplicas: 1,
  620. },
  621. {
  622. rs: ReplicaSet{
  623. Spec: ReplicaSetSpec{
  624. Replicas: newInt32(0),
  625. Template: v1.PodTemplateSpec{
  626. ObjectMeta: v1.ObjectMeta{
  627. Labels: map[string]string{
  628. "foo": "bar",
  629. },
  630. },
  631. },
  632. },
  633. },
  634. expectReplicas: 0,
  635. },
  636. {
  637. rs: ReplicaSet{
  638. Spec: ReplicaSetSpec{
  639. Replicas: newInt32(3),
  640. Template: v1.PodTemplateSpec{
  641. ObjectMeta: v1.ObjectMeta{
  642. Labels: map[string]string{
  643. "foo": "bar",
  644. },
  645. },
  646. },
  647. },
  648. },
  649. expectReplicas: 3,
  650. },
  651. }
  652. for _, test := range tests {
  653. rs := &test.rs
  654. obj2 := roundTrip(t, runtime.Object(rs))
  655. rs2, ok := obj2.(*ReplicaSet)
  656. if !ok {
  657. t.Errorf("unexpected object: %v", rs2)
  658. t.FailNow()
  659. }
  660. if rs2.Spec.Replicas == nil {
  661. t.Errorf("unexpected nil Replicas")
  662. } else if test.expectReplicas != *rs2.Spec.Replicas {
  663. t.Errorf("expected: %d replicas, got: %d", test.expectReplicas, *rs2.Spec.Replicas)
  664. }
  665. }
  666. }
  667. func TestDefaultRequestIsNotSetForReplicaSet(t *testing.T) {
  668. s := v1.PodSpec{}
  669. s.Containers = []v1.Container{
  670. {
  671. Resources: v1.ResourceRequirements{
  672. Limits: v1.ResourceList{
  673. v1.ResourceCPU: resource.MustParse("100m"),
  674. },
  675. },
  676. },
  677. }
  678. rs := &ReplicaSet{
  679. Spec: ReplicaSetSpec{
  680. Replicas: newInt32(3),
  681. Template: v1.PodTemplateSpec{
  682. ObjectMeta: v1.ObjectMeta{
  683. Labels: map[string]string{
  684. "foo": "bar",
  685. },
  686. },
  687. Spec: s,
  688. },
  689. },
  690. }
  691. output := roundTrip(t, runtime.Object(rs))
  692. rs2 := output.(*ReplicaSet)
  693. defaultRequest := rs2.Spec.Template.Spec.Containers[0].Resources.Requests
  694. requestValue := defaultRequest[v1.ResourceCPU]
  695. if requestValue.String() != "0" {
  696. t.Errorf("Expected 0 request value, got: %s", requestValue.String())
  697. }
  698. }
  699. func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
  700. data, err := runtime.Encode(api.Codecs.LegacyCodec(SchemeGroupVersion), obj)
  701. if err != nil {
  702. t.Errorf("%v\n %#v", err, obj)
  703. return nil
  704. }
  705. obj2, err := runtime.Decode(api.Codecs.UniversalDecoder(), data)
  706. if err != nil {
  707. t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
  708. return nil
  709. }
  710. obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
  711. err = api.Scheme.Convert(obj2, obj3, nil)
  712. if err != nil {
  713. t.Errorf("%v\nSource: %#v", err, obj2)
  714. return nil
  715. }
  716. return obj3
  717. }
  718. func newInt32(val int32) *int32 {
  719. p := new(int32)
  720. *p = val
  721. return p
  722. }
  723. func newString(val string) *string {
  724. p := new(string)
  725. *p = val
  726. return p
  727. }
  728. func newBool(val bool) *bool {
  729. b := new(bool)
  730. *b = val
  731. return b
  732. }