handlers_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 lifecycle
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "net/http"
  18. "reflect"
  19. "strings"
  20. "testing"
  21. "time"
  22. "k8s.io/api/core/v1"
  23. "k8s.io/apimachinery/pkg/util/intstr"
  24. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  25. "k8s.io/kubernetes/pkg/kubelet/util/format"
  26. )
  27. func TestResolvePortInt(t *testing.T) {
  28. expected := 80
  29. port, err := resolvePort(intstr.FromInt(expected), &v1.Container{})
  30. if port != expected {
  31. t.Errorf("expected: %d, saw: %d", expected, port)
  32. }
  33. if err != nil {
  34. t.Errorf("unexpected error: %v", err)
  35. }
  36. }
  37. func TestResolvePortString(t *testing.T) {
  38. expected := 80
  39. name := "foo"
  40. container := &v1.Container{
  41. Ports: []v1.ContainerPort{
  42. {Name: name, ContainerPort: int32(expected)},
  43. },
  44. }
  45. port, err := resolvePort(intstr.FromString(name), container)
  46. if port != expected {
  47. t.Errorf("expected: %d, saw: %d", expected, port)
  48. }
  49. if err != nil {
  50. t.Errorf("unexpected error: %v", err)
  51. }
  52. }
  53. func TestResolvePortStringUnknown(t *testing.T) {
  54. expected := int32(80)
  55. name := "foo"
  56. container := &v1.Container{
  57. Ports: []v1.ContainerPort{
  58. {Name: "bar", ContainerPort: expected},
  59. },
  60. }
  61. port, err := resolvePort(intstr.FromString(name), container)
  62. if port != -1 {
  63. t.Errorf("expected: -1, saw: %d", port)
  64. }
  65. if err == nil {
  66. t.Error("unexpected non-error")
  67. }
  68. }
  69. type fakeContainerCommandRunner struct {
  70. Cmd []string
  71. ID kubecontainer.ContainerID
  72. Err error
  73. Msg string
  74. }
  75. func (f *fakeContainerCommandRunner) RunInContainer(id kubecontainer.ContainerID, cmd []string, timeout time.Duration) ([]byte, error) {
  76. f.Cmd = cmd
  77. f.ID = id
  78. return []byte(f.Msg), f.Err
  79. }
  80. func TestRunHandlerExec(t *testing.T) {
  81. fakeCommandRunner := fakeContainerCommandRunner{}
  82. handlerRunner := NewHandlerRunner(&fakeHTTP{}, &fakeCommandRunner, nil)
  83. containerID := kubecontainer.ContainerID{Type: "test", ID: "abc1234"}
  84. containerName := "containerFoo"
  85. container := v1.Container{
  86. Name: containerName,
  87. Lifecycle: &v1.Lifecycle{
  88. PostStart: &v1.Handler{
  89. Exec: &v1.ExecAction{
  90. Command: []string{"ls", "-a"},
  91. },
  92. },
  93. },
  94. }
  95. pod := v1.Pod{}
  96. pod.ObjectMeta.Name = "podFoo"
  97. pod.ObjectMeta.Namespace = "nsFoo"
  98. pod.Spec.Containers = []v1.Container{container}
  99. _, err := handlerRunner.Run(containerID, &pod, &container, container.Lifecycle.PostStart)
  100. if err != nil {
  101. t.Errorf("unexpected error: %v", err)
  102. }
  103. if fakeCommandRunner.ID != containerID ||
  104. !reflect.DeepEqual(container.Lifecycle.PostStart.Exec.Command, fakeCommandRunner.Cmd) {
  105. t.Errorf("unexpected commands: %v", fakeCommandRunner)
  106. }
  107. }
  108. type fakeHTTP struct {
  109. url string
  110. err error
  111. resp *http.Response
  112. }
  113. func (f *fakeHTTP) Get(url string) (*http.Response, error) {
  114. f.url = url
  115. return f.resp, f.err
  116. }
  117. func TestRunHandlerHttp(t *testing.T) {
  118. fakeHttp := fakeHTTP{}
  119. handlerRunner := NewHandlerRunner(&fakeHttp, &fakeContainerCommandRunner{}, nil)
  120. containerID := kubecontainer.ContainerID{Type: "test", ID: "abc1234"}
  121. containerName := "containerFoo"
  122. container := v1.Container{
  123. Name: containerName,
  124. Lifecycle: &v1.Lifecycle{
  125. PostStart: &v1.Handler{
  126. HTTPGet: &v1.HTTPGetAction{
  127. Host: "foo",
  128. Port: intstr.FromInt(8080),
  129. Path: "bar",
  130. },
  131. },
  132. },
  133. }
  134. pod := v1.Pod{}
  135. pod.ObjectMeta.Name = "podFoo"
  136. pod.ObjectMeta.Namespace = "nsFoo"
  137. pod.Spec.Containers = []v1.Container{container}
  138. _, err := handlerRunner.Run(containerID, &pod, &container, container.Lifecycle.PostStart)
  139. if err != nil {
  140. t.Errorf("unexpected error: %v", err)
  141. }
  142. if fakeHttp.url != "http://foo:8080/bar" {
  143. t.Errorf("unexpected url: %s", fakeHttp.url)
  144. }
  145. }
  146. func TestRunHandlerNil(t *testing.T) {
  147. handlerRunner := NewHandlerRunner(&fakeHTTP{}, &fakeContainerCommandRunner{}, nil)
  148. containerID := kubecontainer.ContainerID{Type: "test", ID: "abc1234"}
  149. podName := "podFoo"
  150. podNamespace := "nsFoo"
  151. containerName := "containerFoo"
  152. container := v1.Container{
  153. Name: containerName,
  154. Lifecycle: &v1.Lifecycle{
  155. PostStart: &v1.Handler{},
  156. },
  157. }
  158. pod := v1.Pod{}
  159. pod.ObjectMeta.Name = podName
  160. pod.ObjectMeta.Namespace = podNamespace
  161. pod.Spec.Containers = []v1.Container{container}
  162. _, err := handlerRunner.Run(containerID, &pod, &container, container.Lifecycle.PostStart)
  163. if err == nil {
  164. t.Errorf("expect error, but got nil")
  165. }
  166. }
  167. func TestRunHandlerExecFailure(t *testing.T) {
  168. expectedErr := fmt.Errorf("invalid command")
  169. fakeCommandRunner := fakeContainerCommandRunner{Err: expectedErr, Msg: expectedErr.Error()}
  170. handlerRunner := NewHandlerRunner(&fakeHTTP{}, &fakeCommandRunner, nil)
  171. containerID := kubecontainer.ContainerID{Type: "test", ID: "abc1234"}
  172. containerName := "containerFoo"
  173. command := []string{"ls", "--a"}
  174. container := v1.Container{
  175. Name: containerName,
  176. Lifecycle: &v1.Lifecycle{
  177. PostStart: &v1.Handler{
  178. Exec: &v1.ExecAction{
  179. Command: command,
  180. },
  181. },
  182. },
  183. }
  184. pod := v1.Pod{}
  185. pod.ObjectMeta.Name = "podFoo"
  186. pod.ObjectMeta.Namespace = "nsFoo"
  187. pod.Spec.Containers = []v1.Container{container}
  188. expectedErrMsg := fmt.Sprintf("Exec lifecycle hook (%s) for Container %q in Pod %q failed - error: %v, message: %q", command, containerName, format.Pod(&pod), expectedErr, expectedErr.Error())
  189. msg, err := handlerRunner.Run(containerID, &pod, &container, container.Lifecycle.PostStart)
  190. if err == nil {
  191. t.Errorf("expected error: %v", expectedErr)
  192. }
  193. if msg != expectedErrMsg {
  194. t.Errorf("unexpected error message: %q; expected %q", msg, expectedErrMsg)
  195. }
  196. }
  197. func TestRunHandlerHttpFailure(t *testing.T) {
  198. expectedErr := fmt.Errorf("fake http error")
  199. expectedResp := http.Response{
  200. Body: ioutil.NopCloser(strings.NewReader(expectedErr.Error())),
  201. }
  202. fakeHttp := fakeHTTP{err: expectedErr, resp: &expectedResp}
  203. handlerRunner := NewHandlerRunner(&fakeHttp, &fakeContainerCommandRunner{}, nil)
  204. containerName := "containerFoo"
  205. containerID := kubecontainer.ContainerID{Type: "test", ID: "abc1234"}
  206. container := v1.Container{
  207. Name: containerName,
  208. Lifecycle: &v1.Lifecycle{
  209. PostStart: &v1.Handler{
  210. HTTPGet: &v1.HTTPGetAction{
  211. Host: "foo",
  212. Port: intstr.FromInt(8080),
  213. Path: "bar",
  214. },
  215. },
  216. },
  217. }
  218. pod := v1.Pod{}
  219. pod.ObjectMeta.Name = "podFoo"
  220. pod.ObjectMeta.Namespace = "nsFoo"
  221. pod.Spec.Containers = []v1.Container{container}
  222. expectedErrMsg := fmt.Sprintf("Http lifecycle hook (%s) for Container %q in Pod %q failed - error: %v, message: %q", "bar", containerName, format.Pod(&pod), expectedErr, expectedErr.Error())
  223. msg, err := handlerRunner.Run(containerID, &pod, &container, container.Lifecycle.PostStart)
  224. if err == nil {
  225. t.Errorf("expected error: %v", expectedErr)
  226. }
  227. if msg != expectedErrMsg {
  228. t.Errorf("unexpected error message: %q; expected %q", msg, expectedErrMsg)
  229. }
  230. if fakeHttp.url != "http://foo:8080/bar" {
  231. t.Errorf("unexpected url: %s", fakeHttp.url)
  232. }
  233. }