utils_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 util
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/util/diff"
  19. "k8s.io/kubernetes/pkg/apis/scheduling"
  20. )
  21. // TestGetPodPriority tests GetPodPriority function.
  22. func TestGetPodPriority(t *testing.T) {
  23. p := int32(20)
  24. tests := []struct {
  25. name string
  26. pod *v1.Pod
  27. expectedPriority int32
  28. }{
  29. {
  30. name: "no priority pod resolves to static default priority",
  31. pod: &v1.Pod{
  32. Spec: v1.PodSpec{Containers: []v1.Container{
  33. {Name: "container", Image: "image"}},
  34. },
  35. },
  36. expectedPriority: scheduling.DefaultPriorityWhenNoDefaultClassExists,
  37. },
  38. {
  39. name: "pod with priority resolves correctly",
  40. pod: &v1.Pod{
  41. Spec: v1.PodSpec{Containers: []v1.Container{
  42. {Name: "container", Image: "image"}},
  43. Priority: &p,
  44. },
  45. },
  46. expectedPriority: p,
  47. },
  48. }
  49. for _, test := range tests {
  50. if GetPodPriority(test.pod) != test.expectedPriority {
  51. t.Errorf("expected pod priority: %v, got %v", test.expectedPriority, GetPodPriority(test.pod))
  52. }
  53. }
  54. }
  55. // TestSortableList tests SortableList by storing pods in the list and sorting
  56. // them by their priority.
  57. func TestSortableList(t *testing.T) {
  58. higherPriority := func(pod1, pod2 interface{}) bool {
  59. return GetPodPriority(pod1.(*v1.Pod)) > GetPodPriority(pod2.(*v1.Pod))
  60. }
  61. podList := SortableList{CompFunc: higherPriority}
  62. // Add a few Pods with different priorities from lowest to highest priority.
  63. for i := 0; i < 10; i++ {
  64. var p = int32(i)
  65. pod := &v1.Pod{
  66. Spec: v1.PodSpec{
  67. Containers: []v1.Container{
  68. {
  69. Name: "container",
  70. Image: "image",
  71. },
  72. },
  73. Priority: &p,
  74. },
  75. }
  76. podList.Items = append(podList.Items, pod)
  77. }
  78. podList.Sort()
  79. if len(podList.Items) != 10 {
  80. t.Errorf("expected length of list was 10, got: %v", len(podList.Items))
  81. }
  82. var prevPriority = int32(10)
  83. for _, p := range podList.Items {
  84. if *p.(*v1.Pod).Spec.Priority >= prevPriority {
  85. t.Errorf("Pods are not soreted. Current pod pririty is %v, while previous one was %v.", *p.(*v1.Pod).Spec.Priority, prevPriority)
  86. }
  87. }
  88. }
  89. func TestGetContainerPorts(t *testing.T) {
  90. tests := []struct {
  91. pod1 *v1.Pod
  92. pod2 *v1.Pod
  93. expected []*v1.ContainerPort
  94. }{
  95. {
  96. pod1: &v1.Pod{
  97. Spec: v1.PodSpec{
  98. Containers: []v1.Container{
  99. {
  100. Ports: []v1.ContainerPort{
  101. {
  102. ContainerPort: 8001,
  103. Protocol: v1.ProtocolTCP,
  104. },
  105. {
  106. ContainerPort: 8002,
  107. Protocol: v1.ProtocolTCP,
  108. },
  109. },
  110. },
  111. {
  112. Ports: []v1.ContainerPort{
  113. {
  114. ContainerPort: 8003,
  115. Protocol: v1.ProtocolTCP,
  116. },
  117. {
  118. ContainerPort: 8004,
  119. Protocol: v1.ProtocolTCP,
  120. },
  121. },
  122. },
  123. },
  124. },
  125. },
  126. pod2: &v1.Pod{
  127. Spec: v1.PodSpec{
  128. Containers: []v1.Container{
  129. {
  130. Ports: []v1.ContainerPort{
  131. {
  132. ContainerPort: 8011,
  133. Protocol: v1.ProtocolTCP,
  134. },
  135. {
  136. ContainerPort: 8012,
  137. Protocol: v1.ProtocolTCP,
  138. },
  139. },
  140. },
  141. {
  142. Ports: []v1.ContainerPort{
  143. {
  144. ContainerPort: 8013,
  145. Protocol: v1.ProtocolTCP,
  146. },
  147. {
  148. ContainerPort: 8014,
  149. Protocol: v1.ProtocolTCP,
  150. },
  151. },
  152. },
  153. },
  154. },
  155. },
  156. expected: []*v1.ContainerPort{
  157. {
  158. ContainerPort: 8001,
  159. Protocol: v1.ProtocolTCP,
  160. },
  161. {
  162. ContainerPort: 8002,
  163. Protocol: v1.ProtocolTCP,
  164. },
  165. {
  166. ContainerPort: 8003,
  167. Protocol: v1.ProtocolTCP,
  168. },
  169. {
  170. ContainerPort: 8004,
  171. Protocol: v1.ProtocolTCP,
  172. },
  173. {
  174. ContainerPort: 8011,
  175. Protocol: v1.ProtocolTCP,
  176. },
  177. {
  178. ContainerPort: 8012,
  179. Protocol: v1.ProtocolTCP,
  180. },
  181. {
  182. ContainerPort: 8013,
  183. Protocol: v1.ProtocolTCP,
  184. },
  185. {
  186. ContainerPort: 8014,
  187. Protocol: v1.ProtocolTCP,
  188. },
  189. },
  190. },
  191. }
  192. for _, test := range tests {
  193. result := GetContainerPorts(test.pod1, test.pod2)
  194. if !reflect.DeepEqual(test.expected, result) {
  195. t.Errorf("Got different result than expected.\nDifference detected on:\n%s", diff.ObjectGoPrintSideBySide(test.expected, result))
  196. }
  197. }
  198. }