recreate_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 deployment
  14. import (
  15. "fmt"
  16. "testing"
  17. apps "k8s.io/api/apps/v1"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/types"
  21. "k8s.io/client-go/informers"
  22. "k8s.io/client-go/kubernetes/fake"
  23. "k8s.io/client-go/tools/record"
  24. "k8s.io/kubernetes/pkg/controller"
  25. )
  26. func TestScaleDownOldReplicaSets(t *testing.T) {
  27. tests := []struct {
  28. oldRSSizes []int
  29. d *apps.Deployment
  30. }{
  31. {
  32. oldRSSizes: []int{3},
  33. d: newDeployment("foo", 3, nil, nil, nil, map[string]string{"foo": "bar"}),
  34. },
  35. }
  36. for i := range tests {
  37. t.Logf("running scenario %d", i)
  38. test := tests[i]
  39. var oldRSs []*apps.ReplicaSet
  40. var expected []runtime.Object
  41. for n, size := range test.oldRSSizes {
  42. rs := newReplicaSet(test.d, fmt.Sprintf("%s-%d", test.d.Name, n), size)
  43. oldRSs = append(oldRSs, rs)
  44. rsCopy := rs.DeepCopy()
  45. zero := int32(0)
  46. rsCopy.Spec.Replicas = &zero
  47. expected = append(expected, rsCopy)
  48. if *(oldRSs[n].Spec.Replicas) == *(expected[n].(*apps.ReplicaSet).Spec.Replicas) {
  49. t.Errorf("broken test - original and expected RS have the same size")
  50. }
  51. }
  52. kc := fake.NewSimpleClientset(expected...)
  53. informers := informers.NewSharedInformerFactory(kc, controller.NoResyncPeriodFunc())
  54. c, err := NewDeploymentController(informers.Apps().V1().Deployments(), informers.Apps().V1().ReplicaSets(), informers.Core().V1().Pods(), kc)
  55. if err != nil {
  56. t.Fatalf("error creating Deployment controller: %v", err)
  57. }
  58. c.eventRecorder = &record.FakeRecorder{}
  59. c.scaleDownOldReplicaSetsForRecreate(oldRSs, test.d)
  60. for j := range oldRSs {
  61. rs := oldRSs[j]
  62. if *rs.Spec.Replicas != 0 {
  63. t.Errorf("rs %q has non-zero replicas", rs.Name)
  64. }
  65. }
  66. }
  67. }
  68. func TestOldPodsRunning(t *testing.T) {
  69. tests := []struct {
  70. name string
  71. newRS *apps.ReplicaSet
  72. oldRSs []*apps.ReplicaSet
  73. podMap map[types.UID][]*v1.Pod
  74. hasOldPodsRunning bool
  75. }{
  76. {
  77. name: "no old RSs",
  78. hasOldPodsRunning: false,
  79. },
  80. {
  81. name: "old RSs with running pods",
  82. oldRSs: []*apps.ReplicaSet{rsWithUID("some-uid"), rsWithUID("other-uid")},
  83. podMap: podMapWithUIDs([]string{"some-uid", "other-uid"}),
  84. hasOldPodsRunning: true,
  85. },
  86. {
  87. name: "old RSs without pods but with non-zero status replicas",
  88. oldRSs: []*apps.ReplicaSet{newRSWithStatus("rs-1", 0, 1, nil)},
  89. hasOldPodsRunning: true,
  90. },
  91. {
  92. name: "old RSs without pods or non-zero status replicas",
  93. oldRSs: []*apps.ReplicaSet{newRSWithStatus("rs-1", 0, 0, nil)},
  94. hasOldPodsRunning: false,
  95. },
  96. {
  97. name: "old RSs with zero status replicas but pods in terminal state are present",
  98. oldRSs: []*apps.ReplicaSet{newRSWithStatus("rs-1", 0, 0, nil)},
  99. podMap: map[types.UID][]*v1.Pod{
  100. "uid-1": {
  101. {
  102. Status: v1.PodStatus{
  103. Phase: v1.PodFailed,
  104. },
  105. },
  106. {
  107. Status: v1.PodStatus{
  108. Phase: v1.PodSucceeded,
  109. },
  110. },
  111. },
  112. },
  113. hasOldPodsRunning: false,
  114. },
  115. {
  116. name: "old RSs with zero status replicas but pod in unknown phase present",
  117. oldRSs: []*apps.ReplicaSet{newRSWithStatus("rs-1", 0, 0, nil)},
  118. podMap: map[types.UID][]*v1.Pod{
  119. "uid-1": {
  120. {
  121. Status: v1.PodStatus{
  122. Phase: v1.PodUnknown,
  123. },
  124. },
  125. },
  126. },
  127. hasOldPodsRunning: true,
  128. },
  129. {
  130. name: "old RSs with zero status replicas with pending pod present",
  131. oldRSs: []*apps.ReplicaSet{newRSWithStatus("rs-1", 0, 0, nil)},
  132. podMap: map[types.UID][]*v1.Pod{
  133. "uid-1": {
  134. {
  135. Status: v1.PodStatus{
  136. Phase: v1.PodPending,
  137. },
  138. },
  139. },
  140. },
  141. hasOldPodsRunning: true,
  142. },
  143. {
  144. name: "old RSs with zero status replicas with running pod present",
  145. oldRSs: []*apps.ReplicaSet{newRSWithStatus("rs-1", 0, 0, nil)},
  146. podMap: map[types.UID][]*v1.Pod{
  147. "uid-1": {
  148. {
  149. Status: v1.PodStatus{
  150. Phase: v1.PodRunning,
  151. },
  152. },
  153. },
  154. },
  155. hasOldPodsRunning: true,
  156. },
  157. {
  158. name: "old RSs with zero status replicas but pods in terminal state and pending are present",
  159. oldRSs: []*apps.ReplicaSet{newRSWithStatus("rs-1", 0, 0, nil)},
  160. podMap: map[types.UID][]*v1.Pod{
  161. "uid-1": {
  162. {
  163. Status: v1.PodStatus{
  164. Phase: v1.PodFailed,
  165. },
  166. },
  167. {
  168. Status: v1.PodStatus{
  169. Phase: v1.PodSucceeded,
  170. },
  171. },
  172. },
  173. "uid-2": {},
  174. "uid-3": {
  175. {
  176. Status: v1.PodStatus{
  177. Phase: v1.PodPending,
  178. },
  179. },
  180. },
  181. },
  182. hasOldPodsRunning: true,
  183. },
  184. }
  185. for _, test := range tests {
  186. t.Run(test.name, func(t *testing.T) {
  187. if expected, got := test.hasOldPodsRunning, oldPodsRunning(test.newRS, test.oldRSs, test.podMap); expected != got {
  188. t.Errorf("%s: expected %t, got %t", test.name, expected, got)
  189. }
  190. })
  191. }
  192. }
  193. func rsWithUID(uid string) *apps.ReplicaSet {
  194. d := newDeployment("foo", 1, nil, nil, nil, map[string]string{"foo": "bar"})
  195. rs := newReplicaSet(d, fmt.Sprintf("foo-%s", uid), 0)
  196. rs.UID = types.UID(uid)
  197. return rs
  198. }
  199. func podMapWithUIDs(uids []string) map[types.UID][]*v1.Pod {
  200. podMap := make(map[types.UID][]*v1.Pod)
  201. for _, uid := range uids {
  202. podMap[types.UID(uid)] = []*v1.Pod{
  203. { /* supposedly a pod */ },
  204. { /* supposedly another pod pod */ },
  205. }
  206. }
  207. return podMap
  208. }