From 438e333edc4667a4ed55543f81a4c7e9ae9a28ef Mon Sep 17 00:00:00 2001 From: Lyndon Shi Date: Fri, 13 Oct 2023 23:03:25 -0700 Subject: [PATCH] second test case Signed-off-by: Lyndon Shi --- pkg/action/hooks_test.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/pkg/action/hooks_test.go b/pkg/action/hooks_test.go index 83822f085..520d0851d 100644 --- a/pkg/action/hooks_test.go +++ b/pkg/action/hooks_test.go @@ -25,7 +25,9 @@ import ( func Test_hookByWeight(t *testing.T) { hooks := []*release.Hook{ - {Weight: 1, Kind: "Pod", Name: "podA"}, + // The previous sorter sorts by (Weight, Name) so would fail this test as + // "PodA" < "ServiceAccountA". + {Weight: 1, Kind: "Pod", Name: "PodA"}, {Weight: 1, Kind: "ServiceAccount", Name: "ServiceAccountB"}, {Weight: 1, Kind: "ServiceAccount", Name: "ServiceAccountA"}, {Weight: 0, Kind: "Job", Name: "Job"}, @@ -50,3 +52,32 @@ func Test_hookByWeight(t *testing.T) { } } } + +func Test_hookByWeight_KindSorted(t *testing.T) { + // This test assumes that the list of hooks starts off sorted by Kind. + hooks := []*release.Hook{ + {Weight: 1, Kind: "ServiceAccount", Name: "ServiceAccountB"}, + {Weight: 1, Kind: "ServiceAccount", Name: "ServiceAccountA"}, + {Weight: 0, Kind: "Pod", Name: "podB"}, + {Weight: -1, Kind: "Pod", Name: "podA"}, + {Weight: 0, Kind: "Job", Name: "Job"}, + {Weight: 1, Kind: "APIService", Name: "APIServiceA"}, + } + + sort.Stable(hookByWeight(hooks)) + + expected := []*release.Hook{ + {Weight: -1, Kind: "Pod", Name: "podA"}, + {Weight: 0, Kind: "Pod", Name: "podB"}, + {Weight: 0, Kind: "Job", Name: "Job"}, + {Weight: 1, Kind: "ServiceAccount", Name: "ServiceAccountA"}, + {Weight: 1, Kind: "ServiceAccount", Name: "ServiceAccountB"}, + {Weight: 1, Kind: "APIService", Name: "APIServiceA"}, + } + + for i, hook := range hooks { + if hook.Name != expected[i].Name { + t.Errorf("Expected hook %d to be %s, got %s", i, expected[i].Name, hook.Name) + } + } +}