defaults_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Copyright 2017 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. storagev1beta1 "k8s.io/api/storage/v1beta1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/kubernetes/pkg/api/legacyscheme"
  20. _ "k8s.io/kubernetes/pkg/apis/storage/install"
  21. )
  22. func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
  23. codec := legacyscheme.Codecs.LegacyCodec(storagev1beta1.SchemeGroupVersion)
  24. data, err := runtime.Encode(codec, obj)
  25. if err != nil {
  26. t.Errorf("%v\n %#v", err, obj)
  27. return nil
  28. }
  29. obj2, err := runtime.Decode(codec, data)
  30. if err != nil {
  31. t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
  32. return nil
  33. }
  34. obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
  35. err = legacyscheme.Scheme.Convert(obj2, obj3, nil)
  36. if err != nil {
  37. t.Errorf("%v\nSource: %#v", err, obj2)
  38. return nil
  39. }
  40. return obj3
  41. }
  42. func TestSetDefaultVolumeBindingMode(t *testing.T) {
  43. class := &storagev1beta1.StorageClass{}
  44. // field should be defaulted
  45. defaultMode := storagev1beta1.VolumeBindingImmediate
  46. output := roundTrip(t, runtime.Object(class)).(*storagev1beta1.StorageClass)
  47. outMode := output.VolumeBindingMode
  48. if outMode == nil {
  49. t.Errorf("Expected VolumeBindingMode to be defaulted to: %+v, got: nil", defaultMode)
  50. } else if *outMode != defaultMode {
  51. t.Errorf("Expected VolumeBindingMode to be defaulted to: %+v, got: %+v", defaultMode, outMode)
  52. }
  53. }
  54. func TestSetDefaultAttachRequired(t *testing.T) {
  55. driver := &storagev1beta1.CSIDriver{}
  56. // field should be defaulted
  57. defaultAttach := true
  58. defaultPodInfo := false
  59. output := roundTrip(t, runtime.Object(driver)).(*storagev1beta1.CSIDriver)
  60. outAttach := output.Spec.AttachRequired
  61. if outAttach == nil {
  62. t.Errorf("Expected AttachRequired to be defaulted to: %+v, got: nil", defaultAttach)
  63. } else if *outAttach != defaultAttach {
  64. t.Errorf("Expected AttachRequired to be defaulted to: %+v, got: %+v", defaultAttach, outAttach)
  65. }
  66. outPodInfo := output.Spec.PodInfoOnMount
  67. if outPodInfo == nil {
  68. t.Errorf("Expected PodInfoOnMount to be defaulted to: %+v, got: nil", defaultPodInfo)
  69. } else if *outPodInfo != defaultPodInfo {
  70. t.Errorf("Expected PodInfoOnMount to be defaulted to: %+v, got: %+v", defaultPodInfo, outPodInfo)
  71. }
  72. }