metadata_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 priorities
  14. import (
  15. "reflect"
  16. "testing"
  17. apps "k8s.io/api/apps/v1"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/api/resource"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. priorityutil "k8s.io/kubernetes/pkg/scheduler/algorithm/priorities/util"
  22. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  23. schedulertesting "k8s.io/kubernetes/pkg/scheduler/testing"
  24. )
  25. func TestPriorityMetadata(t *testing.T) {
  26. nonZeroReqs := &schedulernodeinfo.Resource{}
  27. nonZeroReqs.MilliCPU = priorityutil.DefaultMilliCPURequest
  28. nonZeroReqs.Memory = priorityutil.DefaultMemoryRequest
  29. specifiedReqs := &schedulernodeinfo.Resource{}
  30. specifiedReqs.MilliCPU = 200
  31. specifiedReqs.Memory = 2000
  32. nonPodLimits := &schedulernodeinfo.Resource{}
  33. specifiedPodLimits := &schedulernodeinfo.Resource{}
  34. specifiedPodLimits.MilliCPU = 200
  35. specifiedPodLimits.Memory = 2000
  36. tolerations := []v1.Toleration{{
  37. Key: "foo",
  38. Operator: v1.TolerationOpEqual,
  39. Value: "bar",
  40. Effect: v1.TaintEffectPreferNoSchedule,
  41. }}
  42. podAffinity := &v1.Affinity{
  43. PodAffinity: &v1.PodAffinity{
  44. PreferredDuringSchedulingIgnoredDuringExecution: []v1.WeightedPodAffinityTerm{
  45. {
  46. Weight: 5,
  47. PodAffinityTerm: v1.PodAffinityTerm{
  48. LabelSelector: &metav1.LabelSelector{
  49. MatchExpressions: []metav1.LabelSelectorRequirement{
  50. {
  51. Key: "security",
  52. Operator: metav1.LabelSelectorOpIn,
  53. Values: []string{"S1"},
  54. },
  55. },
  56. },
  57. TopologyKey: "region",
  58. },
  59. },
  60. },
  61. },
  62. }
  63. podWithTolerationsAndAffinity := &v1.Pod{
  64. Spec: v1.PodSpec{
  65. Containers: []v1.Container{
  66. {
  67. Name: "container",
  68. Image: "image",
  69. ImagePullPolicy: "Always",
  70. },
  71. },
  72. Affinity: podAffinity,
  73. Tolerations: tolerations,
  74. },
  75. }
  76. podWithTolerationsAndRequests := &v1.Pod{
  77. Spec: v1.PodSpec{
  78. Containers: []v1.Container{
  79. {
  80. Name: "container",
  81. Image: "image",
  82. ImagePullPolicy: "Always",
  83. Resources: v1.ResourceRequirements{
  84. Requests: v1.ResourceList{
  85. v1.ResourceCPU: resource.MustParse("200m"),
  86. v1.ResourceMemory: resource.MustParse("2000"),
  87. },
  88. },
  89. },
  90. },
  91. Tolerations: tolerations,
  92. },
  93. }
  94. podWithAffinityAndRequests := &v1.Pod{
  95. Spec: v1.PodSpec{
  96. Containers: []v1.Container{
  97. {
  98. Name: "container",
  99. Image: "image",
  100. ImagePullPolicy: "Always",
  101. Resources: v1.ResourceRequirements{
  102. Limits: v1.ResourceList{
  103. v1.ResourceCPU: resource.MustParse("200m"),
  104. v1.ResourceMemory: resource.MustParse("2000"),
  105. },
  106. Requests: v1.ResourceList{
  107. v1.ResourceCPU: resource.MustParse("200m"),
  108. v1.ResourceMemory: resource.MustParse("2000"),
  109. },
  110. },
  111. },
  112. },
  113. Affinity: podAffinity,
  114. },
  115. }
  116. tests := []struct {
  117. pod *v1.Pod
  118. name string
  119. expected interface{}
  120. }{
  121. {
  122. pod: nil,
  123. expected: nil,
  124. name: "pod is nil , priorityMetadata is nil",
  125. },
  126. {
  127. pod: podWithTolerationsAndAffinity,
  128. expected: &priorityMetadata{
  129. nonZeroRequest: nonZeroReqs,
  130. podLimits: nonPodLimits,
  131. podTolerations: tolerations,
  132. affinity: podAffinity,
  133. },
  134. name: "Produce a priorityMetadata with default requests",
  135. },
  136. {
  137. pod: podWithTolerationsAndRequests,
  138. expected: &priorityMetadata{
  139. nonZeroRequest: specifiedReqs,
  140. podLimits: nonPodLimits,
  141. podTolerations: tolerations,
  142. affinity: nil,
  143. },
  144. name: "Produce a priorityMetadata with specified requests",
  145. },
  146. {
  147. pod: podWithAffinityAndRequests,
  148. expected: &priorityMetadata{
  149. nonZeroRequest: specifiedReqs,
  150. podLimits: specifiedPodLimits,
  151. podTolerations: nil,
  152. affinity: podAffinity,
  153. },
  154. name: "Produce a priorityMetadata with specified requests",
  155. },
  156. }
  157. metaDataProducer := NewPriorityMetadataFactory(
  158. schedulertesting.FakeServiceLister([]*v1.Service{}),
  159. schedulertesting.FakeControllerLister([]*v1.ReplicationController{}),
  160. schedulertesting.FakeReplicaSetLister([]*apps.ReplicaSet{}),
  161. schedulertesting.FakeStatefulSetLister([]*apps.StatefulSet{}))
  162. for _, test := range tests {
  163. t.Run(test.name, func(t *testing.T) {
  164. ptData := metaDataProducer(test.pod, nil)
  165. if !reflect.DeepEqual(test.expected, ptData) {
  166. t.Errorf("expected %#v, got %#v", test.expected, ptData)
  167. }
  168. })
  169. }
  170. }