conversion_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright 2016 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/apis/batch"
  19. versioned "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
  20. )
  21. // TestJobSpecConversion tests that ManualSelector and AutoSelector
  22. // are handled correctly.
  23. func TestJobSpecConversion(t *testing.T) {
  24. pTrue := new(bool)
  25. *pTrue = true
  26. pFalse := new(bool)
  27. *pFalse = false
  28. // False or nil convert to true.
  29. // True converts to nil.
  30. tests := []struct {
  31. in *bool
  32. expectOut *bool
  33. }{
  34. {
  35. in: nil,
  36. expectOut: pTrue,
  37. },
  38. {
  39. in: pFalse,
  40. expectOut: pTrue,
  41. },
  42. {
  43. in: pTrue,
  44. expectOut: nil,
  45. },
  46. }
  47. // Test internal -> v1beta1.
  48. for _, test := range tests {
  49. i := &batch.JobSpec{
  50. ManualSelector: test.in,
  51. }
  52. v := versioned.JobSpec{}
  53. if err := api.Scheme.Convert(i, &v, nil); err != nil {
  54. t.Fatalf("unexpected error: %v", err)
  55. }
  56. if !reflect.DeepEqual(test.expectOut, v.AutoSelector) {
  57. t.Fatalf("want v1beta1.AutoSelector %v, got %v", test.expectOut, v.AutoSelector)
  58. }
  59. }
  60. // Test v1beta1 -> internal.
  61. for _, test := range tests {
  62. i := &versioned.JobSpec{
  63. AutoSelector: test.in,
  64. }
  65. e := batch.JobSpec{}
  66. if err := api.Scheme.Convert(i, &e, nil); err != nil {
  67. t.Fatalf("unexpected error: %v", err)
  68. }
  69. if !reflect.DeepEqual(test.expectOut, e.ManualSelector) {
  70. t.Fatalf("want extensions.ManualSelector %v, got %v", test.expectOut, e.ManualSelector)
  71. }
  72. }
  73. }