priorityclass_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 versioned
  14. import (
  15. apiv1 "k8s.io/api/core/v1"
  16. scheduling "k8s.io/api/scheduling/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestPriorityClassV1Generator(t *testing.T) {
  22. var (
  23. preemptLowerPriority = apiv1.PreemptLowerPriority
  24. preemptNever = apiv1.PreemptNever
  25. )
  26. tests := []struct {
  27. name string
  28. params map[string]interface{}
  29. expected *scheduling.PriorityClass
  30. expectErr bool
  31. }{
  32. {
  33. name: "test valid case",
  34. params: map[string]interface{}{
  35. "name": "foo",
  36. "value": int32(1000),
  37. "global-default": false,
  38. "description": "high priority class",
  39. "preemption-policy": "PreemptLowerPriority",
  40. },
  41. expected: &scheduling.PriorityClass{
  42. ObjectMeta: metav1.ObjectMeta{
  43. Name: "foo",
  44. },
  45. Value: int32(1000),
  46. GlobalDefault: false,
  47. Description: "high priority class",
  48. PreemptionPolicy: &preemptLowerPriority,
  49. },
  50. expectErr: false,
  51. },
  52. {
  53. name: "test valid case that field non-preempting is set",
  54. params: map[string]interface{}{
  55. "name": "foo",
  56. "value": int32(1000),
  57. "global-default": false,
  58. "description": "high priority class",
  59. "preemption-policy": "Never",
  60. },
  61. expected: &scheduling.PriorityClass{
  62. ObjectMeta: metav1.ObjectMeta{
  63. Name: "foo",
  64. },
  65. Value: int32(1000),
  66. GlobalDefault: false,
  67. Description: "high priority class",
  68. PreemptionPolicy: &preemptNever,
  69. },
  70. expectErr: false,
  71. },
  72. {
  73. name: "test valid case that as default priority",
  74. params: map[string]interface{}{
  75. "name": "foo",
  76. "value": int32(1000),
  77. "global-default": true,
  78. "description": "high priority class",
  79. "preemption-policy": "PreemptLowerPriority",
  80. },
  81. expected: &scheduling.PriorityClass{
  82. ObjectMeta: metav1.ObjectMeta{
  83. Name: "foo",
  84. },
  85. Value: int32(1000),
  86. GlobalDefault: true,
  87. Description: "high priority class",
  88. PreemptionPolicy: &preemptLowerPriority,
  89. },
  90. expectErr: false,
  91. },
  92. {
  93. name: "test missing required param",
  94. params: map[string]interface{}{
  95. "name": "foo",
  96. "global-default": true,
  97. "description": "high priority class",
  98. "preemption-policy": "PreemptLowerPriority",
  99. },
  100. expectErr: true,
  101. },
  102. }
  103. generator := PriorityClassV1Generator{}
  104. for _, tt := range tests {
  105. t.Run(tt.name, func(t *testing.T) {
  106. obj, err := generator.Generate(tt.params)
  107. if !tt.expectErr && err != nil {
  108. t.Errorf("%s: unexpected error: %v", tt.name, err)
  109. }
  110. if tt.expectErr && err != nil {
  111. return
  112. }
  113. if !reflect.DeepEqual(obj.(*scheduling.PriorityClass), tt.expected) {
  114. t.Errorf("%s:\nexpected:\n%#v\nsaw:\n%#v", tt.name, tt.expected, obj.(*scheduling.PriorityClass))
  115. }
  116. })
  117. }
  118. }