func TestEventSearch(t *testing.T) { c := &testClient{ Request: testRequest{ Method: "GET", Path: testapi.ResourcePath("events", "baz", ""), Query: url.Values{ api.FieldSelectorQueryParam(testapi.Version()): []string{ getInvolvedObjectNameFieldLabel(testapi.Version()) + "=foo,", "involvedObject.namespace=baz,", "involvedObject.kind=Pod", }, api.LabelSelectorQueryParam(testapi.Version()): []string{}, }, }, Response: Response{StatusCode: 200, Body: &api.EventList{}}, } eventList, err := c.Setup().Events("baz").Search( &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "foo", Namespace: "baz", SelfLink: testapi.SelfLink("pods", ""), }, }, ) c.Validate(t, eventList, err) }
// NewListWatchFromClient creates a new ListWatch from the specified client, resource, namespace and field selector. func NewListWatchFromClient(c *client.Client, resource string, namespace string, fieldSelector fields.Selector) *ListWatch { listFunc := func() (runtime.Object, error) { return c.Get().Namespace(namespace).Resource(resource).FieldsSelectorParam(api.FieldSelectorQueryParam(c.APIVersion()), fieldSelector).Do().Get() } watchFunc := func(resourceVersion string) (watch.Interface, error) { return c.Get().Prefix("watch").Namespace(namespace).Resource(resource).FieldsSelectorParam(api.FieldSelectorQueryParam(c.APIVersion()), fieldSelector).Param("resourceVersion", resourceVersion).Watch() } return &ListWatch{ListFunc: listFunc, WatchFunc: watchFunc} }
// Watch returns a watch.Interface that watches the requested namespaces. func (c *namespaces) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) { return c.r.Get(). Prefix("watch"). Resource("namespaces"). Param("resourceVersion", resourceVersion). LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), label). FieldsSelectorParam(api.FieldSelectorQueryParam(c.r.APIVersion()), field). Watch() }
// Watch starts watching for events matching the given selectors. func (e *events) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) { return e.client.Get(). Prefix("watch"). NamespaceIfScoped(e.namespace, len(e.namespace) > 0). Resource("events"). Param("resourceVersion", resourceVersion). LabelsSelectorParam(api.LabelSelectorQueryParam(e.client.APIVersion()), label). FieldsSelectorParam(api.FieldSelectorQueryParam(e.client.APIVersion()), field). Watch() }
// Watch starts watching for secrets matching the given selectors. func (s *secrets) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) { return s.client.Get(). Prefix("watch"). Namespace(s.namespace). Resource("secrets"). Param("resourceVersion", resourceVersion). LabelsSelectorParam(api.LabelSelectorQueryParam(s.client.APIVersion()), label). FieldsSelectorParam(api.FieldSelectorQueryParam(s.client.APIVersion()), field). Watch() }
func (m *Helper) Watch(namespace, resourceVersion, apiVersion string, labelSelector labels.Selector, fieldSelector fields.Selector) (watch.Interface, error) { return m.RESTClient.Get(). Prefix("watch"). NamespaceIfScoped(namespace, m.NamespaceScoped). Resource(m.Resource). Param("resourceVersion", resourceVersion). LabelsSelectorParam(api.LabelSelectorQueryParam(apiVersion), labelSelector). FieldsSelectorParam(api.FieldSelectorQueryParam(apiVersion), fieldSelector). Watch() }
func (c *testClient) ValidateCommon(t *testing.T, err error) { defer c.server.Close() if c.Error { if err == nil { t.Errorf("error expected for %#v, got none", c.Request) } return } if err != nil { t.Errorf("no error expected for %#v, got: %v", c.Request, err) } if c.handler.RequestReceived == nil { t.Errorf("handler had an empty request, %#v", c) return } requestBody := body(c.Request.Body, c.Request.RawBody) actualQuery := c.handler.RequestReceived.URL.Query() t.Logf("got query: %v", actualQuery) t.Logf("path: %v", c.Request.Path) // We check the query manually, so blank it out so that FakeHandler.ValidateRequest // won't check it. c.handler.RequestReceived.URL.RawQuery = "" c.handler.ValidateRequest(t, path.Join(c.Request.Path), c.Request.Method, requestBody) for key, values := range c.Request.Query { validator, ok := c.QueryValidator[key] if !ok { switch key { case api.LabelSelectorQueryParam(testapi.Version()): validator = validateLabels case api.FieldSelectorQueryParam(testapi.Version()): validator = validateFields default: validator = func(a, b string) bool { return a == b } } } observed := actualQuery.Get(key) wanted := strings.Join(values, "") if !validator(wanted, observed) { t.Errorf("Unexpected query arg for key: %s. Expected %s, Received %s", key, wanted, observed) } } if c.Request.Header != "" { if c.handler.RequestReceived.Header.Get(c.Request.Header) == "" { t.Errorf("header %q not found in request %#v", c.Request.Header, c.handler.RequestReceived) } } if expected, received := requestBody, c.handler.RequestBody; expected != nil && *expected != received { t.Errorf("bad body for request %#v: expected %s, got %s", c.Request, *expected, received) } }
// List returns a list of events matching the selectors. func (e *events) List(label labels.Selector, field fields.Selector) (*api.EventList, error) { result := &api.EventList{} err := e.client.Get(). NamespaceIfScoped(e.namespace, len(e.namespace) > 0). Resource("events"). LabelsSelectorParam(api.LabelSelectorQueryParam(e.client.APIVersion()), label). FieldsSelectorParam(api.FieldSelectorQueryParam(e.client.APIVersion()), field). Do(). Into(result) return result, err }
// List returns a list of secrets matching the selectors. func (s *secrets) List(label labels.Selector, field fields.Selector) (*api.SecretList, error) { result := &api.SecretList{} err := s.client.Get(). Namespace(s.namespace). Resource("secrets"). LabelsSelectorParam(api.LabelSelectorQueryParam(s.client.APIVersion()), label). FieldsSelectorParam(api.FieldSelectorQueryParam(s.client.APIVersion()), field). Do(). Into(result) return result, err }
func TestListWatchesCanList(t *testing.T) { fieldSelectorQueryParamName := api.FieldSelectorQueryParam(testapi.Version()) table := []struct { location string resource string namespace string fieldSelector fields.Selector }{ // Minion { location: testapi.ResourcePath("minions", api.NamespaceAll, ""), resource: "minions", namespace: api.NamespaceAll, fieldSelector: parseSelectorOrDie(""), }, // pod with "assigned" field selector. { location: buildLocation( testapi.ResourcePath("pods", api.NamespaceAll, ""), buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}})), resource: "pods", namespace: api.NamespaceAll, fieldSelector: fields.Set{"spec.host": ""}.AsSelector(), }, // pod in namespace "foo" { location: buildLocation( testapi.ResourcePath("pods", "foo", ""), buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}})), resource: "pods", namespace: "foo", fieldSelector: fields.Set{"spec.host": ""}.AsSelector(), }, } for _, item := range table { handler := util.FakeHandler{ StatusCode: 500, ResponseBody: "", T: t, } server := httptest.NewServer(&handler) defer server.Close() client := client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()}) lw := NewListWatchFromClient(client, item.resource, item.namespace, item.fieldSelector) // This test merely tests that the correct request is made. lw.List() handler.ValidateRequest(t, item.location, "GET", nil) } }
// FieldsSelectorParam adds the given selector as a query parameter with the name paramName. func (r *Request) FieldsSelectorParam(s fields.Selector) *Request { if r.err != nil { return r } if s.Empty() { return r } s2, err := s.Transform(func(field, value string) (newField, newValue string, err error) { return fieldMappings.filterField(r.apiVersion, r.resource, field, value) }) if err != nil { r.err = err return r } return r.setParam(api.FieldSelectorQueryParam(r.apiVersion), s2.String()) }
func parseSelectorQueryParams(query url.Values, version, apiResource string) (label labels.Selector, field fields.Selector, err error) { labelString := query.Get(api.LabelSelectorQueryParam(version)) label, err = labels.Parse(labelString) if err != nil { return nil, nil, errors.NewBadRequest(fmt.Sprintf("The 'labels' selector parameter (%s) could not be parsed: %v", labelString, err)) } convertToInternalVersionFunc := func(label, value string) (newLabel, newValue string, err error) { return api.Scheme.ConvertFieldLabel(version, apiResource, label, value) } fieldString := query.Get(api.FieldSelectorQueryParam(version)) field, err = fields.ParseAndTransformSelector(fieldString, convertToInternalVersionFunc) if err != nil { return nil, nil, errors.NewBadRequest(fmt.Sprintf("The 'fields' selector parameter (%s) could not be parsed: %v", fieldString, err)) } return label, field, nil }
func TestWatchParamParsing(t *testing.T) { api.Scheme.AddFieldLabelConversionFunc(testVersion, "foo", func(label, value string) (string, string, error) { return label, value, nil }) simpleStorage := &SimpleRESTStorage{} handler := handle(map[string]RESTStorage{"foo": simpleStorage}) server := httptest.NewServer(handler) defer server.Close() dest, _ := url.Parse(server.URL) dest.Path = "/api/" + testVersion + "/watch/foo" table := []struct { rawQuery string resourceVersion string labelSelector string fieldSelector string namespace string }{ { rawQuery: "resourceVersion=1234", resourceVersion: "1234", labelSelector: "", fieldSelector: "", namespace: api.NamespaceAll, }, { rawQuery: "namespace=default&resourceVersion=314159&" + api.FieldSelectorQueryParam(testVersion) + "=Host%3D&" + api.LabelSelectorQueryParam(testVersion) + "=name%3Dfoo", resourceVersion: "314159", labelSelector: "name=foo", fieldSelector: "Host=", namespace: api.NamespaceDefault, }, { rawQuery: "namespace=watchother&" + api.FieldSelectorQueryParam(testVersion) + "=id%3dfoo&resourceVersion=1492", resourceVersion: "1492", labelSelector: "", fieldSelector: "id=foo", namespace: "watchother", }, { rawQuery: "", resourceVersion: "", labelSelector: "", fieldSelector: "", namespace: api.NamespaceAll, }, } for _, item := range table { simpleStorage.requestedLabelSelector = labels.Everything() simpleStorage.requestedFieldSelector = fields.Everything() simpleStorage.requestedResourceVersion = "5" // Prove this is set in all cases simpleStorage.requestedResourceNamespace = "" dest.RawQuery = item.rawQuery resp, err := http.Get(dest.String()) if err != nil { t.Errorf("%v: unexpected error: %v", item.rawQuery, err) continue } resp.Body.Close() if e, a := item.namespace, simpleStorage.requestedResourceNamespace; e != a { t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a) } if e, a := item.resourceVersion, simpleStorage.requestedResourceVersion; e != a { t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a) } if e, a := item.labelSelector, simpleStorage.requestedLabelSelector.String(); e != a { t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a) } if e, a := item.fieldSelector, simpleStorage.requestedFieldSelector.String(); e != a { t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a) } } }