Skip to content

Commit

Permalink
Do not use the now removed CapacityThresholds in the tests and the ut…
Browse files Browse the repository at this point in the history
…ility methods. (codeready-toolchain#389)

---------

Co-authored-by: Alexey Kazakov <[email protected]>
Co-authored-by: Francisc Munteanu <[email protected]>
Co-authored-by: Feny Mehta <[email protected]>
  • Loading branch information
4 people authored Apr 24, 2024
1 parent e6d41ea commit 581ea65
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 55 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
37 changes: 21 additions & 16 deletions pkg/configuration/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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").
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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": {
Expand All @@ -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",
Expand All @@ -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"])

Expand All @@ -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)

Expand All @@ -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"])
})
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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{
Expand Down Expand Up @@ -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": {
Expand Down
36 changes: 0 additions & 36 deletions pkg/test/config/toolchainconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 581ea65

Please sign in to comment.