Skip to content

Commit 827ece2

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 827ece2

File tree

2 files changed

+115
-15
lines changed

2 files changed

+115
-15
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: 114 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,101 @@ 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+
prunsWithJMissing := []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
{
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,
3071
wantName: []string{"premier", "second", "troisieme"},
3172
},
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+
},
3398
}
3499
for _, tt := range tests {
35100
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()
38105
}
106+
assert.DeepEqual(t, tt.wantName, gotNames)
39107
})
40108
}
41109
}
@@ -53,8 +121,18 @@ func TestPipelineRunSortByStartTime(t *testing.T) {
53121
noCompletionPR.Status.CompletionTime = nil
54122

55123
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+
}
58136

59137
tests := []struct {
60138
name string
@@ -83,19 +161,41 @@ func TestPipelineRunSortByStartTime(t *testing.T) {
83161
{
84162
name: "not started yet",
85163
pruns: []tektonv1.PipelineRun{
86-
*notStartedYet,
87164
*(tektontest.MakePRCompletion(clock, "otherFirst", ns, success, nil, labels, 30)),
88165
*(tektontest.MakePRCompletion(clock, "otherSecond", ns, success, nil, labels, 10)),
166+
*notStartedYet,
89167
},
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"},
91189
},
92190
}
93191
for _, tt := range tests {
94192
t.Run(tt.name, func(t *testing.T) {
95193
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()
98197
}
198+
assert.DeepEqual(t, tt.wantName, gotNames)
99199
})
100200
}
101201
}

0 commit comments

Comments
 (0)