validation_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Copyright 2018 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. "time"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. componentbaseconfig "k8s.io/component-base/config"
  19. "k8s.io/kubernetes/pkg/scheduler/apis/config"
  20. )
  21. func TestValidateKubeSchedulerConfiguration(t *testing.T) {
  22. testTimeout := int64(0)
  23. validConfig := &config.KubeSchedulerConfiguration{
  24. SchedulerName: "me",
  25. HealthzBindAddress: "0.0.0.0:10254",
  26. MetricsBindAddress: "0.0.0.0:10254",
  27. HardPodAffinitySymmetricWeight: 80,
  28. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  29. AcceptContentTypes: "application/json",
  30. ContentType: "application/json",
  31. QPS: 10,
  32. Burst: 10,
  33. },
  34. AlgorithmSource: config.SchedulerAlgorithmSource{
  35. Policy: &config.SchedulerPolicySource{
  36. ConfigMap: &config.SchedulerPolicyConfigMapSource{
  37. Namespace: "name",
  38. Name: "name",
  39. },
  40. },
  41. },
  42. LeaderElection: config.KubeSchedulerLeaderElectionConfiguration{
  43. LockObjectNamespace: "name",
  44. LockObjectName: "name",
  45. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  46. ResourceLock: "configmap",
  47. LeaderElect: true,
  48. LeaseDuration: metav1.Duration{Duration: 30 * time.Second},
  49. RenewDeadline: metav1.Duration{Duration: 15 * time.Second},
  50. RetryPeriod: metav1.Duration{Duration: 5 * time.Second},
  51. },
  52. },
  53. BindTimeoutSeconds: &testTimeout,
  54. PercentageOfNodesToScore: 35,
  55. }
  56. HardPodAffinitySymmetricWeightGt100 := validConfig.DeepCopy()
  57. HardPodAffinitySymmetricWeightGt100.HardPodAffinitySymmetricWeight = 120
  58. HardPodAffinitySymmetricWeightLt0 := validConfig.DeepCopy()
  59. HardPodAffinitySymmetricWeightLt0.HardPodAffinitySymmetricWeight = -1
  60. lockObjectNameNotSet := validConfig.DeepCopy()
  61. lockObjectNameNotSet.LeaderElection.LockObjectName = ""
  62. lockObjectNamespaceNotSet := validConfig.DeepCopy()
  63. lockObjectNamespaceNotSet.LeaderElection.LockObjectNamespace = ""
  64. metricsBindAddrHostInvalid := validConfig.DeepCopy()
  65. metricsBindAddrHostInvalid.MetricsBindAddress = "0.0.0.0.0:9090"
  66. metricsBindAddrPortInvalid := validConfig.DeepCopy()
  67. metricsBindAddrPortInvalid.MetricsBindAddress = "0.0.0.0:909090"
  68. healthzBindAddrHostInvalid := validConfig.DeepCopy()
  69. healthzBindAddrHostInvalid.HealthzBindAddress = "0.0.0.0.0:9090"
  70. healthzBindAddrPortInvalid := validConfig.DeepCopy()
  71. healthzBindAddrPortInvalid.HealthzBindAddress = "0.0.0.0:909090"
  72. enableContentProfilingSetWithoutEnableProfiling := validConfig.DeepCopy()
  73. enableContentProfilingSetWithoutEnableProfiling.EnableProfiling = false
  74. enableContentProfilingSetWithoutEnableProfiling.EnableContentionProfiling = true
  75. bindTimeoutUnset := validConfig.DeepCopy()
  76. bindTimeoutUnset.BindTimeoutSeconds = nil
  77. percentageOfNodesToScore101 := validConfig.DeepCopy()
  78. percentageOfNodesToScore101.PercentageOfNodesToScore = int32(101)
  79. scenarios := map[string]struct {
  80. expectedToFail bool
  81. config *config.KubeSchedulerConfiguration
  82. }{
  83. "good": {
  84. expectedToFail: false,
  85. config: validConfig,
  86. },
  87. "bad-lock-object-names-not-set": {
  88. expectedToFail: true,
  89. config: lockObjectNameNotSet,
  90. },
  91. "bad-lock-object-namespace-not-set": {
  92. expectedToFail: true,
  93. config: lockObjectNamespaceNotSet,
  94. },
  95. "bad-healthz-port-invalid": {
  96. expectedToFail: true,
  97. config: healthzBindAddrPortInvalid,
  98. },
  99. "bad-healthz-host-invalid": {
  100. expectedToFail: true,
  101. config: healthzBindAddrHostInvalid,
  102. },
  103. "bad-metrics-port-invalid": {
  104. expectedToFail: true,
  105. config: metricsBindAddrPortInvalid,
  106. },
  107. "bad-metrics-host-invalid": {
  108. expectedToFail: true,
  109. config: metricsBindAddrHostInvalid,
  110. },
  111. "bad-hard-pod-affinity-symmetric-weight-lt-0": {
  112. expectedToFail: true,
  113. config: HardPodAffinitySymmetricWeightGt100,
  114. },
  115. "bad-hard-pod-affinity-symmetric-weight-gt-100": {
  116. expectedToFail: true,
  117. config: HardPodAffinitySymmetricWeightLt0,
  118. },
  119. "bind-timeout-unset": {
  120. expectedToFail: true,
  121. config: bindTimeoutUnset,
  122. },
  123. "bad-percentage-of-nodes-to-score": {
  124. expectedToFail: true,
  125. config: percentageOfNodesToScore101,
  126. },
  127. }
  128. for name, scenario := range scenarios {
  129. errs := ValidateKubeSchedulerConfiguration(scenario.config)
  130. if len(errs) == 0 && scenario.expectedToFail {
  131. t.Errorf("Unexpected success for scenario: %s", name)
  132. }
  133. if len(errs) > 0 && !scenario.expectedToFail {
  134. t.Errorf("Unexpected failure for scenario: %s - %+v", name, errs)
  135. }
  136. }
  137. }