scheduler_binder_cache_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 scheduling
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. )
  20. func TestUpdateGetBindings(t *testing.T) {
  21. scenarios := map[string]struct {
  22. updateBindings []*bindingInfo
  23. updateProvisionings []*v1.PersistentVolumeClaim
  24. updatePod string
  25. updateNode string
  26. getBindings []*bindingInfo
  27. getProvisionings []*v1.PersistentVolumeClaim
  28. getPod string
  29. getNode string
  30. }{
  31. "no-pod": {
  32. getPod: "pod1",
  33. getNode: "node1",
  34. },
  35. "no-node": {
  36. updatePod: "pod1",
  37. updateNode: "node1",
  38. updateBindings: []*bindingInfo{},
  39. updateProvisionings: []*v1.PersistentVolumeClaim{},
  40. getPod: "pod1",
  41. getNode: "node2",
  42. },
  43. "binding-nil": {
  44. updatePod: "pod1",
  45. updateNode: "node1",
  46. updateBindings: nil,
  47. updateProvisionings: nil,
  48. getPod: "pod1",
  49. getNode: "node1",
  50. },
  51. "binding-exists": {
  52. updatePod: "pod1",
  53. updateNode: "node1",
  54. updateBindings: []*bindingInfo{{pvc: &v1.PersistentVolumeClaim{ObjectMeta: metav1.ObjectMeta{Name: "pvc1"}}}},
  55. updateProvisionings: []*v1.PersistentVolumeClaim{{ObjectMeta: metav1.ObjectMeta{Name: "pvc2"}}},
  56. getPod: "pod1",
  57. getNode: "node1",
  58. getBindings: []*bindingInfo{{pvc: &v1.PersistentVolumeClaim{ObjectMeta: metav1.ObjectMeta{Name: "pvc1"}}}},
  59. getProvisionings: []*v1.PersistentVolumeClaim{{ObjectMeta: metav1.ObjectMeta{Name: "pvc2"}}},
  60. },
  61. }
  62. for name, scenario := range scenarios {
  63. cache := NewPodBindingCache()
  64. // Perform updates
  65. updatePod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: scenario.updatePod, Namespace: "ns"}}
  66. cache.UpdateBindings(updatePod, scenario.updateNode, scenario.updateBindings, scenario.updateProvisionings)
  67. // Verify updated bindings
  68. bindings := cache.GetBindings(updatePod, scenario.updateNode)
  69. if !reflect.DeepEqual(bindings, scenario.updateBindings) {
  70. t.Errorf("Test %v failed: returned bindings after update different. Got %+v, expected %+v", name, bindings, scenario.updateBindings)
  71. }
  72. // Verify updated provisionings
  73. provisionings := cache.GetProvisionedPVCs(updatePod, scenario.updateNode)
  74. if !reflect.DeepEqual(provisionings, scenario.updateProvisionings) {
  75. t.Errorf("Test %v failed: returned provisionings after update different. Got %+v, expected %+v", name, provisionings, scenario.updateProvisionings)
  76. }
  77. // Get bindings
  78. getPod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: scenario.getPod, Namespace: "ns"}}
  79. bindings = cache.GetBindings(getPod, scenario.getNode)
  80. if !reflect.DeepEqual(bindings, scenario.getBindings) {
  81. t.Errorf("Test %v failed: unexpected bindings returned. Got %+v, expected %+v", name, bindings, scenario.updateBindings)
  82. }
  83. // Get provisionings
  84. provisionings = cache.GetProvisionedPVCs(getPod, scenario.getNode)
  85. if !reflect.DeepEqual(provisionings, scenario.getProvisionings) {
  86. t.Errorf("Test %v failed: unexpected bindings returned. Got %+v, expected %+v", name, provisionings, scenario.getProvisionings)
  87. }
  88. }
  89. }
  90. func TestDeleteBindings(t *testing.T) {
  91. initialBindings := []*bindingInfo{{pvc: &v1.PersistentVolumeClaim{ObjectMeta: metav1.ObjectMeta{Name: "pvc1"}}}}
  92. initialProvisionings := []*v1.PersistentVolumeClaim{{ObjectMeta: metav1.ObjectMeta{Name: "pvc2"}}}
  93. cache := NewPodBindingCache()
  94. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "ns"}}
  95. // Get nil bindings and provisionings
  96. bindings := cache.GetBindings(pod, "node1")
  97. if bindings != nil {
  98. t.Errorf("Test failed: expected initial nil bindings, got %+v", bindings)
  99. }
  100. provisionings := cache.GetProvisionedPVCs(pod, "node1")
  101. if provisionings != nil {
  102. t.Errorf("Test failed: expected initial nil provisionings, got %+v", provisionings)
  103. }
  104. // Delete nothing
  105. cache.DeleteBindings(pod)
  106. // Perform updates
  107. cache.UpdateBindings(pod, "node1", initialBindings, initialProvisionings)
  108. // Get bindings and provisionings
  109. bindings = cache.GetBindings(pod, "node1")
  110. if !reflect.DeepEqual(bindings, initialBindings) {
  111. t.Errorf("Test failed: expected bindings %+v, got %+v", initialBindings, bindings)
  112. }
  113. provisionings = cache.GetProvisionedPVCs(pod, "node1")
  114. if !reflect.DeepEqual(provisionings, initialProvisionings) {
  115. t.Errorf("Test failed: expected provisionings %+v, got %+v", initialProvisionings, provisionings)
  116. }
  117. // Delete
  118. cache.DeleteBindings(pod)
  119. // Get bindings and provisionings
  120. bindings = cache.GetBindings(pod, "node1")
  121. if bindings != nil {
  122. t.Errorf("Test failed: expected nil bindings, got %+v", bindings)
  123. }
  124. provisionings = cache.GetProvisionedPVCs(pod, "node1")
  125. if provisionings != nil {
  126. t.Errorf("Test failed: expected nil provisionings, got %+v", provisionings)
  127. }
  128. }