From ea3c709ed4b129a4af56df64a22d657647b390a1 Mon Sep 17 00:00:00 2001 From: danielhelfand Date: Thu, 24 Oct 2019 09:38:46 -0400 Subject: [PATCH] add namespace check to resource delete command --- pkg/cmd/pipelineresource/delete.go | 5 +++++ pkg/cmd/pipelineresource/delete_test.go | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/pipelineresource/delete.go b/pkg/cmd/pipelineresource/delete.go index 8c1969119..53ad37c5e 100644 --- a/pkg/cmd/pipelineresource/delete.go +++ b/pkg/cmd/pipelineresource/delete.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" "github.com/tektoncd/cli/pkg/cli" + validateinput "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 res rm foo -n bar Err: cmd.OutOrStderr(), } + if err := validateinput.NamespaceExists(p); err != nil { + return err + } + if err := checkOptions(opts, s, p, args[0]); err != nil { return err } diff --git a/pkg/cmd/pipelineresource/delete_test.go b/pkg/cmd/pipelineresource/delete_test.go index a3b3eb8ce..e2536e8cd 100644 --- a/pkg/cmd/pipelineresource/delete_test.go +++ b/pkg/cmd/pipelineresource/delete_test.go @@ -23,9 +23,19 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" 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" ) func TestPipelineResourceDelete(t *testing.T) { + ns := []*corev1.Namespace{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "ns", + }, + }, + } + seeds := make([]pipelinetest.Clients, 0) for i := 0; i < 3; i++ { pres := []*v1alpha1.PipelineResource{ @@ -35,7 +45,7 @@ func TestPipelineResourceDelete(t *testing.T) { ), ), } - cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineResources: pres}) + cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineResources: pres, Namespaces: ns}) seeds = append(seeds, cs) } @@ -47,6 +57,14 @@ func TestPipelineResourceDelete(t *testing.T) { wantError bool want string }{ + { + name: "Invalid namespace", + command: []string{"rm", "pre-1", "-n", "invalid"}, + input: seeds[0], + inputStream: nil, + wantError: true, + want: "namespaces \"invalid\" not found", + }, { name: "With force delete flag (shorthand)", command: []string{"rm", "pre-1", "-n", "ns", "-f"}, @@ -91,7 +109,7 @@ func TestPipelineResourceDelete(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} pipelineResource := Command(p) if tp.inputStream != nil {