generate_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "k8s.io/kubernetes/pkg/api"
  18. )
  19. func TestGeneratePodReadyCondition(t *testing.T) {
  20. tests := []struct {
  21. spec *api.PodSpec
  22. containerStatuses []api.ContainerStatus
  23. podPhase api.PodPhase
  24. expected api.PodCondition
  25. }{
  26. {
  27. spec: nil,
  28. containerStatuses: nil,
  29. podPhase: api.PodRunning,
  30. expected: getReadyCondition(false, "UnknownContainerStatuses", ""),
  31. },
  32. {
  33. spec: &api.PodSpec{},
  34. containerStatuses: []api.ContainerStatus{},
  35. podPhase: api.PodRunning,
  36. expected: getReadyCondition(true, "", ""),
  37. },
  38. {
  39. spec: &api.PodSpec{
  40. Containers: []api.Container{
  41. {Name: "1234"},
  42. },
  43. },
  44. containerStatuses: []api.ContainerStatus{},
  45. podPhase: api.PodRunning,
  46. expected: getReadyCondition(false, "ContainersNotReady", "containers with unknown status: [1234]"),
  47. },
  48. {
  49. spec: &api.PodSpec{
  50. Containers: []api.Container{
  51. {Name: "1234"},
  52. {Name: "5678"},
  53. },
  54. },
  55. containerStatuses: []api.ContainerStatus{
  56. getReadyStatus("1234"),
  57. getReadyStatus("5678"),
  58. },
  59. podPhase: api.PodRunning,
  60. expected: getReadyCondition(true, "", ""),
  61. },
  62. {
  63. spec: &api.PodSpec{
  64. Containers: []api.Container{
  65. {Name: "1234"},
  66. {Name: "5678"},
  67. },
  68. },
  69. containerStatuses: []api.ContainerStatus{
  70. getReadyStatus("1234"),
  71. },
  72. podPhase: api.PodRunning,
  73. expected: getReadyCondition(false, "ContainersNotReady", "containers with unknown status: [5678]"),
  74. },
  75. {
  76. spec: &api.PodSpec{
  77. Containers: []api.Container{
  78. {Name: "1234"},
  79. {Name: "5678"},
  80. },
  81. },
  82. containerStatuses: []api.ContainerStatus{
  83. getReadyStatus("1234"),
  84. getNotReadyStatus("5678"),
  85. },
  86. podPhase: api.PodRunning,
  87. expected: getReadyCondition(false, "ContainersNotReady", "containers with unready status: [5678]"),
  88. },
  89. {
  90. spec: &api.PodSpec{
  91. Containers: []api.Container{
  92. {Name: "1234"},
  93. },
  94. },
  95. containerStatuses: []api.ContainerStatus{
  96. getNotReadyStatus("1234"),
  97. },
  98. podPhase: api.PodSucceeded,
  99. expected: getReadyCondition(false, "PodCompleted", ""),
  100. },
  101. }
  102. for i, test := range tests {
  103. condition := GeneratePodReadyCondition(test.spec, test.containerStatuses, test.podPhase)
  104. if !reflect.DeepEqual(condition, test.expected) {
  105. t.Errorf("On test case %v, expected:\n%+v\ngot\n%+v\n", i, test.expected, condition)
  106. }
  107. }
  108. }
  109. func getReadyCondition(ready bool, reason, message string) api.PodCondition {
  110. status := api.ConditionFalse
  111. if ready {
  112. status = api.ConditionTrue
  113. }
  114. return api.PodCondition{
  115. Type: api.PodReady,
  116. Status: status,
  117. Reason: reason,
  118. Message: message,
  119. }
  120. }
  121. func getReadyStatus(cName string) api.ContainerStatus {
  122. return api.ContainerStatus{
  123. Name: cName,
  124. Ready: true,
  125. }
  126. }
  127. func getNotReadyStatus(cName string) api.ContainerStatus {
  128. return api.ContainerStatus{
  129. Name: cName,
  130. Ready: false,
  131. }
  132. }