comparer_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 debugger
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/types"
  19. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  20. )
  21. func TestCompareNodes(t *testing.T) {
  22. tests := []struct {
  23. name string
  24. actual []string
  25. cached []string
  26. missing []string
  27. redundant []string
  28. }{
  29. {
  30. name: "redundant cached value",
  31. actual: []string{"foo", "bar"},
  32. cached: []string{"bar", "foo", "foobar"},
  33. missing: []string{},
  34. redundant: []string{"foobar"},
  35. },
  36. {
  37. name: "missing cached value",
  38. actual: []string{"foo", "bar", "foobar"},
  39. cached: []string{"bar", "foo"},
  40. missing: []string{"foobar"},
  41. redundant: []string{},
  42. },
  43. {
  44. name: "proper cache set",
  45. actual: []string{"foo", "bar", "foobar"},
  46. cached: []string{"bar", "foobar", "foo"},
  47. missing: []string{},
  48. redundant: []string{},
  49. },
  50. }
  51. for _, test := range tests {
  52. t.Run(test.name, func(t *testing.T) {
  53. testCompareNodes(test.actual, test.cached, test.missing, test.redundant, t)
  54. })
  55. }
  56. }
  57. func testCompareNodes(actual, cached, missing, redundant []string, t *testing.T) {
  58. compare := CacheComparer{}
  59. nodes := []*v1.Node{}
  60. for _, nodeName := range actual {
  61. node := &v1.Node{}
  62. node.Name = nodeName
  63. nodes = append(nodes, node)
  64. }
  65. nodeInfo := make(map[string]*schedulernodeinfo.NodeInfo)
  66. for _, nodeName := range cached {
  67. nodeInfo[nodeName] = &schedulernodeinfo.NodeInfo{}
  68. }
  69. m, r := compare.CompareNodes(nodes, nodeInfo)
  70. if !reflect.DeepEqual(m, missing) {
  71. t.Errorf("missing expected to be %s; got %s", missing, m)
  72. }
  73. if !reflect.DeepEqual(r, redundant) {
  74. t.Errorf("redundant expected to be %s; got %s", redundant, r)
  75. }
  76. }
  77. func TestComparePods(t *testing.T) {
  78. tests := []struct {
  79. name string
  80. actual []string
  81. cached []string
  82. queued []string
  83. missing []string
  84. redundant []string
  85. }{
  86. {
  87. name: "redundant cached value",
  88. actual: []string{"foo", "bar"},
  89. cached: []string{"bar", "foo", "foobar"},
  90. queued: []string{},
  91. missing: []string{},
  92. redundant: []string{"foobar"},
  93. },
  94. {
  95. name: "redundant and queued values",
  96. actual: []string{"foo", "bar"},
  97. cached: []string{"foo", "foobar"},
  98. queued: []string{"bar"},
  99. missing: []string{},
  100. redundant: []string{"foobar"},
  101. },
  102. {
  103. name: "missing cached value",
  104. actual: []string{"foo", "bar", "foobar"},
  105. cached: []string{"bar", "foo"},
  106. queued: []string{},
  107. missing: []string{"foobar"},
  108. redundant: []string{},
  109. },
  110. {
  111. name: "missing and queued values",
  112. actual: []string{"foo", "bar", "foobar"},
  113. cached: []string{"foo"},
  114. queued: []string{"bar"},
  115. missing: []string{"foobar"},
  116. redundant: []string{},
  117. },
  118. {
  119. name: "correct cache set",
  120. actual: []string{"foo", "bar", "foobar"},
  121. cached: []string{"bar", "foobar", "foo"},
  122. queued: []string{},
  123. missing: []string{},
  124. redundant: []string{},
  125. },
  126. {
  127. name: "queued cache value",
  128. actual: []string{"foo", "bar", "foobar"},
  129. cached: []string{"foobar", "foo"},
  130. queued: []string{"bar"},
  131. missing: []string{},
  132. redundant: []string{},
  133. },
  134. }
  135. for _, test := range tests {
  136. t.Run(test.name, func(t *testing.T) {
  137. testComparePods(test.actual, test.cached, test.queued, test.missing, test.redundant, t)
  138. })
  139. }
  140. }
  141. func testComparePods(actual, cached, queued, missing, redundant []string, t *testing.T) {
  142. compare := CacheComparer{}
  143. pods := []*v1.Pod{}
  144. for _, uid := range actual {
  145. pod := &v1.Pod{}
  146. pod.UID = types.UID(uid)
  147. pods = append(pods, pod)
  148. }
  149. queuedPods := []*v1.Pod{}
  150. for _, uid := range queued {
  151. pod := &v1.Pod{}
  152. pod.UID = types.UID(uid)
  153. queuedPods = append(queuedPods, pod)
  154. }
  155. nodeInfo := make(map[string]*schedulernodeinfo.NodeInfo)
  156. for _, uid := range cached {
  157. pod := &v1.Pod{}
  158. pod.UID = types.UID(uid)
  159. pod.Namespace = "ns"
  160. pod.Name = uid
  161. nodeInfo[uid] = schedulernodeinfo.NewNodeInfo(pod)
  162. }
  163. m, r := compare.ComparePods(pods, queuedPods, nodeInfo)
  164. if !reflect.DeepEqual(m, missing) {
  165. t.Errorf("missing expected to be %s; got %s", missing, m)
  166. }
  167. if !reflect.DeepEqual(r, redundant) {
  168. t.Errorf("redundant expected to be %s; got %s", redundant, r)
  169. }
  170. }