Merge pull request #89688 from cprayer/kubectl-808

Added 'No resources found' message to logs command
This commit is contained in:
Kubernetes Prow Robot 2020-12-08 16:27:54 -08:00 committed by GitHub
commit 4425569da6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 0 deletions

View File

@ -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",
],
)

View File

@ -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

View File

@ -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