validation_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Copyright 2015 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. "errors"
  16. "fmt"
  17. "testing"
  18. "k8s.io/kubernetes/pkg/scheduler/api"
  19. )
  20. func TestValidatePolicy(t *testing.T) {
  21. tests := []struct {
  22. policy api.Policy
  23. expected error
  24. name string
  25. }{
  26. {
  27. name: "no weight defined in policy",
  28. policy: api.Policy{Priorities: []api.PriorityPolicy{{Name: "NoWeightPriority"}}},
  29. expected: errors.New("Priority NoWeightPriority should have a positive weight applied to it or it has overflown"),
  30. },
  31. {
  32. name: "policy weight is not positive",
  33. policy: api.Policy{Priorities: []api.PriorityPolicy{{Name: "NoWeightPriority", Weight: 0}}},
  34. expected: errors.New("Priority NoWeightPriority should have a positive weight applied to it or it has overflown"),
  35. },
  36. {
  37. name: "valid weight priority",
  38. policy: api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: 2}}},
  39. expected: nil,
  40. },
  41. {
  42. name: "invalid negative weight policy",
  43. policy: api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: -2}}},
  44. expected: errors.New("Priority WeightPriority should have a positive weight applied to it or it has overflown"),
  45. },
  46. {
  47. name: "policy weight exceeds maximum",
  48. policy: api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: api.MaxWeight}}},
  49. expected: errors.New("Priority WeightPriority should have a positive weight applied to it or it has overflown"),
  50. },
  51. {
  52. name: "valid weight in policy extender config",
  53. policy: api.Policy{ExtenderConfigs: []api.ExtenderConfig{{URLPrefix: "http://127.0.0.1:8081/extender", PrioritizeVerb: "prioritize", Weight: 2}}},
  54. expected: nil,
  55. },
  56. {
  57. name: "invalid negative weight in policy extender config",
  58. policy: api.Policy{ExtenderConfigs: []api.ExtenderConfig{{URLPrefix: "http://127.0.0.1:8081/extender", PrioritizeVerb: "prioritize", Weight: -2}}},
  59. expected: errors.New("Priority for extender http://127.0.0.1:8081/extender should have a positive weight applied to it"),
  60. },
  61. {
  62. name: "valid filter verb and url prefix",
  63. policy: api.Policy{ExtenderConfigs: []api.ExtenderConfig{{URLPrefix: "http://127.0.0.1:8081/extender", FilterVerb: "filter"}}},
  64. expected: nil,
  65. },
  66. {
  67. name: "valid preemt verb and urlprefix",
  68. policy: api.Policy{ExtenderConfigs: []api.ExtenderConfig{{URLPrefix: "http://127.0.0.1:8081/extender", PreemptVerb: "preempt"}}},
  69. expected: nil,
  70. },
  71. {
  72. name: "invalid multiple extenders",
  73. policy: api.Policy{
  74. ExtenderConfigs: []api.ExtenderConfig{
  75. {URLPrefix: "http://127.0.0.1:8081/extender", BindVerb: "bind"},
  76. {URLPrefix: "http://127.0.0.1:8082/extender", BindVerb: "bind"},
  77. }},
  78. expected: errors.New("Only one extender can implement bind, found 2"),
  79. },
  80. {
  81. name: "invalid duplicate extender resource name",
  82. policy: api.Policy{
  83. ExtenderConfigs: []api.ExtenderConfig{
  84. {URLPrefix: "http://127.0.0.1:8081/extender", ManagedResources: []api.ExtenderManagedResource{{Name: "foo.com/bar"}}},
  85. {URLPrefix: "http://127.0.0.1:8082/extender", BindVerb: "bind", ManagedResources: []api.ExtenderManagedResource{{Name: "foo.com/bar"}}},
  86. }},
  87. expected: errors.New("Duplicate extender managed resource name foo.com/bar"),
  88. },
  89. {
  90. name: "invalid extended resource name",
  91. policy: api.Policy{
  92. ExtenderConfigs: []api.ExtenderConfig{
  93. {URLPrefix: "http://127.0.0.1:8081/extender", ManagedResources: []api.ExtenderManagedResource{{Name: "kubernetes.io/foo"}}},
  94. }},
  95. expected: errors.New("kubernetes.io/foo is an invalid extended resource name"),
  96. },
  97. }
  98. for _, test := range tests {
  99. t.Run(test.name, func(t *testing.T) {
  100. actual := ValidatePolicy(test.policy)
  101. if fmt.Sprint(test.expected) != fmt.Sprint(actual) {
  102. t.Errorf("expected: %s, actual: %s", test.expected, actual)
  103. }
  104. })
  105. }
  106. }