github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/releaseutil/sorter_test.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package releaseutil // import "github.com/stefanmcshane/helm/pkg/releaseutil" 18 19 import ( 20 "testing" 21 "time" 22 23 rspb "github.com/stefanmcshane/helm/pkg/release" 24 helmtime "github.com/stefanmcshane/helm/pkg/time" 25 ) 26 27 // note: this test data is shared with filter_test.go. 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 }