defaults_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright 2019 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 v1_test
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/scheduling/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/kubernetes/pkg/api/legacyscheme"
  20. apiv1 "k8s.io/api/core/v1"
  21. utilfeature "k8s.io/apiserver/pkg/util/feature"
  22. featuregatetesting "k8s.io/component-base/featuregate/testing"
  23. // enforce that all types are installed
  24. _ "k8s.io/kubernetes/pkg/api/testapi"
  25. "k8s.io/kubernetes/pkg/features"
  26. )
  27. func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
  28. codec := legacyscheme.Codecs.LegacyCodec(v1.SchemeGroupVersion)
  29. data, err := runtime.Encode(codec, obj)
  30. if err != nil {
  31. t.Errorf("%v\n %#v", err, obj)
  32. return nil
  33. }
  34. obj2, err := runtime.Decode(codec, data)
  35. if err != nil {
  36. t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
  37. return nil
  38. }
  39. obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
  40. err = legacyscheme.Scheme.Convert(obj2, obj3, nil)
  41. if err != nil {
  42. t.Errorf("%v\nSource: %#v", err, obj2)
  43. return nil
  44. }
  45. return obj3
  46. }
  47. func TestSetDefaultPreemptionPolicy(t *testing.T) {
  48. priorityClass := &v1.PriorityClass{}
  49. // set NonPreemptingPriority true
  50. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.NonPreemptingPriority, true)()
  51. output := roundTrip(t, runtime.Object(priorityClass)).(*v1.PriorityClass)
  52. if output.PreemptionPolicy == nil || *output.PreemptionPolicy != apiv1.PreemptLowerPriority {
  53. t.Errorf("Expected PriorityClass.PreemptionPolicy value: %+v\ngot: %+v\n", apiv1.PreemptLowerPriority, output.PreemptionPolicy)
  54. }
  55. }