From 004c4c4567f870928fe95e41a2aca881c9f0ce97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Golicz?= Date: Mon, 18 Nov 2024 18:52:20 +0100 Subject: [PATCH] adding unit tests for ExtendWithSeedSelector extender function --- pkg/gardener/shoot/converter.go | 2 +- ...ed_in_shoot_region.go => seed_selector.go} | 6 +- .../shoot/extender/seed_selector_test.go | 74 +++++++++++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) rename pkg/gardener/shoot/extender/{seed_in_shoot_region.go => seed_selector.go} (69%) create mode 100644 pkg/gardener/shoot/extender/seed_selector_test.go diff --git a/pkg/gardener/shoot/converter.go b/pkg/gardener/shoot/converter.go index 14d6316c..e8ef7f90 100644 --- a/pkg/gardener/shoot/converter.go +++ b/pkg/gardener/shoot/converter.go @@ -17,7 +17,7 @@ func baseExtenders(cfg config.ConverterConfig) []Extend { return []Extend{ extender2.ExtendWithAnnotations, extender2.ExtendWithLabels, - extender2.ExtendWithSeedInSameRegion, + extender2.ExtendWithSeedSelector, extender2.NewDNSExtender(cfg.DNS.SecretName, cfg.DNS.DomainPrefix, cfg.DNS.ProviderType), extender2.NewOidcExtender(cfg.Kubernetes.DefaultOperatorOidc), extender2.ExtendWithCloudProfile, diff --git a/pkg/gardener/shoot/extender/seed_in_shoot_region.go b/pkg/gardener/shoot/extender/seed_selector.go similarity index 69% rename from pkg/gardener/shoot/extender/seed_in_shoot_region.go rename to pkg/gardener/shoot/extender/seed_selector.go index 89e721ba..459a22c6 100644 --- a/pkg/gardener/shoot/extender/seed_in_shoot_region.go +++ b/pkg/gardener/shoot/extender/seed_selector.go @@ -10,9 +10,9 @@ const ( seedRegionSelectorLabel = "seed.gardener.cloud/region" ) -// ExtendWithSeedInSameRegion creates a new extender function that can enforce shoot seed location to be the same as shoot region -// When EnforceSeedLocation flag in set on RuntimeCR to true it adds special seedSelector field with labelSelector to match seed region with shoot region -func ExtendWithSeedInSameRegion(runtime imv1.Runtime, shoot *gardener.Shoot) error { +// ExtendWithSeedSelector creates a new extender function that can enforce shoot seed location to be the same region as shoot +// When EnforceSeedLocation flag in set on RuntimeCR to true it adds a special seedSelector field with labelSelector set to match seed region with shoot region +func ExtendWithSeedSelector(runtime imv1.Runtime, shoot *gardener.Shoot) error { if runtime.Spec.Shoot.EnforceSeedLocation != nil && *runtime.Spec.Shoot.EnforceSeedLocation && runtime.Spec.Shoot.Region != "" { shoot.Spec.SeedSelector = &gardener.SeedSelector{ LabelSelector: metav1.LabelSelector{ diff --git a/pkg/gardener/shoot/extender/seed_selector_test.go b/pkg/gardener/shoot/extender/seed_selector_test.go new file mode 100644 index 00000000..30e31996 --- /dev/null +++ b/pkg/gardener/shoot/extender/seed_selector_test.go @@ -0,0 +1,74 @@ +package extender + +import ( + "testing" + + imv1 "github.com/kyma-project/infrastructure-manager/api/v1" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSeedSelectorExtender(t *testing.T) { + t.Run("Add and populate seed selector field if RuntimeCR has SeedInSameRegionFlag set to true", func(t *testing.T) { + // given + runtimeShoot := getRuntimeWithSeedInSameRegionFlag(true) + shoot := fixEmptyGardenerShoot("test", "dev") + + // when + err := ExtendWithSeedSelector(runtimeShoot, &shoot) + + // then + require.NoError(t, err) + assert.NotNil(t, shoot.Spec.SeedSelector) + assert.Equal(t, runtimeShoot.Spec.Shoot.Region, shoot.Spec.SeedSelector.LabelSelector.MatchLabels[seedRegionSelectorLabel]) + }) + + t.Run("Don't add seed selector field if RuntimeCR has SeedInSameRegionFlag set to false", func(t *testing.T) { + // given + runtimeShoot := getRuntimeWithSeedInSameRegionFlag(false) + shoot := fixEmptyGardenerShoot("test", "dev") + + // when + err := ExtendWithSeedSelector(runtimeShoot, &shoot) + + // then + require.NoError(t, err) + assert.Nil(t, shoot.Spec.SeedSelector) + }) + + t.Run("Don't add seed selector field if RuntimeCR has no SeedInSameRegionFlag set", func(t *testing.T) { + // given + runtimeShoot := getRuntimeWithoutSeedInSameRegionFlag() + shoot := fixEmptyGardenerShoot("test", "dev") + + // when + err := ExtendWithSeedSelector(runtimeShoot, &shoot) + + // then + require.NoError(t, err) + assert.Nil(t, shoot.Spec.SeedSelector) + }) +} + +func getRuntimeWithSeedInSameRegionFlag(enabled bool) imv1.Runtime { + return imv1.Runtime{ + Spec: imv1.RuntimeSpec{ + Shoot: imv1.RuntimeShoot{ + Name: "myshoot", + EnforceSeedLocation: &enabled, + Region: "far-far-away", + }, + }, + } +} + +func getRuntimeWithoutSeedInSameRegionFlag() imv1.Runtime { + return imv1.Runtime{ + Spec: imv1.RuntimeSpec{ + Shoot: imv1.RuntimeShoot{ + Name: "myshoot", + Region: "far-far-away", + }, + }, + } +}