replica_set_utils_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. Copyright 2017 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. // If you make changes to this file, you should also make the corresponding change in ReplicationController.
  14. package replicaset
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. apps "k8s.io/api/apps/v1"
  20. "k8s.io/api/core/v1"
  21. )
  22. func TestCalculateStatus(t *testing.T) {
  23. labelMap := map[string]string{"name": "foo"}
  24. fullLabelMap := map[string]string{"name": "foo", "type": "production"}
  25. notFullyLabelledRS := newReplicaSet(1, labelMap)
  26. // Set replica num to 2 for status condition testing (diff < 0, diff > 0)
  27. fullyLabelledRS := newReplicaSet(2, fullLabelMap)
  28. longMinReadySecondsRS := newReplicaSet(1, fullLabelMap)
  29. longMinReadySecondsRS.Spec.MinReadySeconds = 3600
  30. rsStatusTests := []struct {
  31. name string
  32. replicaset *apps.ReplicaSet
  33. filteredPods []*v1.Pod
  34. expectedReplicaSetStatus apps.ReplicaSetStatus
  35. }{
  36. {
  37. "1 fully labelled pod",
  38. fullyLabelledRS,
  39. []*v1.Pod{
  40. newPod("pod1", fullyLabelledRS, v1.PodRunning, nil, true),
  41. },
  42. apps.ReplicaSetStatus{
  43. Replicas: 1,
  44. FullyLabeledReplicas: 1,
  45. ReadyReplicas: 1,
  46. AvailableReplicas: 1,
  47. },
  48. },
  49. {
  50. "1 not fully labelled pod",
  51. notFullyLabelledRS,
  52. []*v1.Pod{
  53. newPod("pod1", notFullyLabelledRS, v1.PodRunning, nil, true),
  54. },
  55. apps.ReplicaSetStatus{
  56. Replicas: 1,
  57. FullyLabeledReplicas: 0,
  58. ReadyReplicas: 1,
  59. AvailableReplicas: 1,
  60. },
  61. },
  62. {
  63. "2 fully labelled pods",
  64. fullyLabelledRS,
  65. []*v1.Pod{
  66. newPod("pod1", fullyLabelledRS, v1.PodRunning, nil, true),
  67. newPod("pod2", fullyLabelledRS, v1.PodRunning, nil, true),
  68. },
  69. apps.ReplicaSetStatus{
  70. Replicas: 2,
  71. FullyLabeledReplicas: 2,
  72. ReadyReplicas: 2,
  73. AvailableReplicas: 2,
  74. },
  75. },
  76. {
  77. "2 not fully labelled pods",
  78. notFullyLabelledRS,
  79. []*v1.Pod{
  80. newPod("pod1", notFullyLabelledRS, v1.PodRunning, nil, true),
  81. newPod("pod2", notFullyLabelledRS, v1.PodRunning, nil, true),
  82. },
  83. apps.ReplicaSetStatus{
  84. Replicas: 2,
  85. FullyLabeledReplicas: 0,
  86. ReadyReplicas: 2,
  87. AvailableReplicas: 2,
  88. },
  89. },
  90. {
  91. "1 fully labelled pod, 1 not fully labelled pod",
  92. notFullyLabelledRS,
  93. []*v1.Pod{
  94. newPod("pod1", notFullyLabelledRS, v1.PodRunning, nil, true),
  95. newPod("pod2", fullyLabelledRS, v1.PodRunning, nil, true),
  96. },
  97. apps.ReplicaSetStatus{
  98. Replicas: 2,
  99. FullyLabeledReplicas: 1,
  100. ReadyReplicas: 2,
  101. AvailableReplicas: 2,
  102. },
  103. },
  104. {
  105. "1 non-ready pod",
  106. fullyLabelledRS,
  107. []*v1.Pod{
  108. newPod("pod1", fullyLabelledRS, v1.PodPending, nil, true),
  109. },
  110. apps.ReplicaSetStatus{
  111. Replicas: 1,
  112. FullyLabeledReplicas: 1,
  113. ReadyReplicas: 0,
  114. AvailableReplicas: 0,
  115. },
  116. },
  117. {
  118. "1 ready but non-available pod",
  119. longMinReadySecondsRS,
  120. []*v1.Pod{
  121. newPod("pod1", longMinReadySecondsRS, v1.PodRunning, nil, true),
  122. },
  123. apps.ReplicaSetStatus{
  124. Replicas: 1,
  125. FullyLabeledReplicas: 1,
  126. ReadyReplicas: 1,
  127. AvailableReplicas: 0,
  128. },
  129. },
  130. }
  131. for _, test := range rsStatusTests {
  132. replicaSetStatus := calculateStatus(test.replicaset, test.filteredPods, nil)
  133. if !reflect.DeepEqual(replicaSetStatus, test.expectedReplicaSetStatus) {
  134. t.Errorf("%s: unexpected replicaset status: expected %v, got %v", test.name, test.expectedReplicaSetStatus, replicaSetStatus)
  135. }
  136. }
  137. }
  138. func TestCalculateStatusConditions(t *testing.T) {
  139. labelMap := map[string]string{"name": "foo"}
  140. rs := newReplicaSet(2, labelMap)
  141. replicaFailureRS := newReplicaSet(10, labelMap)
  142. replicaFailureRS.Status.Conditions = []apps.ReplicaSetCondition{
  143. {
  144. Type: apps.ReplicaSetReplicaFailure,
  145. Status: v1.ConditionTrue,
  146. },
  147. }
  148. rsStatusConditionTests := []struct {
  149. name string
  150. replicaset *apps.ReplicaSet
  151. filteredPods []*v1.Pod
  152. manageReplicasErr error
  153. expectedReplicaSetConditions []apps.ReplicaSetCondition
  154. }{
  155. {
  156. "manageReplicasErr != nil && failureCond == nil, diff < 0",
  157. rs,
  158. []*v1.Pod{
  159. newPod("pod1", rs, v1.PodRunning, nil, true),
  160. },
  161. fmt.Errorf("fake manageReplicasErr"),
  162. []apps.ReplicaSetCondition{
  163. {
  164. Type: apps.ReplicaSetReplicaFailure,
  165. Status: v1.ConditionTrue,
  166. Reason: "FailedCreate",
  167. Message: "fake manageReplicasErr",
  168. },
  169. },
  170. },
  171. {
  172. "manageReplicasErr != nil && failureCond == nil, diff > 0",
  173. rs,
  174. []*v1.Pod{
  175. newPod("pod1", rs, v1.PodRunning, nil, true),
  176. newPod("pod2", rs, v1.PodRunning, nil, true),
  177. newPod("pod3", rs, v1.PodRunning, nil, true),
  178. },
  179. fmt.Errorf("fake manageReplicasErr"),
  180. []apps.ReplicaSetCondition{
  181. {
  182. Type: apps.ReplicaSetReplicaFailure,
  183. Status: v1.ConditionTrue,
  184. Reason: "FailedDelete",
  185. Message: "fake manageReplicasErr",
  186. },
  187. },
  188. },
  189. {
  190. "manageReplicasErr == nil && failureCond != nil",
  191. replicaFailureRS,
  192. []*v1.Pod{
  193. newPod("pod1", replicaFailureRS, v1.PodRunning, nil, true),
  194. },
  195. nil,
  196. nil,
  197. },
  198. {
  199. "manageReplicasErr != nil && failureCond != nil",
  200. replicaFailureRS,
  201. []*v1.Pod{
  202. newPod("pod1", replicaFailureRS, v1.PodRunning, nil, true),
  203. },
  204. fmt.Errorf("fake manageReplicasErr"),
  205. []apps.ReplicaSetCondition{
  206. {
  207. Type: apps.ReplicaSetReplicaFailure,
  208. Status: v1.ConditionTrue,
  209. },
  210. },
  211. },
  212. {
  213. "manageReplicasErr == nil && failureCond == nil",
  214. rs,
  215. []*v1.Pod{
  216. newPod("pod1", rs, v1.PodRunning, nil, true),
  217. },
  218. nil,
  219. nil,
  220. },
  221. }
  222. for _, test := range rsStatusConditionTests {
  223. replicaSetStatus := calculateStatus(test.replicaset, test.filteredPods, test.manageReplicasErr)
  224. // all test cases have at most 1 status condition
  225. if len(replicaSetStatus.Conditions) > 0 {
  226. test.expectedReplicaSetConditions[0].LastTransitionTime = replicaSetStatus.Conditions[0].LastTransitionTime
  227. }
  228. if !reflect.DeepEqual(replicaSetStatus.Conditions, test.expectedReplicaSetConditions) {
  229. t.Errorf("%s: unexpected replicaset status: expected %v, got %v", test.name, test.expectedReplicaSetConditions, replicaSetStatus.Conditions)
  230. }
  231. }
  232. }