plugins_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 factory
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. "k8s.io/kubernetes/pkg/scheduler/algorithm/priorities"
  18. "k8s.io/kubernetes/pkg/scheduler/api"
  19. )
  20. func TestAlgorithmNameValidation(t *testing.T) {
  21. algorithmNamesShouldValidate := []string{
  22. "1SomeAlgo1rithm",
  23. "someAlgor-ithm1",
  24. }
  25. algorithmNamesShouldNotValidate := []string{
  26. "-SomeAlgorithm",
  27. "SomeAlgorithm-",
  28. "Some,Alg:orithm",
  29. }
  30. for _, name := range algorithmNamesShouldValidate {
  31. t.Run(name, func(t *testing.T) {
  32. if !validName.MatchString(name) {
  33. t.Errorf("should be a valid algorithm name but is not valid.")
  34. }
  35. })
  36. }
  37. for _, name := range algorithmNamesShouldNotValidate {
  38. t.Run(name, func(t *testing.T) {
  39. if validName.MatchString(name) {
  40. t.Errorf("should be an invalid algorithm name but is valid.")
  41. }
  42. })
  43. }
  44. }
  45. func TestValidatePriorityConfigOverFlow(t *testing.T) {
  46. tests := []struct {
  47. description string
  48. configs []priorities.PriorityConfig
  49. expected bool
  50. }{
  51. {
  52. description: "one of the weights is MaxInt",
  53. configs: []priorities.PriorityConfig{{Weight: api.MaxInt}, {Weight: 5}},
  54. expected: true,
  55. },
  56. {
  57. description: "after multiplication with MaxPriority the weight is larger than MaxWeight",
  58. configs: []priorities.PriorityConfig{{Weight: api.MaxInt/api.MaxPriority + api.MaxPriority}, {Weight: 5}},
  59. expected: true,
  60. },
  61. {
  62. description: "normal weights",
  63. configs: []priorities.PriorityConfig{{Weight: 10000}, {Weight: 5}},
  64. expected: false,
  65. },
  66. }
  67. for _, test := range tests {
  68. t.Run(test.description, func(t *testing.T) {
  69. err := validateSelectedConfigs(test.configs)
  70. if test.expected {
  71. if err == nil {
  72. t.Errorf("Expected Overflow")
  73. }
  74. } else {
  75. if err != nil {
  76. t.Errorf("Did not expect an overflow")
  77. }
  78. }
  79. })
  80. }
  81. }
  82. func TestBuildScoringFunctionShapeFromRequestedToCapacityRatioArguments(t *testing.T) {
  83. arguments := api.RequestedToCapacityRatioArguments{
  84. UtilizationShape: []api.UtilizationShapePoint{
  85. {Utilization: 10, Score: 1},
  86. {Utilization: 30, Score: 5},
  87. {Utilization: 70, Score: 2},
  88. }}
  89. builtShape := buildScoringFunctionShapeFromRequestedToCapacityRatioArguments(&arguments)
  90. expectedShape, _ := priorities.NewFunctionShape([]priorities.FunctionShapePoint{
  91. {Utilization: 10, Score: 1},
  92. {Utilization: 30, Score: 5},
  93. {Utilization: 70, Score: 2},
  94. })
  95. assert.Equal(t, expectedShape, builtShape)
  96. }