@@ -22,78 +22,77 @@ import (
22
22
23
23
"github.com/google/go-cmp/cmp"
24
24
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
25
- ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing"
26
25
"github.com/tektoncd/pipeline/test"
27
- tb "github.com/tektoncd/pipeline/test/builder"
28
- "go.uber.org/zap"
29
- "go.uber.org/zap/zaptest/observer"
30
26
corev1 "k8s.io/api/core/v1"
27
+ kerrors "k8s.io/apimachinery/pkg/api/errors"
31
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
29
"knative.dev/pkg/apis"
33
30
)
34
31
35
32
func TestCancelTaskRun (t * testing.T ) {
36
- testCases := []struct {
37
- name string
38
- taskRun * v1alpha1.TaskRun
39
- pod * corev1.Pod
40
- expectedStatus apis.Condition
33
+ namespace := "the-namespace"
34
+ taskRunName := "the-taskrun"
35
+ wantStatus := & apis.Condition {
36
+ Type : apis .ConditionSucceeded ,
37
+ Status : corev1 .ConditionFalse ,
38
+ Reason : "TaskRunCancelled" ,
39
+ Message : `TaskRun "the-taskrun" was cancelled` ,
40
+ }
41
+ for _ , c := range []struct {
42
+ desc string
43
+ taskRun * v1alpha1.TaskRun
44
+ pod * corev1.Pod
41
45
}{{
42
- name : "no-pod-scheduled" ,
43
- taskRun : tb .TaskRun ("test-taskrun-run-cancelled" , "foo" , tb .TaskRunSpec (
44
- tb .TaskRunTaskRef (simpleTask .Name ),
45
- tb .TaskRunCancelled ,
46
- ), tb .TaskRunStatus (tb .StatusCondition (apis.Condition {
47
- Type : apis .ConditionSucceeded ,
48
- Status : corev1 .ConditionUnknown ,
49
- }))),
50
- expectedStatus : apis.Condition {
51
- Type : apis .ConditionSucceeded ,
52
- Status : corev1 .ConditionFalse ,
53
- Reason : "TaskRunCancelled" ,
54
- Message : `TaskRun "test-taskrun-run-cancelled" was cancelled` ,
46
+ desc : "no pod scheduled" ,
47
+ taskRun : & v1alpha1.TaskRun {
48
+ ObjectMeta : metav1.ObjectMeta {
49
+ Name : taskRunName ,
50
+ Namespace : namespace ,
51
+ },
52
+ Spec : v1alpha1.TaskRunSpec {
53
+ Status : v1alpha1 .TaskRunSpecStatusCancelled ,
54
+ },
55
55
},
56
56
}, {
57
- name : "pod-scheduled" ,
58
- taskRun : tb .TaskRun ("test-taskrun-run-cancelled" , "foo" , tb .TaskRunSpec (
59
- tb .TaskRunTaskRef (simpleTask .Name ),
60
- tb .TaskRunCancelled ,
61
- ), tb .TaskRunStatus (tb .StatusCondition (apis.Condition {
62
- Type : apis .ConditionSucceeded ,
63
- Status : corev1 .ConditionUnknown ,
64
- }), tb .PodName ("foo-is-bar" ))),
57
+ desc : "pod scheduled" ,
58
+ taskRun : & v1alpha1.TaskRun {
59
+ ObjectMeta : metav1.ObjectMeta {
60
+ Name : taskRunName ,
61
+ Namespace : namespace ,
62
+ },
63
+ Spec : v1alpha1.TaskRunSpec {
64
+ Status : v1alpha1 .TaskRunSpecStatusCancelled ,
65
+ },
66
+ },
65
67
pod : & corev1.Pod {ObjectMeta : metav1.ObjectMeta {
66
- Namespace : "foo" ,
67
- Name : "foo-is-bar" ,
68
+ Namespace : namespace ,
69
+ Name : "the-pod" ,
70
+ Labels : map [string ]string {
71
+ "tekton.dev/taskRun" : taskRunName ,
72
+ },
68
73
}},
69
- expectedStatus : apis.Condition {
70
- Type : apis .ConditionSucceeded ,
71
- Status : corev1 .ConditionFalse ,
72
- Reason : "TaskRunCancelled" ,
73
- Message : `TaskRun "test-taskrun-run-cancelled" was cancelled` ,
74
- },
75
- }}
76
-
77
- for _ , tc := range testCases {
78
- t .Run (tc .name , func (t * testing.T ) {
74
+ }} {
75
+ t .Run (c .desc , func (t * testing.T ) {
79
76
d := test.Data {
80
- TaskRuns : []* v1alpha1.TaskRun {tc .taskRun },
77
+ TaskRuns : []* v1alpha1.TaskRun {c .taskRun },
81
78
}
82
- if tc .pod != nil {
83
- d .Pods = []* corev1.Pod {tc .pod }
79
+ if c .pod != nil {
80
+ d .Pods = []* corev1.Pod {c .pod }
84
81
}
85
82
86
- ctx , _ := ttesting .SetupFakeContext (t )
87
- ctx , cancel := context .WithCancel (ctx )
83
+ testAssets , cancel := getTaskRunController (t , d )
88
84
defer cancel ()
89
- c , _ := test .SeedTestData (t , ctx , d )
90
- observer , _ := observer .New (zap .InfoLevel )
91
- err := cancelTaskRun (tc .taskRun , c .Kube , zap .New (observer ).Sugar ())
92
- if err != nil {
85
+ if err := testAssets .Controller .Reconciler .Reconcile (context .Background (), getRunName (c .taskRun )); err != nil {
93
86
t .Fatal (err )
94
87
}
95
- if d := cmp .Diff (tc .taskRun .Status .GetCondition (apis .ConditionSucceeded ), & tc .expectedStatus , ignoreLastTransitionTime ); d != "" {
96
- t .Fatalf ("-want, +got: %v" , d )
88
+ if d := cmp .Diff (wantStatus , c .taskRun .Status .GetCondition (apis .ConditionSucceeded ), ignoreLastTransitionTime ); d != "" {
89
+ t .Errorf ("Diff(-want, +got): %s" , d )
90
+ }
91
+
92
+ if c .pod != nil {
93
+ if _ , err := testAssets .Controller .Reconciler .(* Reconciler ).KubeClientSet .CoreV1 ().Pods (c .taskRun .Namespace ).Get (c .pod .Name , metav1.GetOptions {}); ! kerrors .IsNotFound (err ) {
94
+ t .Errorf ("Pod was not deleted; wanted not-found error, got %v" , err )
95
+ }
97
96
}
98
97
})
99
98
}
0 commit comments