github.com/koderover/helm@v2.17.0+incompatible/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 "k8s.io/helm/pkg/releaseutil" 18 19 import ( 20 "testing" 21 "time" 22 23 "k8s.io/helm/pkg/proto/hapi/chart" 24 rspb "k8s.io/helm/pkg/proto/hapi/release" 25 "k8s.io/helm/pkg/timeconv" 26 ) 27 28 // note: this test data is shared with filter_test.go. 29 30 var releases = []*rspb.Release{ 31 tsRelease("quiet-bear", 2, 2000, rspb.Status_SUPERSEDED), 32 tsRelease("angry-bird", 4, 3000, rspb.Status_DEPLOYED), 33 tsRelease("happy-cats", 1, 4000, rspb.Status_DELETED), 34 tsRelease("vocal-dogs", 3, 6000, rspb.Status_DELETED), 35 } 36 37 func tsRelease(name string, vers int32, dur time.Duration, code rspb.Status_Code) *rspb.Release { 38 tmsp := timeconv.Timestamp(time.Now().Add(time.Duration(dur))) 39 info := &rspb.Info{Status: &rspb.Status{Code: code}, LastDeployed: tmsp} 40 return &rspb.Release{ 41 Name: name, 42 Version: vers, 43 Info: info, 44 Chart: &chart.Chart{ 45 Metadata: &chart.Metadata{ 46 Name: name, 47 }, 48 }, 49 } 50 } 51 52 func check(t *testing.T, by string, fn func(int, int) bool) { 53 for i := len(releases) - 1; i > 0; i-- { 54 if fn(i, i-1) { 55 t.Errorf("release at positions '(%d,%d)' not sorted by %s", i-1, i, by) 56 } 57 } 58 } 59 60 func TestSortByName(t *testing.T) { 61 SortByName(releases) 62 63 check(t, "ByName", func(i, j int) bool { 64 ni := releases[i].Name 65 nj := releases[j].Name 66 return ni < nj 67 }) 68 } 69 70 func TestSortByDate(t *testing.T) { 71 SortByDate(releases) 72 73 check(t, "ByDate", func(i, j int) bool { 74 ti := releases[i].Info.LastDeployed.Seconds 75 tj := releases[j].Info.LastDeployed.Seconds 76 return ti < tj 77 }) 78 } 79 80 func TestSortByRevision(t *testing.T) { 81 SortByRevision(releases) 82 83 check(t, "ByRevision", func(i, j int) bool { 84 vi := releases[i].Version 85 vj := releases[j].Version 86 return vi < vj 87 }) 88 } 89 90 func TestSortByChartName(t *testing.T) { 91 SortByChartName(releases) 92 93 check(t, "ByChartName", func(i, j int) bool { 94 ni := releases[i].Chart.Metadata.Name 95 nj := releases[j].Chart.Metadata.Name 96 return ni < nj 97 }) 98 }