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

Function TestWatchLabelSelector

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

Source from the content-addressed store, hash-verified

1933}
1934
1935func TestWatchLabelSelector(t *testing.T) {
1936 pods, events := watchTestData()
1937
1938 tf := cmdtesting.NewTestFactory().WithNamespace("test")
1939 defer tf.Cleanup()
1940 codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
1941
1942 podList := &corev1.PodList{
1943 Items: pods,
1944 ListMeta: metav1.ListMeta{
1945 ResourceVersion: "10",
1946 },
1947 }
1948 tf.UnstructuredClient = &fake.RESTClient{
1949 NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
1950 Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
1951 if req.URL.Query().Get(metav1.LabelSelectorQueryParam("v1")) != "a=b" {
1952 t.Fatalf("request url: %#v,and request: %#v", req.URL, req)
1953 }
1954 switch req.URL.Path {
1955 case "/namespaces/test/pods":
1956 if req.URL.Query().Get("watch") == "true" {
1957 return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: watchBody(codec, events[2:])}, nil
1958 }
1959 return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, podList)}, nil
1960 default:
1961 t.Fatalf("request url: %#v,and request: %#v", req.URL, req)
1962 return nil, nil
1963 }
1964 }),
1965 }
1966
1967 streams, _, buf, _ := genericiooptions.NewTestIOStreams()
1968 cmd := NewCmdGet("kubectl", tf, streams)
1969 cmd.SetOut(buf)
1970 cmd.SetErr(buf)
1971
1972 cmd.Flags().Set("watch", "true")
1973 cmd.Flags().Set("selector", "a=b")
1974 cmd.Run(cmd, []string{"pods"})
1975
1976 expected := `NAME AGE
1977bar <unknown>
1978foo <unknown>
1979foo <unknown>
1980foo <unknown>
1981`
1982 if e, a := expected, buf.String(); e != a {
1983 t.Errorf("expected\n%v\ngot\n%v", e, a)
1984 }
1985}
1986
1987func TestWatchTableLabelSelector(t *testing.T) {
1988 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…