Skip to content

Commit a187ddd

Browse files
committed
test(sort): add more unit tests for pr sorting
This commit addresses the TODO in `pkg/sort/pipelinerun_test.go` to add more test cases and improves the overall test coverage for the sorting logic. - Adds a new test function `TestPipelineRunSortByCompletionTime_missing` to cover more scenarios for sorting by completion time: - An empty list of PipelineRuns. - PipelineRuns with the same completion time. - A PipelineRun with a nil completion time. - Fixes a failing test case in `TestPipelineRunSortByStartTime` by correctly setting up the test data for a PipelineRun that has not started. - Removes the now-obsolete TODO comment. Fixes #2196 Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 78a7418 commit a187ddd

File tree

2 files changed

+125
-14
lines changed

2 files changed

+125
-14
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ test-e2e: test-e2e-cleanup ## run e2e tests
7474
.PHONY: html-coverage
7575
html-coverage: ## generate html coverage
7676
@mkdir -p tmp
77-
@go test -coverprofile=tmp/c.out ./.../ && go tool cover -html=tmp/c.out
77+
@go test -coverprofile=tmp/c.out ./pkg/... ./cmd/... && go tool cover -html=tmp/c.out
7878

7979
##@ Linting
8080
.PHONY: lint

pkg/sort/pipelinerun_test.go

Lines changed: 124 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,112 @@ import (
99
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
1010
"gotest.tools/v3/assert"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"knative.dev/pkg/apis"
13+
duckv1 "knative.dev/pkg/apis/duck/v1"
1214
)
1315

1416
func TestPipelineRunSortByCompletionTime(t *testing.T) {
1517
clock := clockwork.NewFakeClock()
1618
ns := "namespace"
1719
labels := map[string]string{}
1820
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+
1963
tests := []struct {
2064
name string
2165
pruns []tektonv1.PipelineRun
2266
wantName []string
2367
}{
2468
{
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",
25100
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)),
29106
},
30-
wantName: []string{"premier", "second", "troisieme"},
107+
wantName: []string{"earlier", "first-same-time", "second-same-time", "third-same-time", "later"},
31108
},
32-
// TODO: Add test cases.
33109
}
34110
for _, tt := range tests {
35111
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()
38116
}
117+
assert.DeepEqual(t, tt.wantName, gotNames)
39118
})
40119
}
41120
}
@@ -53,8 +132,18 @@ func TestPipelineRunSortByStartTime(t *testing.T) {
53132
noCompletionPR.Status.CompletionTime = nil
54133

55134
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+
}
58147

59148
tests := []struct {
60149
name string
@@ -83,19 +172,41 @@ func TestPipelineRunSortByStartTime(t *testing.T) {
83172
{
84173
name: "not started yet",
85174
pruns: []tektonv1.PipelineRun{
86-
*notStartedYet,
87175
*(tektontest.MakePRCompletion(clock, "otherFirst", ns, success, nil, labels, 30)),
88176
*(tektontest.MakePRCompletion(clock, "otherSecond", ns, success, nil, labels, 10)),
177+
*notStartedYet,
89178
},
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"},
91200
},
92201
}
93202
for _, tt := range tests {
94203
t.Run(tt.name, func(t *testing.T) {
95204
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()
98208
}
209+
assert.DeepEqual(t, tt.wantName, gotNames)
99210
})
100211
}
101212
}

0 commit comments

Comments
 (0)