1
16
17 package releaseutil
18
19 import (
20 "testing"
21 "time"
22
23 rspb "helm.sh/helm/v3/pkg/release"
24 helmtime "helm.sh/helm/v3/pkg/time"
25 )
26
27
28
29 var releases = []*rspb.Release{
30 tsRelease("quiet-bear", 2, 2000, rspb.StatusSuperseded),
31 tsRelease("angry-bird", 4, 3000, rspb.StatusDeployed),
32 tsRelease("happy-cats", 1, 4000, rspb.StatusUninstalled),
33 tsRelease("vocal-dogs", 3, 6000, rspb.StatusUninstalled),
34 }
35
36 func tsRelease(name string, vers int, dur time.Duration, status rspb.Status) *rspb.Release {
37 info := &rspb.Info{Status: status, LastDeployed: helmtime.Now().Add(dur)}
38 return &rspb.Release{
39 Name: name,
40 Version: vers,
41 Info: info,
42 }
43 }
44
45 func check(t *testing.T, by string, fn func(int, int) bool) {
46 for i := len(releases) - 1; i > 0; i-- {
47 if fn(i, i-1) {
48 t.Errorf("release at positions '(%d,%d)' not sorted by %s", i-1, i, by)
49 }
50 }
51 }
52
53 func TestSortByName(t *testing.T) {
54 SortByName(releases)
55
56 check(t, "ByName", func(i, j int) bool {
57 ni := releases[i].Name
58 nj := releases[j].Name
59 return ni < nj
60 })
61 }
62
63 func TestSortByDate(t *testing.T) {
64 SortByDate(releases)
65
66 check(t, "ByDate", func(i, j int) bool {
67 ti := releases[i].Info.LastDeployed.Second()
68 tj := releases[j].Info.LastDeployed.Second()
69 return ti < tj
70 })
71 }
72
73 func TestSortByRevision(t *testing.T) {
74 SortByRevision(releases)
75
76 check(t, "ByRevision", func(i, j int) bool {
77 vi := releases[i].Version
78 vj := releases[j].Version
79 return vi < vj
80 })
81 }
82
83 func TestReverseSortByName(t *testing.T) {
84 Reverse(releases, SortByName)
85 check(t, "ByName", func(i, j int) bool {
86 ni := releases[i].Name
87 nj := releases[j].Name
88 return ni > nj
89 })
90 }
91
92 func TestReverseSortByDate(t *testing.T) {
93 Reverse(releases, SortByDate)
94 check(t, "ByDate", func(i, j int) bool {
95 ti := releases[i].Info.LastDeployed.Second()
96 tj := releases[j].Info.LastDeployed.Second()
97 return ti > tj
98 })
99 }
100
101 func TestReverseSortByRevision(t *testing.T) {
102 Reverse(releases, SortByRevision)
103 check(t, "ByRevision", func(i, j int) bool {
104 vi := releases[i].Version
105 vj := releases[j].Version
106 return vi > vj
107 })
108 }
109
View as plain text