rollout_status_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 kubectl
  14. import (
  15. "fmt"
  16. "testing"
  17. apps "k8s.io/api/apps/v1"
  18. api "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  21. "k8s.io/kubernetes/pkg/kubectl/scheme"
  22. )
  23. func TestDeploymentStatusViewerStatus(t *testing.T) {
  24. tests := []struct {
  25. name string
  26. generation int64
  27. specReplicas int32
  28. status apps.DeploymentStatus
  29. msg string
  30. done bool
  31. }{
  32. {
  33. name: "test1",
  34. generation: 0,
  35. specReplicas: 1,
  36. status: apps.DeploymentStatus{
  37. ObservedGeneration: 1,
  38. Replicas: 1,
  39. UpdatedReplicas: 0,
  40. AvailableReplicas: 1,
  41. UnavailableReplicas: 0,
  42. },
  43. msg: "Waiting for deployment \"foo\" rollout to finish: 0 out of 1 new replicas have been updated...\n",
  44. done: false,
  45. },
  46. {
  47. name: "test2",
  48. generation: 1,
  49. specReplicas: 1,
  50. status: apps.DeploymentStatus{
  51. ObservedGeneration: 1,
  52. Replicas: 2,
  53. UpdatedReplicas: 1,
  54. AvailableReplicas: 2,
  55. UnavailableReplicas: 0,
  56. },
  57. msg: "Waiting for deployment \"foo\" rollout to finish: 1 old replicas are pending termination...\n",
  58. done: false,
  59. },
  60. {
  61. name: "test3",
  62. generation: 1,
  63. specReplicas: 2,
  64. status: apps.DeploymentStatus{
  65. ObservedGeneration: 1,
  66. Replicas: 2,
  67. UpdatedReplicas: 2,
  68. AvailableReplicas: 1,
  69. UnavailableReplicas: 1,
  70. },
  71. msg: "Waiting for deployment \"foo\" rollout to finish: 1 of 2 updated replicas are available...\n",
  72. done: false,
  73. },
  74. {
  75. name: "test4",
  76. generation: 1,
  77. specReplicas: 2,
  78. status: apps.DeploymentStatus{
  79. ObservedGeneration: 1,
  80. Replicas: 2,
  81. UpdatedReplicas: 2,
  82. AvailableReplicas: 2,
  83. UnavailableReplicas: 0,
  84. },
  85. msg: "deployment \"foo\" successfully rolled out\n",
  86. done: true,
  87. },
  88. {
  89. name: "test5",
  90. generation: 2,
  91. specReplicas: 2,
  92. status: apps.DeploymentStatus{
  93. ObservedGeneration: 1,
  94. Replicas: 2,
  95. UpdatedReplicas: 2,
  96. AvailableReplicas: 2,
  97. UnavailableReplicas: 0,
  98. },
  99. msg: "Waiting for deployment spec update to be observed...\n",
  100. done: false,
  101. },
  102. }
  103. for _, test := range tests {
  104. t.Run(test.name, func(t *testing.T) {
  105. d := &apps.Deployment{
  106. ObjectMeta: metav1.ObjectMeta{
  107. Namespace: "bar",
  108. Name: "foo",
  109. UID: "8764ae47-9092-11e4-8393-42010af018ff",
  110. Generation: test.generation,
  111. },
  112. Spec: apps.DeploymentSpec{
  113. Replicas: &test.specReplicas,
  114. },
  115. Status: test.status,
  116. }
  117. unstructuredD := &unstructured.Unstructured{}
  118. err := scheme.Scheme.Convert(d, unstructuredD, nil)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. dsv := &DeploymentStatusViewer{}
  123. msg, done, err := dsv.Status(unstructuredD, 0)
  124. if err != nil {
  125. t.Fatalf("DeploymentStatusViewer.Status(): %v", err)
  126. }
  127. if done != test.done || msg != test.msg {
  128. t.Errorf("DeploymentStatusViewer.Status() for deployment with generation %d, %d replicas specified, and status %+v returned %q, %t, want %q, %t",
  129. test.generation,
  130. test.specReplicas,
  131. test.status,
  132. msg,
  133. done,
  134. test.msg,
  135. test.done,
  136. )
  137. }
  138. })
  139. }
  140. }
  141. func TestDaemonSetStatusViewerStatus(t *testing.T) {
  142. tests := []struct {
  143. name string
  144. generation int64
  145. status apps.DaemonSetStatus
  146. msg string
  147. done bool
  148. }{
  149. {
  150. name: "test1",
  151. generation: 0,
  152. status: apps.DaemonSetStatus{
  153. ObservedGeneration: 1,
  154. UpdatedNumberScheduled: 0,
  155. DesiredNumberScheduled: 1,
  156. NumberAvailable: 0,
  157. },
  158. msg: "Waiting for daemon set \"foo\" rollout to finish: 0 out of 1 new pods have been updated...\n",
  159. done: false,
  160. },
  161. {
  162. name: "test2",
  163. generation: 1,
  164. status: apps.DaemonSetStatus{
  165. ObservedGeneration: 1,
  166. UpdatedNumberScheduled: 2,
  167. DesiredNumberScheduled: 2,
  168. NumberAvailable: 1,
  169. },
  170. msg: "Waiting for daemon set \"foo\" rollout to finish: 1 of 2 updated pods are available...\n",
  171. done: false,
  172. },
  173. {
  174. name: "test3",
  175. generation: 1,
  176. status: apps.DaemonSetStatus{
  177. ObservedGeneration: 1,
  178. UpdatedNumberScheduled: 2,
  179. DesiredNumberScheduled: 2,
  180. NumberAvailable: 2,
  181. },
  182. msg: "daemon set \"foo\" successfully rolled out\n",
  183. done: true,
  184. },
  185. {
  186. name: "test4",
  187. generation: 2,
  188. status: apps.DaemonSetStatus{
  189. ObservedGeneration: 1,
  190. UpdatedNumberScheduled: 2,
  191. DesiredNumberScheduled: 2,
  192. NumberAvailable: 2,
  193. },
  194. msg: "Waiting for daemon set spec update to be observed...\n",
  195. done: false,
  196. },
  197. }
  198. for _, test := range tests {
  199. t.Run(test.name, func(t *testing.T) {
  200. d := &apps.DaemonSet{
  201. ObjectMeta: metav1.ObjectMeta{
  202. Namespace: "bar",
  203. Name: "foo",
  204. UID: "8764ae47-9092-11e4-8393-42010af018ff",
  205. Generation: test.generation,
  206. },
  207. Spec: apps.DaemonSetSpec{
  208. UpdateStrategy: apps.DaemonSetUpdateStrategy{
  209. Type: apps.RollingUpdateDaemonSetStrategyType,
  210. },
  211. },
  212. Status: test.status,
  213. }
  214. unstructuredD := &unstructured.Unstructured{}
  215. err := scheme.Scheme.Convert(d, unstructuredD, nil)
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. dsv := &DaemonSetStatusViewer{}
  220. msg, done, err := dsv.Status(unstructuredD, 0)
  221. if err != nil {
  222. t.Fatalf("unexpected error: %v", err)
  223. }
  224. if done != test.done || msg != test.msg {
  225. t.Errorf("daemon set with generation %d, %d pods specified, and status:\n%+v\nreturned:\n%q, %t\nwant:\n%q, %t",
  226. test.generation,
  227. d.Status.DesiredNumberScheduled,
  228. test.status,
  229. msg,
  230. done,
  231. test.msg,
  232. test.done,
  233. )
  234. }
  235. })
  236. }
  237. }
  238. func TestStatefulSetStatusViewerStatus(t *testing.T) {
  239. tests := []struct {
  240. name string
  241. generation int64
  242. strategy apps.StatefulSetUpdateStrategy
  243. status apps.StatefulSetStatus
  244. msg string
  245. done bool
  246. err bool
  247. }{
  248. {
  249. name: "on delete returns an error",
  250. generation: 1,
  251. strategy: apps.StatefulSetUpdateStrategy{Type: apps.OnDeleteStatefulSetStrategyType},
  252. status: apps.StatefulSetStatus{
  253. ObservedGeneration: 1,
  254. Replicas: 0,
  255. ReadyReplicas: 1,
  256. CurrentReplicas: 0,
  257. UpdatedReplicas: 0,
  258. },
  259. msg: "",
  260. done: true,
  261. err: true,
  262. },
  263. {
  264. name: "unobserved update is not complete",
  265. generation: 2,
  266. strategy: apps.StatefulSetUpdateStrategy{Type: apps.RollingUpdateStatefulSetStrategyType},
  267. status: apps.StatefulSetStatus{
  268. ObservedGeneration: 1,
  269. Replicas: 3,
  270. ReadyReplicas: 3,
  271. CurrentReplicas: 3,
  272. UpdatedReplicas: 0,
  273. },
  274. msg: "Waiting for statefulset spec update to be observed...\n",
  275. done: false,
  276. err: false,
  277. },
  278. {
  279. name: "if all pods are not ready the update is not complete",
  280. generation: 1,
  281. strategy: apps.StatefulSetUpdateStrategy{Type: apps.RollingUpdateStatefulSetStrategyType},
  282. status: apps.StatefulSetStatus{
  283. ObservedGeneration: 2,
  284. Replicas: 3,
  285. ReadyReplicas: 2,
  286. CurrentReplicas: 3,
  287. UpdatedReplicas: 0,
  288. },
  289. msg: fmt.Sprintf("Waiting for %d pods to be ready...\n", 1),
  290. done: false,
  291. err: false,
  292. },
  293. {
  294. name: "partition update completes when all replicas above the partition are updated",
  295. generation: 1,
  296. strategy: apps.StatefulSetUpdateStrategy{Type: apps.RollingUpdateStatefulSetStrategyType,
  297. RollingUpdate: func() *apps.RollingUpdateStatefulSetStrategy {
  298. partition := int32(2)
  299. return &apps.RollingUpdateStatefulSetStrategy{Partition: &partition}
  300. }()},
  301. status: apps.StatefulSetStatus{
  302. ObservedGeneration: 2,
  303. Replicas: 3,
  304. ReadyReplicas: 3,
  305. CurrentReplicas: 2,
  306. UpdatedReplicas: 1,
  307. },
  308. msg: fmt.Sprintf("partitioned roll out complete: %d new pods have been updated...\n", 1),
  309. done: true,
  310. err: false,
  311. },
  312. {
  313. name: "partition update is in progress if all pods above the partition have not been updated",
  314. generation: 1,
  315. strategy: apps.StatefulSetUpdateStrategy{Type: apps.RollingUpdateStatefulSetStrategyType,
  316. RollingUpdate: func() *apps.RollingUpdateStatefulSetStrategy {
  317. partition := int32(2)
  318. return &apps.RollingUpdateStatefulSetStrategy{Partition: &partition}
  319. }()},
  320. status: apps.StatefulSetStatus{
  321. ObservedGeneration: 2,
  322. Replicas: 3,
  323. ReadyReplicas: 3,
  324. CurrentReplicas: 3,
  325. UpdatedReplicas: 0,
  326. },
  327. msg: fmt.Sprintf("Waiting for partitioned roll out to finish: %d out of %d new pods have been updated...\n", 0, 1),
  328. done: true,
  329. err: false,
  330. },
  331. {
  332. name: "update completes when all replicas are current",
  333. generation: 1,
  334. strategy: apps.StatefulSetUpdateStrategy{Type: apps.RollingUpdateStatefulSetStrategyType},
  335. status: apps.StatefulSetStatus{
  336. ObservedGeneration: 2,
  337. Replicas: 3,
  338. ReadyReplicas: 3,
  339. CurrentReplicas: 3,
  340. UpdatedReplicas: 3,
  341. CurrentRevision: "foo",
  342. UpdateRevision: "foo",
  343. },
  344. msg: fmt.Sprintf("statefulset rolling update complete %d pods at revision %s...\n", 3, "foo"),
  345. done: true,
  346. err: false,
  347. },
  348. }
  349. for _, test := range tests {
  350. t.Run(test.name, func(t *testing.T) {
  351. s := newStatefulSet(3)
  352. s.Status = test.status
  353. s.Spec.UpdateStrategy = test.strategy
  354. s.Generation = test.generation
  355. unstructuredS := &unstructured.Unstructured{}
  356. err := scheme.Scheme.Convert(s, unstructuredS, nil)
  357. if err != nil {
  358. t.Fatal(err)
  359. }
  360. dsv := &StatefulSetStatusViewer{}
  361. msg, done, err := dsv.Status(unstructuredS, 0)
  362. if test.err && err == nil {
  363. t.Fatalf("%s: expected error", test.name)
  364. }
  365. if !test.err && err != nil {
  366. t.Fatalf("%s: %s", test.name, err)
  367. }
  368. if done && !test.done {
  369. t.Errorf("%s: want done %v got %v", test.name, done, test.done)
  370. }
  371. if msg != test.msg {
  372. t.Errorf("%s: want message %s got %s", test.name, test.msg, msg)
  373. }
  374. })
  375. }
  376. }
  377. func TestDaemonSetStatusViewerStatusWithWrongUpdateStrategyType(t *testing.T) {
  378. d := &apps.DaemonSet{
  379. ObjectMeta: metav1.ObjectMeta{
  380. Namespace: "bar",
  381. Name: "foo",
  382. UID: "8764ae47-9092-11e4-8393-42010af018ff",
  383. },
  384. Spec: apps.DaemonSetSpec{
  385. UpdateStrategy: apps.DaemonSetUpdateStrategy{
  386. Type: apps.OnDeleteDaemonSetStrategyType,
  387. },
  388. },
  389. }
  390. unstructuredD := &unstructured.Unstructured{}
  391. err := scheme.Scheme.Convert(d, unstructuredD, nil)
  392. if err != nil {
  393. t.Fatal(err)
  394. }
  395. dsv := &DaemonSetStatusViewer{}
  396. msg, done, err := dsv.Status(unstructuredD, 0)
  397. errMsg := "rollout status is only available for RollingUpdate strategy type"
  398. if err == nil || err.Error() != errMsg {
  399. t.Errorf("Status for daemon sets with UpdateStrategy type different than RollingUpdate should return error. Instead got: msg: %s\ndone: %t\n err: %v", msg, done, err)
  400. }
  401. }
  402. func newStatefulSet(replicas int32) *apps.StatefulSet {
  403. return &apps.StatefulSet{
  404. ObjectMeta: metav1.ObjectMeta{
  405. Name: "foo",
  406. Namespace: metav1.NamespaceDefault,
  407. Labels: map[string]string{"a": "b"},
  408. },
  409. Spec: apps.StatefulSetSpec{
  410. PodManagementPolicy: apps.OrderedReadyPodManagement,
  411. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
  412. Template: api.PodTemplateSpec{
  413. ObjectMeta: metav1.ObjectMeta{
  414. Labels: map[string]string{"a": "b"},
  415. },
  416. Spec: api.PodSpec{
  417. Containers: []api.Container{
  418. {
  419. Name: "test",
  420. Image: "test_image",
  421. ImagePullPolicy: api.PullIfNotPresent,
  422. },
  423. },
  424. RestartPolicy: api.RestartPolicyAlways,
  425. DNSPolicy: api.DNSClusterFirst,
  426. },
  427. },
  428. Replicas: &replicas,
  429. UpdateStrategy: apps.StatefulSetUpdateStrategy{Type: apps.RollingUpdateStatefulSetStrategyType},
  430. },
  431. Status: apps.StatefulSetStatus{},
  432. }
  433. }