diff --git a/pkg/actions/delete/delete.go b/pkg/actions/delete/delete.go new file mode 100644 index 000000000..047a270a6 --- /dev/null +++ b/pkg/actions/delete/delete.go @@ -0,0 +1,36 @@ +// Copyright © 2020 The Tekton Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package delete + +import ( + "github.com/tektoncd/cli/pkg/actions" + "github.com/tektoncd/cli/pkg/cli" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +func Delete(gr schema.GroupVersionResource, clients *cli.Clients, trname, n string, op *metav1.DeleteOptions) error { + gvr, err := actions.GetGVR(gr, clients.Tekton.Discovery()) + if err != nil { + return err + } + + err = clients.Dynamic.Resource(*gvr).Namespace(n).Delete(trname, op) + if err != nil { + return err + } + + return nil +} diff --git a/pkg/actions/gvr.go b/pkg/actions/gvr.go new file mode 100644 index 000000000..bc18788bc --- /dev/null +++ b/pkg/actions/gvr.go @@ -0,0 +1,36 @@ +// Copyright © 2020 The Tekton Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package actions + +import ( + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/discovery" + "k8s.io/client-go/restmapper" +) + +func GetGVR(gr schema.GroupVersionResource, discovery discovery.DiscoveryInterface) (*schema.GroupVersionResource, error) { + apiGroupRes, err := restmapper.GetAPIGroupResources(discovery) + if err != nil { + return nil, err + } + + rm := restmapper.NewDiscoveryRESTMapper(apiGroupRes) + gvr, err := rm.ResourceFor(gr) + if err != nil { + return nil, err + } + + return &gvr, nil +} diff --git a/pkg/list/list.go b/pkg/actions/list/list.go similarity index 76% rename from pkg/list/list.go rename to pkg/actions/list/list.go index 88042a242..53f30fe0c 100644 --- a/pkg/list/list.go +++ b/pkg/actions/list/list.go @@ -17,14 +17,13 @@ package list import ( "io" + "github.com/tektoncd/cli/pkg/actions" "github.com/tektoncd/cli/pkg/cli" "github.com/tektoncd/cli/pkg/printer" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" cliopts "k8s.io/cli-runtime/pkg/genericclioptions" - "k8s.io/client-go/discovery" - "k8s.io/client-go/restmapper" ) func PrintObject(groupResource schema.GroupVersionResource, w io.Writer, p cli.Params, f *cliopts.PrintFlags, ns string) error { @@ -42,7 +41,7 @@ func PrintObject(groupResource schema.GroupVersionResource, w io.Writer, p cli.P } func AllObjecs(gr schema.GroupVersionResource, clients *cli.Clients, n string, op metav1.ListOptions) (*unstructured.UnstructuredList, error) { - gvr, err := getGVR(gr, clients.Tekton.Discovery()) + gvr, err := actions.GetGVR(gr, clients.Tekton.Discovery()) if err != nil { return nil, err } @@ -54,18 +53,3 @@ func AllObjecs(gr schema.GroupVersionResource, clients *cli.Clients, n string, o return allRes, nil } - -func getGVR(gr schema.GroupVersionResource, discovery discovery.DiscoveryInterface) (*schema.GroupVersionResource, error) { - apiGroupRes, err := restmapper.GetAPIGroupResources(discovery) - if err != nil { - return nil, err - } - - rm := restmapper.NewDiscoveryRESTMapper(apiGroupRes) - gvr, err := rm.ResourceFor(gr) - if err != nil { - return nil, err - } - - return &gvr, nil -} diff --git a/pkg/cmd/clustertask/list.go b/pkg/cmd/clustertask/list.go index 7ad799a8c..f8eab72ec 100644 --- a/pkg/cmd/clustertask/list.go +++ b/pkg/cmd/clustertask/list.go @@ -20,9 +20,9 @@ import ( "text/tabwriter" "github.com/spf13/cobra" + "github.com/tektoncd/cli/pkg/actions/list" "github.com/tektoncd/cli/pkg/cli" "github.com/tektoncd/cli/pkg/formatted" - "github.com/tektoncd/cli/pkg/list" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" diff --git a/pkg/cmd/pipeline/list.go b/pkg/cmd/pipeline/list.go index afb1b3454..393cc5b4d 100644 --- a/pkg/cmd/pipeline/list.go +++ b/pkg/cmd/pipeline/list.go @@ -21,9 +21,9 @@ import ( "text/template" "github.com/spf13/cobra" + "github.com/tektoncd/cli/pkg/actions/list" "github.com/tektoncd/cli/pkg/cli" "github.com/tektoncd/cli/pkg/formatted" - "github.com/tektoncd/cli/pkg/list" "github.com/tektoncd/cli/pkg/pipeline" validate "github.com/tektoncd/cli/pkg/validate" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" diff --git a/pkg/cmd/task/list.go b/pkg/cmd/task/list.go index b4d678a7e..e3b2a49aa 100644 --- a/pkg/cmd/task/list.go +++ b/pkg/cmd/task/list.go @@ -20,9 +20,9 @@ import ( "text/tabwriter" "github.com/spf13/cobra" + "github.com/tektoncd/cli/pkg/actions/list" "github.com/tektoncd/cli/pkg/cli" "github.com/tektoncd/cli/pkg/formatted" - "github.com/tektoncd/cli/pkg/list" validate "github.com/tektoncd/cli/pkg/validate" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" diff --git a/pkg/cmd/taskrun/delete.go b/pkg/cmd/taskrun/delete.go index e8fd75391..4a6a83b43 100644 --- a/pkg/cmd/taskrun/delete.go +++ b/pkg/cmd/taskrun/delete.go @@ -19,11 +19,14 @@ import ( "fmt" "github.com/spf13/cobra" + traction "github.com/tektoncd/cli/pkg/actions/delete" "github.com/tektoncd/cli/pkg/cli" "github.com/tektoncd/cli/pkg/deleter" "github.com/tektoncd/cli/pkg/options" + trlist "github.com/tektoncd/cli/pkg/taskrun/list" validate "github.com/tektoncd/cli/pkg/validate" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" cliopts "k8s.io/cli-runtime/pkg/genericclioptions" ) @@ -77,6 +80,7 @@ or } func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, opts *options.DeleteOptions) error { + trGroupResource := schema.GroupVersionResource{Group: "tekton.dev", Resource: "taskruns"} cs, err := p.Clients() if err != nil { return fmt.Errorf("failed to create tekton client") @@ -85,16 +89,16 @@ func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, opts *options switch { case opts.DeleteAllNs: d = deleter.New("TaskRun", func(taskRunName string) error { - return cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(taskRunName, &metav1.DeleteOptions{}) + return traction.Delete(trGroupResource, cs, taskRunName, p.Namespace(), &metav1.DeleteOptions{}) }) - trs, err := allTaskRunNames(p, cs, opts.Keep) + trs, err := allTaskRunNames(cs, opts.Keep, p.Namespace()) if err != nil { return err } d.Delete(s, trs) case opts.ParentResourceName == "": d = deleter.New("TaskRun", func(taskRunName string) error { - return cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(taskRunName, &metav1.DeleteOptions{}) + return traction.Delete(trGroupResource, cs, taskRunName, p.Namespace(), &metav1.DeleteOptions{}) }) d.Delete(s, trNames) default: @@ -102,7 +106,7 @@ func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, opts *options return errors.New("the task should not be deleted") }) d.WithRelated("TaskRun", taskRunLister(p, cs), func(taskRunName string) error { - return cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(taskRunName, &metav1.DeleteOptions{}) + return traction.Delete(trGroupResource, cs, taskRunName, p.Namespace(), &metav1.DeleteOptions{}) }) d.DeleteRelated(s, []string{opts.ParentResourceName}) } @@ -137,8 +141,9 @@ func taskRunLister(p cli.Params, cs *cli.Clients) func(string) ([]string, error) } } -func allTaskRunNames(p cli.Params, cs *cli.Clients, keep int) ([]string, error) { - taskRuns, err := cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).List(metav1.ListOptions{}) +func allTaskRunNames(cs *cli.Clients, keep int, ns string) ([]string, error) { + + taskRuns, err := trlist.TaskRuns(cs, metav1.ListOptions{}, ns) if err != nil { return nil, err } diff --git a/pkg/cmd/taskrun/delete_test.go b/pkg/cmd/taskrun/delete_test.go index 846c33ebd..e8b113cdb 100644 --- a/pkg/cmd/taskrun/delete_test.go +++ b/pkg/cmd/taskrun/delete_test.go @@ -20,16 +20,20 @@ import ( "testing" "github.com/tektoncd/cli/pkg/test" + cb "github.com/tektoncd/cli/pkg/test/builder" + testDynamic "github.com/tektoncd/cli/pkg/test/dynamic" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" "github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources" 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" + "k8s.io/client-go/dynamic" "knative.dev/pkg/apis" ) func TestTaskRunDelete(t *testing.T) { + version := "v1alpha1" ns := []*corev1.Namespace{ { ObjectMeta: metav1.ObjectMeta{ @@ -38,47 +42,289 @@ func TestTaskRunDelete(t *testing.T) { }, } - seeds := make([]pipelinetest.Clients, 0) - for i := 0; i < 5; i++ { - trs := []*v1alpha1.TaskRun{ - tb.TaskRun("tr0-1", "ns", - tb.TaskRunLabel("tekton.dev/task", "random"), - tb.TaskRunSpec(tb.TaskRunTaskRef("random")), - tb.TaskRunStatus( - tb.StatusCondition(apis.Condition{ - Status: corev1.ConditionTrue, - Reason: resources.ReasonSucceeded, - }), - ), + trdata := []*v1alpha1.TaskRun{ + tb.TaskRun("tr0-1", "ns", + tb.TaskRunLabel("tekton.dev/task", "random"), + tb.TaskRunSpec(tb.TaskRunTaskRef("random")), + tb.TaskRunStatus( + tb.StatusCondition(apis.Condition{ + Status: corev1.ConditionTrue, + Reason: resources.ReasonSucceeded, + }), ), - tb.TaskRun("tr0-2", "ns", - tb.TaskRunLabel("tekton.dev/task", "random"), - tb.TaskRunSpec(tb.TaskRunTaskRef("random")), - tb.TaskRunStatus( - tb.StatusCondition(apis.Condition{ - Status: corev1.ConditionTrue, - Reason: resources.ReasonSucceeded, - }), - ), + ), + tb.TaskRun("tr0-2", "ns", + tb.TaskRunLabel("tekton.dev/task", "random"), + tb.TaskRunSpec(tb.TaskRunTaskRef("random")), + tb.TaskRunStatus( + tb.StatusCondition(apis.Condition{ + Status: corev1.ConditionTrue, + Reason: resources.ReasonSucceeded, + }), ), - tb.TaskRun("tr0-3", "ns", - tb.TaskRunLabel("tekton.dev/task", "random"), - tb.TaskRunSpec(tb.TaskRunTaskRef("random")), - tb.TaskRunStatus( - tb.StatusCondition(apis.Condition{ - Status: corev1.ConditionTrue, - Reason: resources.ReasonSucceeded, - }), - ), + ), + tb.TaskRun("tr0-3", "ns", + tb.TaskRunLabel("tekton.dev/task", "random"), + tb.TaskRunSpec(tb.TaskRunTaskRef("random")), + tb.TaskRunStatus( + tb.StatusCondition(apis.Condition{ + Status: corev1.ConditionTrue, + Reason: resources.ReasonSucceeded, + }), ), + ), + } + type clients struct { + pipelineClient pipelinetest.Clients + dynamicClient dynamic.Interface + } + + seeds := make([]clients, 0) + for i := 0; i < 5; i++ { + trs := trdata + cs, _ := test.SeedTestData(t, pipelinetest.Data{TaskRuns: trs, Namespaces: ns}) + cs.Pipeline.Resources = cb.APIResourceList(version, "taskrun") + dc, err := testDynamic.Client( + cb.UnstructuredTR(trdata[0], version), + cb.UnstructuredTR(trdata[1], version), + cb.UnstructuredTR(trdata[2], version), + ) + if err != nil { + t.Errorf("unable to create dynamic clinet: %v", err) } + seeds = append(seeds, clients{cs, dc}) + } + + testParams := []struct { + name string + command []string + dynamic dynamic.Interface + input pipelinetest.Clients + inputStream io.Reader + wantError bool + want string + }{ + { + name: "Invalid namespace", + command: []string{"rm", "tr0-1", "-n", "invalid"}, + dynamic: seeds[0].dynamicClient, + input: seeds[0].pipelineClient, + inputStream: nil, + wantError: true, + want: "namespaces \"invalid\" not found", + }, + { + name: "With force delete flag (shorthand)", + command: []string{"rm", "tr0-1", "-n", "ns", "-f"}, + dynamic: seeds[0].dynamicClient, + input: seeds[0].pipelineClient, + inputStream: nil, + wantError: false, + want: "TaskRuns deleted: \"tr0-1\"\n", + }, + { + name: "With force delete flag", + command: []string{"rm", "tr0-1", "-n", "ns", "--force"}, + dynamic: seeds[1].dynamicClient, + input: seeds[1].pipelineClient, + inputStream: nil, + wantError: false, + want: "TaskRuns deleted: \"tr0-1\"\n", + }, + { + name: "Without force delete flag, reply no", + command: []string{"rm", "tr0-1", "-n", "ns"}, + dynamic: seeds[2].dynamicClient, + input: seeds[2].pipelineClient, + inputStream: strings.NewReader("n"), + wantError: true, + want: "canceled deleting taskrun \"tr0-1\"", + }, + { + name: "Without force delete flag, reply yes", + command: []string{"rm", "tr0-1", "-n", "ns"}, + dynamic: seeds[2].dynamicClient, + input: seeds[2].pipelineClient, + inputStream: strings.NewReader("y"), + wantError: false, + want: "Are you sure you want to delete taskrun \"tr0-1\" (y/n): TaskRuns deleted: \"tr0-1\"\n", + }, + { + name: "Remove non existent resource", + command: []string{"rm", "nonexistent", "-n", "ns"}, + dynamic: seeds[2].dynamicClient, + input: seeds[2].pipelineClient, + inputStream: nil, + wantError: true, + want: "failed to delete taskrun \"nonexistent\": taskruns.tekton.dev \"nonexistent\" not found", + }, + { + name: "Remove multiple non existent resources", + command: []string{"rm", "nonexistent", "nonexistent2", "-n", "ns"}, + dynamic: seeds[2].dynamicClient, + input: seeds[2].pipelineClient, + inputStream: nil, + wantError: true, + want: "failed to delete taskrun \"nonexistent\": taskruns.tekton.dev \"nonexistent\" not found; failed to delete taskrun \"nonexistent2\": taskruns.tekton.dev \"nonexistent2\" not found", + }, + { + name: "Attempt remove forgetting to include taskrun names", + command: []string{"rm", "-n", "ns"}, + dynamic: seeds[2].dynamicClient, + input: seeds[2].pipelineClient, + inputStream: nil, + wantError: true, + want: "must provide taskrun name(s) or use --task flag or --all flag to use delete", + }, + { + name: "Remove taskruns of a task", + command: []string{"rm", "--task", "task", "-n", "ns"}, + dynamic: seeds[0].dynamicClient, + input: seeds[0].pipelineClient, + inputStream: strings.NewReader("y"), + wantError: false, + want: `Are you sure you want to delete all taskruns related to task "task" (y/n): `, + }, + { + name: "Delete all with prompt", + command: []string{"delete", "--all", "-n", "ns"}, + dynamic: seeds[3].dynamicClient, + input: seeds[3].pipelineClient, + inputStream: strings.NewReader("y"), + wantError: false, + want: "Are you sure you want to delete all taskruns in namespace \"ns\" (y/n): All TaskRuns deleted in namespace \"ns\"\n", + }, + { + name: "Delete all with -f", + command: []string{"delete", "--all", "-f", "-n", "ns"}, + dynamic: seeds[4].dynamicClient, + input: seeds[4].pipelineClient, + inputStream: nil, + wantError: false, + want: "All TaskRuns deleted in namespace \"ns\"\n", + }, + { + name: "Delete all keeping 2", + command: []string{"delete", "--all", "-f", "--keep", "2", "-n", "ns"}, + dynamic: seeds[4].dynamicClient, + input: seeds[4].pipelineClient, + inputStream: nil, + wantError: false, + want: "All but 2 TaskRuns deleted in namespace \"ns\"\n", + }, + { + name: "Keep -1 is a no go", + command: []string{"delete", "--all", "-f", "--keep", "-1", "-n", "ns"}, + dynamic: seeds[4].dynamicClient, + input: seeds[4].pipelineClient, + inputStream: nil, + wantError: true, + want: "keep option should not be lower than 0", + }, + { + name: "Error from using taskrun name with --all", + command: []string{"delete", "taskrun", "--all", "-n", "ns"}, + dynamic: seeds[4].dynamicClient, + input: seeds[4].pipelineClient, + inputStream: nil, + wantError: true, + want: "--all flag should not have any arguments or flags specified with it", + }, + } + + for _, tp := range testParams { + t.Run(tp.name, func(t *testing.T) { + p := &test.Params{Tekton: tp.input.Pipeline, Kube: tp.input.Kube, Dynamic: tp.dynamic} + taskrun := Command(p) + + if tp.inputStream != nil { + taskrun.SetIn(tp.inputStream) + } + + out, err := test.ExecuteCommand(taskrun, tp.command...) + if tp.wantError { + if err == nil { + t.Errorf("error expected here") + } else { + test.AssertOutput(t, tp.want, err.Error()) + } + } else { + if err != nil { + t.Errorf("unexpected Error") + } + test.AssertOutput(t, tp.want, out) + } + }) + } +} + +func TestTaskRunDelete_v1beta1(t *testing.T) { + version := "v1beta1" + ns := []*corev1.Namespace{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "ns", + }, + }, + } + + trdata := []*v1alpha1.TaskRun{ + tb.TaskRun("tr0-1", "ns", + tb.TaskRunLabel("tekton.dev/task", "random"), + tb.TaskRunSpec(tb.TaskRunTaskRef("random")), + tb.TaskRunStatus( + tb.StatusCondition(apis.Condition{ + Status: corev1.ConditionTrue, + Reason: resources.ReasonSucceeded, + }), + ), + ), + tb.TaskRun("tr0-2", "ns", + tb.TaskRunLabel("tekton.dev/task", "random"), + tb.TaskRunSpec(tb.TaskRunTaskRef("random")), + tb.TaskRunStatus( + tb.StatusCondition(apis.Condition{ + Status: corev1.ConditionTrue, + Reason: resources.ReasonSucceeded, + }), + ), + ), + tb.TaskRun("tr0-3", "ns", + tb.TaskRunLabel("tekton.dev/task", "random"), + tb.TaskRunSpec(tb.TaskRunTaskRef("random")), + tb.TaskRunStatus( + tb.StatusCondition(apis.Condition{ + Status: corev1.ConditionTrue, + Reason: resources.ReasonSucceeded, + }), + ), + ), + } + type clients struct { + pipelineClient pipelinetest.Clients + dynamicClient dynamic.Interface + } + + seeds := make([]clients, 0) + for i := 0; i < 5; i++ { + trs := trdata cs, _ := test.SeedTestData(t, pipelinetest.Data{TaskRuns: trs, Namespaces: ns}) - seeds = append(seeds, cs) + cs.Pipeline.Resources = cb.APIResourceList(version, "taskrun") + dc, err := testDynamic.Client( + cb.UnstructuredTR(trdata[0], version), + cb.UnstructuredTR(trdata[1], version), + cb.UnstructuredTR(trdata[2], version), + ) + if err != nil { + t.Errorf("unable to create dynamic clinet: %v", err) + } + seeds = append(seeds, clients{cs, dc}) } testParams := []struct { name string command []string + dynamic dynamic.Interface input pipelinetest.Clients inputStream io.Reader wantError bool @@ -87,7 +333,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "Invalid namespace", command: []string{"rm", "tr0-1", "-n", "invalid"}, - input: seeds[0], + dynamic: seeds[0].dynamicClient, + input: seeds[0].pipelineClient, inputStream: nil, wantError: true, want: "namespaces \"invalid\" not found", @@ -95,7 +342,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "With force delete flag (shorthand)", command: []string{"rm", "tr0-1", "-n", "ns", "-f"}, - input: seeds[0], + dynamic: seeds[0].dynamicClient, + input: seeds[0].pipelineClient, inputStream: nil, wantError: false, want: "TaskRuns deleted: \"tr0-1\"\n", @@ -103,7 +351,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "With force delete flag", command: []string{"rm", "tr0-1", "-n", "ns", "--force"}, - input: seeds[1], + dynamic: seeds[1].dynamicClient, + input: seeds[1].pipelineClient, inputStream: nil, wantError: false, want: "TaskRuns deleted: \"tr0-1\"\n", @@ -111,7 +360,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "Without force delete flag, reply no", command: []string{"rm", "tr0-1", "-n", "ns"}, - input: seeds[2], + dynamic: seeds[2].dynamicClient, + input: seeds[2].pipelineClient, inputStream: strings.NewReader("n"), wantError: true, want: "canceled deleting taskrun \"tr0-1\"", @@ -119,7 +369,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "Without force delete flag, reply yes", command: []string{"rm", "tr0-1", "-n", "ns"}, - input: seeds[2], + dynamic: seeds[2].dynamicClient, + input: seeds[2].pipelineClient, inputStream: strings.NewReader("y"), wantError: false, want: "Are you sure you want to delete taskrun \"tr0-1\" (y/n): TaskRuns deleted: \"tr0-1\"\n", @@ -127,7 +378,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "Remove non existent resource", command: []string{"rm", "nonexistent", "-n", "ns"}, - input: seeds[2], + dynamic: seeds[2].dynamicClient, + input: seeds[2].pipelineClient, inputStream: nil, wantError: true, want: "failed to delete taskrun \"nonexistent\": taskruns.tekton.dev \"nonexistent\" not found", @@ -135,7 +387,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "Remove multiple non existent resources", command: []string{"rm", "nonexistent", "nonexistent2", "-n", "ns"}, - input: seeds[2], + dynamic: seeds[2].dynamicClient, + input: seeds[2].pipelineClient, inputStream: nil, wantError: true, want: "failed to delete taskrun \"nonexistent\": taskruns.tekton.dev \"nonexistent\" not found; failed to delete taskrun \"nonexistent2\": taskruns.tekton.dev \"nonexistent2\" not found", @@ -143,7 +396,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "Attempt remove forgetting to include taskrun names", command: []string{"rm", "-n", "ns"}, - input: seeds[2], + dynamic: seeds[2].dynamicClient, + input: seeds[2].pipelineClient, inputStream: nil, wantError: true, want: "must provide taskrun name(s) or use --task flag or --all flag to use delete", @@ -151,7 +405,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "Remove taskruns of a task", command: []string{"rm", "--task", "task", "-n", "ns"}, - input: seeds[0], + dynamic: seeds[0].dynamicClient, + input: seeds[0].pipelineClient, inputStream: strings.NewReader("y"), wantError: false, want: `Are you sure you want to delete all taskruns related to task "task" (y/n): `, @@ -159,7 +414,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "Delete all with prompt", command: []string{"delete", "--all", "-n", "ns"}, - input: seeds[3], + dynamic: seeds[3].dynamicClient, + input: seeds[3].pipelineClient, inputStream: strings.NewReader("y"), wantError: false, want: "Are you sure you want to delete all taskruns in namespace \"ns\" (y/n): All TaskRuns deleted in namespace \"ns\"\n", @@ -167,7 +423,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "Delete all with -f", command: []string{"delete", "--all", "-f", "-n", "ns"}, - input: seeds[4], + dynamic: seeds[4].dynamicClient, + input: seeds[4].pipelineClient, inputStream: nil, wantError: false, want: "All TaskRuns deleted in namespace \"ns\"\n", @@ -175,7 +432,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "Delete all keeping 2", command: []string{"delete", "--all", "-f", "--keep", "2", "-n", "ns"}, - input: seeds[4], + dynamic: seeds[4].dynamicClient, + input: seeds[4].pipelineClient, inputStream: nil, wantError: false, want: "All but 2 TaskRuns deleted in namespace \"ns\"\n", @@ -183,7 +441,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "Keep -1 is a no go", command: []string{"delete", "--all", "-f", "--keep", "-1", "-n", "ns"}, - input: seeds[4], + dynamic: seeds[4].dynamicClient, + input: seeds[4].pipelineClient, inputStream: nil, wantError: true, want: "keep option should not be lower than 0", @@ -191,7 +450,8 @@ func TestTaskRunDelete(t *testing.T) { { name: "Error from using taskrun name with --all", command: []string{"delete", "taskrun", "--all", "-n", "ns"}, - input: seeds[4], + dynamic: seeds[4].dynamicClient, + input: seeds[4].pipelineClient, inputStream: nil, wantError: true, want: "--all flag should not have any arguments or flags specified with it", @@ -200,7 +460,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, Kube: tp.input.Kube} + p := &test.Params{Tekton: tp.input.Pipeline, Kube: tp.input.Kube, Dynamic: tp.dynamic} taskrun := Command(p) if tp.inputStream != nil { diff --git a/pkg/pipeline/pipeline.go b/pkg/pipeline/pipeline.go index f34dc707d..52357c823 100644 --- a/pkg/pipeline/pipeline.go +++ b/pkg/pipeline/pipeline.go @@ -18,8 +18,8 @@ import ( "fmt" "os" + plist "github.com/tektoncd/cli/pkg/actions/list" "github.com/tektoncd/cli/pkg/cli" - plist "github.com/tektoncd/cli/pkg/list" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" diff --git a/pkg/taskrun/list/list.go b/pkg/taskrun/list/list.go index 24587541c..c552ea296 100644 --- a/pkg/taskrun/list/list.go +++ b/pkg/taskrun/list/list.go @@ -18,9 +18,9 @@ import ( "fmt" "os" + trlist "github.com/tektoncd/cli/pkg/actions/list" "github.com/tektoncd/cli/pkg/cli" "github.com/tektoncd/cli/pkg/formatted" - trlist "github.com/tektoncd/cli/pkg/list" trsort "github.com/tektoncd/cli/pkg/taskrun/sort" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"