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

Function TestWatchTableLabelSelector

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

Source from the content-addressed store, hash-verified

1985}
1986
1987func TestWatchTableLabelSelector(t *testing.T) {
1988 pods, events := watchTestData()
1989
1990 tf := cmdtesting.NewTestFactory().WithNamespace("test")
1991 defer tf.Cleanup()
1992 codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
1993
1994 podList := &corev1.PodList{
1995 Items: pods,
1996 ListMeta: metav1.ListMeta{
1997 ResourceVersion: "10",
1998 },
1999 }
2000 tf.UnstructuredClient = &fake.RESTClient{
2001 NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
2002 Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
2003 if req.URL.Query().Get(metav1.LabelSelectorQueryParam("v1")) != "a=b" {
2004 t.Fatalf("request url: %#v,and request: %#v", req.URL, req)
2005 }
2006 switch req.URL.Path {
2007 case "/namespaces/test/pods":
2008 if req.URL.Query().Get("watch") == "true" {
2009 return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: podTableWatchBody(codec, events[2:])}, nil
2010 }
2011 return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: podTableObjBody(codec, podList.Items...)}, nil
2012 default:
2013 t.Fatalf("request url: %#v,and request: %#v", req.URL, req)
2014 return nil, nil
2015 }
2016 }),
2017 }
2018
2019 streams, _, buf, _ := genericiooptions.NewTestIOStreams()
2020 cmd := NewCmdGet("kubectl", tf, streams)
2021 cmd.SetOut(buf)
2022 cmd.SetErr(buf)
2023
2024 cmd.Flags().Set("watch", "true")
2025 cmd.Flags().Set("selector", "a=b")
2026 cmd.Run(cmd, []string{"pods"})
2027
2028 expected := `NAME READY STATUS RESTARTS AGE
2029bar 0/0 0 <unknown>
2030foo 0/0 0 <unknown>
2031foo 0/0 0 <unknown>
2032foo 0/0 0 <unknown>
2033`
2034 if e, a := expected, buf.String(); e != a {
2035 t.Errorf("expected\n%v\ngot\n%v", e, a)
2036 }
2037}
2038
2039func TestWatchFieldSelector(t *testing.T) {
2040 pods, events := watchTestData()

Callers

nothing calls this directly

Calls 9

watchTestDataFunction · 0.85
podTableWatchBodyFunction · 0.85
podTableObjBodyFunction · 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…