From 696ec4156fce2bc59eb495af0d12078cf507355c Mon Sep 17 00:00:00 2001 From: Eneko Fernandez Date: Fri, 1 Sep 2023 09:40:42 +0100 Subject: [PATCH 1/2] added failing test --- core/server/primarykinds_test.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/core/server/primarykinds_test.go b/core/server/primarykinds_test.go index 2bef7b63e2..1e8c3bf32a 100644 --- a/core/server/primarykinds_test.go +++ b/core/server/primarykinds_test.go @@ -1,10 +1,12 @@ package server import ( - . "github.com/onsi/gomega" + "fmt" "reflect" "testing" + . "github.com/onsi/gomega" + "k8s.io/apimachinery/pkg/runtime/schema" ) @@ -78,4 +80,27 @@ func TestGetPrimaryKinds(t *testing.T) { g.Expect(primaryKinds.kinds["Pod"]).To(Equal(schema.GroupVersionKind{Group: "core", Version: "v2", Kind: "Pod"})) g.Expect(primaryKinds.kinds["Node"]).To(Equal(schema.GroupVersionKind{Group: "core", Version: "v1beta2", Kind: "Node"})) }) + + t.Run("should return v1 for gitrepositories", func(t *testing.T) { + primaryKinds, err := DefaultPrimaryKinds() + + g.Expect(err).NotTo(HaveOccurred()) + + // Expect the highest version of each kind to be returned + g.Expect(primaryKinds.kinds["GitRepository"]).To(Equal(schema.GroupVersionKind{Group: "source.toolkit.fluxcd.io", Version: "v1", Kind: "GitRepository"})) + }) +} + +func TestDefaultPrimaryKinds(t *testing.T) { + g := NewGomegaWithT(t) + for i := 0; i < 100; i++ { + primaryKinds, err := DefaultPrimaryKinds() + g.Expect(err).NotTo(HaveOccurred()) + + t.Run("should return v1 for gitrepositories", func(t *testing.T) { + // Expect the highest version of each kind to be returned + g.Expect(primaryKinds.kinds["GitRepository"]).To(Equal(schema.GroupVersionKind{Group: "source.toolkit.fluxcd.io", Version: "v1", Kind: "GitRepository"})) + fmt.Printf("iteration: %d", i) + }) + } } From c7ddb2e6b3170ce147b495372b5c44cb2fb00202 Mon Sep 17 00:00:00 2001 From: Eneko Fernandez Date: Fri, 1 Sep 2023 11:18:29 +0100 Subject: [PATCH 2/2] adjusted weighted seems to fix the ranking issue --- core/server/primarykinds.go | 2 +- core/server/primarykinds_test.go | 27 ++++++++------------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/core/server/primarykinds.go b/core/server/primarykinds.go index aed9a22e15..ef508a6ef0 100644 --- a/core/server/primarykinds.go +++ b/core/server/primarykinds.go @@ -25,7 +25,7 @@ const ( // It's multiplied with the digit following the "beta" suffix. // Beta versions are more stable than alpha versions but might still contain bugs. // They are given a higher weight than alpha versions. - betaWeight = 500 + betaWeight = 50 // stableWeight is a weight assigned to stable versions (those without an "alpha" or "beta" suffix). // Stable versions are the final, production-ready releases of software, hence they have the highest weight. diff --git a/core/server/primarykinds_test.go b/core/server/primarykinds_test.go index 1e8c3bf32a..f0cde3fecb 100644 --- a/core/server/primarykinds_test.go +++ b/core/server/primarykinds_test.go @@ -1,7 +1,6 @@ package server import ( - "fmt" "reflect" "testing" @@ -18,8 +17,11 @@ func TestVersionRank(t *testing.T) { gvk2 schema.GroupVersionKind expected int }{ + {schema.GroupVersionKind{Version: "v1"}, schema.GroupVersionKind{Version: "v1alpha1"}, 1}, {schema.GroupVersionKind{Version: "v1"}, schema.GroupVersionKind{Version: "v1beta1"}, 1}, + {schema.GroupVersionKind{Version: "v1"}, schema.GroupVersionKind{Version: "v1beta2"}, 1}, + {schema.GroupVersionKind{Version: "v1"}, schema.GroupVersionKind{Version: "v1beta10"}, 1}, {schema.GroupVersionKind{Version: "v2"}, schema.GroupVersionKind{Version: "v1"}, 1}, {schema.GroupVersionKind{Version: "v2beta1"}, schema.GroupVersionKind{Version: "v1beta1"}, 1}, // Additional test cases @@ -80,27 +82,14 @@ func TestGetPrimaryKinds(t *testing.T) { g.Expect(primaryKinds.kinds["Pod"]).To(Equal(schema.GroupVersionKind{Group: "core", Version: "v2", Kind: "Pod"})) g.Expect(primaryKinds.kinds["Node"]).To(Equal(schema.GroupVersionKind{Group: "core", Version: "v1beta2", Kind: "Node"})) }) - - t.Run("should return v1 for gitrepositories", func(t *testing.T) { - primaryKinds, err := DefaultPrimaryKinds() - - g.Expect(err).NotTo(HaveOccurred()) - - // Expect the highest version of each kind to be returned - g.Expect(primaryKinds.kinds["GitRepository"]).To(Equal(schema.GroupVersionKind{Group: "source.toolkit.fluxcd.io", Version: "v1", Kind: "GitRepository"})) - }) } func TestDefaultPrimaryKinds(t *testing.T) { g := NewGomegaWithT(t) - for i := 0; i < 100; i++ { - primaryKinds, err := DefaultPrimaryKinds() - g.Expect(err).NotTo(HaveOccurred()) + primaryKinds, err := DefaultPrimaryKinds() + g.Expect(err).NotTo(HaveOccurred()) - t.Run("should return v1 for gitrepositories", func(t *testing.T) { - // Expect the highest version of each kind to be returned - g.Expect(primaryKinds.kinds["GitRepository"]).To(Equal(schema.GroupVersionKind{Group: "source.toolkit.fluxcd.io", Version: "v1", Kind: "GitRepository"})) - fmt.Printf("iteration: %d", i) - }) - } + t.Run("should return v1 GitRepository", func(t *testing.T) { + g.Expect(primaryKinds.kinds["GitRepository"]).To(Equal(schema.GroupVersionKind{Group: "source.toolkit.fluxcd.io", Version: "v1", Kind: "GitRepository"})) + }) }