-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(*): add sorter/filter utilties to releaseutil
- Loading branch information
fibonacci1729
committed
Oct 12, 2016
1 parent
7a5ee45
commit 24fe864
Showing
8 changed files
with
243 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package releaseutil // import "k8s.io/helm/pkg/releaseutil" | ||
|
||
import ( | ||
rspb "k8s.io/helm/pkg/proto/hapi/release" | ||
"testing" | ||
) | ||
|
||
func TestFilterAny(t *testing.T) { | ||
ls := Any(StatusFilter(rspb.Status_DELETED)).Filter(releases) | ||
if len(ls) != 2 { | ||
t.Fatalf("expected 2 results, got '%d'", len(ls)) | ||
} | ||
|
||
r0, r1 := ls[0], ls[1] | ||
switch { | ||
case r0.Info.Status.Code != rspb.Status_DELETED: | ||
t.Fatalf("expected DELETED result, got '%s'", r1.Info.Status.Code) | ||
case r1.Info.Status.Code != rspb.Status_DELETED: | ||
t.Fatalf("expected DELETED result, got '%s'", r1.Info.Status.Code) | ||
} | ||
} | ||
|
||
func TestFilterAll(t *testing.T) { | ||
fn := FilterFunc(func(rls *rspb.Release) bool { | ||
// true if not deleted and version < 4 | ||
v0 := !StatusFilter(rspb.Status_DELETED).Check(rls) | ||
v1 := rls.Version < 4 | ||
return v0 && v1 | ||
}) | ||
|
||
ls := All(fn).Filter(releases) | ||
if len(ls) != 1 { | ||
t.Fatalf("expected 1 result, got '%d'", len(ls)) | ||
} | ||
|
||
switch r0 := ls[0]; { | ||
case r0.Version == 4: | ||
t.Fatal("got release with status revision 4") | ||
case r0.Info.Status.Code == rspb.Status_DELETED: | ||
t.Fatal("got release with status DELTED") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package releaseutil // import "k8s.io/helm/pkg/releaseutil" | ||
|
||
import ( | ||
"sort" | ||
|
||
rspb "k8s.io/helm/pkg/proto/hapi/release" | ||
) | ||
|
||
type sorter struct { | ||
list []*rspb.Release | ||
less func(int, int) bool | ||
} | ||
|
||
func (s *sorter) Len() int { return len(s.list) } | ||
func (s *sorter) Less(i, j int) bool { return s.less(i, j) } | ||
func (s *sorter) Swap(i, j int) { s.list[i], s.list[j] = s.list[j], s.list[i] } | ||
|
||
// Reverse reverses the list of releases sorted by the sort func. | ||
func Reverse(list []*rspb.Release, sortFn func([]*rspb.Release)) { | ||
sortFn(list) | ||
for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 { | ||
list[i], list[j] = list[j], list[i] | ||
} | ||
} | ||
|
||
// SortByName returns the list of releases sorted | ||
// in lexicographical order. | ||
func SortByName(list []*rspb.Release) { | ||
s := &sorter{list: list} | ||
s.less = func(i, j int) bool { | ||
ni := s.list[i].Name | ||
nj := s.list[j].Name | ||
return ni < nj | ||
} | ||
sort.Sort(s) | ||
} | ||
|
||
// SortByDate returns the list of releases sorted by a | ||
// release's last deployed time (in seconds). | ||
func SortByDate(list []*rspb.Release) { | ||
s := &sorter{list: list} | ||
|
||
s.less = func(i, j int) bool { | ||
ti := s.list[i].Info.LastDeployed.Seconds | ||
tj := s.list[j].Info.LastDeployed.Seconds | ||
return ti < tj | ||
} | ||
sort.Sort(s) | ||
} | ||
|
||
// SortByRevision returns the list of releases sorted by a | ||
// release's revision number (release.Version). | ||
func SortByRevision(list []*rspb.Release) { | ||
s := &sorter{list: list} | ||
s.less = func(i, j int) bool { | ||
vi := s.list[i].Version | ||
vj := s.list[j].Version | ||
return vi < vj | ||
} | ||
sort.Sort(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package releaseutil // import "k8s.io/helm/pkg/releaseutil" | ||
|
||
import ( | ||
rspb "k8s.io/helm/pkg/proto/hapi/release" | ||
"k8s.io/helm/pkg/timeconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// note: this test data is shared with filter_test.go. | ||
|
||
var releases = []*rspb.Release{ | ||
tsRelease("quiet-bear", 2, 2000, rspb.Status_SUPERSEDED), | ||
tsRelease("angry-bird", 4, 3000, rspb.Status_DEPLOYED), | ||
tsRelease("happy-cats", 1, 4000, rspb.Status_DELETED), | ||
tsRelease("vocal-dogs", 3, 6000, rspb.Status_DELETED), | ||
} | ||
|
||
func tsRelease(name string, vers int32, dur time.Duration, code rspb.Status_Code) *rspb.Release { | ||
tmsp := timeconv.Timestamp(time.Now().Add(time.Duration(dur))) | ||
info := &rspb.Info{Status: &rspb.Status{Code: code}, LastDeployed: tmsp} | ||
return &rspb.Release{ | ||
Name: name, | ||
Version: vers, | ||
Info: info, | ||
} | ||
} | ||
|
||
func check(t *testing.T, by string, fn func(int, int) bool) { | ||
for i := len(releases) - 1; i > 0; i-- { | ||
if fn(i, i-1) { | ||
t.Errorf("release at positions '(%d,%d)' not sorted by %s", i-1, i, by) | ||
} | ||
} | ||
} | ||
|
||
func TestSortByName(t *testing.T) { | ||
SortByName(releases) | ||
|
||
check(t, "ByName", func(i, j int) bool { | ||
ni := releases[i].Name | ||
nj := releases[j].Name | ||
return ni < nj | ||
}) | ||
} | ||
|
||
func TestSortByDate(t *testing.T) { | ||
SortByDate(releases) | ||
|
||
check(t, "ByDate", func(i, j int) bool { | ||
ti := releases[i].Info.LastDeployed.Seconds | ||
tj := releases[j].Info.LastDeployed.Seconds | ||
return ti < tj | ||
}) | ||
} | ||
|
||
func TestSortByRevision(t *testing.T) { | ||
SortByRevision(releases) | ||
|
||
check(t, "ByRevision", func(i, j int) bool { | ||
vi := releases[i].Version | ||
vj := releases[j].Version | ||
return vi < vj | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.