@@ -9,33 +9,101 @@ import (
9
9
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
10
10
"gotest.tools/v3/assert"
11
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
+ "knative.dev/pkg/apis"
13
+ duckv1 "knative.dev/pkg/apis/duck/v1"
12
14
)
13
15
14
16
func TestPipelineRunSortByCompletionTime (t * testing.T ) {
15
17
clock := clockwork .NewFakeClock ()
16
18
ns := "namespace"
17
19
labels := map [string ]string {}
18
20
success := tektonv1 .PipelineRunReasonSuccessful .String ()
21
+
22
+ pruns := []tektonv1.PipelineRun {
23
+ * (tektontest .MakePRCompletion (clock , "troisieme" , ns , success , nil , labels , 30 )),
24
+ * (tektontest .MakePRCompletion (clock , "premier" , ns , success , nil , labels , 10 )),
25
+ * (tektontest .MakePRCompletion (clock , "second" , ns , success , nil , labels , 20 )),
26
+ }
27
+
28
+ noCompletionTimePR := tektonv1.PipelineRun {
29
+ ObjectMeta : metav1.ObjectMeta {
30
+ Name : "no-completion-time" ,
31
+ Namespace : ns ,
32
+ Labels : labels ,
33
+ },
34
+ Status : tektonv1.PipelineRunStatus {
35
+ Status : duckv1.Status {
36
+ Conditions : duckv1.Conditions {
37
+ {
38
+ Type : apis .ConditionSucceeded ,
39
+ Status : "True" ,
40
+ Reason : success ,
41
+ },
42
+ },
43
+ },
44
+ },
45
+ }
46
+
47
+ prunsMissing := []tektonv1.PipelineRun {
48
+ * (tektontest .MakePRCompletion (clock , "troisieme" , ns , success , nil , labels , 30 )),
49
+ * (tektontest .MakePRCompletion (clock , "premier" , ns , success , nil , labels , 10 )),
50
+ noCompletionTimePR ,
51
+ }
52
+
53
+ prunsWithOneMissing := []tektonv1.PipelineRun {
54
+ * (tektontest .MakePRCompletion (clock , "premier" , ns , success , nil , labels , 10 )),
55
+ noCompletionTimePR ,
56
+ }
57
+
58
+ prunsWithJMissing := []tektonv1.PipelineRun {
59
+ noCompletionTimePR ,
60
+ * (tektontest .MakePRCompletion (clock , "premier" , ns , success , nil , labels , 10 )),
61
+ }
62
+
19
63
tests := []struct {
20
64
name string
21
65
pruns []tektonv1.PipelineRun
22
66
wantName []string
23
67
}{
24
68
{
25
- pruns : []tektonv1.PipelineRun {
26
- * (tektontest .MakePRCompletion (clock , "troisieme" , ns , success , nil , labels , 30 )),
27
- * (tektontest .MakePRCompletion (clock , "premier" , ns , success , nil , labels , 10 )),
28
- * (tektontest .MakePRCompletion (clock , "second" , ns , success , nil , labels , 20 )),
29
- },
69
+ name : "sort by completion time" ,
70
+ pruns : pruns ,
30
71
wantName : []string {"premier" , "second" , "troisieme" },
31
72
},
32
- // TODO: Add test cases.
73
+ {
74
+ name : "sort by completion time with missing" ,
75
+ pruns : prunsMissing ,
76
+ wantName : []string {"no-completion-time" , "premier" , "troisieme" },
77
+ },
78
+ {
79
+ name : "sort by completion time with one missing" ,
80
+ pruns : prunsWithOneMissing ,
81
+ wantName : []string {"no-completion-time" , "premier" },
82
+ },
83
+ {
84
+ name : "sort by completion time with j missing" ,
85
+ pruns : prunsWithJMissing ,
86
+ wantName : []string {"no-completion-time" , "premier" },
87
+ },
88
+ {
89
+ name : "empty list" ,
90
+ pruns : []tektonv1.PipelineRun {},
91
+ wantName : []string {},
92
+ },
93
+ {
94
+ name : "single item" ,
95
+ pruns : []tektonv1.PipelineRun {* (tektontest .MakePRCompletion (clock , "premier" , ns , success , nil , labels , 10 ))},
96
+ wantName : []string {"premier" },
97
+ },
33
98
}
34
99
for _ , tt := range tests {
35
100
t .Run (tt .name , func (t * testing.T ) {
36
- for key , value := range PipelineRunSortByCompletionTime (tt .pruns ) {
37
- assert .Equal (t , tt .wantName [key ], value .GetName ())
101
+ got := PipelineRunSortByCompletionTime (tt .pruns )
102
+ gotNames := make ([]string , len (got ))
103
+ for i , pr := range got {
104
+ gotNames [i ] = pr .GetName ()
38
105
}
106
+ assert .DeepEqual (t , tt .wantName , gotNames )
39
107
})
40
108
}
41
109
}
@@ -53,8 +121,18 @@ func TestPipelineRunSortByStartTime(t *testing.T) {
53
121
noCompletionPR .Status .CompletionTime = nil
54
122
55
123
notStartedYet := tektontest .MakePRCompletion (clock , "notStarted" , ns , success , nil , labels , 5 )
56
- noCompletionPR .Status .StartTime = nil
57
- noCompletionPR .Status .CompletionTime = nil
124
+ notStartedYet .Status .StartTime = nil
125
+ notStartedYet .Status .CompletionTime = nil
126
+
127
+ prunsWithOneNotStarted := []tektonv1.PipelineRun {
128
+ * (tektontest .MakePRCompletion (clock , "otherFirst" , ns , success , nil , labels , 30 )),
129
+ * notStartedYet ,
130
+ }
131
+
132
+ prunsWithJNotStarted := []tektonv1.PipelineRun {
133
+ * notStartedYet ,
134
+ * (tektontest .MakePRCompletion (clock , "otherFirst" , ns , success , nil , labels , 30 )),
135
+ }
58
136
59
137
tests := []struct {
60
138
name string
@@ -83,19 +161,41 @@ func TestPipelineRunSortByStartTime(t *testing.T) {
83
161
{
84
162
name : "not started yet" ,
85
163
pruns : []tektonv1.PipelineRun {
86
- * notStartedYet ,
87
164
* (tektontest .MakePRCompletion (clock , "otherFirst" , ns , success , nil , labels , 30 )),
88
165
* (tektontest .MakePRCompletion (clock , "otherSecond" , ns , success , nil , labels , 10 )),
166
+ * notStartedYet ,
89
167
},
90
- wantName : []string {"otherFirst" , "otherSecond" , "notStarted" },
168
+ wantName : []string {"notStarted" , "otherFirst" , "otherSecond" },
169
+ },
170
+ {
171
+ name : "not started yet single" ,
172
+ pruns : prunsWithOneNotStarted ,
173
+ wantName : []string {"notStarted" , "otherFirst" },
174
+ },
175
+ {
176
+ name : "not started yet single j" ,
177
+ pruns : prunsWithJNotStarted ,
178
+ wantName : []string {"notStarted" , "otherFirst" },
179
+ },
180
+ {
181
+ name : "empty list" ,
182
+ pruns : []tektonv1.PipelineRun {},
183
+ wantName : []string {},
184
+ },
185
+ {
186
+ name : "single item" ,
187
+ pruns : []tektonv1.PipelineRun {* startedEarlierPR },
188
+ wantName : []string {"earlier" },
91
189
},
92
190
}
93
191
for _ , tt := range tests {
94
192
t .Run (tt .name , func (t * testing.T ) {
95
193
PipelineRunSortByStartTime (tt .pruns )
96
- for key , value := range tt .pruns {
97
- assert .Equal (t , tt .wantName [key ], value .GetName ())
194
+ gotNames := make ([]string , len (tt .pruns ))
195
+ for i , pr := range tt .pruns {
196
+ gotNames [i ] = pr .GetName ()
98
197
}
198
+ assert .DeepEqual (t , tt .wantName , gotNames )
99
199
})
100
200
}
101
201
}
0 commit comments