diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/logs/BUILD b/staging/src/k8s.io/kubectl/pkg/cmd/logs/BUILD index 101b9c42082..91fd1ba73b2 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/logs/BUILD +++ b/staging/src/k8s.io/kubectl/pkg/cmd/logs/BUILD @@ -32,7 +32,9 @@ go_test( "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", "//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library", "//staging/src/k8s.io/client-go/rest:go_default_library", + "//staging/src/k8s.io/client-go/rest/fake:go_default_library", "//staging/src/k8s.io/kubectl/pkg/cmd/testing:go_default_library", + "//staging/src/k8s.io/kubectl/pkg/scheme:go_default_library", ], ) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs.go b/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs.go index 3953a396a7c..c8259700d1c 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs.go @@ -275,6 +275,9 @@ func (o *LogsOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []str return errors.New("expected a resource") } o.Object = infos[0].Object + if o.Selector != "" && len(o.Object.(*corev1.PodList).Items) == 0 { + fmt.Fprintf(o.ErrOut, "No resources found in %s namespace.\n", o.Namespace) + } } return nil diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go index 93cd13cde51..e9f27bdc0ee 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go @@ -23,6 +23,7 @@ import ( "fmt" "io" "io/ioutil" + "net/http" "strings" "sync" "testing" @@ -34,7 +35,9 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/cli-runtime/pkg/genericclioptions" restclient "k8s.io/client-go/rest" + "k8s.io/client-go/rest/fake" cmdtesting "k8s.io/kubectl/pkg/cmd/testing" + "k8s.io/kubectl/pkg/scheme" ) func TestLog(t *testing.T) { @@ -663,6 +666,51 @@ func TestDefaultConsumeRequest(t *testing.T) { } } +func TestNoResourceFoundMessage(t *testing.T) { + tf := cmdtesting.NewTestFactory().WithNamespace("test") + defer tf.Cleanup() + + ns := scheme.Codecs.WithoutConversion() + codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...) + pods, _, _ := cmdtesting.EmptyTestData() + tf.UnstructuredClient = &fake.RESTClient{ + NegotiatedSerializer: ns, + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { + switch req.URL.Path { + case "/namespaces/test/pods": + if req.URL.Query().Get("labelSelector") == "foo" { + return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, pods)}, nil + } + t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) + return nil, nil + default: + t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) + return nil, nil + } + }), + } + + streams, _, buf, errbuf := genericclioptions.NewTestIOStreams() + cmd := NewCmdLogs(tf, streams) + o := NewLogsOptions(streams, false) + o.Selector = "foo" + err := o.Complete(tf, cmd, []string{}) + + if err != nil { + t.Fatalf("Unexpected error, expected none, got %v", err) + } + + expected := "" + if e, a := expected, buf.String(); e != a { + t.Errorf("expected to find:\n\t%s\nfound:\n\t%s\n", e, a) + } + + expectedErr := "No resources found in test namespace.\n" + if e, a := expectedErr, errbuf.String(); e != a { + t.Errorf("expected to find:\n\t%s\nfound:\n\t%s\n", e, a) + } +} + type responseWrapperMock struct { data io.Reader err error