binder_test.go 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. "testing"
  16. "k8s.io/api/core/v1"
  17. storage "k8s.io/api/storage/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. utilfeature "k8s.io/apiserver/pkg/util/feature"
  20. featuregatetesting "k8s.io/component-base/featuregate/testing"
  21. pvutil "k8s.io/kubernetes/pkg/controller/volume/persistentvolume/util"
  22. "k8s.io/kubernetes/pkg/features"
  23. )
  24. // Test single call to syncClaim and syncVolume methods.
  25. // 1. Fill in the controller with initial data
  26. // 2. Call the tested function (syncClaim/syncVolume) via
  27. // controllerTest.testCall *once*.
  28. // 3. Compare resulting volumes and claims with expected volumes and claims.
  29. func TestSync(t *testing.T) {
  30. labels := map[string]string{
  31. "foo": "true",
  32. "bar": "false",
  33. }
  34. tests := []controllerTest{
  35. // [Unit test set 1] User did not care which PV they get.
  36. // Test the matching with no claim.Spec.VolumeName and with various
  37. // volumes.
  38. {
  39. // syncClaim binds to a matching unbound volume.
  40. "1-1 - successful bind",
  41. newVolumeArray("volume1-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  42. newVolumeArray("volume1-1", "1Gi", "uid1-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  43. newClaimArray("claim1-1", "uid1-1", "1Gi", "", v1.ClaimPending, nil),
  44. newClaimArray("claim1-1", "uid1-1", "1Gi", "volume1-1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  45. noevents, noerrors, testSyncClaim,
  46. },
  47. {
  48. // syncClaim does not do anything when there is no matching volume.
  49. "1-2 - noop",
  50. newVolumeArray("volume1-2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  51. newVolumeArray("volume1-2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  52. newClaimArray("claim1-2", "uid1-2", "10Gi", "", v1.ClaimPending, nil),
  53. newClaimArray("claim1-2", "uid1-2", "10Gi", "", v1.ClaimPending, nil),
  54. []string{"Normal FailedBinding"},
  55. noerrors, testSyncClaim,
  56. },
  57. {
  58. // syncClaim resets claim.Status to Pending when there is no
  59. // matching volume.
  60. "1-3 - reset to Pending",
  61. newVolumeArray("volume1-3", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  62. newVolumeArray("volume1-3", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  63. newClaimArray("claim1-3", "uid1-3", "10Gi", "", v1.ClaimBound, nil),
  64. newClaimArray("claim1-3", "uid1-3", "10Gi", "", v1.ClaimPending, nil),
  65. []string{"Normal FailedBinding"},
  66. noerrors, testSyncClaim,
  67. },
  68. {
  69. // syncClaim binds claims to the smallest matching volume
  70. "1-4 - smallest volume",
  71. []*v1.PersistentVolume{
  72. newVolume("volume1-4_1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  73. newVolume("volume1-4_2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  74. },
  75. []*v1.PersistentVolume{
  76. newVolume("volume1-4_1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  77. newVolume("volume1-4_2", "1Gi", "uid1-4", "claim1-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  78. },
  79. newClaimArray("claim1-4", "uid1-4", "1Gi", "", v1.ClaimPending, nil),
  80. newClaimArray("claim1-4", "uid1-4", "1Gi", "volume1-4_2", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  81. noevents, noerrors, testSyncClaim,
  82. },
  83. {
  84. // syncClaim binds a claim only to volume that points to it (by
  85. // name), even though a smaller one is available.
  86. "1-5 - prebound volume by name - success",
  87. []*v1.PersistentVolume{
  88. newVolume("volume1-5_1", "10Gi", "", "claim1-5", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  89. newVolume("volume1-5_2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  90. },
  91. []*v1.PersistentVolume{
  92. newVolume("volume1-5_1", "10Gi", "uid1-5", "claim1-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  93. newVolume("volume1-5_2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  94. },
  95. newClaimArray("claim1-5", "uid1-5", "1Gi", "", v1.ClaimPending, nil),
  96. withExpectedCapacity("10Gi", newClaimArray("claim1-5", "uid1-5", "1Gi", "volume1-5_1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  97. noevents, noerrors, testSyncClaim,
  98. },
  99. {
  100. // syncClaim binds a claim only to volume that points to it (by
  101. // UID), even though a smaller one is available.
  102. "1-6 - prebound volume by UID - success",
  103. []*v1.PersistentVolume{
  104. newVolume("volume1-6_1", "10Gi", "uid1-6", "claim1-6", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  105. newVolume("volume1-6_2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  106. },
  107. []*v1.PersistentVolume{
  108. newVolume("volume1-6_1", "10Gi", "uid1-6", "claim1-6", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  109. newVolume("volume1-6_2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  110. },
  111. newClaimArray("claim1-6", "uid1-6", "1Gi", "", v1.ClaimPending, nil),
  112. withExpectedCapacity("10Gi", newClaimArray("claim1-6", "uid1-6", "1Gi", "volume1-6_1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  113. noevents, noerrors, testSyncClaim,
  114. },
  115. {
  116. // syncClaim does not bind claim to a volume prebound to a claim with
  117. // same name and different UID
  118. "1-7 - prebound volume to different claim",
  119. newVolumeArray("volume1-7", "10Gi", "uid1-777", "claim1-7", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  120. newVolumeArray("volume1-7", "10Gi", "uid1-777", "claim1-7", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  121. newClaimArray("claim1-7", "uid1-7", "1Gi", "", v1.ClaimPending, nil),
  122. newClaimArray("claim1-7", "uid1-7", "1Gi", "", v1.ClaimPending, nil),
  123. []string{"Normal FailedBinding"},
  124. noerrors, testSyncClaim,
  125. },
  126. {
  127. // syncClaim completes binding - simulates controller crash after
  128. // PV.ClaimRef is saved
  129. "1-8 - complete bind after crash - PV bound",
  130. newVolumeArray("volume1-8", "1Gi", "uid1-8", "claim1-8", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  131. newVolumeArray("volume1-8", "1Gi", "uid1-8", "claim1-8", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  132. newClaimArray("claim1-8", "uid1-8", "1Gi", "", v1.ClaimPending, nil),
  133. newClaimArray("claim1-8", "uid1-8", "1Gi", "volume1-8", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  134. noevents, noerrors, testSyncClaim,
  135. },
  136. {
  137. // syncClaim completes binding - simulates controller crash after
  138. // PV.Status is saved
  139. "1-9 - complete bind after crash - PV status saved",
  140. newVolumeArray("volume1-9", "1Gi", "uid1-9", "claim1-9", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  141. newVolumeArray("volume1-9", "1Gi", "uid1-9", "claim1-9", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  142. newClaimArray("claim1-9", "uid1-9", "1Gi", "", v1.ClaimPending, nil),
  143. newClaimArray("claim1-9", "uid1-9", "1Gi", "volume1-9", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  144. noevents, noerrors, testSyncClaim,
  145. },
  146. {
  147. // syncClaim completes binding - simulates controller crash after
  148. // PVC.VolumeName is saved
  149. "1-10 - complete bind after crash - PVC bound",
  150. newVolumeArray("volume1-10", "1Gi", "uid1-10", "claim1-10", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  151. newVolumeArray("volume1-10", "1Gi", "uid1-10", "claim1-10", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  152. newClaimArray("claim1-10", "uid1-10", "1Gi", "volume1-10", v1.ClaimPending, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  153. newClaimArray("claim1-10", "uid1-10", "1Gi", "volume1-10", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  154. noevents, noerrors, testSyncClaim,
  155. },
  156. {
  157. // syncClaim binds a claim only when the label selector matches the volume
  158. "1-11 - bind when selector matches",
  159. withLabels(labels, newVolumeArray("volume1-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  160. withLabels(labels, newVolumeArray("volume1-1", "1Gi", "uid1-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  161. withLabelSelector(labels, newClaimArray("claim1-1", "uid1-1", "1Gi", "", v1.ClaimPending, nil)),
  162. withLabelSelector(labels, newClaimArray("claim1-1", "uid1-1", "1Gi", "volume1-1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  163. noevents, noerrors, testSyncClaim,
  164. },
  165. {
  166. // syncClaim does not bind a claim when the label selector doesn't match
  167. "1-12 - do not bind when selector does not match",
  168. newVolumeArray("volume1-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  169. newVolumeArray("volume1-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  170. withLabelSelector(labels, newClaimArray("claim1-1", "uid1-1", "1Gi", "", v1.ClaimPending, nil)),
  171. withLabelSelector(labels, newClaimArray("claim1-1", "uid1-1", "1Gi", "", v1.ClaimPending, nil)),
  172. []string{"Normal FailedBinding"},
  173. noerrors, testSyncClaim,
  174. },
  175. {
  176. // syncClaim does not do anything when binding is delayed
  177. "1-13 - delayed binding",
  178. newVolumeArray("volume1-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classWait),
  179. newVolumeArray("volume1-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classWait),
  180. newClaimArray("claim1-1", "uid1-1", "1Gi", "", v1.ClaimPending, &classWait),
  181. newClaimArray("claim1-1", "uid1-1", "1Gi", "", v1.ClaimPending, &classWait),
  182. []string{"Normal WaitForFirstConsumer"},
  183. noerrors, testSyncClaim,
  184. },
  185. {
  186. // syncClaim binds when binding is delayed but PV is prebound to PVC
  187. "1-14 - successful prebound PV",
  188. newVolumeArray("volume1-1", "1Gi", "", "claim1-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classWait),
  189. newVolumeArray("volume1-1", "1Gi", "uid1-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classWait),
  190. newClaimArray("claim1-1", "uid1-1", "1Gi", "", v1.ClaimPending, &classWait),
  191. newClaimArray("claim1-1", "uid1-1", "1Gi", "volume1-1", v1.ClaimBound, &classWait, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  192. noevents, noerrors, testSyncClaim,
  193. },
  194. {
  195. // syncClaim binds pre-bound PVC only to the volume it points to,
  196. // even if there is smaller volume available
  197. "1-15 - successful prebound PVC",
  198. []*v1.PersistentVolume{
  199. newVolume("volume1-15_1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  200. newVolume("volume1-15_2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  201. },
  202. []*v1.PersistentVolume{
  203. newVolume("volume1-15_1", "10Gi", "uid1-15", "claim1-15", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  204. newVolume("volume1-15_2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  205. },
  206. newClaimArray("claim1-15", "uid1-15", "1Gi", "volume1-15_1", v1.ClaimPending, nil),
  207. withExpectedCapacity("10Gi", newClaimArray("claim1-15", "uid1-15", "1Gi", "volume1-15_1", v1.ClaimBound, nil, pvutil.AnnBindCompleted)),
  208. noevents, noerrors, testSyncClaim,
  209. },
  210. {
  211. // syncClaim does not bind pre-bound PVC to PV with different AccessMode
  212. "1-16 - successful prebound PVC",
  213. // PV has ReadWriteOnce
  214. newVolumeArray("volume1-16", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  215. newVolumeArray("volume1-16", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  216. claimWithAccessMode([]v1.PersistentVolumeAccessMode{v1.ReadWriteMany}, newClaimArray("claim1-16", "uid1-16", "1Gi", "volume1-16", v1.ClaimPending, nil)),
  217. claimWithAccessMode([]v1.PersistentVolumeAccessMode{v1.ReadWriteMany}, newClaimArray("claim1-16", "uid1-16", "1Gi", "volume1-16", v1.ClaimPending, nil)),
  218. noevents, noerrors, testSyncClaim,
  219. },
  220. {
  221. // syncClaim does not bind PVC to non-available PV if it's not pre-bind
  222. "1-17 - skip non-available PV if it's not pre-bind",
  223. []*v1.PersistentVolume{
  224. newVolume("volume1-17-pending", "1Gi", "", "", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty),
  225. newVolume("volume1-17-failed", "1Gi", "", "", v1.VolumeFailed, v1.PersistentVolumeReclaimRetain, classEmpty),
  226. newVolume("volume1-17-released", "1Gi", "", "", v1.VolumeReleased, v1.PersistentVolumeReclaimRetain, classEmpty),
  227. newVolume("volume1-17-empty", "1Gi", "", "", "", v1.PersistentVolumeReclaimRetain, classEmpty),
  228. },
  229. []*v1.PersistentVolume{
  230. newVolume("volume1-17-pending", "1Gi", "", "", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty),
  231. newVolume("volume1-17-failed", "1Gi", "", "", v1.VolumeFailed, v1.PersistentVolumeReclaimRetain, classEmpty),
  232. newVolume("volume1-17-released", "1Gi", "", "", v1.VolumeReleased, v1.PersistentVolumeReclaimRetain, classEmpty),
  233. newVolume("volume1-17-empty", "1Gi", "", "", "", v1.PersistentVolumeReclaimRetain, classEmpty),
  234. },
  235. []*v1.PersistentVolumeClaim{
  236. newClaim("claim1-17", "uid1-17", "1Gi", "", v1.ClaimPending, nil),
  237. },
  238. []*v1.PersistentVolumeClaim{
  239. newClaim("claim1-17", "uid1-17", "1Gi", "", v1.ClaimPending, nil),
  240. },
  241. noevents, noerrors, testSyncClaim,
  242. },
  243. // [Unit test set 2] User asked for a specific PV.
  244. // Test the binding when pv.ClaimRef is already set by controller or
  245. // by user.
  246. {
  247. // syncClaim with claim pre-bound to a PV that does not exist
  248. "2-1 - claim prebound to non-existing volume - noop",
  249. novolumes,
  250. novolumes,
  251. newClaimArray("claim2-1", "uid2-1", "10Gi", "volume2-1", v1.ClaimPending, nil),
  252. newClaimArray("claim2-1", "uid2-1", "10Gi", "volume2-1", v1.ClaimPending, nil),
  253. noevents, noerrors, testSyncClaim,
  254. },
  255. {
  256. // syncClaim with claim pre-bound to a PV that does not exist.
  257. // Check that the claim status is reset to Pending
  258. "2-2 - claim prebound to non-existing volume - reset status",
  259. novolumes,
  260. novolumes,
  261. newClaimArray("claim2-2", "uid2-2", "10Gi", "volume2-2", v1.ClaimBound, nil),
  262. newClaimArray("claim2-2", "uid2-2", "10Gi", "volume2-2", v1.ClaimPending, nil),
  263. noevents, noerrors, testSyncClaim,
  264. },
  265. {
  266. // syncClaim with claim pre-bound to a PV that exists and is
  267. // unbound. Check it gets bound and no pvutil.AnnBoundByController is set.
  268. "2-3 - claim prebound to unbound volume",
  269. newVolumeArray("volume2-3", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  270. newVolumeArray("volume2-3", "1Gi", "uid2-3", "claim2-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  271. newClaimArray("claim2-3", "uid2-3", "1Gi", "volume2-3", v1.ClaimPending, nil),
  272. newClaimArray("claim2-3", "uid2-3", "1Gi", "volume2-3", v1.ClaimBound, nil, pvutil.AnnBindCompleted),
  273. noevents, noerrors, testSyncClaim,
  274. },
  275. {
  276. // claim with claim pre-bound to a PV that is pre-bound to the claim
  277. // by name. Check it gets bound and no pvutil.AnnBoundByController is set.
  278. "2-4 - claim prebound to prebound volume by name",
  279. newVolumeArray("volume2-4", "1Gi", "", "claim2-4", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  280. newVolumeArray("volume2-4", "1Gi", "uid2-4", "claim2-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  281. newClaimArray("claim2-4", "uid2-4", "1Gi", "volume2-4", v1.ClaimPending, nil),
  282. newClaimArray("claim2-4", "uid2-4", "1Gi", "volume2-4", v1.ClaimBound, nil, pvutil.AnnBindCompleted),
  283. noevents, noerrors, testSyncClaim,
  284. },
  285. {
  286. // syncClaim with claim pre-bound to a PV that is pre-bound to the
  287. // claim by UID. Check it gets bound and no pvutil.AnnBoundByController is
  288. // set.
  289. "2-5 - claim prebound to prebound volume by UID",
  290. newVolumeArray("volume2-5", "1Gi", "uid2-5", "claim2-5", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  291. newVolumeArray("volume2-5", "1Gi", "uid2-5", "claim2-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  292. newClaimArray("claim2-5", "uid2-5", "1Gi", "volume2-5", v1.ClaimPending, nil),
  293. newClaimArray("claim2-5", "uid2-5", "1Gi", "volume2-5", v1.ClaimBound, nil, pvutil.AnnBindCompleted),
  294. noevents, noerrors, testSyncClaim,
  295. },
  296. {
  297. // syncClaim with claim pre-bound to a PV that is bound to different
  298. // claim. Check it's reset to Pending.
  299. "2-6 - claim prebound to already bound volume",
  300. newVolumeArray("volume2-6", "1Gi", "uid2-6_1", "claim2-6_1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  301. newVolumeArray("volume2-6", "1Gi", "uid2-6_1", "claim2-6_1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  302. newClaimArray("claim2-6", "uid2-6", "1Gi", "volume2-6", v1.ClaimBound, nil),
  303. newClaimArray("claim2-6", "uid2-6", "1Gi", "volume2-6", v1.ClaimPending, nil),
  304. noevents, noerrors, testSyncClaim,
  305. },
  306. {
  307. // syncClaim with claim bound by controller to a PV that is bound to
  308. // different claim. Check it throws an error.
  309. "2-7 - claim bound by controller to already bound volume",
  310. newVolumeArray("volume2-7", "1Gi", "uid2-7_1", "claim2-7_1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  311. newVolumeArray("volume2-7", "1Gi", "uid2-7_1", "claim2-7_1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  312. newClaimArray("claim2-7", "uid2-7", "1Gi", "volume2-7", v1.ClaimBound, nil, pvutil.AnnBoundByController),
  313. newClaimArray("claim2-7", "uid2-7", "1Gi", "volume2-7", v1.ClaimBound, nil, pvutil.AnnBoundByController),
  314. noevents, noerrors, testSyncClaimError,
  315. },
  316. {
  317. // syncClaim with claim pre-bound to a PV that exists and is
  318. // unbound, but does not match the selector. Check it gets bound
  319. // and no pvutil.AnnBoundByController is set.
  320. "2-8 - claim prebound to unbound volume that does not match the selector",
  321. newVolumeArray("volume2-8", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  322. newVolumeArray("volume2-8", "1Gi", "uid2-8", "claim2-8", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  323. withLabelSelector(labels, newClaimArray("claim2-8", "uid2-8", "1Gi", "volume2-8", v1.ClaimPending, nil)),
  324. withLabelSelector(labels, newClaimArray("claim2-8", "uid2-8", "1Gi", "volume2-8", v1.ClaimBound, nil, pvutil.AnnBindCompleted)),
  325. noevents, noerrors, testSyncClaim,
  326. },
  327. {
  328. // syncClaim with claim pre-bound to a PV that exists and is
  329. // unbound, but its size is smaller than requested.
  330. //Check that the claim status is reset to Pending
  331. "2-9 - claim prebound to unbound volume that size is smaller than requested",
  332. newVolumeArray("volume2-9", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  333. newVolumeArray("volume2-9", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  334. newClaimArray("claim2-9", "uid2-9", "2Gi", "volume2-9", v1.ClaimBound, nil),
  335. newClaimArray("claim2-9", "uid2-9", "2Gi", "volume2-9", v1.ClaimPending, nil),
  336. []string{"Warning VolumeMismatch"}, noerrors, testSyncClaim,
  337. },
  338. {
  339. // syncClaim with claim pre-bound to a PV that exists and is
  340. // unbound, but its class does not match. Check that the claim status is reset to Pending
  341. "2-10 - claim prebound to unbound volume that class is different",
  342. newVolumeArray("volume2-10", "1Gi", "1", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  343. newVolumeArray("volume2-10", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  344. newClaimArray("claim2-10", "uid2-10", "1Gi", "volume2-10", v1.ClaimBound, nil),
  345. newClaimArray("claim2-10", "uid2-10", "1Gi", "volume2-10", v1.ClaimPending, nil),
  346. []string{"Warning VolumeMismatch"}, noerrors, testSyncClaim,
  347. },
  348. // [Unit test set 3] Syncing bound claim
  349. {
  350. // syncClaim with claim bound and its claim.Spec.VolumeName is
  351. // removed. Check it's marked as Lost.
  352. "3-1 - bound claim with missing VolumeName",
  353. novolumes,
  354. novolumes,
  355. newClaimArray("claim3-1", "uid3-1", "10Gi", "", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  356. newClaimArray("claim3-1", "uid3-1", "10Gi", "", v1.ClaimLost, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  357. []string{"Warning ClaimLost"}, noerrors, testSyncClaim,
  358. },
  359. {
  360. // syncClaim with claim bound to non-existing volume. Check it's
  361. // marked as Lost.
  362. "3-2 - bound claim with missing volume",
  363. novolumes,
  364. novolumes,
  365. newClaimArray("claim3-2", "uid3-2", "10Gi", "volume3-2", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  366. newClaimArray("claim3-2", "uid3-2", "10Gi", "volume3-2", v1.ClaimLost, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  367. []string{"Warning ClaimLost"}, noerrors, testSyncClaim,
  368. },
  369. {
  370. // syncClaim with claim bound to unbound volume. Check it's bound.
  371. // Also check that Pending phase is set to Bound
  372. "3-3 - bound claim with unbound volume",
  373. newVolumeArray("volume3-3", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  374. newVolumeArray("volume3-3", "10Gi", "uid3-3", "claim3-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  375. newClaimArray("claim3-3", "uid3-3", "10Gi", "volume3-3", v1.ClaimPending, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  376. newClaimArray("claim3-3", "uid3-3", "10Gi", "volume3-3", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  377. noevents, noerrors, testSyncClaim,
  378. },
  379. {
  380. // syncClaim with claim bound to volume with missing (or different)
  381. // volume.Spec.ClaimRef.UID. Check that the claim is marked as lost.
  382. "3-4 - bound claim with prebound volume",
  383. newVolumeArray("volume3-4", "10Gi", "claim3-4-x", "claim3-4", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  384. newVolumeArray("volume3-4", "10Gi", "claim3-4-x", "claim3-4", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  385. newClaimArray("claim3-4", "uid3-4", "10Gi", "volume3-4", v1.ClaimPending, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  386. newClaimArray("claim3-4", "uid3-4", "10Gi", "volume3-4", v1.ClaimLost, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  387. []string{"Warning ClaimMisbound"}, noerrors, testSyncClaim,
  388. },
  389. {
  390. // syncClaim with claim bound to bound volume. Check that the
  391. // controller does not do anything. Also check that Pending phase is
  392. // set to Bound
  393. "3-5 - bound claim with bound volume",
  394. newVolumeArray("volume3-5", "10Gi", "uid3-5", "claim3-5", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  395. newVolumeArray("volume3-5", "10Gi", "uid3-5", "claim3-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  396. newClaimArray("claim3-5", "uid3-5", "10Gi", "volume3-5", v1.ClaimPending, nil, pvutil.AnnBindCompleted),
  397. newClaimArray("claim3-5", "uid3-5", "10Gi", "volume3-5", v1.ClaimBound, nil, pvutil.AnnBindCompleted),
  398. noevents, noerrors, testSyncClaim,
  399. },
  400. {
  401. // syncClaim with claim bound to a volume that is bound to different
  402. // claim. Check that the claim is marked as lost.
  403. // TODO: test that an event is emitted
  404. "3-6 - bound claim with bound volume",
  405. newVolumeArray("volume3-6", "10Gi", "uid3-6-x", "claim3-6-x", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  406. newVolumeArray("volume3-6", "10Gi", "uid3-6-x", "claim3-6-x", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  407. newClaimArray("claim3-6", "uid3-6", "10Gi", "volume3-6", v1.ClaimPending, nil, pvutil.AnnBindCompleted),
  408. newClaimArray("claim3-6", "uid3-6", "10Gi", "volume3-6", v1.ClaimLost, nil, pvutil.AnnBindCompleted),
  409. []string{"Warning ClaimMisbound"}, noerrors, testSyncClaim,
  410. },
  411. {
  412. // syncClaim with claim bound to unbound volume. Check it's bound
  413. // even if the claim's selector doesn't match the volume. Also
  414. // check that Pending phase is set to Bound
  415. "3-7 - bound claim with unbound volume where selector doesn't match",
  416. newVolumeArray("volume3-3", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  417. newVolumeArray("volume3-3", "10Gi", "uid3-3", "claim3-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  418. withLabelSelector(labels, newClaimArray("claim3-3", "uid3-3", "10Gi", "volume3-3", v1.ClaimPending, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  419. withLabelSelector(labels, newClaimArray("claim3-3", "uid3-3", "10Gi", "volume3-3", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  420. noevents, noerrors, testSyncClaim,
  421. },
  422. // [Unit test set 4] All syncVolume tests.
  423. {
  424. // syncVolume with pending volume. Check it's marked as Available.
  425. "4-1 - pending volume",
  426. newVolumeArray("volume4-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  427. newVolumeArray("volume4-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  428. noclaims,
  429. noclaims,
  430. noevents, noerrors, testSyncVolume,
  431. },
  432. {
  433. // syncVolume with prebound pending volume. Check it's marked as
  434. // Available.
  435. "4-2 - pending prebound volume",
  436. newVolumeArray("volume4-2", "10Gi", "", "claim4-2", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  437. newVolumeArray("volume4-2", "10Gi", "", "claim4-2", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  438. noclaims,
  439. noclaims,
  440. noevents, noerrors, testSyncVolume,
  441. },
  442. {
  443. // syncVolume with volume bound to missing claim.
  444. // Check the volume gets Released
  445. "4-3 - bound volume with missing claim",
  446. newVolumeArray("volume4-3", "10Gi", "uid4-3", "claim4-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  447. newVolumeArray("volume4-3", "10Gi", "uid4-3", "claim4-3", v1.VolumeReleased, v1.PersistentVolumeReclaimRetain, classEmpty),
  448. noclaims,
  449. noclaims,
  450. noevents, noerrors, testSyncVolume,
  451. },
  452. {
  453. // syncVolume with volume bound to claim with different UID.
  454. // Check the volume gets Released.
  455. "4-4 - volume bound to claim with different UID",
  456. newVolumeArray("volume4-4", "10Gi", "uid4-4", "claim4-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  457. newVolumeArray("volume4-4", "10Gi", "uid4-4", "claim4-4", v1.VolumeReleased, v1.PersistentVolumeReclaimRetain, classEmpty),
  458. newClaimArray("claim4-4", "uid4-4-x", "10Gi", "volume4-4", v1.ClaimBound, nil, pvutil.AnnBindCompleted),
  459. newClaimArray("claim4-4", "uid4-4-x", "10Gi", "volume4-4", v1.ClaimBound, nil, pvutil.AnnBindCompleted),
  460. noevents, noerrors, testSyncVolume,
  461. },
  462. {
  463. // syncVolume with volume bound by controller to unbound claim.
  464. // Check syncVolume does not do anything.
  465. "4-5 - volume bound by controller to unbound claim",
  466. newVolumeArray("volume4-5", "10Gi", "uid4-5", "claim4-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  467. newVolumeArray("volume4-5", "10Gi", "uid4-5", "claim4-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  468. newClaimArray("claim4-5", "uid4-5", "10Gi", "", v1.ClaimPending, nil),
  469. newClaimArray("claim4-5", "uid4-5", "10Gi", "", v1.ClaimPending, nil),
  470. noevents, noerrors, testSyncVolume,
  471. },
  472. {
  473. // syncVolume with volume bound by user to unbound claim.
  474. // Check syncVolume does not do anything.
  475. "4-5 - volume bound by user to bound claim",
  476. newVolumeArray("volume4-5", "10Gi", "uid4-5", "claim4-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  477. newVolumeArray("volume4-5", "10Gi", "uid4-5", "claim4-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  478. newClaimArray("claim4-5", "uid4-5", "10Gi", "", v1.ClaimPending, nil),
  479. newClaimArray("claim4-5", "uid4-5", "10Gi", "", v1.ClaimPending, nil),
  480. noevents, noerrors, testSyncVolume,
  481. },
  482. {
  483. // syncVolume with volume bound to bound claim.
  484. // Check that the volume is marked as Bound.
  485. "4-6 - volume bound by to bound claim",
  486. newVolumeArray("volume4-6", "10Gi", "uid4-6", "claim4-6", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  487. newVolumeArray("volume4-6", "10Gi", "uid4-6", "claim4-6", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  488. newClaimArray("claim4-6", "uid4-6", "10Gi", "volume4-6", v1.ClaimBound, nil),
  489. newClaimArray("claim4-6", "uid4-6", "10Gi", "volume4-6", v1.ClaimBound, nil),
  490. noevents, noerrors, testSyncVolume,
  491. },
  492. {
  493. // syncVolume with volume bound by controller to claim bound to
  494. // another volume. Check that the volume is rolled back.
  495. "4-7 - volume bound by controller to claim bound somewhere else",
  496. newVolumeArray("volume4-7", "10Gi", "uid4-7", "claim4-7", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  497. newVolumeArray("volume4-7", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  498. newClaimArray("claim4-7", "uid4-7", "10Gi", "volume4-7-x", v1.ClaimBound, nil),
  499. newClaimArray("claim4-7", "uid4-7", "10Gi", "volume4-7-x", v1.ClaimBound, nil),
  500. noevents, noerrors, testSyncVolume,
  501. },
  502. {
  503. // syncVolume with volume bound by user to claim bound to
  504. // another volume. Check that the volume is marked as Available
  505. // and its UID is reset.
  506. "4-8 - volume bound by user to claim bound somewhere else",
  507. newVolumeArray("volume4-8", "10Gi", "uid4-8", "claim4-8", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  508. newVolumeArray("volume4-8", "10Gi", "", "claim4-8", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  509. newClaimArray("claim4-8", "uid4-8", "10Gi", "volume4-8-x", v1.ClaimBound, nil),
  510. newClaimArray("claim4-8", "uid4-8", "10Gi", "volume4-8-x", v1.ClaimBound, nil),
  511. noevents, noerrors, testSyncVolume,
  512. },
  513. // PVC with class
  514. {
  515. // syncVolume binds a claim to requested class even if there is a
  516. // smaller PV available
  517. "13-1 - binding to class",
  518. []*v1.PersistentVolume{
  519. newVolume("volume13-1-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  520. newVolume("volume13-1-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  521. },
  522. []*v1.PersistentVolume{
  523. newVolume("volume13-1-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  524. newVolume("volume13-1-2", "10Gi", "uid13-1", "claim13-1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classGold, pvutil.AnnBoundByController),
  525. },
  526. newClaimArray("claim13-1", "uid13-1", "1Gi", "", v1.ClaimPending, &classGold),
  527. withExpectedCapacity("10Gi", newClaimArray("claim13-1", "uid13-1", "1Gi", "volume13-1-2", v1.ClaimBound, &classGold, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  528. noevents, noerrors, testSyncClaim,
  529. },
  530. {
  531. // syncVolume binds a claim without a class even if there is a
  532. // smaller PV with a class available
  533. "13-2 - binding without a class",
  534. []*v1.PersistentVolume{
  535. newVolume("volume13-2-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  536. newVolume("volume13-2-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  537. },
  538. []*v1.PersistentVolume{
  539. newVolume("volume13-2-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  540. newVolume("volume13-2-2", "10Gi", "uid13-2", "claim13-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  541. },
  542. newClaimArray("claim13-2", "uid13-2", "1Gi", "", v1.ClaimPending, nil),
  543. withExpectedCapacity("10Gi", newClaimArray("claim13-2", "uid13-2", "1Gi", "volume13-2-2", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  544. noevents, noerrors, testSyncClaim,
  545. },
  546. {
  547. // syncVolume binds a claim with given class even if there is a
  548. // smaller PV with different class available
  549. "13-3 - binding to specific a class",
  550. []*v1.PersistentVolume{
  551. newVolume("volume13-3-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classSilver),
  552. newVolume("volume13-3-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  553. },
  554. []*v1.PersistentVolume{
  555. newVolume("volume13-3-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classSilver),
  556. newVolume("volume13-3-2", "10Gi", "uid13-3", "claim13-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classGold, pvutil.AnnBoundByController),
  557. },
  558. newClaimArray("claim13-3", "uid13-3", "1Gi", "", v1.ClaimPending, &classGold),
  559. withExpectedCapacity("10Gi", newClaimArray("claim13-3", "uid13-3", "1Gi", "volume13-3-2", v1.ClaimBound, &classGold, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  560. noevents, noerrors, testSyncClaim,
  561. },
  562. {
  563. // syncVolume binds claim requesting class "" to claim to PV with
  564. // class=""
  565. "13-4 - empty class",
  566. newVolumeArray("volume13-4", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  567. newVolumeArray("volume13-4", "1Gi", "uid13-4", "claim13-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  568. newClaimArray("claim13-4", "uid13-4", "1Gi", "", v1.ClaimPending, &classEmpty),
  569. newClaimArray("claim13-4", "uid13-4", "1Gi", "volume13-4", v1.ClaimBound, &classEmpty, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  570. noevents, noerrors, testSyncClaim,
  571. },
  572. {
  573. // syncVolume binds claim requesting class nil to claim to PV with
  574. // class = ""
  575. "13-5 - nil class",
  576. newVolumeArray("volume13-5", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  577. newVolumeArray("volume13-5", "1Gi", "uid13-5", "claim13-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  578. newClaimArray("claim13-5", "uid13-5", "1Gi", "", v1.ClaimPending, nil),
  579. newClaimArray("claim13-5", "uid13-5", "1Gi", "volume13-5", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  580. noevents, noerrors, testSyncClaim,
  581. },
  582. }
  583. runSyncTests(t, tests, []*storage.StorageClass{
  584. {
  585. ObjectMeta: metav1.ObjectMeta{Name: classWait},
  586. VolumeBindingMode: &modeWait,
  587. },
  588. }, []*v1.Pod{})
  589. }
  590. func TestSyncBlockVolumeDisabled(t *testing.T) {
  591. modeBlock := v1.PersistentVolumeBlock
  592. modeFile := v1.PersistentVolumeFilesystem
  593. // All of these should bind as feature set is not enabled for BlockVolume
  594. // meaning volumeMode will be ignored and dropped
  595. tests := []controllerTest{
  596. {
  597. // syncVolume binds a requested block claim to a block volume
  598. "14-1 - binding to volumeMode block",
  599. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  600. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-1", "10Gi", "uid14-1", "claim14-1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  601. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-1", "uid14-1", "10Gi", "", v1.ClaimPending, nil)),
  602. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-1", "uid14-1", "10Gi", "volume14-1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  603. noevents, noerrors, testSyncClaim,
  604. },
  605. {
  606. // syncVolume binds a requested filesystem claim to a filesystem volume
  607. "14-2 - binding to volumeMode filesystem",
  608. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  609. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-2", "10Gi", "uid14-2", "claim14-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  610. withClaimVolumeMode(&modeFile, newClaimArray("claim14-2", "uid14-2", "10Gi", "", v1.ClaimPending, nil)),
  611. withClaimVolumeMode(&modeFile, newClaimArray("claim14-2", "uid14-2", "10Gi", "volume14-2", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  612. noevents, noerrors, testSyncClaim,
  613. },
  614. {
  615. // syncVolume binds an unspecified volumemode for claim to a specified filesystem volume
  616. "14-3 - binding to volumeMode filesystem using default for claim",
  617. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-3", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  618. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-3", "10Gi", "uid14-3", "claim14-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  619. withClaimVolumeMode(nil, newClaimArray("claim14-3", "uid14-3", "10Gi", "", v1.ClaimPending, nil)),
  620. withClaimVolumeMode(nil, newClaimArray("claim14-3", "uid14-3", "10Gi", "volume14-3", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  621. noevents, noerrors, testSyncClaim,
  622. },
  623. {
  624. // syncVolume binds a requested filesystem claim to an unspecified volumeMode for volume
  625. "14-4 - binding to unspecified volumeMode using requested filesystem for claim",
  626. withVolumeVolumeMode(nil, newVolumeArray("volume14-4", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  627. withVolumeVolumeMode(nil, newVolumeArray("volume14-4", "10Gi", "uid14-4", "claim14-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  628. withClaimVolumeMode(&modeFile, newClaimArray("claim14-4", "uid14-4", "10Gi", "", v1.ClaimPending, nil)),
  629. withClaimVolumeMode(&modeFile, newClaimArray("claim14-4", "uid14-4", "10Gi", "volume14-4", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  630. noevents, noerrors, testSyncClaim,
  631. },
  632. {
  633. // syncVolume binds a requested filesystem claim to an unspecified volumeMode for volume
  634. "14-5 - binding different volumeModes should be ignored",
  635. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-5", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  636. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-5", "10Gi", "uid14-5", "claim14-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  637. withClaimVolumeMode(&modeFile, newClaimArray("claim14-5", "uid14-5", "10Gi", "", v1.ClaimPending, nil)),
  638. withClaimVolumeMode(&modeFile, newClaimArray("claim14-5", "uid14-5", "10Gi", "volume14-5", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  639. noevents, noerrors, testSyncClaim,
  640. },
  641. }
  642. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, false)()
  643. runSyncTests(t, tests, []*storage.StorageClass{
  644. {
  645. ObjectMeta: metav1.ObjectMeta{Name: classWait},
  646. VolumeBindingMode: &modeWait,
  647. },
  648. }, []*v1.Pod{})
  649. }
  650. func TestSyncBlockVolume(t *testing.T) {
  651. modeBlock := v1.PersistentVolumeBlock
  652. modeFile := v1.PersistentVolumeFilesystem
  653. // Tests assume defaulting, so feature enabled will never have nil volumeMode
  654. tests := []controllerTest{
  655. // PVC with VolumeMode
  656. {
  657. // syncVolume binds a requested block claim to a block volume
  658. "14-1 - binding to volumeMode block",
  659. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  660. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-1", "10Gi", "uid14-1", "claim14-1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  661. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-1", "uid14-1", "10Gi", "", v1.ClaimPending, nil)),
  662. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-1", "uid14-1", "10Gi", "volume14-1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  663. noevents, noerrors, testSyncClaim,
  664. },
  665. {
  666. // syncVolume binds a requested filesystem claim to a filesystem volume
  667. "14-2 - binding to volumeMode filesystem",
  668. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  669. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-2", "10Gi", "uid14-2", "claim14-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  670. withClaimVolumeMode(&modeFile, newClaimArray("claim14-2", "uid14-2", "10Gi", "", v1.ClaimPending, nil)),
  671. withClaimVolumeMode(&modeFile, newClaimArray("claim14-2", "uid14-2", "10Gi", "volume14-2", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  672. noevents, noerrors, testSyncClaim,
  673. },
  674. {
  675. // failed syncVolume do not bind to an unspecified volumemode for claim to a specified filesystem volume
  676. "14-3 - do not bind pv volumeMode filesystem and pvc volumeMode block",
  677. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-3", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  678. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-3", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  679. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-3", "uid14-3", "10Gi", "", v1.ClaimPending, nil)),
  680. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-3", "uid14-3", "10Gi", "", v1.ClaimPending, nil)),
  681. []string{"Normal FailedBinding"},
  682. noerrors, testSyncClaim,
  683. },
  684. {
  685. // failed syncVolume do not bind a requested filesystem claim to an unspecified volumeMode for volume
  686. "14-4 - do not bind pv volumeMode block and pvc volumeMode filesystem",
  687. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-4", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  688. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-4", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  689. withClaimVolumeMode(&modeFile, newClaimArray("claim14-4", "uid14-4", "10Gi", "", v1.ClaimPending, nil)),
  690. withClaimVolumeMode(&modeFile, newClaimArray("claim14-4", "uid14-4", "10Gi", "", v1.ClaimPending, nil)),
  691. []string{"Normal FailedBinding"},
  692. noerrors, testSyncClaim,
  693. },
  694. {
  695. // failed syncVolume do not bind when matching class but not matching volumeModes
  696. "14-5 - do not bind when matching class but not volumeMode",
  697. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-5", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold)),
  698. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-5", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold)),
  699. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-5", "uid14-5", "10Gi", "", v1.ClaimPending, &classGold)),
  700. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-5", "uid14-5", "10Gi", "", v1.ClaimPending, &classGold)),
  701. []string{"Warning ProvisioningFailed"},
  702. noerrors, testSyncClaim,
  703. },
  704. {
  705. // failed syncVolume do not bind when matching volumeModes but class does not match
  706. "14-5-1 - do not bind when matching volumeModes but class does not match",
  707. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-5-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold)),
  708. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-5-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold)),
  709. withClaimVolumeMode(&modeFile, newClaimArray("claim14-5-1", "uid14-5-1", "10Gi", "", v1.ClaimPending, &classSilver)),
  710. withClaimVolumeMode(&modeFile, newClaimArray("claim14-5-1", "uid14-5-1", "10Gi", "", v1.ClaimPending, &classSilver)),
  711. []string{"Warning ProvisioningFailed"},
  712. noerrors, testSyncClaim,
  713. },
  714. {
  715. // failed syncVolume do not bind when pvc is prebound to pv with matching volumeModes but class does not match
  716. "14-5-2 - do not bind when pvc is prebound to pv with matching volumeModes but class does not match",
  717. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-5-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold)),
  718. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-5-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold)),
  719. withClaimVolumeMode(&modeFile, newClaimArray("claim14-5-2", "uid14-5-2", "10Gi", "volume14-5-2", v1.ClaimPending, &classSilver)),
  720. withClaimVolumeMode(&modeFile, newClaimArray("claim14-5-2", "uid14-5-2", "10Gi", "volume14-5-2", v1.ClaimPending, &classSilver)),
  721. []string{"Warning VolumeMismatch"},
  722. noerrors, testSyncClaim,
  723. },
  724. {
  725. // syncVolume bind when pv is prebound and volumeModes match
  726. "14-7 - bind when pv volume is prebound and volumeModes match",
  727. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-7", "10Gi", "", "claim14-7", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  728. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-7", "10Gi", "uid14-7", "claim14-7", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty)),
  729. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-7", "uid14-7", "10Gi", "", v1.ClaimPending, nil)),
  730. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-7", "uid14-7", "10Gi", "volume14-7", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  731. noevents, noerrors, testSyncClaim,
  732. },
  733. {
  734. // failed syncVolume do not bind when pvc is prebound to pv with mismatching volumeModes
  735. "14-8 - do not bind when pvc is prebound to pv with mismatching volumeModes",
  736. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-8", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  737. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-8", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  738. withClaimVolumeMode(&modeFile, newClaimArray("claim14-8", "uid14-8", "10Gi", "volume14-8", v1.ClaimPending, nil)),
  739. withClaimVolumeMode(&modeFile, newClaimArray("claim14-8", "uid14-8", "10Gi", "volume14-8", v1.ClaimPending, nil)),
  740. []string{"Warning VolumeMismatch"},
  741. noerrors, testSyncClaim,
  742. },
  743. {
  744. // failed syncVolume do not bind when pvc is prebound to pv with mismatching volumeModes
  745. "14-8-1 - do not bind when pvc is prebound to pv with mismatching volumeModes",
  746. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-8-1", "10Gi", "", "claim14-8-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  747. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-8-1", "10Gi", "", "claim14-8-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  748. withClaimVolumeMode(&modeFile, newClaimArray("claim14-8-1", "uid14-8-1", "10Gi", "", v1.ClaimPending, nil)),
  749. withClaimVolumeMode(&modeFile, newClaimArray("claim14-8-1", "uid14-8-1", "10Gi", "", v1.ClaimPending, nil)),
  750. []string{"Normal FailedBinding"},
  751. noerrors, testSyncClaim,
  752. },
  753. {
  754. // syncVolume binds when pvc is prebound to pv with matching volumeModes block
  755. "14-9 - bind when pvc is prebound to pv with matching volumeModes block",
  756. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-9", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  757. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-9", "10Gi", "uid14-9", "claim14-9", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  758. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-9", "uid14-9", "10Gi", "volume14-9", v1.ClaimPending, nil)),
  759. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-9", "uid14-9", "10Gi", "volume14-9", v1.ClaimBound, nil, pvutil.AnnBindCompleted)),
  760. noevents, noerrors, testSyncClaim,
  761. },
  762. {
  763. // syncVolume binds when pv is prebound to pvc with matching volumeModes block
  764. "14-10 - bind when pv is prebound to pvc with matching volumeModes block",
  765. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-10", "10Gi", "", "claim14-10", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  766. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-10", "10Gi", "uid14-10", "claim14-10", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty)),
  767. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-10", "uid14-10", "10Gi", "", v1.ClaimPending, nil)),
  768. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-10", "uid14-10", "10Gi", "volume14-10", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  769. noevents, noerrors, testSyncClaim,
  770. },
  771. {
  772. // syncVolume binds when pvc is prebound to pv with matching volumeModes filesystem
  773. "14-11 - bind when pvc is prebound to pv with matching volumeModes filesystem",
  774. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-11", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  775. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-11", "10Gi", "uid14-11", "claim14-11", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  776. withClaimVolumeMode(&modeFile, newClaimArray("claim14-11", "uid14-11", "10Gi", "volume14-11", v1.ClaimPending, nil)),
  777. withClaimVolumeMode(&modeFile, newClaimArray("claim14-11", "uid14-11", "10Gi", "volume14-11", v1.ClaimBound, nil, pvutil.AnnBindCompleted)),
  778. noevents, noerrors, testSyncClaim,
  779. },
  780. {
  781. // syncVolume binds when pv is prebound to pvc with matching volumeModes filesystem
  782. "14-12 - bind when pv is prebound to pvc with matching volumeModes filesystem",
  783. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-12", "10Gi", "", "claim14-12", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  784. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-12", "10Gi", "uid14-12", "claim14-12", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty)),
  785. withClaimVolumeMode(&modeFile, newClaimArray("claim14-12", "uid14-12", "10Gi", "", v1.ClaimPending, nil)),
  786. withClaimVolumeMode(&modeFile, newClaimArray("claim14-12", "uid14-12", "10Gi", "volume14-12", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  787. noevents, noerrors, testSyncClaim,
  788. },
  789. {
  790. // syncVolume output warning when pv is prebound to pvc with mismatching volumeMode
  791. "14-13 - output warning when pv is prebound to pvc with different volumeModes",
  792. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-13", "10Gi", "uid14-13", "claim14-13", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  793. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-13", "10Gi", "uid14-13", "claim14-13", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  794. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-13", "uid14-13", "10Gi", "", v1.ClaimPending, nil)),
  795. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-13", "uid14-13", "10Gi", "", v1.ClaimPending, nil)),
  796. []string{"Warning VolumeMismatch"},
  797. noerrors, testSyncVolume,
  798. },
  799. {
  800. // syncVolume output warning when pv is prebound to pvc with mismatching volumeMode
  801. "14-13-1 - output warning when pv is prebound to pvc with different volumeModes",
  802. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-13-1", "10Gi", "uid14-13-1", "claim14-13-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  803. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-13-1", "10Gi", "uid14-13-1", "claim14-13-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  804. withClaimVolumeMode(&modeFile, newClaimArray("claim14-13-1", "uid14-13-1", "10Gi", "", v1.ClaimPending, nil)),
  805. withClaimVolumeMode(&modeFile, newClaimArray("claim14-13-1", "uid14-13-1", "10Gi", "", v1.ClaimPending, nil)),
  806. []string{"Warning VolumeMismatch"},
  807. noerrors, testSyncVolume,
  808. },
  809. {
  810. // syncVolume waits for synClaim without warning when pv is prebound to pvc with matching volumeMode block
  811. "14-14 - wait for synClaim without warning when pv is prebound to pvc with matching volumeModes block",
  812. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-14", "10Gi", "uid14-14", "claim14-14", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  813. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-14", "10Gi", "uid14-14", "claim14-14", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  814. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-14", "uid14-14", "10Gi", "", v1.ClaimPending, nil)),
  815. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-14", "uid14-14", "10Gi", "", v1.ClaimPending, nil)),
  816. noevents, noerrors, testSyncVolume,
  817. },
  818. {
  819. // syncVolume waits for synClaim without warning when pv is prebound to pvc with matching volumeMode file
  820. "14-14-1 - wait for synClaim without warning when pv is prebound to pvc with matching volumeModes file",
  821. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-14-1", "10Gi", "uid14-14-1", "claim14-14-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  822. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-14-1", "10Gi", "uid14-14-1", "claim14-14-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  823. withClaimVolumeMode(&modeFile, newClaimArray("claim14-14-1", "uid14-14-1", "10Gi", "", v1.ClaimPending, nil)),
  824. withClaimVolumeMode(&modeFile, newClaimArray("claim14-14-1", "uid14-14-1", "10Gi", "", v1.ClaimPending, nil)),
  825. noevents, noerrors, testSyncVolume,
  826. },
  827. }
  828. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, true)()
  829. runSyncTests(t, tests, []*storage.StorageClass{}, []*v1.Pod{})
  830. }
  831. // Test multiple calls to syncClaim/syncVolume and periodic sync of all
  832. // volume/claims. The test follows this pattern:
  833. // 0. Load the controller with initial data.
  834. // 1. Call controllerTest.testCall() once as in TestSync()
  835. // 2. For all volumes/claims changed by previous syncVolume/syncClaim calls,
  836. // call appropriate syncVolume/syncClaim (simulating "volume/claim changed"
  837. // events). Go to 2. if these calls change anything.
  838. // 3. When all changes are processed and no new changes were made, call
  839. // syncVolume/syncClaim on all volumes/claims (simulating "periodic sync").
  840. // 4. If some changes were done by step 3., go to 2. (simulation of
  841. // "volume/claim updated" events, eventually performing step 3. again)
  842. // 5. When 3. does not do any changes, finish the tests and compare final set
  843. // of volumes/claims with expected claims/volumes and report differences.
  844. // Some limit of calls in enforced to prevent endless loops.
  845. func TestMultiSync(t *testing.T) {
  846. tests := []controllerTest{
  847. // Test simple binding
  848. {
  849. // syncClaim binds to a matching unbound volume.
  850. "10-1 - successful bind",
  851. newVolumeArray("volume10-1", "1Gi", "", "", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty),
  852. newVolumeArray("volume10-1", "1Gi", "uid10-1", "claim10-1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  853. newClaimArray("claim10-1", "uid10-1", "1Gi", "", v1.ClaimPending, nil),
  854. newClaimArray("claim10-1", "uid10-1", "1Gi", "volume10-1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  855. noevents, noerrors, testSyncClaim,
  856. },
  857. {
  858. // Two controllers bound two PVs to single claim. Test one of them
  859. // wins and the second rolls back.
  860. "10-2 - bind PV race",
  861. []*v1.PersistentVolume{
  862. newVolume("volume10-2-1", "1Gi", "uid10-2", "claim10-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  863. newVolume("volume10-2-2", "1Gi", "uid10-2", "claim10-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  864. },
  865. []*v1.PersistentVolume{
  866. newVolume("volume10-2-1", "1Gi", "uid10-2", "claim10-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  867. newVolume("volume10-2-2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  868. },
  869. newClaimArray("claim10-2", "uid10-2", "1Gi", "volume10-2-1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  870. newClaimArray("claim10-2", "uid10-2", "1Gi", "volume10-2-1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  871. noevents, noerrors, testSyncClaim,
  872. },
  873. }
  874. runMultisyncTests(t, tests, []*storage.StorageClass{}, "")
  875. }