github.com/sri09kanth/helm@v3.0.0-beta.3+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 "helm.sh/helm/pkg/releaseutil"
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	rspb "helm.sh/helm/pkg/release"
    24  )
    25  
    26  // note: this test data is shared with filter_test.go.
    27  
    28  var releases = []*rspb.Release{
    29  	tsRelease("quiet-bear", 2, 2000, rspb.StatusSuperseded),
    30  	tsRelease("angry-bird", 4, 3000, rspb.StatusDeployed),
    31  	tsRelease("happy-cats", 1, 4000, rspb.StatusUninstalled),
    32  	tsRelease("vocal-dogs", 3, 6000, rspb.StatusUninstalled),
    33  }
    34  
    35  func tsRelease(name string, vers int, dur time.Duration, status rspb.Status) *rspb.Release {
    36  	tmsp := time.Now().Add(dur)
    37  	info := &rspb.Info{Status: status, LastDeployed: tmsp}
    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  }