From 581ea6502cadafe6d2a560749cccca77d0bd2af4 Mon Sep 17 00:00:00 2001 From: Lukas Krejci Date: Wed, 24 Apr 2024 19:11:46 +0200 Subject: [PATCH] Do not use the now removed CapacityThresholds in the tests and the utility methods. (#389) --------- Co-authored-by: Alexey Kazakov Co-authored-by: Francisc Munteanu Co-authored-by: Feny Mehta --- go.mod | 2 +- go.sum | 4 ++-- pkg/configuration/cache_test.go | 37 +++++++++++++++++------------- pkg/test/config/toolchainconfig.go | 36 ----------------------------- 4 files changed, 24 insertions(+), 55 deletions(-) diff --git a/go.mod b/go.mod index ee0121e4..971bda2f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/codeready-toolchain/toolchain-common go 1.20 require ( - github.com/codeready-toolchain/api v0.0.0-20240322110702-5ab3840476e9 + github.com/codeready-toolchain/api v0.0.0-20240424103940-03edc96d88fb github.com/go-logr/logr v1.2.3 github.com/gofrs/uuid v3.3.0+incompatible github.com/golang-jwt/jwt/v5 v5.2.0 diff --git a/go.sum b/go.sum index 7bbee26e..428178fc 100644 --- a/go.sum +++ b/go.sum @@ -115,8 +115,8 @@ github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWH github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5/go.mod h1:h6jFvWxBdQXxjopDMZyH2UVceIRfR84bdzbkoKrsWNo= github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoCr5oaCLELYA= github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI= -github.com/codeready-toolchain/api v0.0.0-20240322110702-5ab3840476e9 h1:Lm7bFLrzfJzrUiRGVqtsSaZMpj+akLiR/fvAFjjE9gM= -github.com/codeready-toolchain/api v0.0.0-20240322110702-5ab3840476e9/go.mod h1:cfNN6YPX4TORvhhZXMSjSPesqAHlB3nD/WAfGe4WLKQ= +github.com/codeready-toolchain/api v0.0.0-20240424103940-03edc96d88fb h1:YvEVAetaNYLAyuwO8AmKTtWD9M7SZPhbBzTVMXhrGas= +github.com/codeready-toolchain/api v0.0.0-20240424103940-03edc96d88fb/go.mod h1:ie9p4LenCCS0LsnbWp6/xwpFDdCWYE0KWzUO6Sk1g0E= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= diff --git a/pkg/configuration/cache_test.go b/pkg/configuration/cache_test.go index cc99f4ee..b9d86f70 100644 --- a/pkg/configuration/cache_test.go +++ b/pkg/configuration/cache_test.go @@ -17,6 +17,7 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/utils/pointer" ) func TestCache(t *testing.T) { @@ -47,7 +48,7 @@ func TestCache(t *testing.T) { t.Run("return config that is stored in cache", func(t *testing.T) { // given - originalConfig := NewToolchainConfigObjWithReset(t, testconfig.CapacityThresholds().MaxNumberOfSpaces(testconfig.PerMemberCluster("member1", 321))) //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + originalConfig := NewToolchainConfigObjWithReset(t, testconfig.AutomaticApproval().Enabled(true)) cl := test.NewFakeClient(t, originalConfig) // when @@ -62,7 +63,7 @@ func TestCache(t *testing.T) { t.Run("returns the same when the cache hasn't been updated", func(t *testing.T) { // given - newConfig := NewToolchainConfigObjWithReset(t, testconfig.CapacityThresholds().MaxNumberOfSpaces(testconfig.PerMemberCluster("member1", 666))) //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + newConfig := NewToolchainConfigObjWithReset(t, testconfig.AutomaticApproval().Enabled(true)) cl := test.NewFakeClient(t, newConfig) // when @@ -80,7 +81,7 @@ func TestCache(t *testing.T) { t.Run("returns the new config when the cache was updated", func(t *testing.T) { // given newConfig := NewToolchainConfigObjWithReset(t, - testconfig.CapacityThresholds().ResourceCapacityThreshold(666), //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + testconfig.AutomaticApproval().Enabled(true), testconfig.Deactivation().DeactivatingNotificationDays(5), testconfig.Notifications().Secret(). Ref("notification-secret"). @@ -112,7 +113,7 @@ func TestGetConfigFailed(t *testing.T) { defer restore() // given t.Run("config not found", func(t *testing.T) { - config := NewToolchainConfigObjWithReset(t, testconfig.CapacityThresholds().MaxNumberOfSpaces(testconfig.PerMemberCluster("member1", 321))) //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + config := NewToolchainConfigObjWithReset(t) cl := test.NewFakeClient(t, config) cl.MockGet = func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { return apierrors.NewNotFound(schema.GroupResource{}, "config") @@ -128,7 +129,7 @@ func TestGetConfigFailed(t *testing.T) { }) t.Run("error getting config", func(t *testing.T) { - config := NewToolchainConfigObjWithReset(t, testconfig.CapacityThresholds().MaxNumberOfSpaces(testconfig.PerMemberCluster("member1", 321))) //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + config := NewToolchainConfigObjWithReset(t) cl := test.NewFakeClient(t, config) cl.MockGet = func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { return fmt.Errorf("some error") @@ -144,7 +145,7 @@ func TestGetConfigFailed(t *testing.T) { }) t.Run("load secrets error", func(t *testing.T) { - config := NewToolchainConfigObjWithReset(t, testconfig.CapacityThresholds().MaxNumberOfSpaces(testconfig.PerMemberCluster("member1", 321))) //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + config := NewToolchainConfigObjWithReset(t) // given cl := test.NewFakeClient(t, config) cl.MockList = func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { @@ -173,7 +174,7 @@ func TestGetCachedConfig(t *testing.T) { t.Run("cache filled", func(t *testing.T) { // given - original := NewToolchainConfigObjWithReset(t, testconfig.CapacityThresholds().MaxNumberOfSpaces(testconfig.PerMemberCluster("member", 1))) //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + original := NewToolchainConfigObjWithReset(t, testconfig.AutomaticApproval().Enabled(true)) secretData := map[string]map[string]string{ "notification-secret": { @@ -198,7 +199,7 @@ func TestLoadLatest(t *testing.T) { restore := test.SetEnvVarAndRestore(t, "WATCH_NAMESPACE", test.HostOperatorNs) defer restore() t.Run("config found", func(t *testing.T) { - initConfig := NewToolchainConfigObjWithReset(t, testconfig.CapacityThresholds().ResourceCapacityThreshold(1100)) //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + initConfig := NewToolchainConfigObjWithReset(t, testconfig.AutomaticApproval().Enabled(true)) initSecret := &v1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: "notification-secret", @@ -219,7 +220,7 @@ func TestLoadLatest(t *testing.T) { require.NoError(t, err) toolchaincfg, ok := actual.(*toolchainv1alpha1.ToolchainConfig) require.True(t, ok) - assert.Equal(t, 1100, *toolchaincfg.Spec.Host.CapacityThresholds.ResourceCapacityThreshold.DefaultThreshold) + assert.True(t, *toolchaincfg.Spec.Host.AutomaticApproval.Enabled) assert.Len(t, secrets, 1) assert.Equal(t, "abc123", secrets["notification-secret"]["mailgunAPIKey"]) @@ -231,14 +232,14 @@ func TestLoadLatest(t *testing.T) { require.NoError(t, err) toolchaincfg, ok := actual.(*toolchainv1alpha1.ToolchainConfig) require.True(t, ok) - assert.Equal(t, 1100, *toolchaincfg.Spec.Host.CapacityThresholds.ResourceCapacityThreshold.DefaultThreshold) + assert.True(t, *toolchaincfg.Spec.Host.AutomaticApproval.Enabled) assert.Len(t, secrets, 1) assert.Equal(t, "abc123", secrets["notification-secret"]["mailgunAPIKey"]) }) t.Run("returns the new value when the config has been updated", func(t *testing.T) { // get - changedConfig := UpdateToolchainConfigObjWithReset(t, cl, testconfig.CapacityThresholds().ResourceCapacityThreshold(2000)) //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + changedConfig := UpdateToolchainConfigObjWithReset(t, cl, testconfig.AutomaticApproval().Enabled(false)) err := cl.Update(context.TODO(), changedConfig) require.NoError(t, err) @@ -255,7 +256,7 @@ func TestLoadLatest(t *testing.T) { require.NoError(t, err) toolchaincfg, ok := actual.(*toolchainv1alpha1.ToolchainConfig) require.True(t, ok) - assert.Equal(t, 2000, *toolchaincfg.Spec.Host.CapacityThresholds.ResourceCapacityThreshold.DefaultThreshold) + assert.False(t, *toolchaincfg.Spec.Host.AutomaticApproval.Enabled) assert.Len(t, secrets, 1) assert.Equal(t, "abc456", secrets["notification-secret"]["mailgunAPIKey"]) }) @@ -275,7 +276,7 @@ func TestLoadLatest(t *testing.T) { }) t.Run("get config error", func(t *testing.T) { - initconfig := NewToolchainConfigObjWithReset(t, testconfig.CapacityThresholds().ResourceCapacityThreshold(100)) //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + initconfig := NewToolchainConfigObjWithReset(t) // given cl := test.NewFakeClient(t, initconfig) cl.MockGet = func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { @@ -292,7 +293,7 @@ func TestLoadLatest(t *testing.T) { }) t.Run("load secrets error", func(t *testing.T) { - initconfig := NewToolchainConfigObjWithReset(t, testconfig.CapacityThresholds().ResourceCapacityThreshold(100)) //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + initconfig := NewToolchainConfigObjWithReset(t) // given cl := test.NewFakeClient(t, initconfig) cl.MockList = func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { @@ -316,7 +317,9 @@ func TestMultipleExecutionsInParallel(t *testing.T) { var latch sync.WaitGroup latch.Add(1) var waitForFinished sync.WaitGroup - initconfig := NewToolchainConfigObjWithReset(t, testconfig.CapacityThresholds().MaxNumberOfSpaces(testconfig.PerMemberCluster("member", 1))) //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + initconfig := NewToolchainConfigObjWithReset(t, testconfig.Members().SpecificPerMemberCluster("member", toolchainv1alpha1.MemberOperatorConfigSpec{ + Environment: pointer.String("env"), + })) secret := &v1.Secret{ ObjectMeta: metav1.ObjectMeta{ @@ -349,7 +352,9 @@ func TestMultipleExecutionsInParallel(t *testing.T) { go func(i int) { defer waitForFinished.Done() latch.Wait() - config := NewToolchainConfigObjWithReset(t, testconfig.CapacityThresholds().MaxNumberOfSpaces(testconfig.PerMemberCluster(fmt.Sprintf("member%d", i), i))) //nolint:staticcheck //this is testing the deprecated funcionality so let's keep it in until it is removed + config := NewToolchainConfigObjWithReset(t, testconfig.Members().SpecificPerMemberCluster(fmt.Sprintf("member%d", i), toolchainv1alpha1.MemberOperatorConfigSpec{ + Environment: pointer.String(fmt.Sprintf("env%d", i)), + })) secretData := map[string]map[string]string{ "notification-secret": { diff --git a/pkg/test/config/toolchainconfig.go b/pkg/test/config/toolchainconfig.go index 3e619183..593d61c5 100644 --- a/pkg/test/config/toolchainconfig.go +++ b/pkg/test/config/toolchainconfig.go @@ -85,42 +85,6 @@ func (o AutomaticApprovalOption) Enabled(value bool) AutomaticApprovalOption { return o } -// Deprecated: This is no longer used for anything. -type CapacityThresholdsOption struct { - *ToolchainConfigOptionImpl -} - -// Deprecated: This is no longer used for anything. -func CapacityThresholds() *CapacityThresholdsOption { - c := &CapacityThresholdsOption{ - ToolchainConfigOptionImpl: &ToolchainConfigOptionImpl{}, - } - return c -} - -// Deprecated: This is no longer used for anything. -func (c CapacityThresholdsOption) ResourceCapacityThreshold(defaultThreshold int, perMember ...PerMemberClusterOptionInt) CapacityThresholdsOption { - c.addFunction(func(config *toolchainv1alpha1.ToolchainConfig) { - config.Spec.Host.CapacityThresholds.ResourceCapacityThreshold.DefaultThreshold = &defaultThreshold - config.Spec.Host.CapacityThresholds.ResourceCapacityThreshold.SpecificPerMemberCluster = map[string]int{} - for _, add := range perMember { - add(config.Spec.Host.CapacityThresholds.ResourceCapacityThreshold.SpecificPerMemberCluster) - } - }) - return c -} - -// Deprecated: This is no longer used for anything. -func (c CapacityThresholdsOption) MaxNumberOfSpaces(perMember ...PerMemberClusterOptionInt) CapacityThresholdsOption { - c.addFunction(func(config *toolchainv1alpha1.ToolchainConfig) { - config.Spec.Host.CapacityThresholds.MaxNumberOfSpacesPerMemberCluster = map[string]int{} - for _, add := range perMember { - add(config.Spec.Host.CapacityThresholds.MaxNumberOfSpacesPerMemberCluster) - } - }) - return c -} - type DeactivationOption struct { *ToolchainConfigOptionImpl }