node_label_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright 2016 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. "sort"
  17. "testing"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  21. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  22. )
  23. func TestNewNodeLabelPriority(t *testing.T) {
  24. label1 := map[string]string{"foo": "bar"}
  25. label2 := map[string]string{"bar": "foo"}
  26. label3 := map[string]string{"bar": "baz"}
  27. tests := []struct {
  28. nodes []*v1.Node
  29. label string
  30. presence bool
  31. expectedList schedulerapi.HostPriorityList
  32. name string
  33. }{
  34. {
  35. nodes: []*v1.Node{
  36. {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label1}},
  37. {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
  38. {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
  39. },
  40. expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}},
  41. label: "baz",
  42. presence: true,
  43. name: "no match found, presence true",
  44. },
  45. {
  46. nodes: []*v1.Node{
  47. {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label1}},
  48. {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
  49. {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
  50. },
  51. expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}},
  52. label: "baz",
  53. presence: false,
  54. name: "no match found, presence false",
  55. },
  56. {
  57. nodes: []*v1.Node{
  58. {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label1}},
  59. {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
  60. {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
  61. },
  62. expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}},
  63. label: "foo",
  64. presence: true,
  65. name: "one match found, presence true",
  66. },
  67. {
  68. nodes: []*v1.Node{
  69. {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label1}},
  70. {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
  71. {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
  72. },
  73. expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}},
  74. label: "foo",
  75. presence: false,
  76. name: "one match found, presence false",
  77. },
  78. {
  79. nodes: []*v1.Node{
  80. {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label1}},
  81. {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
  82. {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
  83. },
  84. expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}},
  85. label: "bar",
  86. presence: true,
  87. name: "two matches found, presence true",
  88. },
  89. {
  90. nodes: []*v1.Node{
  91. {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label1}},
  92. {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
  93. {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
  94. },
  95. expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}},
  96. label: "bar",
  97. presence: false,
  98. name: "two matches found, presence false",
  99. },
  100. }
  101. for _, test := range tests {
  102. t.Run(test.name, func(t *testing.T) {
  103. nodeNameToInfo := schedulernodeinfo.CreateNodeNameToInfoMap(nil, test.nodes)
  104. labelPrioritizer := &NodeLabelPrioritizer{
  105. label: test.label,
  106. presence: test.presence,
  107. }
  108. list, err := priorityFunction(labelPrioritizer.CalculateNodeLabelPriorityMap, nil, nil)(nil, nodeNameToInfo, test.nodes)
  109. if err != nil {
  110. t.Errorf("unexpected error: %v", err)
  111. }
  112. // sort the two lists to avoid failures on account of different ordering
  113. sort.Sort(test.expectedList)
  114. sort.Sort(list)
  115. if !reflect.DeepEqual(test.expectedList, list) {
  116. t.Errorf("expected %#v, got %#v", test.expectedList, list)
  117. }
  118. })
  119. }
  120. }