helpers_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. Copyright 2015 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 container
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. )
  19. func TestEnvVarsToMap(t *testing.T) {
  20. vars := []EnvVar{
  21. {
  22. Name: "foo",
  23. Value: "bar",
  24. },
  25. {
  26. Name: "zoo",
  27. Value: "baz",
  28. },
  29. }
  30. varMap := EnvVarsToMap(vars)
  31. if e, a := len(vars), len(varMap); e != a {
  32. t.Errorf("Unexpected map length; expected: %d, got %d", e, a)
  33. }
  34. if a := varMap["foo"]; a != "bar" {
  35. t.Errorf("Unexpected value of key 'foo': %v", a)
  36. }
  37. if a := varMap["zoo"]; a != "baz" {
  38. t.Errorf("Unexpected value of key 'zoo': %v", a)
  39. }
  40. }
  41. func TestExpandCommandAndArgs(t *testing.T) {
  42. cases := []struct {
  43. name string
  44. container *api.Container
  45. envs []EnvVar
  46. expectedCommand []string
  47. expectedArgs []string
  48. }{
  49. {
  50. name: "none",
  51. container: &api.Container{},
  52. },
  53. {
  54. name: "command expanded",
  55. container: &api.Container{
  56. Command: []string{"foo", "$(VAR_TEST)", "$(VAR_TEST2)"},
  57. },
  58. envs: []EnvVar{
  59. {
  60. Name: "VAR_TEST",
  61. Value: "zoo",
  62. },
  63. {
  64. Name: "VAR_TEST2",
  65. Value: "boo",
  66. },
  67. },
  68. expectedCommand: []string{"foo", "zoo", "boo"},
  69. },
  70. {
  71. name: "args expanded",
  72. container: &api.Container{
  73. Args: []string{"zap", "$(VAR_TEST)", "$(VAR_TEST2)"},
  74. },
  75. envs: []EnvVar{
  76. {
  77. Name: "VAR_TEST",
  78. Value: "hap",
  79. },
  80. {
  81. Name: "VAR_TEST2",
  82. Value: "trap",
  83. },
  84. },
  85. expectedArgs: []string{"zap", "hap", "trap"},
  86. },
  87. {
  88. name: "both expanded",
  89. container: &api.Container{
  90. Command: []string{"$(VAR_TEST2)--$(VAR_TEST)", "foo", "$(VAR_TEST3)"},
  91. Args: []string{"foo", "$(VAR_TEST)", "$(VAR_TEST2)"},
  92. },
  93. envs: []EnvVar{
  94. {
  95. Name: "VAR_TEST",
  96. Value: "zoo",
  97. },
  98. {
  99. Name: "VAR_TEST2",
  100. Value: "boo",
  101. },
  102. {
  103. Name: "VAR_TEST3",
  104. Value: "roo",
  105. },
  106. },
  107. expectedCommand: []string{"boo--zoo", "foo", "roo"},
  108. expectedArgs: []string{"foo", "zoo", "boo"},
  109. },
  110. }
  111. for _, tc := range cases {
  112. actualCommand, actualArgs := ExpandContainerCommandAndArgs(tc.container, tc.envs)
  113. if e, a := tc.expectedCommand, actualCommand; !reflect.DeepEqual(e, a) {
  114. t.Errorf("%v: unexpected command; expected %v, got %v", tc.name, e, a)
  115. }
  116. if e, a := tc.expectedArgs, actualArgs; !reflect.DeepEqual(e, a) {
  117. t.Errorf("%v: unexpected args; expected %v, got %v", tc.name, e, a)
  118. }
  119. }
  120. }
  121. func TestShouldContainerBeRestarted(t *testing.T) {
  122. pod := &api.Pod{
  123. ObjectMeta: api.ObjectMeta{
  124. UID: "12345678",
  125. Name: "foo",
  126. Namespace: "new",
  127. },
  128. Spec: api.PodSpec{
  129. Containers: []api.Container{
  130. {Name: "no-history"},
  131. {Name: "alive"},
  132. {Name: "succeed"},
  133. {Name: "failed"},
  134. {Name: "unknown"},
  135. },
  136. },
  137. }
  138. podStatus := &PodStatus{
  139. ID: pod.UID,
  140. Name: pod.Name,
  141. Namespace: pod.Namespace,
  142. ContainerStatuses: []*ContainerStatus{
  143. {
  144. Name: "alive",
  145. State: ContainerStateRunning,
  146. },
  147. {
  148. Name: "succeed",
  149. State: ContainerStateExited,
  150. ExitCode: 0,
  151. },
  152. {
  153. Name: "failed",
  154. State: ContainerStateExited,
  155. ExitCode: 1,
  156. },
  157. {
  158. Name: "alive",
  159. State: ContainerStateExited,
  160. ExitCode: 2,
  161. },
  162. {
  163. Name: "unknown",
  164. State: ContainerStateUnknown,
  165. },
  166. {
  167. Name: "failed",
  168. State: ContainerStateExited,
  169. ExitCode: 3,
  170. },
  171. },
  172. }
  173. policies := []api.RestartPolicy{
  174. api.RestartPolicyNever,
  175. api.RestartPolicyOnFailure,
  176. api.RestartPolicyAlways,
  177. }
  178. expected := map[string][]bool{
  179. "no-history": {true, true, true},
  180. "alive": {false, false, false},
  181. "succeed": {false, false, true},
  182. "failed": {false, true, true},
  183. "unknown": {true, true, true},
  184. }
  185. for _, c := range pod.Spec.Containers {
  186. for i, policy := range policies {
  187. pod.Spec.RestartPolicy = policy
  188. e := expected[c.Name][i]
  189. r := ShouldContainerBeRestarted(&c, pod, podStatus)
  190. if r != e {
  191. t.Errorf("Restart for container %q with restart policy %q expected %t, got %t",
  192. c.Name, policy, e, r)
  193. }
  194. }
  195. }
  196. }