Skip to content

Commit

Permalink
Throw error when deleting tr/pr with non-existing task/pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
rudeigerc authored and tekton-robot committed Aug 19, 2020
1 parent 9d2b4b9 commit 781f094
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 22 deletions.
18 changes: 10 additions & 8 deletions pkg/cmd/pipelinerun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,16 @@ func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string, opts *opt
d.DeleteRelated(s, []string{opts.ParentResourceName})
}
if !opts.DeleteAllNs {
switch {
case opts.Keep > 0:
// Should only occur in case of --pipeline and --keep being used together
fmt.Fprintf(s.Out, "All but %d PipelineRuns associated with Pipeline %q deleted in namespace %q\n", opts.Keep, opts.ParentResourceName, p.Namespace())
case opts.ParentResourceName != "":
fmt.Fprintf(s.Out, "All PipelineRuns associated with Pipeline %q deleted in namespace %q\n", opts.ParentResourceName, p.Namespace())
default:
d.PrintSuccesses(s)
if d.Errors() == nil {
switch {
case opts.Keep > 0:
// Should only occur in case of --pipeline and --keep being used together
fmt.Fprintf(s.Out, "All but %d PipelineRuns associated with Pipeline %q deleted in namespace %q\n", opts.Keep, opts.ParentResourceName, p.Namespace())
case opts.ParentResourceName != "":
fmt.Fprintf(s.Out, "All PipelineRuns associated with Pipeline %q deleted in namespace %q\n", opts.ParentResourceName, p.Namespace())
default:
d.PrintSuccesses(s)
}
}
} else if opts.DeleteAllNs {
if d.Errors() == nil {
Expand Down
18 changes: 18 additions & 0 deletions pkg/cmd/pipelinerun/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@ func TestPipelineRunDelete(t *testing.T) {
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Error from deleting PipelineRun with non-existing Pipeline",
command: []string{"delete", "pipelinerun", "-p", "non-existing-pipeline"},
dynamic: seeds[4].dynamicClient,
input: seeds[4].pipelineClient,
inputStream: nil,
wantError: true,
want: "no PipelineRuns associated with Pipeline \"non-existing-pipeline\"",
},
{
name: "Remove pipelineruns of a pipeline using --keep",
command: []string{"rm", "--pipeline", "pipeline", "-n", "ns", "--keep", "2"},
Expand Down Expand Up @@ -594,6 +603,15 @@ func TestPipelineRunDelete_v1beta1(t *testing.T) {
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Error from deleting PipelineRun with non-existing Pipeline",
command: []string{"delete", "pipelinerun", "-p", "non-existing-pipeline"},
dynamic: seeds[4].dynamicClient,
input: seeds[4].pipelineClient,
inputStream: nil,
wantError: true,
want: "no PipelineRuns associated with Pipeline \"non-existing-pipeline\"",
},
{
name: "Remove pipelineruns of a pipeline using --keep",
command: []string{"rm", "--pipeline", "pipeline", "-n", "ns", "--keep", "2"},
Expand Down
18 changes: 10 additions & 8 deletions pkg/cmd/taskrun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,16 @@ func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, opts *options
}

if !opts.DeleteAllNs {
switch {
case opts.Keep > 0:
// Should only occur in case of --task flag and --keep being used together
fmt.Fprintf(s.Out, "All but %d TaskRuns associated with Task %q deleted in namespace %q\n", opts.Keep, opts.ParentResourceName, p.Namespace())
case opts.ParentResourceName != "":
fmt.Fprintf(s.Out, "All TaskRuns associated with Task %q deleted in namespace %q\n", opts.ParentResourceName, p.Namespace())
default:
d.PrintSuccesses(s)
if d.Errors() == nil {
switch {
case opts.Keep > 0:
// Should only occur in case of --task flag and --keep being used together
fmt.Fprintf(s.Out, "All but %d TaskRuns associated with Task %q deleted in namespace %q\n", opts.Keep, opts.ParentResourceName, p.Namespace())
case opts.ParentResourceName != "":
fmt.Fprintf(s.Out, "All TaskRuns associated with Task %q deleted in namespace %q\n", opts.ParentResourceName, p.Namespace())
default:
d.PrintSuccesses(s)
}
}
} else if opts.DeleteAllNs {
if d.Errors() == nil {
Expand Down
18 changes: 18 additions & 0 deletions pkg/cmd/taskrun/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ func TestTaskRunDelete(t *testing.T) {
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Error from deleting TaskRun with non-existing Task",
command: []string{"delete", "taskrun", "-t", "non-existing-task"},
dynamic: seeds[4].dynamicClient,
input: seeds[4].pipelineClient,
inputStream: nil,
wantError: true,
want: "no TaskRuns associated with Task \"non-existing-task\"",
},
{
name: "Remove taskruns of a task with --keep",
command: []string{"rm", "--task", "random", "-n", "ns", "--keep", "2"},
Expand Down Expand Up @@ -543,6 +552,15 @@ func TestTaskRunDelete_v1beta1(t *testing.T) {
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Error from deleting TaskRun with non-existing Task",
command: []string{"delete", "taskrun", "-t", "non-existing-task"},
dynamic: seeds[4].dynamicClient,
input: seeds[4].pipelineClient,
inputStream: nil,
wantError: true,
want: "no TaskRuns associated with Task \"non-existing-task\"",
},
{
name: "Remove taskruns of a task with --keep",
command: []string{"rm", "--task", "random", "-n", "ns", "--keep", "2"},
Expand Down
17 changes: 11 additions & 6 deletions pkg/deleter/deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,18 @@ func (d *Deleter) deleteRelatedList(streams *cli.Stream, resourceName string) {
err = fmt.Errorf("failed to list %ss: %s", strings.ToLower(d.relatedKind), err)
d.appendError(streams, err)
} else {
for _, subresource := range related {
if err := d.deleteRelated(subresource); err != nil {
err = fmt.Errorf("failed to delete %s %q: %s", d.relatedKind, subresource, err)
d.appendError(streams, err)
} else {
d.successfulRelatedDeletes = append(d.successfulRelatedDeletes, subresource)
if len(related) > 0 {
for _, subresource := range related {
if err := d.deleteRelated(subresource); err != nil {
err = fmt.Errorf("failed to delete %s %q: %s", d.relatedKind, subresource, err)
d.appendError(streams, err)
} else {
d.successfulRelatedDeletes = append(d.successfulRelatedDeletes, subresource)
}
}
} else {
err = fmt.Errorf("no %ss associated with %s %q", d.relatedKind, d.kind, resourceName)
d.appendError(streams, err)
}
}
}
Expand Down

0 comments on commit 781f094

Please sign in to comment.