pet_set_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 petset
  14. import (
  15. "fmt"
  16. "math/rand"
  17. "reflect"
  18. "testing"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/apis/apps"
  21. "k8s.io/kubernetes/pkg/client/cache"
  22. "k8s.io/kubernetes/pkg/controller"
  23. )
  24. func newFakePetSetController() (*PetSetController, *fakePetClient) {
  25. fpc := newFakePetClient()
  26. return &PetSetController{
  27. kubeClient: nil,
  28. blockingPetStore: newUnHealthyPetTracker(fpc),
  29. podStoreSynced: func() bool { return true },
  30. psStore: cache.StoreToPetSetLister{Store: cache.NewStore(controller.KeyFunc)},
  31. podStore: cache.StoreToPodLister{Indexer: cache.NewIndexer(controller.KeyFunc, cache.Indexers{})},
  32. newSyncer: func(blockingPet *pcb) *petSyncer {
  33. return &petSyncer{fpc, blockingPet}
  34. },
  35. }, fpc
  36. }
  37. func checkPets(ps *apps.PetSet, creates, deletes int, fc *fakePetClient, t *testing.T) {
  38. if fc.petsCreated != creates || fc.petsDeleted != deletes {
  39. t.Errorf("Found (creates: %d, deletes: %d), expected (creates: %d, deletes: %d)", fc.petsCreated, fc.petsDeleted, creates, deletes)
  40. }
  41. gotClaims := map[string]api.PersistentVolumeClaim{}
  42. for _, pvc := range fc.claims {
  43. gotClaims[pvc.Name] = pvc
  44. }
  45. for i := range fc.pets {
  46. expectedPet, _ := newPCB(fmt.Sprintf("%v", i), ps)
  47. if identityHash(ps, fc.pets[i].pod) != identityHash(ps, expectedPet.pod) {
  48. t.Errorf("Unexpected pet at index %d", i)
  49. }
  50. for _, pvc := range expectedPet.pvcs {
  51. gotPVC, ok := gotClaims[pvc.Name]
  52. if !ok {
  53. t.Errorf("PVC %v not created for pet %v", pvc.Name, expectedPet.pod.Name)
  54. }
  55. if !reflect.DeepEqual(gotPVC.Spec, pvc.Spec) {
  56. t.Errorf("got PVC %v differs from created pvc", pvc.Name)
  57. }
  58. }
  59. }
  60. }
  61. func scalePetSet(t *testing.T, ps *apps.PetSet, psc *PetSetController, fc *fakePetClient, scale int) []error {
  62. errs := []error{}
  63. for i := 0; i < scale; i++ {
  64. pl := fc.getPodList()
  65. if len(pl) != i {
  66. t.Errorf("Unexpected number of pets, expected %d found %d", i, len(fc.pets))
  67. }
  68. _, syncErrs := psc.syncPetSet(ps, pl)
  69. errs = append(errs, syncErrs...)
  70. fc.setHealthy(i)
  71. checkPets(ps, i+1, 0, fc, t)
  72. }
  73. return errs
  74. }
  75. func saturatePetSet(t *testing.T, ps *apps.PetSet, psc *PetSetController, fc *fakePetClient) {
  76. errs := scalePetSet(t, ps, psc, fc, ps.Spec.Replicas)
  77. if len(errs) != 0 {
  78. t.Errorf("%v", errs)
  79. }
  80. }
  81. func TestPetSetControllerCreates(t *testing.T) {
  82. psc, fc := newFakePetSetController()
  83. replicas := 3
  84. ps := newPetSet(replicas)
  85. saturatePetSet(t, ps, psc, fc)
  86. podList := fc.getPodList()
  87. // Deleted pet gets recreated
  88. fc.pets = fc.pets[:replicas-1]
  89. if _, errs := psc.syncPetSet(ps, podList); len(errs) != 0 {
  90. t.Errorf("%v", errs)
  91. }
  92. checkPets(ps, replicas+1, 0, fc, t)
  93. }
  94. func TestPetSetControllerDeletes(t *testing.T) {
  95. psc, fc := newFakePetSetController()
  96. replicas := 4
  97. ps := newPetSet(replicas)
  98. saturatePetSet(t, ps, psc, fc)
  99. // Drain
  100. errs := []error{}
  101. ps.Spec.Replicas = 0
  102. knownPods := fc.getPodList()
  103. for i := replicas - 1; i >= 0; i-- {
  104. if len(fc.pets) != i+1 {
  105. t.Errorf("Unexpected number of pets, expected %d found %d", i, len(fc.pets))
  106. }
  107. _, syncErrs := psc.syncPetSet(ps, knownPods)
  108. errs = append(errs, syncErrs...)
  109. }
  110. if len(errs) != 0 {
  111. t.Errorf("%v", errs)
  112. }
  113. checkPets(ps, replicas, replicas, fc, t)
  114. }
  115. func TestPetSetControllerRespectsTermination(t *testing.T) {
  116. psc, fc := newFakePetSetController()
  117. replicas := 4
  118. ps := newPetSet(replicas)
  119. saturatePetSet(t, ps, psc, fc)
  120. fc.setDeletionTimestamp(replicas - 1)
  121. ps.Spec.Replicas = 2
  122. _, errs := psc.syncPetSet(ps, fc.getPodList())
  123. if len(errs) != 0 {
  124. t.Errorf("%v", errs)
  125. }
  126. // Finding a pod with the deletion timestamp will pause all deletions.
  127. knownPods := fc.getPodList()
  128. if len(knownPods) != 4 {
  129. t.Errorf("Pods deleted prematurely before deletion timestamp expired, len %d", len(knownPods))
  130. }
  131. fc.pets = fc.pets[:replicas-1]
  132. _, errs = psc.syncPetSet(ps, fc.getPodList())
  133. if len(errs) != 0 {
  134. t.Errorf("%v", errs)
  135. }
  136. checkPets(ps, replicas, 1, fc, t)
  137. }
  138. func TestPetSetControllerRespectsOrder(t *testing.T) {
  139. psc, fc := newFakePetSetController()
  140. replicas := 4
  141. ps := newPetSet(replicas)
  142. saturatePetSet(t, ps, psc, fc)
  143. errs := []error{}
  144. ps.Spec.Replicas = 0
  145. // Shuffle known list and check that pets are deleted in reverse
  146. knownPods := fc.getPodList()
  147. for i := range knownPods {
  148. j := rand.Intn(i + 1)
  149. knownPods[i], knownPods[j] = knownPods[j], knownPods[i]
  150. }
  151. for i := 0; i < replicas; i++ {
  152. if len(fc.pets) != replicas-i {
  153. t.Errorf("Unexpected number of pets, expected %d found %d", i, len(fc.pets))
  154. }
  155. _, syncErrs := psc.syncPetSet(ps, knownPods)
  156. errs = append(errs, syncErrs...)
  157. checkPets(ps, replicas, i+1, fc, t)
  158. }
  159. if len(errs) != 0 {
  160. t.Errorf("%v", errs)
  161. }
  162. }
  163. func TestPetSetControllerBlocksScaling(t *testing.T) {
  164. psc, fc := newFakePetSetController()
  165. replicas := 5
  166. ps := newPetSet(replicas)
  167. scalePetSet(t, ps, psc, fc, 3)
  168. // Create 4th pet, then before flipping it to healthy, kill the first pet.
  169. // There should only be 1 not-healty pet at a time.
  170. pl := fc.getPodList()
  171. if _, errs := psc.syncPetSet(ps, pl); len(errs) != 0 {
  172. t.Errorf("%v", errs)
  173. }
  174. deletedPod := pl[0]
  175. fc.deletePetAtIndex(0)
  176. pl = fc.getPodList()
  177. if _, errs := psc.syncPetSet(ps, pl); len(errs) != 0 {
  178. t.Errorf("%v", errs)
  179. }
  180. newPodList := fc.getPodList()
  181. for _, p := range newPodList {
  182. if p.Name == deletedPod.Name {
  183. t.Errorf("Deleted pod was created while existing pod was unhealthy")
  184. }
  185. }
  186. fc.setHealthy(len(newPodList) - 1)
  187. if _, errs := psc.syncPetSet(ps, pl); len(errs) != 0 {
  188. t.Errorf("%v", errs)
  189. }
  190. found := false
  191. for _, p := range fc.getPodList() {
  192. if p.Name == deletedPod.Name {
  193. found = true
  194. break
  195. }
  196. }
  197. if !found {
  198. t.Errorf("Deleted pod was not created after existing pods became healthy")
  199. }
  200. }
  201. func TestPetSetBlockingPetIsCleared(t *testing.T) {
  202. psc, fc := newFakePetSetController()
  203. ps := newPetSet(3)
  204. scalePetSet(t, ps, psc, fc, 1)
  205. if blocking, err := psc.blockingPetStore.Get(ps, fc.getPodList()); err != nil || blocking != nil {
  206. t.Errorf("Unexpected blocking pet %v, err %v", blocking, err)
  207. }
  208. // 1 not yet healthy pet
  209. psc.syncPetSet(ps, fc.getPodList())
  210. if blocking, err := psc.blockingPetStore.Get(ps, fc.getPodList()); err != nil || blocking == nil {
  211. t.Errorf("Expected blocking pet %v, err %v", blocking, err)
  212. }
  213. // Deleting the petset should clear the blocking pet
  214. if err := psc.psStore.Store.Delete(ps); err != nil {
  215. t.Fatalf("Unable to delete pet %v from petset controller store.", ps.Name)
  216. }
  217. if errs := psc.Sync(fmt.Sprintf("%v/%v", ps.Namespace, ps.Name)); len(errs) != 0 {
  218. t.Errorf("Error during sync of deleted petset %v", errs)
  219. }
  220. fc.pets = []*pcb{}
  221. fc.petsCreated = 0
  222. if blocking, err := psc.blockingPetStore.Get(ps, fc.getPodList()); err != nil || blocking != nil {
  223. t.Errorf("Unexpected blocking pet %v, err %v", blocking, err)
  224. }
  225. saturatePetSet(t, ps, psc, fc)
  226. // Make sure we don't leak the final blockin pet in the store
  227. psc.syncPetSet(ps, fc.getPodList())
  228. if p, exists, err := psc.blockingPetStore.store.GetByKey(fmt.Sprintf("%v/%v", ps.Namespace, ps.Name)); err != nil || exists {
  229. t.Errorf("Unexpected blocking pet, err %v: %+v", err, p)
  230. }
  231. }