github.com/y-taka-23/helm@v2.8.0+incompatible/pkg/releaseutil/sorter_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 rspb "k8s.io/helm/pkg/proto/hapi/release" 24 "k8s.io/helm/pkg/timeconv" 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.Status_SUPERSEDED), 31 tsRelease("angry-bird", 4, 3000, rspb.Status_DEPLOYED), 32 tsRelease("happy-cats", 1, 4000, rspb.Status_DELETED), 33 tsRelease("vocal-dogs", 3, 6000, rspb.Status_DELETED), 34 } 35 36 func tsRelease(name string, vers int32, dur time.Duration, code rspb.Status_Code) *rspb.Release { 37 tmsp := timeconv.Timestamp(time.Now().Add(time.Duration(dur))) 38 info := &rspb.Info{Status: &rspb.Status{Code: code}, LastDeployed: tmsp} 39 return &rspb.Release{ 40 Name: name, 41 Version: vers, 42 Info: info, 43 } 44 } 45 46 func check(t *testing.T, by string, fn func(int, int) bool) { 47 for i := len(releases) - 1; i > 0; i-- { 48 if fn(i, i-1) { 49 t.Errorf("release at positions '(%d,%d)' not sorted by %s", i-1, i, by) 50 } 51 } 52 } 53 54 func TestSortByName(t *testing.T) { 55 SortByName(releases) 56 57 check(t, "ByName", func(i, j int) bool { 58 ni := releases[i].Name 59 nj := releases[j].Name 60 return ni < nj 61 }) 62 } 63 64 func TestSortByDate(t *testing.T) { 65 SortByDate(releases) 66 67 check(t, "ByDate", func(i, j int) bool { 68 ti := releases[i].Info.LastDeployed.Seconds 69 tj := releases[j].Info.LastDeployed.Seconds 70 return ti < tj 71 }) 72 } 73 74 func TestSortByRevision(t *testing.T) { 75 SortByRevision(releases) 76 77 check(t, "ByRevision", func(i, j int) bool { 78 vi := releases[i].Version 79 vj := releases[j].Version 80 return vi < vj 81 }) 82 }