pods_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 unversioned_test
  14. import (
  15. "net/http"
  16. "net/url"
  17. "testing"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/testapi"
  20. "k8s.io/kubernetes/pkg/api/unversioned"
  21. "k8s.io/kubernetes/pkg/client/unversioned/testclient/simple"
  22. "k8s.io/kubernetes/pkg/labels"
  23. )
  24. func TestListEmptyPods(t *testing.T) {
  25. ns := api.NamespaceDefault
  26. c := &simple.Client{
  27. Request: simple.Request{Method: "GET", Path: testapi.Default.ResourcePath("pods", ns, ""), Query: simple.BuildQueryValues(nil)},
  28. Response: simple.Response{StatusCode: http.StatusOK, Body: &api.PodList{}},
  29. }
  30. podList, err := c.Setup(t).Pods(ns).List(api.ListOptions{})
  31. defer c.Close()
  32. c.Validate(t, podList, err)
  33. }
  34. func TestListPods(t *testing.T) {
  35. ns := api.NamespaceDefault
  36. c := &simple.Client{
  37. Request: simple.Request{Method: "GET", Path: testapi.Default.ResourcePath("pods", ns, ""), Query: simple.BuildQueryValues(nil)},
  38. Response: simple.Response{StatusCode: http.StatusOK,
  39. Body: &api.PodList{
  40. Items: []api.Pod{
  41. {
  42. Status: api.PodStatus{
  43. Phase: api.PodRunning,
  44. },
  45. ObjectMeta: api.ObjectMeta{
  46. Labels: map[string]string{
  47. "foo": "bar",
  48. "name": "baz",
  49. },
  50. },
  51. },
  52. },
  53. },
  54. },
  55. }
  56. receivedPodList, err := c.Setup(t).Pods(ns).List(api.ListOptions{})
  57. defer c.Close()
  58. c.Validate(t, receivedPodList, err)
  59. }
  60. func TestListPodsLabels(t *testing.T) {
  61. ns := api.NamespaceDefault
  62. labelSelectorQueryParamName := unversioned.LabelSelectorQueryParam(testapi.Default.GroupVersion().String())
  63. c := &simple.Client{
  64. Request: simple.Request{
  65. Method: "GET",
  66. Path: testapi.Default.ResourcePath("pods", ns, ""),
  67. Query: simple.BuildQueryValues(url.Values{labelSelectorQueryParamName: []string{"foo=bar,name=baz"}})},
  68. Response: simple.Response{
  69. StatusCode: http.StatusOK,
  70. Body: &api.PodList{
  71. Items: []api.Pod{
  72. {
  73. Status: api.PodStatus{
  74. Phase: api.PodRunning,
  75. },
  76. ObjectMeta: api.ObjectMeta{
  77. Labels: map[string]string{
  78. "foo": "bar",
  79. "name": "baz",
  80. },
  81. },
  82. },
  83. },
  84. },
  85. },
  86. }
  87. c.Setup(t)
  88. defer c.Close()
  89. c.QueryValidator[labelSelectorQueryParamName] = simple.ValidateLabels
  90. selector := labels.Set{"foo": "bar", "name": "baz"}.AsSelector()
  91. options := api.ListOptions{LabelSelector: selector}
  92. receivedPodList, err := c.Pods(ns).List(options)
  93. c.Validate(t, receivedPodList, err)
  94. }
  95. func TestGetPod(t *testing.T) {
  96. ns := api.NamespaceDefault
  97. c := &simple.Client{
  98. Request: simple.Request{Method: "GET", Path: testapi.Default.ResourcePath("pods", ns, "foo"), Query: simple.BuildQueryValues(nil)},
  99. Response: simple.Response{
  100. StatusCode: http.StatusOK,
  101. Body: &api.Pod{
  102. Status: api.PodStatus{
  103. Phase: api.PodRunning,
  104. },
  105. ObjectMeta: api.ObjectMeta{
  106. Labels: map[string]string{
  107. "foo": "bar",
  108. "name": "baz",
  109. },
  110. },
  111. },
  112. },
  113. }
  114. receivedPod, err := c.Setup(t).Pods(ns).Get("foo")
  115. defer c.Close()
  116. c.Validate(t, receivedPod, err)
  117. }
  118. func TestGetPodWithNoName(t *testing.T) {
  119. ns := api.NamespaceDefault
  120. c := &simple.Client{Error: true}
  121. receivedPod, err := c.Setup(t).Pods(ns).Get("")
  122. defer c.Close()
  123. if (err != nil) && (err.Error() != simple.NameRequiredError) {
  124. t.Errorf("Expected error: %v, but got %v", simple.NameRequiredError, err)
  125. }
  126. c.Validate(t, receivedPod, err)
  127. }
  128. func TestDeletePod(t *testing.T) {
  129. ns := api.NamespaceDefault
  130. c := &simple.Client{
  131. Request: simple.Request{Method: "DELETE", Path: testapi.Default.ResourcePath("pods", ns, "foo"), Query: simple.BuildQueryValues(nil)},
  132. Response: simple.Response{StatusCode: http.StatusOK},
  133. }
  134. err := c.Setup(t).Pods(ns).Delete("foo", nil)
  135. defer c.Close()
  136. c.Validate(t, nil, err)
  137. }
  138. func TestCreatePod(t *testing.T) {
  139. ns := api.NamespaceDefault
  140. requestPod := &api.Pod{
  141. Status: api.PodStatus{
  142. Phase: api.PodRunning,
  143. },
  144. ObjectMeta: api.ObjectMeta{
  145. Labels: map[string]string{
  146. "foo": "bar",
  147. "name": "baz",
  148. },
  149. },
  150. }
  151. c := &simple.Client{
  152. Request: simple.Request{Method: "POST", Path: testapi.Default.ResourcePath("pods", ns, ""), Query: simple.BuildQueryValues(nil), Body: requestPod},
  153. Response: simple.Response{
  154. StatusCode: http.StatusOK,
  155. Body: requestPod,
  156. },
  157. }
  158. receivedPod, err := c.Setup(t).Pods(ns).Create(requestPod)
  159. defer c.Close()
  160. c.Validate(t, receivedPod, err)
  161. }
  162. func TestUpdatePod(t *testing.T) {
  163. ns := api.NamespaceDefault
  164. requestPod := &api.Pod{
  165. ObjectMeta: api.ObjectMeta{
  166. Name: "foo",
  167. ResourceVersion: "1",
  168. Labels: map[string]string{
  169. "foo": "bar",
  170. "name": "baz",
  171. },
  172. },
  173. Status: api.PodStatus{
  174. Phase: api.PodRunning,
  175. },
  176. }
  177. c := &simple.Client{
  178. Request: simple.Request{Method: "PUT", Path: testapi.Default.ResourcePath("pods", ns, "foo"), Query: simple.BuildQueryValues(nil)},
  179. Response: simple.Response{StatusCode: http.StatusOK, Body: requestPod},
  180. }
  181. receivedPod, err := c.Setup(t).Pods(ns).Update(requestPod)
  182. defer c.Close()
  183. c.Validate(t, receivedPod, err)
  184. }
  185. func TestPodGetLogs(t *testing.T) {
  186. ns := api.NamespaceDefault
  187. opts := &api.PodLogOptions{
  188. Follow: true,
  189. Timestamps: true,
  190. }
  191. c := &simple.Client{
  192. Request: simple.Request{
  193. Method: "GET",
  194. Path: testapi.Default.ResourcePath("pods", ns, "podName") + "/log",
  195. Query: url.Values{
  196. "follow": []string{"true"},
  197. "timestamps": []string{"true"},
  198. },
  199. },
  200. Response: simple.Response{StatusCode: http.StatusOK},
  201. }
  202. body, err := c.Setup(t).Pods(ns).GetLogs("podName", opts).Stream()
  203. defer c.Close()
  204. if err != nil {
  205. t.Fatalf("unexpected error: %v", err)
  206. }
  207. defer body.Close()
  208. c.ValidateCommon(t, err)
  209. }