From 86839f249031c348412409565b05f7ca5b3e3eab Mon Sep 17 00:00:00 2001 From: Bradley Skuse Date: Thu, 19 Dec 2019 16:21:24 +0800 Subject: [PATCH] Fix: helm3 - kind sorter incorrectly compares unknown and namespace Signed-off-by: Bradley Skuse Fix style error from CI Signed-off-by: Bradley Skuse Fix: helm3 - kind sorter incorrectly compares unknown and namespace Fix: helm3 - kind sorter incorrectly compares unknown and namespace Fix: helm3 - kind sorter incorrectly compares unknown and namespace (cherry picked from commit e2946c7e343f0b0404b5f4e9ecabdccd970a87c8) --- pkg/releaseutil/kind_sorter.go | 10 +++++----- pkg/releaseutil/kind_sorter_test.go | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/pkg/releaseutil/kind_sorter.go b/pkg/releaseutil/kind_sorter.go index a5110a100..41eeae722 100644 --- a/pkg/releaseutil/kind_sorter.go +++ b/pkg/releaseutil/kind_sorter.go @@ -134,13 +134,13 @@ func (k *kindSorter) Less(i, j int) bool { b := k.manifests[j] first, aok := k.ordering[a.Head.Kind] second, bok := k.ordering[b.Head.Kind] - // if same kind (including unknown) sub sort alphanumeric - if first == second { - // if both are unknown and of different kind sort by kind alphabetically - if !aok && !bok && a.Head.Kind != b.Head.Kind { + + if !aok && !bok { + // if both are unknown then sort alphabetically by kind, keep original order if same kind + if a.Head.Kind != b.Head.Kind { return a.Head.Kind < b.Head.Kind } - return a.Name < b.Name + return first < second } // unknown kind is last if !aok { diff --git a/pkg/releaseutil/kind_sorter_test.go b/pkg/releaseutil/kind_sorter_test.go index 93d8ae782..dcc5b847b 100644 --- a/pkg/releaseutil/kind_sorter_test.go +++ b/pkg/releaseutil/kind_sorter_test.go @@ -245,3 +245,24 @@ func TestKindSorterSubSort(t *testing.T) { }) } } + +func TestKindSorterNamespaceAgainstUnknown(t *testing.T) { + unknown := Manifest{ + Name: "a", + Head: &SimpleHead{Kind: "Unknown"}, + } + namespace := Manifest{ + Name: "b", + Head: &SimpleHead{Kind: "Namespace"}, + } + + manifests := []Manifest{unknown, namespace} + sortByKind(manifests, InstallOrder) + + expectedOrder := []Manifest{namespace, unknown} + for i, manifest := range manifests { + if expectedOrder[i].Name != manifest.Name { + t.Errorf("Expected %s, got %s", expectedOrder[i].Name, manifest.Name) + } + } +}