Skip to content

Commit

Permalink
adding unit tests for ExtendWithSeedSelector extender function
Browse files Browse the repository at this point in the history
  • Loading branch information
koala7659 committed Nov 18, 2024
1 parent 3cc3bd7 commit 004c4c4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/gardener/shoot/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
74 changes: 74 additions & 0 deletions pkg/gardener/shoot/extender/seed_selector_test.go
Original file line number Diff line number Diff line change
@@ -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",
},
},
}
}

0 comments on commit 004c4c4

Please sign in to comment.