diff --git a/pkg/kubectl/cmd/delete.go b/pkg/kubectl/cmd/delete.go index a5bbf75291d80..462eb2eae78cc 100644 --- a/pkg/kubectl/cmd/delete.go +++ b/pkg/kubectl/cmd/delete.go @@ -20,7 +20,6 @@ import ( "fmt" "io" - "github.com/golang/glog" "github.com/spf13/cobra" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors" @@ -70,7 +69,7 @@ Examples: Do() found := 0 - r.IgnoreErrors(errors.IsNotFound).Visit(func(r *resource.Info) error { + err := r.IgnoreErrors(errors.IsNotFound).Visit(func(r *resource.Info) error { found++ if err := resource.NewHelper(r.Client, r.Mapping).Delete(r.Namespace, r.Name); err != nil { return err @@ -78,8 +77,9 @@ Examples: fmt.Fprintf(out, "%s\n", r.Name) return nil }) + checkErr(err) if found == 0 { - glog.V(2).Infof("No resource(s) found") + fmt.Fprintf(cmd.Out(), "No resources found\n") } }, } diff --git a/pkg/kubectl/cmd/delete_test.go b/pkg/kubectl/cmd/delete_test.go index 3447895c5950c..ced3b9712713c 100644 --- a/pkg/kubectl/cmd/delete_test.go +++ b/pkg/kubectl/cmd/delete_test.go @@ -22,6 +22,7 @@ import ( "strings" "testing" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" ) @@ -82,6 +83,37 @@ func TestDeleteObjectIgnoreNotFound(t *testing.T) { } } +func TestDeleteNoObjects(t *testing.T) { + f, tf, codec := NewAPIFactory() + tf.Printer = &testPrinter{} + tf.Client = &client.FakeRESTClient{ + Codec: codec, + Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + switch p, m := req.URL.Path, req.Method; { + case p == "/ns/test/pods" && m == "GET": + return &http.Response{StatusCode: 200, Body: objBody(codec, &api.PodList{})}, nil + default: + t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) + return nil, nil + } + }), + } + buf := bytes.NewBuffer([]byte{}) + stderr := bytes.NewBuffer([]byte{}) + + cmd := f.NewCmdDelete(buf) + cmd.SetOutput(stderr) + cmd.Flags().String("namespace", "test", "") + cmd.Run(cmd, []string{"pods"}) + + if buf.String() != "" { + t.Errorf("unexpected output: %s", buf.String()) + } + if stderr.String() != "No resources found\n" { + t.Errorf("unexpected output: %s", stderr.String()) + } +} + func TestDeleteMultipleObject(t *testing.T) { pods, svc := testData()