predicate_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 lifecycle
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/api/resource"
  19. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  20. )
  21. var (
  22. quantity = *resource.NewQuantity(1, resource.DecimalSI)
  23. extendedResourceName1 = "example.com/er1"
  24. extendedResourceName2 = "example.com/er2"
  25. )
  26. func TestRemoveMissingExtendedResources(t *testing.T) {
  27. for _, test := range []struct {
  28. desc string
  29. pod *v1.Pod
  30. node *v1.Node
  31. expectedPod *v1.Pod
  32. }{
  33. {
  34. desc: "requests in Limits should be ignored",
  35. pod: makeTestPod(
  36. v1.ResourceList{}, // Requests
  37. v1.ResourceList{"foo.com/bar": quantity}, // Limits
  38. ),
  39. node: makeTestNode(
  40. v1.ResourceList{"foo.com/baz": quantity}, // Allocatable
  41. ),
  42. expectedPod: makeTestPod(
  43. v1.ResourceList{}, // Requests
  44. v1.ResourceList{"foo.com/bar": quantity}, // Limits
  45. ),
  46. },
  47. {
  48. desc: "requests for resources available in node should not be removed",
  49. pod: makeTestPod(
  50. v1.ResourceList{"foo.com/bar": quantity}, // Requests
  51. v1.ResourceList{}, // Limits
  52. ),
  53. node: makeTestNode(
  54. v1.ResourceList{"foo.com/bar": quantity}, // Allocatable
  55. ),
  56. expectedPod: makeTestPod(
  57. v1.ResourceList{"foo.com/bar": quantity}, // Requests
  58. v1.ResourceList{}), // Limits
  59. },
  60. {
  61. desc: "requests for resources unavailable in node should be removed",
  62. pod: makeTestPod(
  63. v1.ResourceList{"foo.com/bar": quantity}, // Requests
  64. v1.ResourceList{}, // Limits
  65. ),
  66. node: makeTestNode(
  67. v1.ResourceList{"foo.com/baz": quantity}, // Allocatable
  68. ),
  69. expectedPod: makeTestPod(
  70. v1.ResourceList{}, // Requests
  71. v1.ResourceList{}, // Limits
  72. ),
  73. },
  74. } {
  75. nodeInfo := schedulernodeinfo.NewNodeInfo()
  76. nodeInfo.SetNode(test.node)
  77. pod := removeMissingExtendedResources(test.pod, nodeInfo)
  78. if !reflect.DeepEqual(pod, test.expectedPod) {
  79. t.Errorf("%s: Expected pod\n%v\ngot\n%v\n", test.desc, test.expectedPod, pod)
  80. }
  81. }
  82. }
  83. func makeTestPod(requests, limits v1.ResourceList) *v1.Pod {
  84. return &v1.Pod{
  85. Spec: v1.PodSpec{
  86. Containers: []v1.Container{
  87. {
  88. Resources: v1.ResourceRequirements{
  89. Requests: requests,
  90. Limits: limits,
  91. },
  92. },
  93. },
  94. },
  95. }
  96. }
  97. func makeTestNode(allocatable v1.ResourceList) *v1.Node {
  98. return &v1.Node{
  99. Status: v1.NodeStatus{
  100. Allocatable: allocatable,
  101. },
  102. }
  103. }