From 97c3babe0083602ec2c6ff9e5e81061dcb46b3e8 Mon Sep 17 00:00:00 2001 From: danielhelfand Date: Mon, 21 Oct 2019 14:02:58 -0400 Subject: [PATCH] add namespace check to taskrun delete command --- pkg/cmd/taskrun/delete.go | 5 +++++ pkg/cmd/taskrun/delete_test.go | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/taskrun/delete.go b/pkg/cmd/taskrun/delete.go index 8cf540316..be6f8d8ea 100644 --- a/pkg/cmd/taskrun/delete.go +++ b/pkg/cmd/taskrun/delete.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" "github.com/tektoncd/cli/pkg/cli" + validate "github.com/tektoncd/cli/pkg/helper/validate" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" cliopts "k8s.io/cli-runtime/pkg/genericclioptions" ) @@ -56,6 +57,10 @@ tkn tr rm foo -n bar Err: cmd.OutOrStderr(), } + if err := validate.NamespaceExists(p); err != nil { + return err + } + if err := checkOptions(opts, s, p, args[0]); err != nil { return err } diff --git a/pkg/cmd/taskrun/delete_test.go b/pkg/cmd/taskrun/delete_test.go index ba6bff619..667d1daad 100644 --- a/pkg/cmd/taskrun/delete_test.go +++ b/pkg/cmd/taskrun/delete_test.go @@ -25,10 +25,19 @@ import ( pipelinetest "github.com/tektoncd/pipeline/test" tb "github.com/tektoncd/pipeline/test/builder" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" ) func TestTaskRunDelete(t *testing.T) { + ns := []*corev1.Namespace{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "ns", + }, + }, + } + seeds := make([]pipelinetest.Clients, 0) for i := 0; i < 3; i++ { trs := []*v1alpha1.TaskRun{ @@ -43,7 +52,7 @@ func TestTaskRunDelete(t *testing.T) { ), ), } - cs, _ := test.SeedTestData(t, pipelinetest.Data{TaskRuns: trs}) + cs, _ := test.SeedTestData(t, pipelinetest.Data{TaskRuns: trs, Namespaces: ns}) seeds = append(seeds, cs) } @@ -55,6 +64,14 @@ func TestTaskRunDelete(t *testing.T) { wantError bool want string }{ + { + name: "Invalid namespace", + command: []string{"rm", "tr0-1", "-n", "invalid"}, + input: seeds[0], + inputStream: nil, + wantError: true, + want: "namespaces \"invalid\" not found", + }, { name: "With force delete flag (shorthand)", command: []string{"rm", "tr0-1", "-n", "ns", "-f"}, @@ -99,7 +116,7 @@ func TestTaskRunDelete(t *testing.T) { for _, tp := range testParams { t.Run(tp.name, func(t *testing.T) { - p := &test.Params{Tekton: tp.input.Pipeline} + p := &test.Params{Tekton: tp.input.Pipeline, Kube: tp.input.Kube} taskrun := Command(p) if tp.inputStream != nil {