volume_binding_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2019 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 volumebinding
  14. import (
  15. "context"
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. v1 "k8s.io/api/core/v1"
  20. volumescheduling "k8s.io/kubernetes/pkg/controller/volume/scheduling"
  21. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  22. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  23. "k8s.io/kubernetes/pkg/scheduler/volumebinder"
  24. )
  25. func TestVolumeBinding(t *testing.T) {
  26. findErr := fmt.Errorf("find err")
  27. volState := v1.PodSpec{
  28. Volumes: []v1.Volume{
  29. {
  30. VolumeSource: v1.VolumeSource{
  31. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{},
  32. },
  33. },
  34. },
  35. }
  36. table := []struct {
  37. name string
  38. pod *v1.Pod
  39. node *v1.Node
  40. volumeBinderConfig *volumescheduling.FakeVolumeBinderConfig
  41. wantStatus *framework.Status
  42. }{
  43. {
  44. name: "nothing",
  45. pod: &v1.Pod{},
  46. node: &v1.Node{},
  47. wantStatus: nil,
  48. },
  49. {
  50. name: "all bound",
  51. pod: &v1.Pod{Spec: volState},
  52. node: &v1.Node{},
  53. volumeBinderConfig: &volumescheduling.FakeVolumeBinderConfig{
  54. AllBound: true,
  55. FindUnboundSatsified: true,
  56. FindBoundSatsified: true,
  57. },
  58. wantStatus: nil,
  59. },
  60. {
  61. name: "unbound/no matches",
  62. pod: &v1.Pod{Spec: volState},
  63. node: &v1.Node{},
  64. volumeBinderConfig: &volumescheduling.FakeVolumeBinderConfig{
  65. FindUnboundSatsified: false,
  66. FindBoundSatsified: true,
  67. },
  68. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonBindConflict),
  69. },
  70. {
  71. name: "bound and unbound unsatisfied",
  72. pod: &v1.Pod{Spec: volState},
  73. node: &v1.Node{},
  74. volumeBinderConfig: &volumescheduling.FakeVolumeBinderConfig{
  75. FindUnboundSatsified: false,
  76. FindBoundSatsified: false,
  77. },
  78. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonNodeConflict,
  79. ErrReasonBindConflict),
  80. },
  81. {
  82. name: "unbound/found matches/bind succeeds",
  83. pod: &v1.Pod{Spec: volState},
  84. node: &v1.Node{},
  85. volumeBinderConfig: &volumescheduling.FakeVolumeBinderConfig{
  86. FindUnboundSatsified: true,
  87. FindBoundSatsified: true,
  88. },
  89. wantStatus: nil,
  90. },
  91. {
  92. name: "predicate error",
  93. pod: &v1.Pod{Spec: volState},
  94. node: &v1.Node{},
  95. volumeBinderConfig: &volumescheduling.FakeVolumeBinderConfig{
  96. FindErr: findErr,
  97. },
  98. wantStatus: framework.NewStatus(framework.Error, findErr.Error()),
  99. },
  100. }
  101. for _, item := range table {
  102. t.Run(item.name, func(t *testing.T) {
  103. nodeInfo := schedulernodeinfo.NewNodeInfo()
  104. nodeInfo.SetNode(item.node)
  105. fakeVolumeBinder := volumebinder.NewFakeVolumeBinder(item.volumeBinderConfig)
  106. p := &VolumeBinding{
  107. binder: fakeVolumeBinder,
  108. }
  109. gotStatus := p.Filter(context.Background(), nil, item.pod, nodeInfo)
  110. if !reflect.DeepEqual(gotStatus, item.wantStatus) {
  111. t.Errorf("status does not match: %v, want: %v", gotStatus, item.wantStatus)
  112. }
  113. })
  114. }
  115. }