MCPcopy
hub / github.com/kubernetes/kubectl / TestWatchFieldSelector

Function TestWatchFieldSelector

pkg/cmd/get/get_test.go:2039–2089  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2037}
2038
2039func TestWatchFieldSelector(t *testing.T) {
2040 pods, events := watchTestData()
2041
2042 tf := cmdtesting.NewTestFactory().WithNamespace("test")
2043 defer tf.Cleanup()
2044 codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
2045
2046 podList := &corev1.PodList{
2047 Items: pods,
2048 ListMeta: metav1.ListMeta{
2049 ResourceVersion: "10",
2050 },
2051 }
2052 tf.UnstructuredClient = &fake.RESTClient{
2053 NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
2054 Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
2055 if req.URL.Query().Get(metav1.FieldSelectorQueryParam("v1")) != "a=b" {
2056 t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
2057 }
2058 switch req.URL.Path {
2059 case "/namespaces/test/pods":
2060 if req.URL.Query().Get("watch") == "true" {
2061 return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: watchBody(codec, events[2:])}, nil
2062 }
2063 return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, podList)}, nil
2064 default:
2065 t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
2066 return nil, nil
2067 }
2068 }),
2069 }
2070
2071 streams, _, buf, _ := genericiooptions.NewTestIOStreams()
2072 cmd := NewCmdGet("kubectl", tf, streams)
2073 cmd.SetOut(buf)
2074 cmd.SetErr(buf)
2075
2076 cmd.Flags().Set("watch", "true")
2077 cmd.Flags().Set("field-selector", "a=b")
2078 cmd.Run(cmd, []string{"pods"})
2079
2080 expected := `NAME AGE
2081bar <unknown>
2082foo <unknown>
2083foo <unknown>
2084foo <unknown>
2085`
2086 if e, a := expected, buf.String(); e != a {
2087 t.Errorf("expected\n%v\ngot\n%v", e, a)
2088 }
2089}
2090
2091func TestWatchTableFieldSelector(t *testing.T) {
2092 pods, events := watchTestData()

Callers

nothing calls this directly

Calls 8

watchTestDataFunction · 0.85
watchBodyFunction · 0.85
NewCmdGetFunction · 0.85
WithNamespaceMethod · 0.80
CleanupMethod · 0.80
GetMethod · 0.65
StringMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…