generate_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. Copyright 2014 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 status
  14. import (
  15. "reflect"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "k8s.io/api/core/v1"
  19. )
  20. func TestGenerateContainersReadyCondition(t *testing.T) {
  21. tests := []struct {
  22. spec *v1.PodSpec
  23. containerStatuses []v1.ContainerStatus
  24. podPhase v1.PodPhase
  25. expectReady v1.PodCondition
  26. }{
  27. {
  28. spec: nil,
  29. containerStatuses: nil,
  30. podPhase: v1.PodRunning,
  31. expectReady: getPodCondition(v1.ContainersReady, v1.ConditionFalse, UnknownContainerStatuses, ""),
  32. },
  33. {
  34. spec: &v1.PodSpec{},
  35. containerStatuses: []v1.ContainerStatus{},
  36. podPhase: v1.PodRunning,
  37. expectReady: getPodCondition(v1.ContainersReady, v1.ConditionTrue, "", ""),
  38. },
  39. {
  40. spec: &v1.PodSpec{
  41. Containers: []v1.Container{
  42. {Name: "1234"},
  43. },
  44. },
  45. containerStatuses: []v1.ContainerStatus{},
  46. podPhase: v1.PodRunning,
  47. expectReady: getPodCondition(v1.ContainersReady, v1.ConditionFalse, ContainersNotReady, "containers with unknown status: [1234]"),
  48. },
  49. {
  50. spec: &v1.PodSpec{
  51. Containers: []v1.Container{
  52. {Name: "1234"},
  53. {Name: "5678"},
  54. },
  55. },
  56. containerStatuses: []v1.ContainerStatus{
  57. getReadyStatus("1234"),
  58. getReadyStatus("5678"),
  59. },
  60. podPhase: v1.PodRunning,
  61. expectReady: getPodCondition(v1.ContainersReady, v1.ConditionTrue, "", ""),
  62. },
  63. {
  64. spec: &v1.PodSpec{
  65. Containers: []v1.Container{
  66. {Name: "1234"},
  67. {Name: "5678"},
  68. },
  69. },
  70. containerStatuses: []v1.ContainerStatus{
  71. getReadyStatus("1234"),
  72. },
  73. podPhase: v1.PodRunning,
  74. expectReady: getPodCondition(v1.ContainersReady, v1.ConditionFalse, ContainersNotReady, "containers with unknown status: [5678]"),
  75. },
  76. {
  77. spec: &v1.PodSpec{
  78. Containers: []v1.Container{
  79. {Name: "1234"},
  80. {Name: "5678"},
  81. },
  82. },
  83. containerStatuses: []v1.ContainerStatus{
  84. getReadyStatus("1234"),
  85. getNotReadyStatus("5678"),
  86. },
  87. podPhase: v1.PodRunning,
  88. expectReady: getPodCondition(v1.ContainersReady, v1.ConditionFalse, ContainersNotReady, "containers with unready status: [5678]"),
  89. },
  90. {
  91. spec: &v1.PodSpec{
  92. Containers: []v1.Container{
  93. {Name: "1234"},
  94. },
  95. },
  96. containerStatuses: []v1.ContainerStatus{
  97. getNotReadyStatus("1234"),
  98. },
  99. podPhase: v1.PodSucceeded,
  100. expectReady: getPodCondition(v1.ContainersReady, v1.ConditionFalse, PodCompleted, ""),
  101. },
  102. }
  103. for i, test := range tests {
  104. ready := GenerateContainersReadyCondition(test.spec, test.containerStatuses, test.podPhase)
  105. if !reflect.DeepEqual(ready, test.expectReady) {
  106. t.Errorf("On test case %v, expectReady:\n%+v\ngot\n%+v\n", i, test.expectReady, ready)
  107. }
  108. }
  109. }
  110. func TestGeneratePodReadyCondition(t *testing.T) {
  111. tests := []struct {
  112. spec *v1.PodSpec
  113. conditions []v1.PodCondition
  114. containerStatuses []v1.ContainerStatus
  115. podPhase v1.PodPhase
  116. expectReady v1.PodCondition
  117. }{
  118. {
  119. spec: nil,
  120. conditions: nil,
  121. containerStatuses: nil,
  122. podPhase: v1.PodRunning,
  123. expectReady: getPodCondition(v1.PodReady, v1.ConditionFalse, UnknownContainerStatuses, ""),
  124. },
  125. {
  126. spec: &v1.PodSpec{},
  127. conditions: nil,
  128. containerStatuses: []v1.ContainerStatus{},
  129. podPhase: v1.PodRunning,
  130. expectReady: getPodCondition(v1.PodReady, v1.ConditionTrue, "", ""),
  131. },
  132. {
  133. spec: &v1.PodSpec{
  134. Containers: []v1.Container{
  135. {Name: "1234"},
  136. },
  137. },
  138. conditions: nil,
  139. containerStatuses: []v1.ContainerStatus{},
  140. podPhase: v1.PodRunning,
  141. expectReady: getPodCondition(v1.PodReady, v1.ConditionFalse, ContainersNotReady, "containers with unknown status: [1234]"),
  142. },
  143. {
  144. spec: &v1.PodSpec{
  145. Containers: []v1.Container{
  146. {Name: "1234"},
  147. {Name: "5678"},
  148. },
  149. },
  150. conditions: nil,
  151. containerStatuses: []v1.ContainerStatus{
  152. getReadyStatus("1234"),
  153. getReadyStatus("5678"),
  154. },
  155. podPhase: v1.PodRunning,
  156. expectReady: getPodCondition(v1.PodReady, v1.ConditionTrue, "", ""),
  157. },
  158. {
  159. spec: &v1.PodSpec{
  160. Containers: []v1.Container{
  161. {Name: "1234"},
  162. {Name: "5678"},
  163. },
  164. },
  165. conditions: nil,
  166. containerStatuses: []v1.ContainerStatus{
  167. getReadyStatus("1234"),
  168. },
  169. podPhase: v1.PodRunning,
  170. expectReady: getPodCondition(v1.PodReady, v1.ConditionFalse, ContainersNotReady, "containers with unknown status: [5678]"),
  171. },
  172. {
  173. spec: &v1.PodSpec{
  174. Containers: []v1.Container{
  175. {Name: "1234"},
  176. {Name: "5678"},
  177. },
  178. },
  179. conditions: nil,
  180. containerStatuses: []v1.ContainerStatus{
  181. getReadyStatus("1234"),
  182. getNotReadyStatus("5678"),
  183. },
  184. podPhase: v1.PodRunning,
  185. expectReady: getPodCondition(v1.PodReady, v1.ConditionFalse, ContainersNotReady, "containers with unready status: [5678]"),
  186. },
  187. {
  188. spec: &v1.PodSpec{
  189. Containers: []v1.Container{
  190. {Name: "1234"},
  191. },
  192. },
  193. conditions: nil,
  194. containerStatuses: []v1.ContainerStatus{
  195. getNotReadyStatus("1234"),
  196. },
  197. podPhase: v1.PodSucceeded,
  198. expectReady: getPodCondition(v1.PodReady, v1.ConditionFalse, PodCompleted, ""),
  199. },
  200. {
  201. spec: &v1.PodSpec{
  202. ReadinessGates: []v1.PodReadinessGate{
  203. {ConditionType: v1.PodConditionType("gate1")},
  204. },
  205. },
  206. conditions: nil,
  207. containerStatuses: []v1.ContainerStatus{},
  208. podPhase: v1.PodRunning,
  209. expectReady: getPodCondition(v1.PodReady, v1.ConditionFalse, ReadinessGatesNotReady, `corresponding condition of pod readiness gate "gate1" does not exist.`),
  210. },
  211. {
  212. spec: &v1.PodSpec{
  213. ReadinessGates: []v1.PodReadinessGate{
  214. {ConditionType: v1.PodConditionType("gate1")},
  215. },
  216. },
  217. conditions: []v1.PodCondition{
  218. getPodCondition("gate1", v1.ConditionFalse, "", ""),
  219. },
  220. containerStatuses: []v1.ContainerStatus{},
  221. podPhase: v1.PodRunning,
  222. expectReady: getPodCondition(v1.PodReady, v1.ConditionFalse, ReadinessGatesNotReady, `the status of pod readiness gate "gate1" is not "True", but False`),
  223. },
  224. {
  225. spec: &v1.PodSpec{
  226. ReadinessGates: []v1.PodReadinessGate{
  227. {ConditionType: v1.PodConditionType("gate1")},
  228. },
  229. },
  230. conditions: []v1.PodCondition{
  231. getPodCondition("gate1", v1.ConditionTrue, "", ""),
  232. },
  233. containerStatuses: []v1.ContainerStatus{},
  234. podPhase: v1.PodRunning,
  235. expectReady: getPodCondition(v1.PodReady, v1.ConditionTrue, "", ""),
  236. },
  237. {
  238. spec: &v1.PodSpec{
  239. ReadinessGates: []v1.PodReadinessGate{
  240. {ConditionType: v1.PodConditionType("gate1")},
  241. {ConditionType: v1.PodConditionType("gate2")},
  242. },
  243. },
  244. conditions: []v1.PodCondition{
  245. getPodCondition("gate1", v1.ConditionTrue, "", ""),
  246. },
  247. containerStatuses: []v1.ContainerStatus{},
  248. podPhase: v1.PodRunning,
  249. expectReady: getPodCondition(v1.PodReady, v1.ConditionFalse, ReadinessGatesNotReady, `corresponding condition of pod readiness gate "gate2" does not exist.`),
  250. },
  251. {
  252. spec: &v1.PodSpec{
  253. ReadinessGates: []v1.PodReadinessGate{
  254. {ConditionType: v1.PodConditionType("gate1")},
  255. {ConditionType: v1.PodConditionType("gate2")},
  256. },
  257. },
  258. conditions: []v1.PodCondition{
  259. getPodCondition("gate1", v1.ConditionTrue, "", ""),
  260. getPodCondition("gate2", v1.ConditionFalse, "", ""),
  261. },
  262. containerStatuses: []v1.ContainerStatus{},
  263. podPhase: v1.PodRunning,
  264. expectReady: getPodCondition(v1.PodReady, v1.ConditionFalse, ReadinessGatesNotReady, `the status of pod readiness gate "gate2" is not "True", but False`),
  265. },
  266. {
  267. spec: &v1.PodSpec{
  268. ReadinessGates: []v1.PodReadinessGate{
  269. {ConditionType: v1.PodConditionType("gate1")},
  270. {ConditionType: v1.PodConditionType("gate2")},
  271. },
  272. },
  273. conditions: []v1.PodCondition{
  274. getPodCondition("gate1", v1.ConditionTrue, "", ""),
  275. getPodCondition("gate2", v1.ConditionTrue, "", ""),
  276. },
  277. containerStatuses: []v1.ContainerStatus{},
  278. podPhase: v1.PodRunning,
  279. expectReady: getPodCondition(v1.PodReady, v1.ConditionTrue, "", ""),
  280. },
  281. {
  282. spec: &v1.PodSpec{
  283. Containers: []v1.Container{
  284. {Name: "1234"},
  285. },
  286. ReadinessGates: []v1.PodReadinessGate{
  287. {ConditionType: v1.PodConditionType("gate1")},
  288. },
  289. },
  290. conditions: []v1.PodCondition{
  291. getPodCondition("gate1", v1.ConditionTrue, "", ""),
  292. },
  293. containerStatuses: []v1.ContainerStatus{getNotReadyStatus("1234")},
  294. podPhase: v1.PodRunning,
  295. expectReady: getPodCondition(v1.PodReady, v1.ConditionFalse, ContainersNotReady, "containers with unready status: [1234]"),
  296. },
  297. }
  298. for i, test := range tests {
  299. ready := GeneratePodReadyCondition(test.spec, test.conditions, test.containerStatuses, test.podPhase)
  300. if !reflect.DeepEqual(ready, test.expectReady) {
  301. t.Errorf("On test case %v, expectReady:\n%+v\ngot\n%+v\n", i, test.expectReady, ready)
  302. }
  303. }
  304. }
  305. func TestGeneratePodInitializedCondition(t *testing.T) {
  306. noInitContainer := &v1.PodSpec{}
  307. oneInitContainer := &v1.PodSpec{
  308. InitContainers: []v1.Container{
  309. {Name: "1234"},
  310. },
  311. }
  312. twoInitContainer := &v1.PodSpec{
  313. InitContainers: []v1.Container{
  314. {Name: "1234"},
  315. {Name: "5678"},
  316. },
  317. }
  318. tests := []struct {
  319. spec *v1.PodSpec
  320. containerStatuses []v1.ContainerStatus
  321. podPhase v1.PodPhase
  322. expected v1.PodCondition
  323. }{
  324. {
  325. spec: twoInitContainer,
  326. containerStatuses: nil,
  327. podPhase: v1.PodRunning,
  328. expected: v1.PodCondition{
  329. Status: v1.ConditionFalse,
  330. Reason: UnknownContainerStatuses,
  331. },
  332. },
  333. {
  334. spec: noInitContainer,
  335. containerStatuses: []v1.ContainerStatus{},
  336. podPhase: v1.PodRunning,
  337. expected: v1.PodCondition{
  338. Status: v1.ConditionTrue,
  339. Reason: "",
  340. },
  341. },
  342. {
  343. spec: oneInitContainer,
  344. containerStatuses: []v1.ContainerStatus{},
  345. podPhase: v1.PodRunning,
  346. expected: v1.PodCondition{
  347. Status: v1.ConditionFalse,
  348. Reason: ContainersNotInitialized,
  349. },
  350. },
  351. {
  352. spec: twoInitContainer,
  353. containerStatuses: []v1.ContainerStatus{
  354. getReadyStatus("1234"),
  355. getReadyStatus("5678"),
  356. },
  357. podPhase: v1.PodRunning,
  358. expected: v1.PodCondition{
  359. Status: v1.ConditionTrue,
  360. Reason: "",
  361. },
  362. },
  363. {
  364. spec: twoInitContainer,
  365. containerStatuses: []v1.ContainerStatus{
  366. getReadyStatus("1234"),
  367. },
  368. podPhase: v1.PodRunning,
  369. expected: v1.PodCondition{
  370. Status: v1.ConditionFalse,
  371. Reason: ContainersNotInitialized,
  372. },
  373. },
  374. {
  375. spec: twoInitContainer,
  376. containerStatuses: []v1.ContainerStatus{
  377. getReadyStatus("1234"),
  378. getNotReadyStatus("5678"),
  379. },
  380. podPhase: v1.PodRunning,
  381. expected: v1.PodCondition{
  382. Status: v1.ConditionFalse,
  383. Reason: ContainersNotInitialized,
  384. },
  385. },
  386. {
  387. spec: oneInitContainer,
  388. containerStatuses: []v1.ContainerStatus{
  389. getReadyStatus("1234"),
  390. },
  391. podPhase: v1.PodSucceeded,
  392. expected: v1.PodCondition{
  393. Status: v1.ConditionTrue,
  394. Reason: PodCompleted,
  395. },
  396. },
  397. }
  398. for _, test := range tests {
  399. test.expected.Type = v1.PodInitialized
  400. condition := GeneratePodInitializedCondition(test.spec, test.containerStatuses, test.podPhase)
  401. assert.Equal(t, test.expected.Type, condition.Type)
  402. assert.Equal(t, test.expected.Status, condition.Status)
  403. assert.Equal(t, test.expected.Reason, condition.Reason)
  404. }
  405. }
  406. func getPodCondition(conditionType v1.PodConditionType, status v1.ConditionStatus, reason, message string) v1.PodCondition {
  407. return v1.PodCondition{
  408. Type: conditionType,
  409. Status: status,
  410. Reason: reason,
  411. Message: message,
  412. }
  413. }
  414. func getReadyStatus(cName string) v1.ContainerStatus {
  415. return v1.ContainerStatus{
  416. Name: cName,
  417. Ready: true,
  418. }
  419. }
  420. func getNotReadyStatus(cName string) v1.ContainerStatus {
  421. return v1.ContainerStatus{
  422. Name: cName,
  423. Ready: false,
  424. }
  425. }