Fix handling empty result when invoking kubectl get

This commit is contained in:
Maciej Szulik 2019-08-14 16:52:28 +02:00
parent 9b54021c65
commit 09ecba5213
No known key found for this signature in database
GPG Key ID: F15E55D276FA84C4
2 changed files with 56 additions and 2 deletions

View File

@ -750,8 +750,8 @@ func (o *GetOptions) printGeneric(r *resource.Result) error {
}
var obj runtime.Object
if !singleItemImplied || len(infos) > 1 {
// we have more than one item, so coerce all items into a list.
if !singleItemImplied || len(infos) != 1 {
// we have zero or multple items, so coerce all items into a list.
// we don't want an *unstructured.Unstructured list yet, as we
// may be dealing with non-unstructured objects. Compose all items
// into an corev1.List, and then decode using an unstructured scheme.

View File

@ -702,6 +702,60 @@ func TestGetObjectIgnoreNotFound(t *testing.T) {
}
}
func TestEmptyResult(t *testing.T) {
cmdtesting.InitTestErrorHandler(t)
tf := cmdtesting.NewTestFactory().WithNamespace("test")
defer tf.Cleanup()
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &corev1.PodList{})}, nil
}),
}
streams, _, _, errbuf := genericclioptions.NewTestIOStreams()
cmd := NewCmdGet("kubectl", tf, streams)
// we're assuming that an empty file is being passed from stdin
cmd.Flags().Set("filename", "-")
cmd.Run(cmd, []string{})
if !strings.Contains(errbuf.String(), "No resources found") {
t.Errorf("unexpected output: %q", errbuf.String())
}
}
func TestEmptyResultJSON(t *testing.T) {
cmdtesting.InitTestErrorHandler(t)
tf := cmdtesting.NewTestFactory().WithNamespace("test")
defer tf.Cleanup()
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &corev1.PodList{})}, nil
}),
}
streams, _, outbuf, errbuf := genericclioptions.NewTestIOStreams()
cmd := NewCmdGet("kubectl", tf, streams)
// we're assuming that an empty file is being passed from stdin
cmd.Flags().Set("filename", "-")
cmd.Flags().Set("output", "json")
cmd.Run(cmd, []string{})
if errbuf.Len() > 0 {
t.Errorf("unexpected error: %q", errbuf.String())
}
if !strings.Contains(outbuf.String(), `"items": []`) {
t.Errorf("unexpected output: %q", outbuf.String())
}
}
func TestGetSortedObjects(t *testing.T) {
pods := &corev1.PodList{
ListMeta: metav1.ListMeta{