binder_test.go 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. v1 "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. {
  244. // syncClaim that scheduled to a selected node
  245. "1-18 - successful pre-bound PV to PVC provisioning",
  246. newVolumeArray("volume1-18", "1Gi", "uid1-18", "claim1-18", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classWait),
  247. newVolumeArray("volume1-18", "1Gi", "uid1-18", "claim1-18", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classWait),
  248. claimWithAnnotation(pvutil.AnnSelectedNode, "node1",
  249. newClaimArray("claim1-18", "uid1-18", "1Gi", "", v1.ClaimPending, &classWait)),
  250. claimWithAnnotation(pvutil.AnnSelectedNode, "node1",
  251. newClaimArray("claim1-18", "uid1-18", "1Gi", "volume1-18", v1.ClaimBound, &classWait, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  252. noevents, noerrors, testSyncClaim,
  253. },
  254. // [Unit test set 2] User asked for a specific PV.
  255. // Test the binding when pv.ClaimRef is already set by controller or
  256. // by user.
  257. {
  258. // syncClaim with claim pre-bound to a PV that does not exist
  259. "2-1 - claim prebound to non-existing volume - noop",
  260. novolumes,
  261. novolumes,
  262. newClaimArray("claim2-1", "uid2-1", "10Gi", "volume2-1", v1.ClaimPending, nil),
  263. newClaimArray("claim2-1", "uid2-1", "10Gi", "volume2-1", v1.ClaimPending, nil),
  264. noevents, noerrors, testSyncClaim,
  265. },
  266. {
  267. // syncClaim with claim pre-bound to a PV that does not exist.
  268. // Check that the claim status is reset to Pending
  269. "2-2 - claim prebound to non-existing volume - reset status",
  270. novolumes,
  271. novolumes,
  272. newClaimArray("claim2-2", "uid2-2", "10Gi", "volume2-2", v1.ClaimBound, nil),
  273. newClaimArray("claim2-2", "uid2-2", "10Gi", "volume2-2", v1.ClaimPending, nil),
  274. noevents, noerrors, testSyncClaim,
  275. },
  276. {
  277. // syncClaim with claim pre-bound to a PV that exists and is
  278. // unbound. Check it gets bound and no pvutil.AnnBoundByController is set.
  279. "2-3 - claim prebound to unbound volume",
  280. newVolumeArray("volume2-3", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  281. newVolumeArray("volume2-3", "1Gi", "uid2-3", "claim2-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  282. newClaimArray("claim2-3", "uid2-3", "1Gi", "volume2-3", v1.ClaimPending, nil),
  283. newClaimArray("claim2-3", "uid2-3", "1Gi", "volume2-3", v1.ClaimBound, nil, pvutil.AnnBindCompleted),
  284. noevents, noerrors, testSyncClaim,
  285. },
  286. {
  287. // claim with claim pre-bound to a PV that is pre-bound to the claim
  288. // by name. Check it gets bound and no pvutil.AnnBoundByController is set.
  289. "2-4 - claim prebound to prebound volume by name",
  290. newVolumeArray("volume2-4", "1Gi", "", "claim2-4", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  291. newVolumeArray("volume2-4", "1Gi", "uid2-4", "claim2-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  292. newClaimArray("claim2-4", "uid2-4", "1Gi", "volume2-4", v1.ClaimPending, nil),
  293. newClaimArray("claim2-4", "uid2-4", "1Gi", "volume2-4", v1.ClaimBound, nil, pvutil.AnnBindCompleted),
  294. noevents, noerrors, testSyncClaim,
  295. },
  296. {
  297. // syncClaim with claim pre-bound to a PV that is pre-bound to the
  298. // claim by UID. Check it gets bound and no pvutil.AnnBoundByController is
  299. // set.
  300. "2-5 - claim prebound to prebound volume by UID",
  301. newVolumeArray("volume2-5", "1Gi", "uid2-5", "claim2-5", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  302. newVolumeArray("volume2-5", "1Gi", "uid2-5", "claim2-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  303. newClaimArray("claim2-5", "uid2-5", "1Gi", "volume2-5", v1.ClaimPending, nil),
  304. newClaimArray("claim2-5", "uid2-5", "1Gi", "volume2-5", v1.ClaimBound, nil, pvutil.AnnBindCompleted),
  305. noevents, noerrors, testSyncClaim,
  306. },
  307. {
  308. // syncClaim with claim pre-bound to a PV that is bound to different
  309. // claim. Check it's reset to Pending.
  310. "2-6 - claim prebound to already bound volume",
  311. newVolumeArray("volume2-6", "1Gi", "uid2-6_1", "claim2-6_1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  312. newVolumeArray("volume2-6", "1Gi", "uid2-6_1", "claim2-6_1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  313. newClaimArray("claim2-6", "uid2-6", "1Gi", "volume2-6", v1.ClaimBound, nil),
  314. newClaimArray("claim2-6", "uid2-6", "1Gi", "volume2-6", v1.ClaimPending, nil),
  315. noevents, noerrors, testSyncClaim,
  316. },
  317. {
  318. // syncClaim with claim bound by controller to a PV that is bound to
  319. // different claim. Check it throws an error.
  320. "2-7 - claim bound by controller to already bound volume",
  321. newVolumeArray("volume2-7", "1Gi", "uid2-7_1", "claim2-7_1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  322. newVolumeArray("volume2-7", "1Gi", "uid2-7_1", "claim2-7_1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  323. newClaimArray("claim2-7", "uid2-7", "1Gi", "volume2-7", v1.ClaimBound, nil, pvutil.AnnBoundByController),
  324. newClaimArray("claim2-7", "uid2-7", "1Gi", "volume2-7", v1.ClaimBound, nil, pvutil.AnnBoundByController),
  325. noevents, noerrors, testSyncClaimError,
  326. },
  327. {
  328. // syncClaim with claim pre-bound to a PV that exists and is
  329. // unbound, but does not match the selector. Check it gets bound
  330. // and no pvutil.AnnBoundByController is set.
  331. "2-8 - claim prebound to unbound volume that does not match the selector",
  332. newVolumeArray("volume2-8", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  333. newVolumeArray("volume2-8", "1Gi", "uid2-8", "claim2-8", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  334. withLabelSelector(labels, newClaimArray("claim2-8", "uid2-8", "1Gi", "volume2-8", v1.ClaimPending, nil)),
  335. withLabelSelector(labels, newClaimArray("claim2-8", "uid2-8", "1Gi", "volume2-8", v1.ClaimBound, nil, pvutil.AnnBindCompleted)),
  336. noevents, noerrors, testSyncClaim,
  337. },
  338. {
  339. // syncClaim with claim pre-bound to a PV that exists and is
  340. // unbound, but its size is smaller than requested.
  341. //Check that the claim status is reset to Pending
  342. "2-9 - claim prebound to unbound volume that size is smaller than requested",
  343. newVolumeArray("volume2-9", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  344. newVolumeArray("volume2-9", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  345. newClaimArray("claim2-9", "uid2-9", "2Gi", "volume2-9", v1.ClaimBound, nil),
  346. newClaimArray("claim2-9", "uid2-9", "2Gi", "volume2-9", v1.ClaimPending, nil),
  347. []string{"Warning VolumeMismatch"}, noerrors, testSyncClaim,
  348. },
  349. {
  350. // syncClaim with claim pre-bound to a PV that exists and is
  351. // unbound, but its class does not match. Check that the claim status is reset to Pending
  352. "2-10 - claim prebound to unbound volume that class is different",
  353. newVolumeArray("volume2-10", "1Gi", "1", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  354. newVolumeArray("volume2-10", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  355. newClaimArray("claim2-10", "uid2-10", "1Gi", "volume2-10", v1.ClaimBound, nil),
  356. newClaimArray("claim2-10", "uid2-10", "1Gi", "volume2-10", v1.ClaimPending, nil),
  357. []string{"Warning VolumeMismatch"}, noerrors, testSyncClaim,
  358. },
  359. // [Unit test set 3] Syncing bound claim
  360. {
  361. // syncClaim with claim bound and its claim.Spec.VolumeName is
  362. // removed. Check it's marked as Lost.
  363. "3-1 - bound claim with missing VolumeName",
  364. novolumes,
  365. novolumes,
  366. newClaimArray("claim3-1", "uid3-1", "10Gi", "", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  367. newClaimArray("claim3-1", "uid3-1", "10Gi", "", v1.ClaimLost, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  368. []string{"Warning ClaimLost"}, noerrors, testSyncClaim,
  369. },
  370. {
  371. // syncClaim with claim bound to non-existing volume. Check it's
  372. // marked as Lost.
  373. "3-2 - bound claim with missing volume",
  374. novolumes,
  375. novolumes,
  376. newClaimArray("claim3-2", "uid3-2", "10Gi", "volume3-2", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  377. newClaimArray("claim3-2", "uid3-2", "10Gi", "volume3-2", v1.ClaimLost, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  378. []string{"Warning ClaimLost"}, noerrors, testSyncClaim,
  379. },
  380. {
  381. // syncClaim with claim bound to unbound volume. Check it's bound.
  382. // Also check that Pending phase is set to Bound
  383. "3-3 - bound claim with unbound volume",
  384. newVolumeArray("volume3-3", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  385. newVolumeArray("volume3-3", "10Gi", "uid3-3", "claim3-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  386. newClaimArray("claim3-3", "uid3-3", "10Gi", "volume3-3", v1.ClaimPending, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  387. newClaimArray("claim3-3", "uid3-3", "10Gi", "volume3-3", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  388. noevents, noerrors, testSyncClaim,
  389. },
  390. {
  391. // syncClaim with claim bound to volume with missing (or different)
  392. // volume.Spec.ClaimRef.UID. Check that the claim is marked as lost.
  393. "3-4 - bound claim with prebound volume",
  394. newVolumeArray("volume3-4", "10Gi", "claim3-4-x", "claim3-4", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  395. newVolumeArray("volume3-4", "10Gi", "claim3-4-x", "claim3-4", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  396. newClaimArray("claim3-4", "uid3-4", "10Gi", "volume3-4", v1.ClaimPending, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  397. newClaimArray("claim3-4", "uid3-4", "10Gi", "volume3-4", v1.ClaimLost, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  398. []string{"Warning ClaimMisbound"}, noerrors, testSyncClaim,
  399. },
  400. {
  401. // syncClaim with claim bound to bound volume. Check that the
  402. // controller does not do anything. Also check that Pending phase is
  403. // set to Bound
  404. "3-5 - bound claim with bound volume",
  405. newVolumeArray("volume3-5", "10Gi", "uid3-5", "claim3-5", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  406. newVolumeArray("volume3-5", "10Gi", "uid3-5", "claim3-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  407. newClaimArray("claim3-5", "uid3-5", "10Gi", "volume3-5", v1.ClaimPending, nil, pvutil.AnnBindCompleted),
  408. newClaimArray("claim3-5", "uid3-5", "10Gi", "volume3-5", v1.ClaimBound, nil, pvutil.AnnBindCompleted),
  409. noevents, noerrors, testSyncClaim,
  410. },
  411. {
  412. // syncClaim with claim bound to a volume that is bound to different
  413. // claim. Check that the claim is marked as lost.
  414. // TODO: test that an event is emitted
  415. "3-6 - bound claim with bound volume",
  416. newVolumeArray("volume3-6", "10Gi", "uid3-6-x", "claim3-6-x", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  417. newVolumeArray("volume3-6", "10Gi", "uid3-6-x", "claim3-6-x", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  418. newClaimArray("claim3-6", "uid3-6", "10Gi", "volume3-6", v1.ClaimPending, nil, pvutil.AnnBindCompleted),
  419. newClaimArray("claim3-6", "uid3-6", "10Gi", "volume3-6", v1.ClaimLost, nil, pvutil.AnnBindCompleted),
  420. []string{"Warning ClaimMisbound"}, noerrors, testSyncClaim,
  421. },
  422. {
  423. // syncClaim with claim bound to unbound volume. Check it's bound
  424. // even if the claim's selector doesn't match the volume. Also
  425. // check that Pending phase is set to Bound
  426. "3-7 - bound claim with unbound volume where selector doesn't match",
  427. newVolumeArray("volume3-3", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  428. newVolumeArray("volume3-3", "10Gi", "uid3-3", "claim3-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  429. withLabelSelector(labels, newClaimArray("claim3-3", "uid3-3", "10Gi", "volume3-3", v1.ClaimPending, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  430. withLabelSelector(labels, newClaimArray("claim3-3", "uid3-3", "10Gi", "volume3-3", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  431. noevents, noerrors, testSyncClaim,
  432. },
  433. // [Unit test set 4] All syncVolume tests.
  434. {
  435. // syncVolume with pending volume. Check it's marked as Available.
  436. "4-1 - pending volume",
  437. newVolumeArray("volume4-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  438. newVolumeArray("volume4-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  439. noclaims,
  440. noclaims,
  441. noevents, noerrors, testSyncVolume,
  442. },
  443. {
  444. // syncVolume with prebound pending volume. Check it's marked as
  445. // Available.
  446. "4-2 - pending prebound volume",
  447. newVolumeArray("volume4-2", "10Gi", "", "claim4-2", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  448. newVolumeArray("volume4-2", "10Gi", "", "claim4-2", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  449. noclaims,
  450. noclaims,
  451. noevents, noerrors, testSyncVolume,
  452. },
  453. {
  454. // syncVolume with volume bound to missing claim.
  455. // Check the volume gets Released
  456. "4-3 - bound volume with missing claim",
  457. newVolumeArray("volume4-3", "10Gi", "uid4-3", "claim4-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  458. newVolumeArray("volume4-3", "10Gi", "uid4-3", "claim4-3", v1.VolumeReleased, v1.PersistentVolumeReclaimRetain, classEmpty),
  459. noclaims,
  460. noclaims,
  461. noevents, noerrors, testSyncVolume,
  462. },
  463. {
  464. // syncVolume with volume bound to claim with different UID.
  465. // Check the volume gets Released.
  466. "4-4 - volume bound to claim with different UID",
  467. newVolumeArray("volume4-4", "10Gi", "uid4-4", "claim4-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  468. newVolumeArray("volume4-4", "10Gi", "uid4-4", "claim4-4", v1.VolumeReleased, v1.PersistentVolumeReclaimRetain, classEmpty),
  469. newClaimArray("claim4-4", "uid4-4-x", "10Gi", "volume4-4", v1.ClaimBound, nil, pvutil.AnnBindCompleted),
  470. newClaimArray("claim4-4", "uid4-4-x", "10Gi", "volume4-4", v1.ClaimBound, nil, pvutil.AnnBindCompleted),
  471. noevents, noerrors, testSyncVolume,
  472. },
  473. {
  474. // syncVolume with volume bound by controller to unbound claim.
  475. // Check syncVolume does not do anything.
  476. "4-5 - volume bound by controller to unbound claim",
  477. newVolumeArray("volume4-5", "10Gi", "uid4-5", "claim4-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  478. newVolumeArray("volume4-5", "10Gi", "uid4-5", "claim4-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  479. newClaimArray("claim4-5", "uid4-5", "10Gi", "", v1.ClaimPending, nil),
  480. newClaimArray("claim4-5", "uid4-5", "10Gi", "", v1.ClaimPending, nil),
  481. noevents, noerrors, testSyncVolume,
  482. },
  483. {
  484. // syncVolume with volume bound by user to unbound claim.
  485. // Check syncVolume does not do anything.
  486. "4-5 - volume bound by user to bound claim",
  487. newVolumeArray("volume4-5", "10Gi", "uid4-5", "claim4-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  488. newVolumeArray("volume4-5", "10Gi", "uid4-5", "claim4-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  489. newClaimArray("claim4-5", "uid4-5", "10Gi", "", v1.ClaimPending, nil),
  490. newClaimArray("claim4-5", "uid4-5", "10Gi", "", v1.ClaimPending, nil),
  491. noevents, noerrors, testSyncVolume,
  492. },
  493. {
  494. // syncVolume with volume bound to bound claim.
  495. // Check that the volume is marked as Bound.
  496. "4-6 - volume bound by to bound claim",
  497. newVolumeArray("volume4-6", "10Gi", "uid4-6", "claim4-6", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  498. newVolumeArray("volume4-6", "10Gi", "uid4-6", "claim4-6", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  499. newClaimArray("claim4-6", "uid4-6", "10Gi", "volume4-6", v1.ClaimBound, nil),
  500. newClaimArray("claim4-6", "uid4-6", "10Gi", "volume4-6", v1.ClaimBound, nil),
  501. noevents, noerrors, testSyncVolume,
  502. },
  503. {
  504. // syncVolume with volume bound by controller to claim bound to
  505. // another volume. Check that the volume is rolled back.
  506. "4-7 - volume bound by controller to claim bound somewhere else",
  507. newVolumeArray("volume4-7", "10Gi", "uid4-7", "claim4-7", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  508. newVolumeArray("volume4-7", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  509. newClaimArray("claim4-7", "uid4-7", "10Gi", "volume4-7-x", v1.ClaimBound, nil),
  510. newClaimArray("claim4-7", "uid4-7", "10Gi", "volume4-7-x", v1.ClaimBound, nil),
  511. noevents, noerrors, testSyncVolume,
  512. },
  513. {
  514. // syncVolume with volume bound by user to claim bound to
  515. // another volume. Check that the volume is marked as Available
  516. // and its UID is reset.
  517. "4-8 - volume bound by user to claim bound somewhere else",
  518. newVolumeArray("volume4-8", "10Gi", "uid4-8", "claim4-8", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  519. newVolumeArray("volume4-8", "10Gi", "", "claim4-8", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  520. newClaimArray("claim4-8", "uid4-8", "10Gi", "volume4-8-x", v1.ClaimBound, nil),
  521. newClaimArray("claim4-8", "uid4-8", "10Gi", "volume4-8-x", v1.ClaimBound, nil),
  522. noevents, noerrors, testSyncVolume,
  523. },
  524. // PVC with class
  525. {
  526. // syncVolume binds a claim to requested class even if there is a
  527. // smaller PV available
  528. "13-1 - binding to class",
  529. []*v1.PersistentVolume{
  530. newVolume("volume13-1-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  531. newVolume("volume13-1-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  532. },
  533. []*v1.PersistentVolume{
  534. newVolume("volume13-1-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  535. newVolume("volume13-1-2", "10Gi", "uid13-1", "claim13-1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classGold, pvutil.AnnBoundByController),
  536. },
  537. newClaimArray("claim13-1", "uid13-1", "1Gi", "", v1.ClaimPending, &classGold),
  538. withExpectedCapacity("10Gi", newClaimArray("claim13-1", "uid13-1", "1Gi", "volume13-1-2", v1.ClaimBound, &classGold, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  539. noevents, noerrors, testSyncClaim,
  540. },
  541. {
  542. // syncVolume binds a claim without a class even if there is a
  543. // smaller PV with a class available
  544. "13-2 - binding without a class",
  545. []*v1.PersistentVolume{
  546. newVolume("volume13-2-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  547. newVolume("volume13-2-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  548. },
  549. []*v1.PersistentVolume{
  550. newVolume("volume13-2-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  551. newVolume("volume13-2-2", "10Gi", "uid13-2", "claim13-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  552. },
  553. newClaimArray("claim13-2", "uid13-2", "1Gi", "", v1.ClaimPending, nil),
  554. withExpectedCapacity("10Gi", newClaimArray("claim13-2", "uid13-2", "1Gi", "volume13-2-2", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  555. noevents, noerrors, testSyncClaim,
  556. },
  557. {
  558. // syncVolume binds a claim with given class even if there is a
  559. // smaller PV with different class available
  560. "13-3 - binding to specific a class",
  561. []*v1.PersistentVolume{
  562. newVolume("volume13-3-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classSilver),
  563. newVolume("volume13-3-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  564. },
  565. []*v1.PersistentVolume{
  566. newVolume("volume13-3-1", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classSilver),
  567. newVolume("volume13-3-2", "10Gi", "uid13-3", "claim13-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classGold, pvutil.AnnBoundByController),
  568. },
  569. newClaimArray("claim13-3", "uid13-3", "1Gi", "", v1.ClaimPending, &classGold),
  570. withExpectedCapacity("10Gi", newClaimArray("claim13-3", "uid13-3", "1Gi", "volume13-3-2", v1.ClaimBound, &classGold, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  571. noevents, noerrors, testSyncClaim,
  572. },
  573. {
  574. // syncVolume binds claim requesting class "" to claim to PV with
  575. // class=""
  576. "13-4 - empty class",
  577. newVolumeArray("volume13-4", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  578. newVolumeArray("volume13-4", "1Gi", "uid13-4", "claim13-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  579. newClaimArray("claim13-4", "uid13-4", "1Gi", "", v1.ClaimPending, &classEmpty),
  580. newClaimArray("claim13-4", "uid13-4", "1Gi", "volume13-4", v1.ClaimBound, &classEmpty, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  581. noevents, noerrors, testSyncClaim,
  582. },
  583. {
  584. // syncVolume binds claim requesting class nil to claim to PV with
  585. // class = ""
  586. "13-5 - nil class",
  587. newVolumeArray("volume13-5", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  588. newVolumeArray("volume13-5", "1Gi", "uid13-5", "claim13-5", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  589. newClaimArray("claim13-5", "uid13-5", "1Gi", "", v1.ClaimPending, nil),
  590. newClaimArray("claim13-5", "uid13-5", "1Gi", "volume13-5", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  591. noevents, noerrors, testSyncClaim,
  592. },
  593. }
  594. runSyncTests(t, tests, []*storage.StorageClass{
  595. {
  596. ObjectMeta: metav1.ObjectMeta{Name: classWait},
  597. VolumeBindingMode: &modeWait,
  598. },
  599. }, []*v1.Pod{})
  600. }
  601. func TestSyncBlockVolumeDisabled(t *testing.T) {
  602. modeBlock := v1.PersistentVolumeBlock
  603. modeFile := v1.PersistentVolumeFilesystem
  604. // All of these should bind as feature set is not enabled for BlockVolume
  605. // meaning volumeMode will be ignored and dropped
  606. tests := []controllerTest{
  607. {
  608. // syncVolume binds a requested block claim to a block volume
  609. "14-1 - binding to volumeMode block",
  610. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  611. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  612. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-1", "uid14-1", "10Gi", "", v1.ClaimPending, nil)),
  613. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-1", "uid14-1", "10Gi", "", v1.ClaimPending, nil)),
  614. noevents, noerrors, testSyncClaim,
  615. },
  616. {
  617. // syncVolume binds a requested filesystem claim to a filesystem volume
  618. "14-2 - binding to volumeMode filesystem",
  619. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  620. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-2", "10Gi", "uid14-2", "claim14-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  621. withClaimVolumeMode(&modeFile, newClaimArray("claim14-2", "uid14-2", "10Gi", "", v1.ClaimPending, nil)),
  622. withClaimVolumeMode(&modeFile, newClaimArray("claim14-2", "uid14-2", "10Gi", "volume14-2", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  623. noevents, noerrors, testSyncClaim,
  624. },
  625. {
  626. // syncVolume binds an unspecified volumemode for claim to a specified filesystem volume
  627. "14-3 - binding to volumeMode filesystem using default for claim",
  628. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-3", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  629. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-3", "10Gi", "uid14-3", "claim14-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  630. withClaimVolumeMode(nil, newClaimArray("claim14-3", "uid14-3", "10Gi", "", v1.ClaimPending, nil)),
  631. withClaimVolumeMode(nil, newClaimArray("claim14-3", "uid14-3", "10Gi", "volume14-3", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  632. noevents, noerrors, testSyncClaim,
  633. },
  634. {
  635. // syncVolume binds a requested filesystem claim to an unspecified volumeMode for volume
  636. "14-4 - binding to unspecified volumeMode using requested filesystem for claim",
  637. withVolumeVolumeMode(nil, newVolumeArray("volume14-4", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  638. withVolumeVolumeMode(nil, newVolumeArray("volume14-4", "10Gi", "uid14-4", "claim14-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  639. withClaimVolumeMode(&modeFile, newClaimArray("claim14-4", "uid14-4", "10Gi", "", v1.ClaimPending, nil)),
  640. withClaimVolumeMode(&modeFile, newClaimArray("claim14-4", "uid14-4", "10Gi", "volume14-4", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  641. noevents, noerrors, testSyncClaim,
  642. },
  643. {
  644. // syncVolume binds a requested filesystem claim to an unspecified volumeMode for volume
  645. "14-5 - binding different volumeModes should be ignored",
  646. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-5", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  647. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-5", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  648. withClaimVolumeMode(&modeFile, newClaimArray("claim14-5", "uid14-5", "10Gi", "", v1.ClaimPending, nil)),
  649. withClaimVolumeMode(&modeFile, newClaimArray("claim14-5", "uid14-5", "10Gi", "", v1.ClaimPending, nil)),
  650. noevents, noerrors, testSyncClaim,
  651. },
  652. }
  653. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, false)()
  654. runSyncTests(t, tests, []*storage.StorageClass{
  655. {
  656. ObjectMeta: metav1.ObjectMeta{Name: classWait},
  657. VolumeBindingMode: &modeWait,
  658. },
  659. }, []*v1.Pod{})
  660. }
  661. func TestSyncBlockVolume(t *testing.T) {
  662. modeBlock := v1.PersistentVolumeBlock
  663. modeFile := v1.PersistentVolumeFilesystem
  664. // Tests assume defaulting, so feature enabled will never have nil volumeMode
  665. tests := []controllerTest{
  666. // PVC with VolumeMode
  667. {
  668. // syncVolume binds a requested block claim to a block volume
  669. "14-1 - binding to volumeMode block",
  670. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  671. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-1", "10Gi", "uid14-1", "claim14-1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  672. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-1", "uid14-1", "10Gi", "", v1.ClaimPending, nil)),
  673. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-1", "uid14-1", "10Gi", "volume14-1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  674. noevents, noerrors, testSyncClaim,
  675. },
  676. {
  677. // syncVolume binds a requested filesystem claim to a filesystem volume
  678. "14-2 - binding to volumeMode filesystem",
  679. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  680. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-2", "10Gi", "uid14-2", "claim14-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  681. withClaimVolumeMode(&modeFile, newClaimArray("claim14-2", "uid14-2", "10Gi", "", v1.ClaimPending, nil)),
  682. withClaimVolumeMode(&modeFile, newClaimArray("claim14-2", "uid14-2", "10Gi", "volume14-2", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  683. noevents, noerrors, testSyncClaim,
  684. },
  685. {
  686. // failed syncVolume do not bind to an unspecified volumemode for claim to a specified filesystem volume
  687. "14-3 - do not bind pv volumeMode filesystem and pvc volumeMode block",
  688. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-3", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  689. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-3", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  690. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-3", "uid14-3", "10Gi", "", v1.ClaimPending, nil)),
  691. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-3", "uid14-3", "10Gi", "", v1.ClaimPending, nil)),
  692. []string{"Normal FailedBinding"},
  693. noerrors, testSyncClaim,
  694. },
  695. {
  696. // failed syncVolume do not bind a requested filesystem claim to an unspecified volumeMode for volume
  697. "14-4 - do not bind pv volumeMode block and pvc volumeMode filesystem",
  698. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-4", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  699. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-4", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  700. withClaimVolumeMode(&modeFile, newClaimArray("claim14-4", "uid14-4", "10Gi", "", v1.ClaimPending, nil)),
  701. withClaimVolumeMode(&modeFile, newClaimArray("claim14-4", "uid14-4", "10Gi", "", v1.ClaimPending, nil)),
  702. []string{"Normal FailedBinding"},
  703. noerrors, testSyncClaim,
  704. },
  705. {
  706. // failed syncVolume do not bind when matching class but not matching volumeModes
  707. "14-5 - do not bind when matching class but not volumeMode",
  708. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-5", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold)),
  709. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-5", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold)),
  710. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-5", "uid14-5", "10Gi", "", v1.ClaimPending, &classGold)),
  711. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-5", "uid14-5", "10Gi", "", v1.ClaimPending, &classGold)),
  712. []string{"Warning ProvisioningFailed"},
  713. noerrors, testSyncClaim,
  714. },
  715. {
  716. // failed syncVolume do not bind when matching volumeModes but class does not match
  717. "14-5-1 - do not bind when matching volumeModes but class does not match",
  718. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-5-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold)),
  719. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-5-1", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold)),
  720. withClaimVolumeMode(&modeFile, newClaimArray("claim14-5-1", "uid14-5-1", "10Gi", "", v1.ClaimPending, &classSilver)),
  721. withClaimVolumeMode(&modeFile, newClaimArray("claim14-5-1", "uid14-5-1", "10Gi", "", v1.ClaimPending, &classSilver)),
  722. []string{"Warning ProvisioningFailed"},
  723. noerrors, testSyncClaim,
  724. },
  725. {
  726. // failed syncVolume do not bind when pvc is prebound to pv with matching volumeModes but class does not match
  727. "14-5-2 - do not bind when pvc is prebound to pv with matching volumeModes but class does not match",
  728. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-5-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold)),
  729. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-5-2", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold)),
  730. withClaimVolumeMode(&modeFile, newClaimArray("claim14-5-2", "uid14-5-2", "10Gi", "volume14-5-2", v1.ClaimPending, &classSilver)),
  731. withClaimVolumeMode(&modeFile, newClaimArray("claim14-5-2", "uid14-5-2", "10Gi", "volume14-5-2", v1.ClaimPending, &classSilver)),
  732. []string{"Warning VolumeMismatch"},
  733. noerrors, testSyncClaim,
  734. },
  735. {
  736. // syncVolume bind when pv is prebound and volumeModes match
  737. "14-7 - bind when pv volume is prebound and volumeModes match",
  738. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-7", "10Gi", "", "claim14-7", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  739. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-7", "10Gi", "uid14-7", "claim14-7", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty)),
  740. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-7", "uid14-7", "10Gi", "", v1.ClaimPending, nil)),
  741. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-7", "uid14-7", "10Gi", "volume14-7", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  742. noevents, noerrors, testSyncClaim,
  743. },
  744. {
  745. // failed syncVolume do not bind when pvc is prebound to pv with mismatching volumeModes
  746. "14-8 - do not bind when pvc is prebound to pv with mismatching volumeModes",
  747. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-8", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  748. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-8", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  749. withClaimVolumeMode(&modeFile, newClaimArray("claim14-8", "uid14-8", "10Gi", "volume14-8", v1.ClaimPending, nil)),
  750. withClaimVolumeMode(&modeFile, newClaimArray("claim14-8", "uid14-8", "10Gi", "volume14-8", v1.ClaimPending, nil)),
  751. []string{"Warning VolumeMismatch"},
  752. noerrors, testSyncClaim,
  753. },
  754. {
  755. // failed syncVolume do not bind when pvc is prebound to pv with mismatching volumeModes
  756. "14-8-1 - do not bind when pvc is prebound to pv with mismatching volumeModes",
  757. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-8-1", "10Gi", "", "claim14-8-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  758. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-8-1", "10Gi", "", "claim14-8-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  759. withClaimVolumeMode(&modeFile, newClaimArray("claim14-8-1", "uid14-8-1", "10Gi", "", v1.ClaimPending, nil)),
  760. withClaimVolumeMode(&modeFile, newClaimArray("claim14-8-1", "uid14-8-1", "10Gi", "", v1.ClaimPending, nil)),
  761. []string{"Normal FailedBinding"},
  762. noerrors, testSyncClaim,
  763. },
  764. {
  765. // syncVolume binds when pvc is prebound to pv with matching volumeModes block
  766. "14-9 - bind when pvc is prebound to pv with matching volumeModes block",
  767. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-9", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  768. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-9", "10Gi", "uid14-9", "claim14-9", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  769. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-9", "uid14-9", "10Gi", "volume14-9", v1.ClaimPending, nil)),
  770. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-9", "uid14-9", "10Gi", "volume14-9", v1.ClaimBound, nil, pvutil.AnnBindCompleted)),
  771. noevents, noerrors, testSyncClaim,
  772. },
  773. {
  774. // syncVolume binds when pv is prebound to pvc with matching volumeModes block
  775. "14-10 - bind when pv is prebound to pvc with matching volumeModes block",
  776. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-10", "10Gi", "", "claim14-10", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  777. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-10", "10Gi", "uid14-10", "claim14-10", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty)),
  778. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-10", "uid14-10", "10Gi", "", v1.ClaimPending, nil)),
  779. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-10", "uid14-10", "10Gi", "volume14-10", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  780. noevents, noerrors, testSyncClaim,
  781. },
  782. {
  783. // syncVolume binds when pvc is prebound to pv with matching volumeModes filesystem
  784. "14-11 - bind when pvc is prebound to pv with matching volumeModes filesystem",
  785. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-11", "10Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  786. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-11", "10Gi", "uid14-11", "claim14-11", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  787. withClaimVolumeMode(&modeFile, newClaimArray("claim14-11", "uid14-11", "10Gi", "volume14-11", v1.ClaimPending, nil)),
  788. withClaimVolumeMode(&modeFile, newClaimArray("claim14-11", "uid14-11", "10Gi", "volume14-11", v1.ClaimBound, nil, pvutil.AnnBindCompleted)),
  789. noevents, noerrors, testSyncClaim,
  790. },
  791. {
  792. // syncVolume binds when pv is prebound to pvc with matching volumeModes filesystem
  793. "14-12 - bind when pv is prebound to pvc with matching volumeModes filesystem",
  794. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-12", "10Gi", "", "claim14-12", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty)),
  795. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-12", "10Gi", "uid14-12", "claim14-12", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty)),
  796. withClaimVolumeMode(&modeFile, newClaimArray("claim14-12", "uid14-12", "10Gi", "", v1.ClaimPending, nil)),
  797. withClaimVolumeMode(&modeFile, newClaimArray("claim14-12", "uid14-12", "10Gi", "volume14-12", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  798. noevents, noerrors, testSyncClaim,
  799. },
  800. {
  801. // syncVolume output warning when pv is prebound to pvc with mismatching volumeMode
  802. "14-13 - output warning when pv is prebound to pvc with different volumeModes",
  803. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-13", "10Gi", "uid14-13", "claim14-13", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  804. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-13", "10Gi", "uid14-13", "claim14-13", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  805. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-13", "uid14-13", "10Gi", "", v1.ClaimPending, nil)),
  806. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-13", "uid14-13", "10Gi", "", v1.ClaimPending, nil)),
  807. []string{"Warning VolumeMismatch"},
  808. noerrors, testSyncVolume,
  809. },
  810. {
  811. // syncVolume output warning when pv is prebound to pvc with mismatching volumeMode
  812. "14-13-1 - output warning when pv is prebound to pvc with different volumeModes",
  813. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-13-1", "10Gi", "uid14-13-1", "claim14-13-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  814. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-13-1", "10Gi", "uid14-13-1", "claim14-13-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  815. withClaimVolumeMode(&modeFile, newClaimArray("claim14-13-1", "uid14-13-1", "10Gi", "", v1.ClaimPending, nil)),
  816. withClaimVolumeMode(&modeFile, newClaimArray("claim14-13-1", "uid14-13-1", "10Gi", "", v1.ClaimPending, nil)),
  817. []string{"Warning VolumeMismatch"},
  818. noerrors, testSyncVolume,
  819. },
  820. {
  821. // syncVolume waits for synClaim without warning when pv is prebound to pvc with matching volumeMode block
  822. "14-14 - wait for synClaim without warning when pv is prebound to pvc with matching volumeModes block",
  823. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-14", "10Gi", "uid14-14", "claim14-14", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  824. withVolumeVolumeMode(&modeBlock, newVolumeArray("volume14-14", "10Gi", "uid14-14", "claim14-14", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  825. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-14", "uid14-14", "10Gi", "", v1.ClaimPending, nil)),
  826. withClaimVolumeMode(&modeBlock, newClaimArray("claim14-14", "uid14-14", "10Gi", "", v1.ClaimPending, nil)),
  827. noevents, noerrors, testSyncVolume,
  828. },
  829. {
  830. // syncVolume waits for synClaim without warning when pv is prebound to pvc with matching volumeMode file
  831. "14-14-1 - wait for synClaim without warning when pv is prebound to pvc with matching volumeModes file",
  832. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-14-1", "10Gi", "uid14-14-1", "claim14-14-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  833. withVolumeVolumeMode(&modeFile, newVolumeArray("volume14-14-1", "10Gi", "uid14-14-1", "claim14-14-1", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController)),
  834. withClaimVolumeMode(&modeFile, newClaimArray("claim14-14-1", "uid14-14-1", "10Gi", "", v1.ClaimPending, nil)),
  835. withClaimVolumeMode(&modeFile, newClaimArray("claim14-14-1", "uid14-14-1", "10Gi", "", v1.ClaimPending, nil)),
  836. noevents, noerrors, testSyncVolume,
  837. },
  838. }
  839. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, true)()
  840. runSyncTests(t, tests, []*storage.StorageClass{}, []*v1.Pod{})
  841. }
  842. // Test multiple calls to syncClaim/syncVolume and periodic sync of all
  843. // volume/claims. The test follows this pattern:
  844. // 0. Load the controller with initial data.
  845. // 1. Call controllerTest.testCall() once as in TestSync()
  846. // 2. For all volumes/claims changed by previous syncVolume/syncClaim calls,
  847. // call appropriate syncVolume/syncClaim (simulating "volume/claim changed"
  848. // events). Go to 2. if these calls change anything.
  849. // 3. When all changes are processed and no new changes were made, call
  850. // syncVolume/syncClaim on all volumes/claims (simulating "periodic sync").
  851. // 4. If some changes were done by step 3., go to 2. (simulation of
  852. // "volume/claim updated" events, eventually performing step 3. again)
  853. // 5. When 3. does not do any changes, finish the tests and compare final set
  854. // of volumes/claims with expected claims/volumes and report differences.
  855. // Some limit of calls in enforced to prevent endless loops.
  856. func TestMultiSync(t *testing.T) {
  857. tests := []controllerTest{
  858. // Test simple binding
  859. {
  860. // syncClaim binds to a matching unbound volume.
  861. "10-1 - successful bind",
  862. newVolumeArray("volume10-1", "1Gi", "", "", v1.VolumePending, v1.PersistentVolumeReclaimRetain, classEmpty),
  863. newVolumeArray("volume10-1", "1Gi", "uid10-1", "claim10-1", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  864. newClaimArray("claim10-1", "uid10-1", "1Gi", "", v1.ClaimPending, nil),
  865. newClaimArray("claim10-1", "uid10-1", "1Gi", "volume10-1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  866. noevents, noerrors, testSyncClaim,
  867. },
  868. {
  869. // Two controllers bound two PVs to single claim. Test one of them
  870. // wins and the second rolls back.
  871. "10-2 - bind PV race",
  872. []*v1.PersistentVolume{
  873. newVolume("volume10-2-1", "1Gi", "uid10-2", "claim10-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  874. newVolume("volume10-2-2", "1Gi", "uid10-2", "claim10-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  875. },
  876. []*v1.PersistentVolume{
  877. newVolume("volume10-2-1", "1Gi", "uid10-2", "claim10-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  878. newVolume("volume10-2-2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  879. },
  880. newClaimArray("claim10-2", "uid10-2", "1Gi", "volume10-2-1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  881. newClaimArray("claim10-2", "uid10-2", "1Gi", "volume10-2-1", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  882. noevents, noerrors, testSyncClaim,
  883. },
  884. }
  885. runMultisyncTests(t, tests, []*storage.StorageClass{}, "")
  886. }