Skip to content

Commit

Permalink
Merge pull request kubernetes#3468 from smarterclayton/better_log_on_…
Browse files Browse the repository at this point in the history
…empty_delete

Write to STDERR when Delete contains no resources
  • Loading branch information
bgrant0607 committed Jan 14, 2015
2 parents dbe1bd9 + da2b03d commit 6459b1a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/kubectl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"io"

"github.com/golang/glog"
"github.com/spf13/cobra"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
Expand Down Expand Up @@ -70,16 +69,17 @@ 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
}
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")
}
},
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/kubectl/cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
)

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 6459b1a

Please sign in to comment.