services_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/url"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/testapi"
  19. "k8s.io/kubernetes/pkg/api/unversioned"
  20. "k8s.io/kubernetes/pkg/client/unversioned/testclient/simple"
  21. "k8s.io/kubernetes/pkg/labels"
  22. )
  23. func TestListServices(t *testing.T) {
  24. ns := api.NamespaceDefault
  25. c := &simple.Client{
  26. Request: simple.Request{
  27. Method: "GET",
  28. Path: testapi.Default.ResourcePath("services", ns, ""),
  29. Query: simple.BuildQueryValues(nil)},
  30. Response: simple.Response{StatusCode: 200,
  31. Body: &api.ServiceList{
  32. Items: []api.Service{
  33. {
  34. ObjectMeta: api.ObjectMeta{
  35. Name: "name",
  36. Labels: map[string]string{
  37. "foo": "bar",
  38. "name": "baz",
  39. },
  40. },
  41. Spec: api.ServiceSpec{
  42. Selector: map[string]string{
  43. "one": "two",
  44. },
  45. },
  46. },
  47. },
  48. },
  49. },
  50. }
  51. receivedServiceList, err := c.Setup(t).Services(ns).List(api.ListOptions{})
  52. defer c.Close()
  53. t.Logf("received services: %v %#v", err, receivedServiceList)
  54. c.Validate(t, receivedServiceList, err)
  55. }
  56. func TestListServicesLabels(t *testing.T) {
  57. ns := api.NamespaceDefault
  58. labelSelectorQueryParamName := unversioned.LabelSelectorQueryParam(testapi.Default.GroupVersion().String())
  59. c := &simple.Client{
  60. Request: simple.Request{
  61. Method: "GET",
  62. Path: testapi.Default.ResourcePath("services", ns, ""),
  63. Query: simple.BuildQueryValues(url.Values{labelSelectorQueryParamName: []string{"foo=bar,name=baz"}})},
  64. Response: simple.Response{StatusCode: 200,
  65. Body: &api.ServiceList{
  66. Items: []api.Service{
  67. {
  68. ObjectMeta: api.ObjectMeta{
  69. Name: "name",
  70. Labels: map[string]string{
  71. "foo": "bar",
  72. "name": "baz",
  73. },
  74. },
  75. Spec: api.ServiceSpec{
  76. Selector: map[string]string{
  77. "one": "two",
  78. },
  79. },
  80. },
  81. },
  82. },
  83. },
  84. }
  85. c.Setup(t)
  86. defer c.Close()
  87. c.QueryValidator[labelSelectorQueryParamName] = simple.ValidateLabels
  88. selector := labels.Set{"foo": "bar", "name": "baz"}.AsSelector()
  89. options := api.ListOptions{LabelSelector: selector}
  90. receivedServiceList, err := c.Services(ns).List(options)
  91. c.Validate(t, receivedServiceList, err)
  92. }
  93. func TestGetService(t *testing.T) {
  94. ns := api.NamespaceDefault
  95. c := &simple.Client{
  96. Request: simple.Request{
  97. Method: "GET",
  98. Path: testapi.Default.ResourcePath("services", ns, "1"),
  99. Query: simple.BuildQueryValues(nil)},
  100. Response: simple.Response{StatusCode: 200, Body: &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}}},
  101. }
  102. response, err := c.Setup(t).Services(ns).Get("1")
  103. defer c.Close()
  104. c.Validate(t, response, err)
  105. }
  106. func TestGetServiceWithNoName(t *testing.T) {
  107. ns := api.NamespaceDefault
  108. c := &simple.Client{Error: true}
  109. receivedPod, err := c.Setup(t).Services(ns).Get("")
  110. defer c.Close()
  111. if (err != nil) && (err.Error() != simple.NameRequiredError) {
  112. t.Errorf("Expected error: %v, but got %v", simple.NameRequiredError, err)
  113. }
  114. c.Validate(t, receivedPod, err)
  115. }
  116. func TestCreateService(t *testing.T) {
  117. ns := api.NamespaceDefault
  118. c := &simple.Client{
  119. Request: simple.Request{
  120. Method: "POST",
  121. Path: testapi.Default.ResourcePath("services", ns, ""),
  122. Body: &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}},
  123. Query: simple.BuildQueryValues(nil)},
  124. Response: simple.Response{StatusCode: 200, Body: &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}}},
  125. }
  126. response, err := c.Setup(t).Services(ns).Create(&api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1"}})
  127. defer c.Close()
  128. c.Validate(t, response, err)
  129. }
  130. func TestUpdateService(t *testing.T) {
  131. ns := api.NamespaceDefault
  132. svc := &api.Service{ObjectMeta: api.ObjectMeta{Name: "service-1", ResourceVersion: "1"}}
  133. c := &simple.Client{
  134. Request: simple.Request{Method: "PUT", Path: testapi.Default.ResourcePath("services", ns, "service-1"), Body: svc, Query: simple.BuildQueryValues(nil)},
  135. Response: simple.Response{StatusCode: 200, Body: svc},
  136. }
  137. response, err := c.Setup(t).Services(ns).Update(svc)
  138. defer c.Close()
  139. c.Validate(t, response, err)
  140. }
  141. func TestDeleteService(t *testing.T) {
  142. ns := api.NamespaceDefault
  143. c := &simple.Client{
  144. Request: simple.Request{Method: "DELETE", Path: testapi.Default.ResourcePath("services", ns, "1"), Query: simple.BuildQueryValues(nil)},
  145. Response: simple.Response{StatusCode: 200},
  146. }
  147. err := c.Setup(t).Services(ns).Delete("1")
  148. defer c.Close()
  149. c.Validate(t, nil, err)
  150. }
  151. func TestUpdateServiceStatus(t *testing.T) {
  152. ns := api.NamespaceDefault
  153. lbStatus := api.LoadBalancerStatus{
  154. Ingress: []api.LoadBalancerIngress{
  155. {IP: "127.0.0.1"},
  156. },
  157. }
  158. requestService := &api.Service{
  159. ObjectMeta: api.ObjectMeta{
  160. Name: "foo",
  161. Namespace: ns,
  162. ResourceVersion: "1",
  163. },
  164. Status: api.ServiceStatus{
  165. LoadBalancer: lbStatus,
  166. },
  167. }
  168. c := &simple.Client{
  169. Request: simple.Request{
  170. Method: "PUT",
  171. Path: testapi.Default.ResourcePath("services", ns, "foo") + "/status",
  172. Query: simple.BuildQueryValues(nil),
  173. },
  174. Response: simple.Response{
  175. StatusCode: 200,
  176. Body: &api.Service{
  177. ObjectMeta: api.ObjectMeta{
  178. Name: "foo",
  179. Labels: map[string]string{
  180. "foo": "bar",
  181. "name": "baz",
  182. },
  183. },
  184. Spec: api.ServiceSpec{},
  185. Status: api.ServiceStatus{
  186. LoadBalancer: lbStatus,
  187. },
  188. },
  189. },
  190. }
  191. receivedService, err := c.Setup(t).Services(ns).UpdateStatus(requestService)
  192. defer c.Close()
  193. c.Validate(t, receivedService, err)
  194. }
  195. func TestServiceProxyGet(t *testing.T) {
  196. body := "OK"
  197. ns := api.NamespaceDefault
  198. c := &simple.Client{
  199. Request: simple.Request{
  200. Method: "GET",
  201. Path: testapi.Default.ResourcePath("services", ns, "service-1") + "/proxy/foo",
  202. Query: simple.BuildQueryValues(url.Values{"param-name": []string{"param-value"}}),
  203. },
  204. Response: simple.Response{StatusCode: 200, RawBody: &body},
  205. }
  206. response, err := c.Setup(t).Services(ns).ProxyGet("", "service-1", "", "foo", map[string]string{"param-name": "param-value"}).DoRaw()
  207. defer c.Close()
  208. c.ValidateRaw(t, response, err)
  209. // With scheme and port specified
  210. c = &simple.Client{
  211. Request: simple.Request{
  212. Method: "GET",
  213. Path: testapi.Default.ResourcePath("services", ns, "https:service-1:my-port") + "/proxy/foo",
  214. Query: simple.BuildQueryValues(url.Values{"param-name": []string{"param-value"}}),
  215. },
  216. Response: simple.Response{StatusCode: 200, RawBody: &body},
  217. }
  218. response, err = c.Setup(t).Services(ns).ProxyGet("https", "service-1", "my-port", "foo", map[string]string{"param-name": "param-value"}).DoRaw()
  219. defer c.Close()
  220. c.ValidateRaw(t, response, err)
  221. }