listwatch_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 tests
  14. import (
  15. "context"
  16. "net/http/httptest"
  17. "net/url"
  18. "testing"
  19. "time"
  20. v1 "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/fields"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "k8s.io/apimachinery/pkg/runtime/schema"
  25. "k8s.io/apimachinery/pkg/watch"
  26. clientset "k8s.io/client-go/kubernetes"
  27. restclient "k8s.io/client-go/rest"
  28. . "k8s.io/client-go/tools/cache"
  29. watchtools "k8s.io/client-go/tools/watch"
  30. utiltesting "k8s.io/client-go/util/testing"
  31. )
  32. func parseSelectorOrDie(s string) fields.Selector {
  33. selector, err := fields.ParseSelector(s)
  34. if err != nil {
  35. panic(err)
  36. }
  37. return selector
  38. }
  39. // buildQueryValues is a convenience function for knowing if a namespace should be in a query param or not
  40. func buildQueryValues(query url.Values) url.Values {
  41. v := url.Values{}
  42. for key, values := range query {
  43. for _, value := range values {
  44. v.Add(key, value)
  45. }
  46. }
  47. return v
  48. }
  49. func buildLocation(resourcePath string, query url.Values) string {
  50. return resourcePath + "?" + query.Encode()
  51. }
  52. func TestListWatchesCanList(t *testing.T) {
  53. fieldSelectorQueryParamName := metav1.FieldSelectorQueryParam("v1")
  54. table := []struct {
  55. location string
  56. resource string
  57. namespace string
  58. fieldSelector fields.Selector
  59. }{
  60. // Node
  61. {
  62. location: "/api/v1/nodes",
  63. resource: "nodes",
  64. namespace: metav1.NamespaceAll,
  65. fieldSelector: parseSelectorOrDie(""),
  66. },
  67. // pod with "assigned" field selector.
  68. {
  69. location: buildLocation(
  70. "/api/v1/pods",
  71. buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}})),
  72. resource: "pods",
  73. namespace: metav1.NamespaceAll,
  74. fieldSelector: fields.Set{"spec.host": ""}.AsSelector(),
  75. },
  76. // pod in namespace "foo"
  77. {
  78. location: buildLocation(
  79. "/api/v1/namespaces/foo/pods",
  80. buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}})),
  81. resource: "pods",
  82. namespace: "foo",
  83. fieldSelector: fields.Set{"spec.host": ""}.AsSelector(),
  84. },
  85. }
  86. for _, item := range table {
  87. handler := utiltesting.FakeHandler{
  88. StatusCode: 500,
  89. ResponseBody: "",
  90. T: t,
  91. }
  92. server := httptest.NewServer(&handler)
  93. defer server.Close()
  94. client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
  95. lw := NewListWatchFromClient(client.CoreV1().RESTClient(), item.resource, item.namespace, item.fieldSelector)
  96. lw.DisableChunking = true
  97. // This test merely tests that the correct request is made.
  98. lw.List(metav1.ListOptions{})
  99. handler.ValidateRequest(t, item.location, "GET", nil)
  100. }
  101. }
  102. func TestListWatchesCanWatch(t *testing.T) {
  103. fieldSelectorQueryParamName := metav1.FieldSelectorQueryParam("v1")
  104. table := []struct {
  105. rv string
  106. location string
  107. resource string
  108. namespace string
  109. fieldSelector fields.Selector
  110. }{
  111. // Node
  112. {
  113. location: buildLocation(
  114. "/api/v1/nodes",
  115. buildQueryValues(url.Values{"watch": []string{"true"}})),
  116. rv: "",
  117. resource: "nodes",
  118. namespace: metav1.NamespaceAll,
  119. fieldSelector: parseSelectorOrDie(""),
  120. },
  121. {
  122. location: buildLocation(
  123. "/api/v1/nodes",
  124. buildQueryValues(url.Values{"resourceVersion": []string{"42"}, "watch": []string{"true"}})),
  125. rv: "42",
  126. resource: "nodes",
  127. namespace: metav1.NamespaceAll,
  128. fieldSelector: parseSelectorOrDie(""),
  129. },
  130. // pod with "assigned" field selector.
  131. {
  132. location: buildLocation(
  133. "/api/v1/pods",
  134. buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}, "resourceVersion": []string{"0"}, "watch": []string{"true"}})),
  135. rv: "0",
  136. resource: "pods",
  137. namespace: metav1.NamespaceAll,
  138. fieldSelector: fields.Set{"spec.host": ""}.AsSelector(),
  139. },
  140. // pod with namespace foo and assigned field selector
  141. {
  142. location: buildLocation(
  143. "/api/v1/namespaces/foo/pods",
  144. buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}, "resourceVersion": []string{"0"}, "watch": []string{"true"}})),
  145. rv: "0",
  146. resource: "pods",
  147. namespace: "foo",
  148. fieldSelector: fields.Set{"spec.host": ""}.AsSelector(),
  149. },
  150. }
  151. for _, item := range table {
  152. handler := utiltesting.FakeHandler{
  153. StatusCode: 500,
  154. ResponseBody: "",
  155. T: t,
  156. }
  157. server := httptest.NewServer(&handler)
  158. defer server.Close()
  159. client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
  160. lw := NewListWatchFromClient(client.CoreV1().RESTClient(), item.resource, item.namespace, item.fieldSelector)
  161. // This test merely tests that the correct request is made.
  162. lw.Watch(metav1.ListOptions{ResourceVersion: item.rv})
  163. handler.ValidateRequest(t, item.location, "GET", nil)
  164. }
  165. }
  166. type lw struct {
  167. list runtime.Object
  168. watch watch.Interface
  169. }
  170. func (w lw) List(options metav1.ListOptions) (runtime.Object, error) {
  171. return w.list, nil
  172. }
  173. func (w lw) Watch(options metav1.ListOptions) (watch.Interface, error) {
  174. return w.watch, nil
  175. }
  176. func TestListWatchUntil(t *testing.T) {
  177. fw := watch.NewFake()
  178. go func() {
  179. obj := &v1.Pod{
  180. ObjectMeta: metav1.ObjectMeta{
  181. ResourceVersion: "2",
  182. },
  183. }
  184. fw.Modify(obj)
  185. }()
  186. listwatch := lw{
  187. list: &v1.PodList{
  188. ListMeta: metav1.ListMeta{
  189. ResourceVersion: "1",
  190. },
  191. Items: []v1.Pod{{}},
  192. },
  193. watch: fw,
  194. }
  195. conditions := []watchtools.ConditionFunc{
  196. func(event watch.Event) (bool, error) {
  197. t.Logf("got %#v", event)
  198. return event.Type == watch.Added, nil
  199. },
  200. func(event watch.Event) (bool, error) {
  201. t.Logf("got %#v", event)
  202. return event.Type == watch.Modified, nil
  203. },
  204. }
  205. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  206. defer cancel()
  207. lastEvent, err := watchtools.ListWatchUntil(ctx, listwatch, conditions...)
  208. if err != nil {
  209. t.Fatalf("expected nil error, got %#v", err)
  210. }
  211. if lastEvent == nil {
  212. t.Fatal("expected an event")
  213. }
  214. if lastEvent.Type != watch.Modified {
  215. t.Fatalf("expected MODIFIED event type, got %v", lastEvent.Type)
  216. }
  217. if got, isPod := lastEvent.Object.(*v1.Pod); !isPod {
  218. t.Fatalf("expected a pod event, got %#v", got)
  219. }
  220. }