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

Function TestWatchResource

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

Source from the content-addressed store, hash-verified

2141}
2142
2143func TestWatchResource(t *testing.T) {
2144 pods, events := watchTestData()
2145
2146 tf := cmdtesting.NewTestFactory().WithNamespace("test")
2147 defer tf.Cleanup()
2148 codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
2149
2150 tf.UnstructuredClient = &fake.RESTClient{
2151 NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
2152 Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
2153 switch req.URL.Path {
2154 case "/namespaces/test/pods/foo":
2155 return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &pods[1])}, nil
2156 case "/namespaces/test/pods":
2157 if req.URL.Query().Get("watch") == "true" && req.URL.Query().Get("fieldSelector") == "metadata.name=foo" {
2158 return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: watchBody(codec, events[1:])}, nil
2159 }
2160 t.Fatalf("request url: %#v,and request: %#v", req.URL, req)
2161 return nil, nil
2162 default:
2163 t.Fatalf("request url: %#v,and request: %#v", req.URL, req)
2164 return nil, nil
2165 }
2166 }),
2167 }
2168
2169 streams, _, buf, _ := genericiooptions.NewTestIOStreams()
2170 cmd := NewCmdGet("kubectl", tf, streams)
2171 cmd.SetOut(buf)
2172 cmd.SetErr(buf)
2173
2174 cmd.Flags().Set("watch", "true")
2175 cmd.Run(cmd, []string{"pods", "foo"})
2176
2177 expected := `NAME AGE
2178foo <unknown>
2179foo <unknown>
2180foo <unknown>
2181`
2182 if e, a := expected, buf.String(); e != a {
2183 t.Errorf("expected\n%v\ngot\n%v", e, a)
2184 }
2185}
2186
2187func TestWatchNonExistObject(t *testing.T) {
2188 pods, _ := 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…