@@ -9,33 +9,112 @@ 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
+ prunsWithUncompletedFirst := []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
{
69
+ name : "sort by completion time" ,
70
+ pruns : pruns ,
71
+ wantName : []string {"premier" , "second" , "troisieme" },
72
+ },
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 with uncompleted item first" ,
85
+ pruns : prunsWithUncompletedFirst ,
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
+ },
98
+ {
99
+ name : "PipelineRuns with same completion time" ,
25
100
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 )),
101
+ * (tektontest .MakePRCompletion (clock , "first-same-time" , ns , success , nil , labels , 15 )),
102
+ * (tektontest .MakePRCompletion (clock , "second-same-time" , ns , success , nil , labels , 15 )),
103
+ * (tektontest .MakePRCompletion (clock , "third-same-time" , ns , success , nil , labels , 15 )),
104
+ * (tektontest .MakePRCompletion (clock , "earlier" , ns , success , nil , labels , 10 )),
105
+ * (tektontest .MakePRCompletion (clock , "later" , ns , success , nil , labels , 20 )),
29
106
},
30
- wantName : []string {"premier " , "second" , "troisieme " },
107
+ wantName : []string {"earlier " , "first-same-time" , " second-same-time " , "third-same-time" , "later " },
31
108
},
32
- // TODO: Add test cases.
33
109
}
34
110
for _ , tt := range tests {
35
111
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 ())
112
+ got := PipelineRunSortByCompletionTime (tt .pruns )
113
+ gotNames := make ([]string , len (got ))
114
+ for i , pr := range got {
115
+ gotNames [i ] = pr .GetName ()
38
116
}
117
+ assert .DeepEqual (t , tt .wantName , gotNames )
39
118
})
40
119
}
41
120
}
@@ -53,8 +132,18 @@ func TestPipelineRunSortByStartTime(t *testing.T) {
53
132
noCompletionPR .Status .CompletionTime = nil
54
133
55
134
notStartedYet := tektontest .MakePRCompletion (clock , "notStarted" , ns , success , nil , labels , 5 )
56
- noCompletionPR .Status .StartTime = nil
57
- noCompletionPR .Status .CompletionTime = nil
135
+ notStartedYet .Status .StartTime = nil
136
+ notStartedYet .Status .CompletionTime = nil
137
+
138
+ prunsWithOneNotStarted := []tektonv1.PipelineRun {
139
+ * (tektontest .MakePRCompletion (clock , "otherFirst" , ns , success , nil , labels , 30 )),
140
+ * notStartedYet ,
141
+ }
142
+
143
+ prunsWithNotStartedFirst := []tektonv1.PipelineRun {
144
+ * notStartedYet ,
145
+ * (tektontest .MakePRCompletion (clock , "otherFirst" , ns , success , nil , labels , 30 )),
146
+ }
58
147
59
148
tests := []struct {
60
149
name string
@@ -83,19 +172,41 @@ func TestPipelineRunSortByStartTime(t *testing.T) {
83
172
{
84
173
name : "not started yet" ,
85
174
pruns : []tektonv1.PipelineRun {
86
- * notStartedYet ,
87
175
* (tektontest .MakePRCompletion (clock , "otherFirst" , ns , success , nil , labels , 30 )),
88
176
* (tektontest .MakePRCompletion (clock , "otherSecond" , ns , success , nil , labels , 10 )),
177
+ * notStartedYet ,
89
178
},
90
- wantName : []string {"otherFirst" , "otherSecond" , "notStarted" },
179
+ wantName : []string {"notStarted" , "otherFirst" , "otherSecond" },
180
+ },
181
+ {
182
+ name : "not started yet single" ,
183
+ pruns : prunsWithOneNotStarted ,
184
+ wantName : []string {"notStarted" , "otherFirst" },
185
+ },
186
+ {
187
+ name : "sort with not-started item first" ,
188
+ pruns : prunsWithNotStartedFirst ,
189
+ wantName : []string {"notStarted" , "otherFirst" },
190
+ },
191
+ {
192
+ name : "empty list" ,
193
+ pruns : []tektonv1.PipelineRun {},
194
+ wantName : []string {},
195
+ },
196
+ {
197
+ name : "single item" ,
198
+ pruns : []tektonv1.PipelineRun {* startedEarlierPR },
199
+ wantName : []string {"earlier" },
91
200
},
92
201
}
93
202
for _ , tt := range tests {
94
203
t .Run (tt .name , func (t * testing.T ) {
95
204
PipelineRunSortByStartTime (tt .pruns )
96
- for key , value := range tt .pruns {
97
- assert .Equal (t , tt .wantName [key ], value .GetName ())
205
+ gotNames := make ([]string , len (tt .pruns ))
206
+ for i , pr := range tt .pruns {
207
+ gotNames [i ] = pr .GetName ()
98
208
}
209
+ assert .DeepEqual (t , tt .wantName , gotNames )
99
210
})
100
211
}
101
212
}
0 commit comments