iterator_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/util/sets"
  19. )
  20. func TestPetQueueCreates(t *testing.T) {
  21. replicas := 3
  22. ps := newPetSet(replicas)
  23. q := NewPetQueue(ps, []*api.Pod{})
  24. for i := 0; i < replicas; i++ {
  25. pet, _ := newPCB(fmt.Sprintf("%v", i), ps)
  26. q.enqueue(pet)
  27. p := q.dequeue()
  28. if p.event != syncPet {
  29. t.Errorf("Failed to retrieve sync event from queue")
  30. }
  31. }
  32. if q.dequeue() != nil {
  33. t.Errorf("Expected no pets")
  34. }
  35. }
  36. func TestPetQueueScaleDown(t *testing.T) {
  37. replicas := 1
  38. ps := newPetSet(replicas)
  39. // knownPods are the pods in the system
  40. knownPods := newPodList(ps, 3)
  41. q := NewPetQueue(ps, knownPods)
  42. // The iterator will insert a single replica, the enqueue
  43. // mimics that behavior.
  44. pet, _ := newPCB(fmt.Sprintf("%v", 0), ps)
  45. q.enqueue(pet)
  46. deletes := sets.NewString(fmt.Sprintf("%v-1", ps.Name), fmt.Sprintf("%v-2", ps.Name))
  47. syncs := sets.NewString(fmt.Sprintf("%v-0", ps.Name))
  48. // Confirm that 2 known pods are deleted
  49. for i := 0; i < 3; i++ {
  50. p := q.dequeue()
  51. switch p.event {
  52. case syncPet:
  53. if !syncs.Has(p.pod.Name) {
  54. t.Errorf("Unexpected sync %v expecting %+v", p.pod.Name, syncs)
  55. }
  56. case deletePet:
  57. if !deletes.Has(p.pod.Name) {
  58. t.Errorf("Unexpected deletes %v expecting %+v", p.pod.Name, deletes)
  59. }
  60. }
  61. }
  62. if q.dequeue() != nil {
  63. t.Errorf("Expected no pets")
  64. }
  65. }
  66. func TestPetQueueScaleUp(t *testing.T) {
  67. replicas := 5
  68. ps := newPetSet(replicas)
  69. // knownPods are pods in the system
  70. knownPods := newPodList(ps, 2)
  71. q := NewPetQueue(ps, knownPods)
  72. for i := 0; i < 5; i++ {
  73. pet, _ := newPCB(fmt.Sprintf("%v", i), ps)
  74. q.enqueue(pet)
  75. }
  76. for i := 4; i >= 0; i-- {
  77. pet := q.dequeue()
  78. expectedName := fmt.Sprintf("%v-%d", ps.Name, i)
  79. if pet.event != syncPet || pet.pod.Name != expectedName {
  80. t.Errorf("Unexpected pet %+v, expected %v", pet.pod.Name, expectedName)
  81. }
  82. }
  83. }
  84. func TestPetSetIteratorRelist(t *testing.T) {
  85. replicas := 5
  86. ps := newPetSet(replicas)
  87. // knownPods are pods in the system
  88. knownPods := newPodList(ps, 5)
  89. for i := range knownPods {
  90. knownPods[i].Spec.NodeName = fmt.Sprintf("foo-node-%v", i)
  91. knownPods[i].Status.Phase = api.PodRunning
  92. }
  93. pi := NewPetSetIterator(ps, knownPods)
  94. // A simple resync should not change identity of pods in the system
  95. i := 0
  96. for pi.Next() {
  97. p := pi.Value()
  98. if identityHash(ps, p.pod) != identityHash(ps, knownPods[i]) {
  99. t.Errorf("Got unexpected identity hash from iterator.")
  100. }
  101. if p.event != syncPet {
  102. t.Errorf("Got unexpected sync event for %v: %v", p.pod.Name, p.event)
  103. }
  104. i++
  105. }
  106. if i != 5 {
  107. t.Errorf("Unexpected iterations %v, this probably means too many/few pets", i)
  108. }
  109. // Scale to 0 should delete all pods in system
  110. ps.Spec.Replicas = 0
  111. pi = NewPetSetIterator(ps, knownPods)
  112. i = 0
  113. for pi.Next() {
  114. p := pi.Value()
  115. if p.event != deletePet {
  116. t.Errorf("Got unexpected sync event for %v: %v", p.pod.Name, p.event)
  117. }
  118. i++
  119. }
  120. if i != 5 {
  121. t.Errorf("Unexpected iterations %v, this probably means too many/few pets", i)
  122. }
  123. // Relist with 0 replicas should no-op
  124. pi = NewPetSetIterator(ps, []*api.Pod{})
  125. if pi.Next() != false {
  126. t.Errorf("Unexpected iteration without any replicas or pods in system")
  127. }
  128. }