mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Fix handling empty result when invoking kubectl get
This commit is contained in:
parent
9b54021c65
commit
09ecba5213
@ -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.
|
||||
|
@ -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{
|
||||
|
Loading…
Reference in New Issue
Block a user