Skip to content

Commit

Permalink
Fix tkn tr delete command
Browse files Browse the repository at this point in the history
Fixes: #1972

Signed-off-by: Shiv Verma <[email protected]>
  • Loading branch information
pratap0007 committed Apr 5, 2023
1 parent 2a04d0b commit 06bdda9
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/taskrun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func ownerPrFinished(cs *cli.Clients, tr v1.TaskRun) bool {
for _, ref := range tr.GetOwnerReferences() {
if ref.Kind == pipeline.PipelineRunControllerName {
var pr *v1.PipelineRun
err := actions.GetV1(pipelineRunGroupResource, cs, tr.Namespace, ref.Name, metav1.GetOptions{}, &pr)
err := actions.GetV1(pipelineRunGroupResource, cs, ref.Name, tr.Namespace, metav1.GetOptions{}, &pr)
if err != nil {
return false
}
Expand Down
278 changes: 277 additions & 1 deletion pkg/cmd/taskrun/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2510,7 +2510,7 @@ func Test_TaskRuns_Delete_With_Running_PipelineRun(t *testing.T) {
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "tekton.dev/v1beta1",
APIVersion: "tekton.dev/v1",
Kind: "PipelineRun",
Name: "pipeline-run-1",
UID: "",
Expand Down Expand Up @@ -2666,3 +2666,279 @@ func Test_TaskRuns_Delete_With_Running_PipelineRun(t *testing.T) {
})
}
}

func Test_TaskRuns_Delete_With_SucessFull_PipelineRun(t *testing.T) {
clock := clockwork.NewFakeClock()

ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
}

tasks := []*v1.Task{
{
ObjectMeta: metav1.ObjectMeta{
Name: "random-1",
Namespace: "ns",
},
},
}

trdata := []*v1.TaskRun{
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "tr0-1",
Labels: map[string]string{"tekton.dev/task": "random"},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "tekton.dev/v1",
Kind: "PipelineRun",
Name: "pipeline-run-1",
UID: "",
Controller: nil,
BlockOwnerDeletion: nil,
}},
},
Spec: v1.TaskRunSpec{
TaskRef: &v1.TaskRef{
Name: "random",
Kind: v1.NamespacedTaskKind,
},
},
Status: v1.TaskRunStatus{
TaskRunStatusFields: v1.TaskRunStatusFields{
CompletionTime: &metav1.Time{
Time: time.Now(),
},
},
Status: duckv1.Status{
Conditions: duckv1.Conditions{
{
Status: corev1.ConditionTrue,
Reason: v1.TaskRunReasonSuccessful.String(),
},
},
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "tr0-2",
Labels: map[string]string{
"tekton.dev/task": "random",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "tekton.dev/v1",
Kind: "PipelineRun",
Name: "pipeline-run-1",
UID: "",
Controller: nil,
BlockOwnerDeletion: nil,
}},
},
Spec: v1.TaskRunSpec{
TaskRef: &v1.TaskRef{
Name: "random",
Kind: v1.NamespacedTaskKind,
},
},
Status: v1.TaskRunStatus{
TaskRunStatusFields: v1.TaskRunStatusFields{
CompletionTime: &metav1.Time{
Time: time.Now(),
},
},
Status: duckv1.Status{
Conditions: duckv1.Conditions{
{
Status: corev1.ConditionTrue,
Reason: v1.TaskRunReasonSuccessful.String(),
},
},
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "tr0-3",
Labels: map[string]string{
"tekton.dev/task": "random",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "tekton.dev/v1",
Kind: "PipelineRun",
Name: "pipeline-run-1",
UID: "",
Controller: nil,
BlockOwnerDeletion: nil,
}},
},
Spec: v1.TaskRunSpec{
TaskRef: &v1.TaskRef{
Name: "random",
Kind: v1.NamespacedTaskKind,
},
},
Status: v1.TaskRunStatus{
TaskRunStatusFields: v1.TaskRunStatusFields{
CompletionTime: &metav1.Time{
Time: time.Now(),
},
},
Status: duckv1.Status{
Conditions: duckv1.Conditions{
{
Status: corev1.ConditionTrue,
Reason: v1.TaskRunReasonSuccessful.String(),
},
},
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "tr0-4",
Labels: map[string]string{
"tekton.dev/task": "random",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "tekton.dev/v1",
Kind: "PipelineRun",
Name: "pipeline-run-1",
UID: "",
Controller: nil,
BlockOwnerDeletion: nil,
}},
},
Spec: v1.TaskRunSpec{
TaskRef: &v1.TaskRef{
Name: "random",
Kind: v1.NamespacedTaskKind,
},
},
Status: v1.TaskRunStatus{
TaskRunStatusFields: v1.TaskRunStatusFields{
CompletionTime: &metav1.Time{
Time: time.Now(),
},
},
Status: duckv1.Status{
Conditions: duckv1.Conditions{
{
Status: corev1.ConditionTrue,
Reason: v1.TaskRunReasonSuccessful.String(),
},
},
},
},
},
}

prdata := []*v1.PipelineRun{
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "pipeline-run-1",
Labels: map[string]string{"tekton.dev/pipeline": "pipeline"},
CreationTimestamp: metav1.Time{Time: clock.Now()},
},
Spec: v1.PipelineRunSpec{
PipelineRef: &v1.PipelineRef{
Name: "pipeline",
},
},
Status: v1.PipelineRunStatus{
Status: duckv1.Status{
Conditions: duckv1.Conditions{
{
Status: corev1.ConditionTrue,
Reason: v1.PipelineRunReasonSuccessful.String(),
},
},
},
PipelineRunStatusFields: v1.PipelineRunStatusFields{
// pipeline run starts now
StartTime: &metav1.Time{Time: clock.Now()},
},
},
},
}

type clients struct {
pipelineClient test.Clients
dynamicClient dynamic.Interface
}

seeds := make([]clients, 0)
trs := trdata
prs := prdata
cs, _ := test.SeedTestData(t, test.Data{TaskRuns: trs, Tasks: tasks, PipelineRuns: prs, Namespaces: ns})
cs.Pipeline.Resources = cb.APIResourceList(version, []string{"taskrun", "pipelinerun"})
tdc := testDynamic.Options{}
dc, err := tdc.Client(
cb.UnstructuredT(tasks[0], version),
cb.UnstructuredTR(trdata[0], version),
cb.UnstructuredTR(trdata[1], version),
cb.UnstructuredTR(trdata[2], version),
cb.UnstructuredTR(trdata[3], version),
cb.UnstructuredPR(prs[0], version),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
}
seeds = append(seeds, clients{cs, dc})

testParams := []struct {
name string
command []string
dynamic dynamic.Interface
input test.Clients
inputStream io.Reader
wantError bool
want string
}{
{
name: "Delete all taskruns and keep only 2",
command: []string{"delete", "--keep", "2"},
dynamic: seeds[0].dynamicClient,
input: seeds[0].pipelineClient,
inputStream: strings.NewReader("y"),
wantError: false,
want: "Are you sure you want to delete all TaskRuns in namespace \"\" keeping 2 TaskRuns (y/n): All but 2 TaskRuns(Completed) deleted in namespace \"\"\n",
},
}
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)
}
})
}
}

0 comments on commit 06bdda9

Please sign in to comment.