delete_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 persistentvolume
  14. import (
  15. "errors"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/apis/extensions"
  19. )
  20. // Test single call to syncVolume, expecting recycling to happen.
  21. // 1. Fill in the controller with initial data
  22. // 2. Call the syncVolume *once*.
  23. // 3. Compare resulting volumes with expected volumes.
  24. func TestDeleteSync(t *testing.T) {
  25. tests := []controllerTest{
  26. {
  27. // delete volume bound by controller
  28. "8-1 - successful delete",
  29. newVolumeArray("volume8-1", "1Gi", "uid8-1", "claim8-1", api.VolumeBound, api.PersistentVolumeReclaimDelete, annBoundByController),
  30. novolumes,
  31. noclaims,
  32. noclaims,
  33. noevents, noerrors,
  34. // Inject deleter into the controller and call syncVolume. The
  35. // deleter simulates one delete() call that succeeds.
  36. wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume),
  37. },
  38. {
  39. // delete volume bound by user
  40. "8-2 - successful delete with prebound volume",
  41. newVolumeArray("volume8-2", "1Gi", "uid8-2", "claim8-2", api.VolumeBound, api.PersistentVolumeReclaimDelete),
  42. novolumes,
  43. noclaims,
  44. noclaims,
  45. noevents, noerrors,
  46. // Inject deleter into the controller and call syncVolume. The
  47. // deleter simulates one delete() call that succeeds.
  48. wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume),
  49. },
  50. {
  51. // delete failure - plugin not found
  52. "8-3 - plugin not found",
  53. newVolumeArray("volume8-3", "1Gi", "uid8-3", "claim8-3", api.VolumeBound, api.PersistentVolumeReclaimDelete),
  54. withMessage("Error getting deleter volume plugin for volume \"volume8-3\": no volume plugin matched", newVolumeArray("volume8-3", "1Gi", "uid8-3", "claim8-3", api.VolumeFailed, api.PersistentVolumeReclaimDelete)),
  55. noclaims,
  56. noclaims,
  57. []string{"Warning VolumeFailedDelete"}, noerrors, testSyncVolume,
  58. },
  59. {
  60. // delete failure - newDeleter returns error
  61. "8-4 - newDeleter returns error",
  62. newVolumeArray("volume8-4", "1Gi", "uid8-4", "claim8-4", api.VolumeBound, api.PersistentVolumeReclaimDelete),
  63. withMessage("Failed to create deleter for volume \"volume8-4\": Mock plugin error: no deleteCalls configured", newVolumeArray("volume8-4", "1Gi", "uid8-4", "claim8-4", api.VolumeFailed, api.PersistentVolumeReclaimDelete)),
  64. noclaims,
  65. noclaims,
  66. []string{"Warning VolumeFailedDelete"}, noerrors,
  67. wrapTestWithReclaimCalls(operationDelete, []error{}, testSyncVolume),
  68. },
  69. {
  70. // delete failure - delete() returns error
  71. "8-5 - delete returns error",
  72. newVolumeArray("volume8-5", "1Gi", "uid8-5", "claim8-5", api.VolumeBound, api.PersistentVolumeReclaimDelete),
  73. withMessage("Delete of volume \"volume8-5\" failed: Mock delete error", newVolumeArray("volume8-5", "1Gi", "uid8-5", "claim8-5", api.VolumeFailed, api.PersistentVolumeReclaimDelete)),
  74. noclaims,
  75. noclaims,
  76. []string{"Warning VolumeFailedDelete"}, noerrors,
  77. wrapTestWithReclaimCalls(operationDelete, []error{errors.New("Mock delete error")}, testSyncVolume),
  78. },
  79. {
  80. // delete success(?) - volume is deleted before doDelete() starts
  81. "8-6 - volume is deleted before deleting",
  82. newVolumeArray("volume8-6", "1Gi", "uid8-6", "claim8-6", api.VolumeBound, api.PersistentVolumeReclaimDelete),
  83. novolumes,
  84. noclaims,
  85. noclaims,
  86. noevents, noerrors,
  87. wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationDelete, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *volumeReactor) {
  88. // Delete the volume before delete operation starts
  89. reactor.lock.Lock()
  90. delete(reactor.volumes, "volume8-6")
  91. reactor.lock.Unlock()
  92. }),
  93. },
  94. {
  95. // delete success(?) - volume is bound just at the time doDelete()
  96. // starts. This simulates "volume no longer needs recycling,
  97. // skipping".
  98. "8-7 - volume is bound before deleting",
  99. newVolumeArray("volume8-7", "1Gi", "uid8-7", "claim8-7", api.VolumeBound, api.PersistentVolumeReclaimDelete, annBoundByController),
  100. newVolumeArray("volume8-7", "1Gi", "uid8-7", "claim8-7", api.VolumeBound, api.PersistentVolumeReclaimDelete, annBoundByController),
  101. noclaims,
  102. newClaimArray("claim8-7", "uid8-7", "10Gi", "volume8-7", api.ClaimBound),
  103. noevents, noerrors,
  104. wrapTestWithInjectedOperation(wrapTestWithReclaimCalls(operationDelete, []error{}, testSyncVolume), func(ctrl *PersistentVolumeController, reactor *volumeReactor) {
  105. reactor.lock.Lock()
  106. defer reactor.lock.Unlock()
  107. // Bind the volume to resurrected claim (this should never
  108. // happen)
  109. claim := newClaim("claim8-7", "uid8-7", "10Gi", "volume8-7", api.ClaimBound)
  110. reactor.claims[claim.Name] = claim
  111. ctrl.claims.Add(claim)
  112. volume := reactor.volumes["volume8-7"]
  113. volume.Status.Phase = api.VolumeBound
  114. }),
  115. },
  116. {
  117. // delete success - volume bound by user is deleted, while a new
  118. // claim is created with another UID.
  119. "8-9 - prebound volume is deleted while the claim exists",
  120. newVolumeArray("volume8-9", "1Gi", "uid8-9", "claim8-9", api.VolumeBound, api.PersistentVolumeReclaimDelete),
  121. novolumes,
  122. newClaimArray("claim8-9", "uid8-9-x", "10Gi", "", api.ClaimPending),
  123. newClaimArray("claim8-9", "uid8-9-x", "10Gi", "", api.ClaimPending),
  124. noevents, noerrors,
  125. // Inject deleter into the controller and call syncVolume. The
  126. // deleter simulates one delete() call that succeeds.
  127. wrapTestWithReclaimCalls(operationDelete, []error{nil}, testSyncVolume),
  128. },
  129. }
  130. runSyncTests(t, tests, []*extensions.StorageClass{})
  131. }
  132. // Test multiple calls to syncClaim/syncVolume and periodic sync of all
  133. // volume/claims. The test follows this pattern:
  134. // 0. Load the controller with initial data.
  135. // 1. Call controllerTest.testCall() once as in TestSync()
  136. // 2. For all volumes/claims changed by previous syncVolume/syncClaim calls,
  137. // call appropriate syncVolume/syncClaim (simulating "volume/claim changed"
  138. // events). Go to 2. if these calls change anything.
  139. // 3. When all changes are processed and no new changes were made, call
  140. // syncVolume/syncClaim on all volumes/claims (simulating "periodic sync").
  141. // 4. If some changes were done by step 3., go to 2. (simulation of
  142. // "volume/claim updated" events, eventually performing step 3. again)
  143. // 5. When 3. does not do any changes, finish the tests and compare final set
  144. // of volumes/claims with expected claims/volumes and report differences.
  145. // Some limit of calls in enforced to prevent endless loops.
  146. func TestDeleteMultiSync(t *testing.T) {
  147. tests := []controllerTest{
  148. {
  149. // delete failure - delete returns error. The controller should
  150. // try again.
  151. "9-1 - delete returns error",
  152. newVolumeArray("volume9-1", "1Gi", "uid9-1", "claim9-1", api.VolumeBound, api.PersistentVolumeReclaimDelete),
  153. novolumes,
  154. noclaims,
  155. noclaims,
  156. []string{"Warning VolumeFailedDelete"}, noerrors,
  157. wrapTestWithReclaimCalls(operationDelete, []error{errors.New("Mock delete error"), nil}, testSyncVolume),
  158. },
  159. }
  160. runMultisyncTests(t, tests, []*extensions.StorageClass{}, "")
  161. }