controller_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. "time"
  17. "github.com/golang/glog"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/client/cache"
  20. "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
  21. "k8s.io/kubernetes/pkg/controller/framework"
  22. )
  23. // Test the real controller methods (add/update/delete claim/volume) with
  24. // a fake API server.
  25. // There is no controller API to 'initiate syncAll now', therefore these tests
  26. // can't reliably simulate periodic sync of volumes/claims - it would be
  27. // either very timing-sensitive or slow to wait for real periodic sync.
  28. func TestControllerSync(t *testing.T) {
  29. tests := []controllerTest{
  30. // [Unit test set 5] - controller tests.
  31. // We test the controller as if
  32. // it was connected to real API server, i.e. we call add/update/delete
  33. // Claim/Volume methods. Also, all changes to volumes and claims are
  34. // sent to add/update/delete Claim/Volume as real controller would do.
  35. {
  36. // addClaim gets a new claim. Check it's bound to a volume.
  37. "5-2 - complete bind",
  38. newVolumeArray("volume5-2", "1Gi", "", "", api.VolumeAvailable, api.PersistentVolumeReclaimRetain),
  39. newVolumeArray("volume5-2", "1Gi", "uid5-2", "claim5-2", api.VolumeBound, api.PersistentVolumeReclaimRetain, annBoundByController),
  40. noclaims, /* added in testAddClaim5_2 */
  41. newClaimArray("claim5-2", "uid5-2", "1Gi", "volume5-2", api.ClaimBound, annBoundByController, annBindCompleted),
  42. noevents, noerrors,
  43. // Custom test function that generates an add event
  44. func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error {
  45. claim := newClaim("claim5-2", "uid5-2", "1Gi", "", api.ClaimPending)
  46. reactor.addClaimEvent(claim)
  47. return nil
  48. },
  49. },
  50. {
  51. // deleteClaim with a bound claim makes bound volume released.
  52. "5-3 - delete claim",
  53. newVolumeArray("volume5-3", "10Gi", "uid5-3", "claim5-3", api.VolumeBound, api.PersistentVolumeReclaimRetain, annBoundByController),
  54. newVolumeArray("volume5-3", "10Gi", "uid5-3", "claim5-3", api.VolumeReleased, api.PersistentVolumeReclaimRetain, annBoundByController),
  55. newClaimArray("claim5-3", "uid5-3", "1Gi", "volume5-3", api.ClaimBound, annBoundByController, annBindCompleted),
  56. noclaims,
  57. noevents, noerrors,
  58. // Custom test function that generates a delete event
  59. func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error {
  60. obj := ctrl.claims.List()[0]
  61. claim := obj.(*api.PersistentVolumeClaim)
  62. reactor.deleteClaimEvent(claim)
  63. return nil
  64. },
  65. },
  66. {
  67. // deleteVolume with a bound volume. Check the claim is Lost.
  68. "5-4 - delete volume",
  69. newVolumeArray("volume5-4", "1Gi", "uid5-4", "claim5-4", api.VolumeBound, api.PersistentVolumeReclaimRetain),
  70. novolumes,
  71. newClaimArray("claim5-4", "uid5-4", "1Gi", "volume5-4", api.ClaimBound, annBoundByController, annBindCompleted),
  72. newClaimArray("claim5-4", "uid5-4", "1Gi", "volume5-4", api.ClaimLost, annBoundByController, annBindCompleted),
  73. []string{"Warning ClaimLost"}, noerrors,
  74. // Custom test function that generates a delete event
  75. func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error {
  76. obj := ctrl.volumes.store.List()[0]
  77. volume := obj.(*api.PersistentVolume)
  78. reactor.deleteVolumeEvent(volume)
  79. return nil
  80. },
  81. },
  82. {
  83. // addVolume with provisioned volume from Kubernetes 1.2. No "action"
  84. // is expected - it should stay bound.
  85. "5-5 - add bound volume from 1.2",
  86. novolumes,
  87. []*api.PersistentVolume{addVolumeAnnotation(newVolume("volume5-5", "1Gi", "uid5-5", "claim5-5", api.VolumeBound, api.PersistentVolumeReclaimDelete), pvProvisioningRequiredAnnotationKey, pvProvisioningCompletedAnnotationValue)},
  88. newClaimArray("claim5-5", "uid5-5", "1Gi", "", api.ClaimPending),
  89. newClaimArray("claim5-5", "uid5-5", "1Gi", "volume5-5", api.ClaimBound, annBindCompleted, annBoundByController),
  90. noevents, noerrors,
  91. // Custom test function that generates a add event
  92. func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error {
  93. volume := newVolume("volume5-5", "1Gi", "uid5-5", "claim5-5", api.VolumeBound, api.PersistentVolumeReclaimDelete)
  94. volume = addVolumeAnnotation(volume, pvProvisioningRequiredAnnotationKey, pvProvisioningCompletedAnnotationValue)
  95. reactor.addVolumeEvent(volume)
  96. return nil
  97. },
  98. },
  99. {
  100. // updateVolume with provisioned volume from Kubernetes 1.2. No
  101. // "action" is expected - it should stay bound.
  102. "5-6 - update bound volume from 1.2",
  103. []*api.PersistentVolume{addVolumeAnnotation(newVolume("volume5-6", "1Gi", "uid5-6", "claim5-6", api.VolumeBound, api.PersistentVolumeReclaimDelete), pvProvisioningRequiredAnnotationKey, pvProvisioningCompletedAnnotationValue)},
  104. []*api.PersistentVolume{addVolumeAnnotation(newVolume("volume5-6", "1Gi", "uid5-6", "claim5-6", api.VolumeBound, api.PersistentVolumeReclaimDelete), pvProvisioningRequiredAnnotationKey, pvProvisioningCompletedAnnotationValue)},
  105. newClaimArray("claim5-6", "uid5-6", "1Gi", "volume5-6", api.ClaimBound),
  106. newClaimArray("claim5-6", "uid5-6", "1Gi", "volume5-6", api.ClaimBound, annBindCompleted),
  107. noevents, noerrors,
  108. // Custom test function that generates a add event
  109. func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error {
  110. volume := newVolume("volume5-6", "1Gi", "uid5-6", "claim5-6", api.VolumeBound, api.PersistentVolumeReclaimDelete)
  111. volume = addVolumeAnnotation(volume, pvProvisioningRequiredAnnotationKey, pvProvisioningCompletedAnnotationValue)
  112. reactor.modifyVolumeEvent(volume)
  113. return nil
  114. },
  115. },
  116. {
  117. // addVolume with unprovisioned volume from Kubernetes 1.2. The
  118. // volume should be deleted.
  119. "5-7 - add unprovisioned volume from 1.2",
  120. novolumes,
  121. novolumes,
  122. newClaimArray("claim5-7", "uid5-7", "1Gi", "", api.ClaimPending),
  123. newClaimArray("claim5-7", "uid5-7", "1Gi", "", api.ClaimPending),
  124. noevents, noerrors,
  125. // Custom test function that generates a add event
  126. func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error {
  127. volume := newVolume("volume5-7", "1Gi", "uid5-7", "claim5-7", api.VolumeBound, api.PersistentVolumeReclaimDelete)
  128. volume = addVolumeAnnotation(volume, pvProvisioningRequiredAnnotationKey, "yes")
  129. reactor.addVolumeEvent(volume)
  130. return nil
  131. },
  132. },
  133. {
  134. // updateVolume with unprovisioned volume from Kubernetes 1.2. The
  135. // volume should be deleted.
  136. "5-8 - update bound volume from 1.2",
  137. novolumes,
  138. novolumes,
  139. newClaimArray("claim5-8", "uid5-8", "1Gi", "", api.ClaimPending),
  140. newClaimArray("claim5-8", "uid5-8", "1Gi", "", api.ClaimPending),
  141. noevents, noerrors,
  142. // Custom test function that generates a add event
  143. func(ctrl *PersistentVolumeController, reactor *volumeReactor, test controllerTest) error {
  144. volume := newVolume("volume5-8", "1Gi", "uid5-8", "claim5-8", api.VolumeBound, api.PersistentVolumeReclaimDelete)
  145. volume = addVolumeAnnotation(volume, pvProvisioningRequiredAnnotationKey, "yes")
  146. reactor.modifyVolumeEvent(volume)
  147. return nil
  148. },
  149. },
  150. }
  151. for _, test := range tests {
  152. glog.V(4).Infof("starting test %q", test.name)
  153. // Initialize the controller
  154. client := &fake.Clientset{}
  155. volumeSource := framework.NewFakePVControllerSource()
  156. claimSource := framework.NewFakePVCControllerSource()
  157. ctrl := newTestController(client, volumeSource, claimSource, nil, true)
  158. reactor := newVolumeReactor(client, ctrl, volumeSource, claimSource, test.errors)
  159. for _, claim := range test.initialClaims {
  160. claimSource.Add(claim)
  161. reactor.claims[claim.Name] = claim
  162. }
  163. for _, volume := range test.initialVolumes {
  164. volumeSource.Add(volume)
  165. reactor.volumes[volume.Name] = volume
  166. }
  167. // Start the controller
  168. stopCh := make(chan struct{})
  169. ctrl.Run(stopCh)
  170. // Wait for the controller to pass initial sync and fill its caches.
  171. for !ctrl.volumeController.HasSynced() ||
  172. !ctrl.claimController.HasSynced() ||
  173. len(ctrl.claims.ListKeys()) < len(test.initialClaims) ||
  174. len(ctrl.volumes.store.ListKeys()) < len(test.initialVolumes) {
  175. time.Sleep(10 * time.Millisecond)
  176. }
  177. glog.V(4).Infof("controller synced, starting test")
  178. // Call the tested function
  179. err := test.test(ctrl, reactor, test)
  180. if err != nil {
  181. t.Errorf("Test %q initial test call failed: %v", test.name, err)
  182. }
  183. // Simulate a periodic resync, just in case some events arrived in a
  184. // wrong order.
  185. ctrl.claims.Resync()
  186. ctrl.volumes.store.Resync()
  187. err = reactor.waitTest(test)
  188. if err != nil {
  189. t.Errorf("Failed to run test %s: %v", test.name, err)
  190. }
  191. close(stopCh)
  192. evaluateTestResults(ctrl, reactor, test, t)
  193. }
  194. }
  195. func storeVersion(t *testing.T, prefix string, c cache.Store, version string, expectedReturn bool) {
  196. pv := newVolume("pvName", "1Gi", "", "", api.VolumeAvailable, api.PersistentVolumeReclaimDelete)
  197. pv.ResourceVersion = version
  198. ret, err := storeObjectUpdate(c, pv, "volume")
  199. if err != nil {
  200. t.Errorf("%s: expected storeObjectUpdate to succeed, got: %v", prefix, err)
  201. }
  202. if expectedReturn != ret {
  203. t.Errorf("%s: expected storeObjectUpdate to return %v, got: %v", prefix, expectedReturn, ret)
  204. }
  205. // find the stored version
  206. pvObj, found, err := c.GetByKey("pvName")
  207. if err != nil {
  208. t.Errorf("expected volume 'pvName' in the cache, got error instead: %v", err)
  209. }
  210. if !found {
  211. t.Errorf("expected volume 'pvName' in the cache but it was not found")
  212. }
  213. pv, ok := pvObj.(*api.PersistentVolume)
  214. if !ok {
  215. t.Errorf("expected volume in the cache, got different object instead: %#v", pvObj)
  216. }
  217. if ret {
  218. if pv.ResourceVersion != version {
  219. t.Errorf("expected volume with version %s in the cache, got %s instead", version, pv.ResourceVersion)
  220. }
  221. } else {
  222. if pv.ResourceVersion == version {
  223. t.Errorf("expected volume with version other than %s in the cache, got %s instead", version, pv.ResourceVersion)
  224. }
  225. }
  226. }
  227. // TestControllerCache tests func storeObjectUpdate()
  228. func TestControllerCache(t *testing.T) {
  229. // Cache under test
  230. c := cache.NewStore(framework.DeletionHandlingMetaNamespaceKeyFunc)
  231. // Store new PV
  232. storeVersion(t, "Step1", c, "1", true)
  233. // Store the same PV
  234. storeVersion(t, "Step2", c, "1", true)
  235. // Store newer PV
  236. storeVersion(t, "Step3", c, "2", true)
  237. // Store older PV - simulating old "PV updated" event or periodic sync with
  238. // old data
  239. storeVersion(t, "Step4", c, "1", false)
  240. // Store newer PV - test integer parsing ("2" > "10" as string,
  241. // while 2 < 10 as integers)
  242. storeVersion(t, "Step5", c, "10", true)
  243. }
  244. func TestControllerCacheParsingError(t *testing.T) {
  245. c := cache.NewStore(framework.DeletionHandlingMetaNamespaceKeyFunc)
  246. // There must be something in the cache to compare with
  247. storeVersion(t, "Step1", c, "1", true)
  248. pv := newVolume("pvName", "1Gi", "", "", api.VolumeAvailable, api.PersistentVolumeReclaimDelete)
  249. pv.ResourceVersion = "xxx"
  250. _, err := storeObjectUpdate(c, pv, "volume")
  251. if err == nil {
  252. t.Errorf("Expected parsing error, got nil instead")
  253. }
  254. }
  255. func addVolumeAnnotation(volume *api.PersistentVolume, annName, annValue string) *api.PersistentVolume {
  256. if volume.Annotations == nil {
  257. volume.Annotations = make(map[string]string)
  258. }
  259. volume.Annotations[annName] = annValue
  260. return volume
  261. }