validation_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 validation
  14. import (
  15. "testing"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. "k8s.io/kubernetes/pkg/apis/scheduling"
  19. )
  20. func TestValidatePriorityClass(t *testing.T) {
  21. spcs := scheduling.SystemPriorityClasses()
  22. successCases := map[string]scheduling.PriorityClass{
  23. "no description": {
  24. ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: ""},
  25. Value: 100,
  26. },
  27. "with description": {
  28. ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: ""},
  29. Value: 200,
  30. GlobalDefault: false,
  31. Description: "Used for the highest priority pods.",
  32. },
  33. "system node critical": {
  34. ObjectMeta: metav1.ObjectMeta{Name: spcs[0].Name, Namespace: ""},
  35. Value: spcs[0].Value,
  36. GlobalDefault: spcs[0].GlobalDefault,
  37. Description: "system priority class 0",
  38. },
  39. }
  40. for k, v := range successCases {
  41. if errs := ValidatePriorityClass(&v); len(errs) != 0 {
  42. t.Errorf("Expected success for %s, got %v", k, errs)
  43. }
  44. }
  45. errorCases := map[string]scheduling.PriorityClass{
  46. "with namespace": {
  47. ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "foo"},
  48. Value: 100,
  49. },
  50. "invalid name": {
  51. ObjectMeta: metav1.ObjectMeta{Name: "tier&1", Namespace: ""},
  52. Value: 100,
  53. },
  54. "incorrect system class name": {
  55. ObjectMeta: metav1.ObjectMeta{Name: spcs[0].Name, Namespace: ""},
  56. Value: 0,
  57. GlobalDefault: spcs[0].GlobalDefault,
  58. },
  59. "incorrect system class value": {
  60. ObjectMeta: metav1.ObjectMeta{Name: "system-something", Namespace: ""},
  61. Value: spcs[0].Value,
  62. GlobalDefault: spcs[0].GlobalDefault,
  63. },
  64. }
  65. for k, v := range errorCases {
  66. if errs := ValidatePriorityClass(&v); len(errs) == 0 {
  67. t.Errorf("Expected error for %s, but it succeeded", k)
  68. }
  69. }
  70. }
  71. func TestValidatePriorityClassUpdate(t *testing.T) {
  72. old := scheduling.PriorityClass{
  73. ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "1"},
  74. Value: 100,
  75. }
  76. successCases := map[string]scheduling.PriorityClass{
  77. "no change": {
  78. ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
  79. Value: 100,
  80. Description: "Used for the highest priority pods.",
  81. },
  82. "change description": {
  83. ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
  84. Value: 100,
  85. Description: "A different description.",
  86. },
  87. "remove description": {
  88. ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
  89. Value: 100,
  90. },
  91. "change globalDefault": {
  92. ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
  93. Value: 100,
  94. GlobalDefault: true,
  95. },
  96. }
  97. for k, v := range successCases {
  98. if errs := ValidatePriorityClassUpdate(&v, &old); len(errs) != 0 {
  99. t.Errorf("Expected success for %s, got %v", k, errs)
  100. }
  101. }
  102. errorCases := map[string]struct {
  103. P scheduling.PriorityClass
  104. T field.ErrorType
  105. }{
  106. "add namespace": {
  107. P: scheduling.PriorityClass{
  108. ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "foo", ResourceVersion: "2"},
  109. Value: 100,
  110. },
  111. T: field.ErrorTypeInvalid,
  112. },
  113. "change name": {
  114. P: scheduling.PriorityClass{
  115. ObjectMeta: metav1.ObjectMeta{Name: "tier2", Namespace: "", ResourceVersion: "2"},
  116. Value: 100,
  117. },
  118. T: field.ErrorTypeInvalid,
  119. },
  120. "remove value": {
  121. P: scheduling.PriorityClass{
  122. ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
  123. },
  124. T: field.ErrorTypeForbidden,
  125. },
  126. "change value": {
  127. P: scheduling.PriorityClass{
  128. ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: "", ResourceVersion: "2"},
  129. Value: 101,
  130. },
  131. T: field.ErrorTypeForbidden,
  132. },
  133. }
  134. for k, v := range errorCases {
  135. errs := ValidatePriorityClassUpdate(&v.P, &old)
  136. if len(errs) == 0 {
  137. t.Errorf("Expected error for %s, but it succeeded", k)
  138. continue
  139. }
  140. for i := range errs {
  141. if errs[i].Type != v.T {
  142. t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
  143. }
  144. }
  145. }
  146. }