From 266b4a1af9272e9101739e7253e55f109cf64262 Mon Sep 17 00:00:00 2001 From: Lukas Krejci Date: Mon, 18 Nov 2024 15:55:50 +0100 Subject: [PATCH] Utility methods for SPC's consumed capacity tests. --- .../spaceprovisionerconfig.go | 27 +- .../spaceprovisionerconfig_assertions.go | 149 ++++++---- .../spaceprovisionerconfig_assertions_test.go | 273 ++++++++++++------ 3 files changed, 315 insertions(+), 134 deletions(-) diff --git a/pkg/test/spaceprovisionerconfig/spaceprovisionerconfig.go b/pkg/test/spaceprovisionerconfig/spaceprovisionerconfig.go index 8caa6e0f..e858cf88 100644 --- a/pkg/test/spaceprovisionerconfig/spaceprovisionerconfig.go +++ b/pkg/test/spaceprovisionerconfig/spaceprovisionerconfig.go @@ -11,13 +11,15 @@ import ( type CreateOption func(*toolchainv1alpha1.SpaceProvisionerConfig) func NewSpaceProvisionerConfig(name string, namespace string, opts ...CreateOption) *toolchainv1alpha1.SpaceProvisionerConfig { - spc := &toolchainv1alpha1.SpaceProvisionerConfig{ + return ModifySpaceProvisionerConfig(&toolchainv1alpha1.SpaceProvisionerConfig{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: namespace, }, - } + }, opts...) +} +func ModifySpaceProvisionerConfig(spc *toolchainv1alpha1.SpaceProvisionerConfig, opts ...CreateOption) *toolchainv1alpha1.SpaceProvisionerConfig { for _, apply := range opts { apply(spc) } @@ -76,3 +78,24 @@ func MaxMemoryUtilizationPercent(number uint) CreateOption { spc.Spec.CapacityThresholds.MaxMemoryUtilizationPercent = number } } + +func WithConsumedSpaceCount(value int) CreateOption { + return func(spc *toolchainv1alpha1.SpaceProvisionerConfig) { + if spc.Status.ConsumedCapacity == nil { + spc.Status.ConsumedCapacity = &toolchainv1alpha1.ConsumedCapacity{} + } + spc.Status.ConsumedCapacity.SpaceCount = value + } +} + +func WithConsumedMemoryUsagePercentInNode(role string, usage int) CreateOption { + return func(spc *toolchainv1alpha1.SpaceProvisionerConfig) { + if spc.Status.ConsumedCapacity == nil { + spc.Status.ConsumedCapacity = &toolchainv1alpha1.ConsumedCapacity{} + } + if spc.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole == nil { + spc.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole = map[string]int{} + } + spc.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole[role] = usage + } +} diff --git a/pkg/test/spaceprovisionerconfig/spaceprovisionerconfig_assertions.go b/pkg/test/spaceprovisionerconfig/spaceprovisionerconfig_assertions.go index 14197dd6..7a3bb74f 100644 --- a/pkg/test/spaceprovisionerconfig/spaceprovisionerconfig_assertions.go +++ b/pkg/test/spaceprovisionerconfig/spaceprovisionerconfig_assertions.go @@ -8,81 +8,132 @@ import ( ) type ( - ready struct{} - notReady struct{} - notReadyWithReason struct { - expectedReason string + readyWithStatusAndReason struct { + expectedStatus corev1.ConditionStatus + expectedReason *string } + + consumedSpaceCount struct { + expectedSpaceCount int + } + + consumedMemoryUsage struct { + expectedMemoryUsage map[string]int + } + + unknownConsumedCapacity struct{} ) var ( - _ assertions.Predicate[*toolchainv1alpha1.SpaceProvisionerConfig] = (*ready)(nil) - _ assertions.Predicate[*toolchainv1alpha1.SpaceProvisionerConfig] = (*notReady)(nil) - _ assertions.Predicate[*toolchainv1alpha1.SpaceProvisionerConfig] = (*notReadyWithReason)(nil) - _ assertions.PredicateMatchFixer[*toolchainv1alpha1.SpaceProvisionerConfig] = (*ready)(nil) - _ assertions.PredicateMatchFixer[*toolchainv1alpha1.SpaceProvisionerConfig] = (*notReady)(nil) - _ assertions.PredicateMatchFixer[*toolchainv1alpha1.SpaceProvisionerConfig] = (*notReadyWithReason)(nil) + _ assertions.PredicateMatchFixer[*toolchainv1alpha1.SpaceProvisionerConfig] = (*readyWithStatusAndReason)(nil) + _ assertions.PredicateMatchFixer[*toolchainv1alpha1.SpaceProvisionerConfig] = (*consumedSpaceCount)(nil) + _ assertions.PredicateMatchFixer[*toolchainv1alpha1.SpaceProvisionerConfig] = (*consumedMemoryUsage)(nil) + _ assertions.PredicateMatchFixer[*toolchainv1alpha1.SpaceProvisionerConfig] = (*unknownConsumedCapacity)(nil) ) -func (*ready) Matches(spc *toolchainv1alpha1.SpaceProvisionerConfig) bool { - return condition.IsTrueWithReason(spc.Status.Conditions, toolchainv1alpha1.ConditionReady, toolchainv1alpha1.SpaceProvisionerConfigValidReason) -} +func (r *readyWithStatusAndReason) Matches(spc *toolchainv1alpha1.SpaceProvisionerConfig) bool { + cond, found := condition.FindConditionByType(spc.Status.Conditions, toolchainv1alpha1.ConditionReady) + if !found { + return false + } -func (*ready) FixToMatch(spc *toolchainv1alpha1.SpaceProvisionerConfig) *toolchainv1alpha1.SpaceProvisionerConfig { - spc.Status.Conditions, _ = condition.AddOrUpdateStatusConditions(spc.Status.Conditions, toolchainv1alpha1.Condition{ - Type: toolchainv1alpha1.ConditionReady, - Status: corev1.ConditionTrue, - Reason: toolchainv1alpha1.SpaceProvisionerConfigValidReason, - }) - return spc -} + if cond.Status != r.expectedStatus { + return false + } -func Ready() assertions.Predicate[*toolchainv1alpha1.SpaceProvisionerConfig] { - return &ready{} -} + if r.expectedReason != nil && cond.Reason != *r.expectedReason { + return false + } -func (*notReady) Matches(spc *toolchainv1alpha1.SpaceProvisionerConfig) bool { - return condition.IsFalse(spc.Status.Conditions, toolchainv1alpha1.ConditionReady) + return true } -func (*notReady) FixToMatch(spc *toolchainv1alpha1.SpaceProvisionerConfig) *toolchainv1alpha1.SpaceProvisionerConfig { +func (r *readyWithStatusAndReason) FixToMatch(spc *toolchainv1alpha1.SpaceProvisionerConfig) *toolchainv1alpha1.SpaceProvisionerConfig { cnd, found := condition.FindConditionByType(spc.Status.Conditions, toolchainv1alpha1.ConditionReady) + cnd.Type = toolchainv1alpha1.ConditionReady + cnd.Status = r.expectedStatus + if r.expectedReason != nil { + cnd.Reason = *r.expectedReason + } if !found { - spc.Status.Conditions = condition.AddStatusConditions(spc.Status.Conditions, toolchainv1alpha1.Condition{ - Type: toolchainv1alpha1.ConditionReady, - Status: corev1.ConditionFalse, - }) + spc.Status.Conditions = condition.AddStatusConditions(spc.Status.Conditions, cnd) } else { - cnd.Status = corev1.ConditionFalse spc.Status.Conditions, _ = condition.AddOrUpdateStatusConditions(spc.Status.Conditions, cnd) } return spc } -func NotReady() assertions.Predicate[*toolchainv1alpha1.SpaceProvisionerConfig] { - return ¬Ready{} +func (p *consumedSpaceCount) Matches(spc *toolchainv1alpha1.SpaceProvisionerConfig) bool { + if spc.Status.ConsumedCapacity == nil { + return false + } + return p.expectedSpaceCount == spc.Status.ConsumedCapacity.SpaceCount } -func (p *notReadyWithReason) Matches(spc *toolchainv1alpha1.SpaceProvisionerConfig) bool { - return condition.IsFalseWithReason(spc.Status.Conditions, toolchainv1alpha1.ConditionReady, p.expectedReason) +func (p *consumedSpaceCount) FixToMatch(spc *toolchainv1alpha1.SpaceProvisionerConfig) *toolchainv1alpha1.SpaceProvisionerConfig { + if spc.Status.ConsumedCapacity == nil { + spc.Status.ConsumedCapacity = &toolchainv1alpha1.ConsumedCapacity{} + } + spc.Status.ConsumedCapacity.SpaceCount = p.expectedSpaceCount + return spc } -func (p *notReadyWithReason) FixToMatch(spc *toolchainv1alpha1.SpaceProvisionerConfig) *toolchainv1alpha1.SpaceProvisionerConfig { - cnd, found := condition.FindConditionByType(spc.Status.Conditions, toolchainv1alpha1.ConditionReady) - if !found { - spc.Status.Conditions = condition.AddStatusConditions(spc.Status.Conditions, toolchainv1alpha1.Condition{ - Type: toolchainv1alpha1.ConditionReady, - Status: corev1.ConditionFalse, - Reason: p.expectedReason, - }) - } else { - cnd.Status = corev1.ConditionFalse - cnd.Reason = p.expectedReason - spc.Status.Conditions, _ = condition.AddOrUpdateStatusConditions(spc.Status.Conditions, cnd) +func (p *consumedMemoryUsage) Matches(spc *toolchainv1alpha1.SpaceProvisionerConfig) bool { + if spc.Status.ConsumedCapacity == nil { + return false + } + if len(spc.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole) != len(p.expectedMemoryUsage) { + return false } + for k, v := range spc.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole { + if p.expectedMemoryUsage[k] != v { + return false + } + } + return true +} + +func (p *consumedMemoryUsage) FixToMatch(spc *toolchainv1alpha1.SpaceProvisionerConfig) *toolchainv1alpha1.SpaceProvisionerConfig { + if spc.Status.ConsumedCapacity == nil { + spc.Status.ConsumedCapacity = &toolchainv1alpha1.ConsumedCapacity{} + } + spc.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole = p.expectedMemoryUsage + return spc +} + +func (p *unknownConsumedCapacity) Matches(spc *toolchainv1alpha1.SpaceProvisionerConfig) bool { + return spc.Status.ConsumedCapacity == nil +} + +func (p *unknownConsumedCapacity) FixToMatch(spc *toolchainv1alpha1.SpaceProvisionerConfig) *toolchainv1alpha1.SpaceProvisionerConfig { + spc.Status.ConsumedCapacity = nil return spc } +func Ready() assertions.Predicate[*toolchainv1alpha1.SpaceProvisionerConfig] { + return &readyWithStatusAndReason{expectedStatus: corev1.ConditionTrue} +} + +func NotReady() assertions.Predicate[*toolchainv1alpha1.SpaceProvisionerConfig] { + return &readyWithStatusAndReason{expectedStatus: corev1.ConditionFalse} +} + func NotReadyWithReason(reason string) assertions.Predicate[*toolchainv1alpha1.SpaceProvisionerConfig] { - return ¬ReadyWithReason{expectedReason: reason} + return &readyWithStatusAndReason{expectedStatus: corev1.ConditionFalse, expectedReason: &reason} +} + +func ReadyStatusAndReason(status corev1.ConditionStatus, reason string) assertions.Predicate[*toolchainv1alpha1.SpaceProvisionerConfig] { + return &readyWithStatusAndReason{expectedStatus: status, expectedReason: &reason} +} + +func ConsumedSpaceCount(value int) assertions.Predicate[*toolchainv1alpha1.SpaceProvisionerConfig] { + return &consumedSpaceCount{expectedSpaceCount: value} +} + +func ConsumedMemoryUsage(values map[string]int) assertions.Predicate[*toolchainv1alpha1.SpaceProvisionerConfig] { + return &consumedMemoryUsage{expectedMemoryUsage: values} +} + +func UnknownConsumedCapacity() assertions.Predicate[*toolchainv1alpha1.SpaceProvisionerConfig] { + return &unknownConsumedCapacity{} } diff --git a/pkg/test/spaceprovisionerconfig/spaceprovisionerconfig_assertions_test.go b/pkg/test/spaceprovisionerconfig/spaceprovisionerconfig_assertions_test.go index eb43e913..1ee5b114 100644 --- a/pkg/test/spaceprovisionerconfig/spaceprovisionerconfig_assertions_test.go +++ b/pkg/test/spaceprovisionerconfig/spaceprovisionerconfig_assertions_test.go @@ -9,158 +9,265 @@ import ( corev1 "k8s.io/api/core/v1" ) -func TestReadyPredicate(t *testing.T) { - t.Run("matching", func(t *testing.T) { +func TestReadyWithStatusAndReasonPredicate(t *testing.T) { + test := func(status corev1.ConditionStatus, reason string, t *testing.T) { + t.Run("matching: status="+string(status)+", reason='"+reason+"'", func(t *testing.T) { + // given + pred := &readyWithStatusAndReason{expectedStatus: status} + spc := NewSpaceProvisionerConfig("spc", "default", WithReadyCondition(status, reason)) + + // when & then + assert.True(t, pred.Matches(spc)) + }) + + t.Run("fixer with no conditions: status="+string(status)+", reason='"+reason+"'", func(t *testing.T) { + // given + pred := &readyWithStatusAndReason{expectedStatus: status} + spc := NewSpaceProvisionerConfig("spc", "default") + + // when + spc = pred.FixToMatch(spc) + + // then + assert.Equal(t, 1, condition.Count(spc.Status.Conditions, toolchainv1alpha1.ConditionReady, status, "")) + }) + t.Run("fixer with different conditions: status="+string(status)+", reason='"+reason+"'", func(t *testing.T) { + // given + pred := &readyWithStatusAndReason{expectedStatus: status} + spc := NewSpaceProvisionerConfig("spc", "default") + spc.Status.Conditions = []toolchainv1alpha1.Condition{ + { + Type: toolchainv1alpha1.ConditionType("made up"), + Status: corev1.ConditionTrue, + }, + } + + // when + spc = pred.FixToMatch(spc) + + // then + assert.Equal(t, 1, condition.Count(spc.Status.Conditions, toolchainv1alpha1.ConditionReady, status, "")) + assert.Len(t, spc.Status.Conditions, 2) + }) + t.Run("fixer with wrong condition: status="+string(status)+", reason='"+reason+"'", func(t *testing.T) { + // given + anotherStatus := corev1.ConditionTrue + if anotherStatus == status { + anotherStatus = corev1.ConditionFalse + } + if anotherStatus == status { + anotherStatus = corev1.ConditionUnknown + } + pred := &readyWithStatusAndReason{expectedStatus: status} + expectedReason := "because" + spc := NewSpaceProvisionerConfig("spc", "default", WithReadyCondition(anotherStatus, expectedReason)) + + // when + spc = pred.FixToMatch(spc) + + // then + assert.Equal(t, 1, condition.Count(spc.Status.Conditions, toolchainv1alpha1.ConditionReady, status, expectedReason)) + }) + } + + for _, status := range []corev1.ConditionStatus{corev1.ConditionTrue, corev1.ConditionFalse, corev1.ConditionUnknown} { + t.Run("no reason specified", func(t *testing.T) { + test(status, "", t) + }) + t.Run("with reason", func(t *testing.T) { + test(status, "the reason", t) + }) + } +} + +func TestConsumedSpaceCountPredicate(t *testing.T) { + t.Run("matches", func(t *testing.T) { // given - pred := &ready{} - spc := NewSpaceProvisionerConfig("spc", "default", WithReadyConditionValid()) + pred := &consumedSpaceCount{expectedSpaceCount: 5} + spc := NewSpaceProvisionerConfig("spc", "default", WithConsumedSpaceCount(5)) // when & then assert.True(t, pred.Matches(spc)) }) + t.Run("doesn't match", func(t *testing.T) { + // given + pred := &consumedSpaceCount{expectedSpaceCount: 5} + spc := NewSpaceProvisionerConfig("spc", "default", WithConsumedSpaceCount(4)) - t.Run("fixer with no conditions", func(t *testing.T) { + // when & then + assert.False(t, pred.Matches(spc)) + }) + t.Run("doesn't match if nil consumed capacity", func(t *testing.T) { // given - pred := &ready{} + pred := &consumedSpaceCount{expectedSpaceCount: 5} spc := NewSpaceProvisionerConfig("spc", "default") - // when - spc = pred.FixToMatch(spc) - - // then - assert.True(t, condition.IsTrue(spc.Status.Conditions, toolchainv1alpha1.ConditionReady)) + // when & then + assert.False(t, pred.Matches(spc)) }) - t.Run("fixer with different conditions", func(t *testing.T) { + t.Run("fixes wrong value", func(t *testing.T) { // given - pred := &ready{} - spc := NewSpaceProvisionerConfig("spc", "default") - spc.Status.Conditions = []toolchainv1alpha1.Condition{ - { - Type: toolchainv1alpha1.ConditionType("made up"), - Status: corev1.ConditionTrue, - }, - } + pred := &consumedSpaceCount{expectedSpaceCount: 5} + spc := NewSpaceProvisionerConfig("spc", "default", WithConsumedSpaceCount(4)) // when - spc = pred.FixToMatch(spc) + fixed := pred.FixToMatch(spc.DeepCopy()) // then - assert.True(t, condition.IsTrue(spc.Status.Conditions, toolchainv1alpha1.ConditionReady)) - assert.Len(t, spc.Status.Conditions, 2) + assert.NotNil(t, fixed.Status.ConsumedCapacity) + assert.Equal(t, 5, fixed.Status.ConsumedCapacity.SpaceCount) }) - t.Run("fixer with wrong condition", func(t *testing.T) { + t.Run("fixes if there's no consumed capacity", func(t *testing.T) { // given - pred := &ready{} - spc := NewSpaceProvisionerConfig("spc", "default", WithReadyConditionInvalid("because")) + pred := &consumedSpaceCount{expectedSpaceCount: 5} + spc := NewSpaceProvisionerConfig("spc", "default") // when - spc = pred.FixToMatch(spc) + fixed := pred.FixToMatch(spc.DeepCopy()) // then - assert.True(t, condition.IsTrueWithReason(spc.Status.Conditions, toolchainv1alpha1.ConditionReady, toolchainv1alpha1.SpaceProvisionerConfigValidReason)) + assert.NotNil(t, fixed.Status.ConsumedCapacity) + assert.Equal(t, 5, fixed.Status.ConsumedCapacity.SpaceCount) }) } -func TestNotReadyPredicate(t *testing.T) { - t.Run("matching", func(t *testing.T) { +func TestConsumedMemoryUsagePredicate(t *testing.T) { + t.Run("matches", func(t *testing.T) { // given - pred := ¬Ready{} - spc := NewSpaceProvisionerConfig("spc", "default", WithReadyConditionInvalid("any reason")) + pred := &consumedMemoryUsage{expectedMemoryUsage: map[string]int{"worker": 5, "master": 20}} + spc := NewSpaceProvisionerConfig("spc", "default", WithConsumedMemoryUsagePercentInNode("worker", 5), WithConsumedMemoryUsagePercentInNode("master", 20)) // when & then assert.True(t, pred.Matches(spc)) }) + t.Run("doesn't match if expecting more keys", func(t *testing.T) { + // given + pred := &consumedMemoryUsage{expectedMemoryUsage: map[string]int{"worker": 5, "master": 20}} + spc := NewSpaceProvisionerConfig("spc", "default", WithConsumedMemoryUsagePercentInNode("worker", 5)) + + // when & then + assert.False(t, pred.Matches(spc)) + }) + t.Run("doesn't match if more keys present", func(t *testing.T) { + // given + pred := &consumedMemoryUsage{expectedMemoryUsage: map[string]int{"worker": 5}} + spc := NewSpaceProvisionerConfig("spc", "default", WithConsumedMemoryUsagePercentInNode("worker", 5), WithConsumedMemoryUsagePercentInNode("master", 20)) + + // when & then + assert.False(t, pred.Matches(spc)) + }) + t.Run("doesn't match if value differs", func(t *testing.T) { + // given + pred := &consumedMemoryUsage{expectedMemoryUsage: map[string]int{"worker": 5, "master": 20}} + spc := NewSpaceProvisionerConfig("spc", "default", WithConsumedMemoryUsagePercentInNode("worker", 5), WithConsumedMemoryUsagePercentInNode("master", 21)) - t.Run("fixer with no conditions", func(t *testing.T) { + // when & then + assert.False(t, pred.Matches(spc)) + }) + t.Run("doesn't match if nil consumed capacity", func(t *testing.T) { // given - pred := ¬Ready{} + pred := &consumedMemoryUsage{expectedMemoryUsage: map[string]int{"worker": 5, "master": 20}} + spc := NewSpaceProvisionerConfig("spc", "default") + + // when & then + assert.False(t, pred.Matches(spc)) + }) + t.Run("fixes no consumed capacity", func(t *testing.T) { + // given + pred := &consumedMemoryUsage{expectedMemoryUsage: map[string]int{"worker": 5, "master": 20}} spc := NewSpaceProvisionerConfig("spc", "default") // when - spc = pred.FixToMatch(spc) + fixed := pred.FixToMatch(spc.DeepCopy()) // then - assert.True(t, condition.IsFalse(spc.Status.Conditions, toolchainv1alpha1.ConditionReady)) + assert.NotNil(t, fixed.Status.ConsumedCapacity) + assert.Len(t, fixed.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole, 2) + assert.Equal(t, 5, fixed.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole["worker"]) + assert.Equal(t, 20, fixed.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole["master"]) }) - t.Run("fixer with different conditions", func(t *testing.T) { + t.Run("fixes different values", func(t *testing.T) { // given - pred := ¬Ready{} - spc := NewSpaceProvisionerConfig("spc", "default") - spc.Status.Conditions = []toolchainv1alpha1.Condition{ - { - Type: toolchainv1alpha1.ConditionType("made up"), - Status: corev1.ConditionTrue, - }, - } + pred := &consumedMemoryUsage{expectedMemoryUsage: map[string]int{"worker": 5, "master": 20}} + spc := NewSpaceProvisionerConfig("spc", "default", + WithConsumedMemoryUsagePercentInNode("worker", 6), + WithConsumedMemoryUsagePercentInNode("master", 21), + ) // when - spc = pred.FixToMatch(spc) + fixed := pred.FixToMatch(spc.DeepCopy()) // then - assert.True(t, condition.IsFalse(spc.Status.Conditions, toolchainv1alpha1.ConditionReady)) - assert.Len(t, spc.Status.Conditions, 2) + assert.NotNil(t, fixed.Status.ConsumedCapacity) + assert.Len(t, fixed.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole, 2) + assert.Equal(t, 5, fixed.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole["worker"]) + assert.Equal(t, 20, fixed.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole["master"]) }) - t.Run("fixer with wrong condition", func(t *testing.T) { + t.Run("fixes different keys", func(t *testing.T) { // given - pred := ¬Ready{} - spc := NewSpaceProvisionerConfig("spc", "default", WithReadyConditionValid()) + pred := &consumedMemoryUsage{expectedMemoryUsage: map[string]int{"worker": 5, "master": 20}} + spc := NewSpaceProvisionerConfig("spc", "default", + WithConsumedMemoryUsagePercentInNode("master", 21), + WithConsumedMemoryUsagePercentInNode("disaster", 80), + ) // when - spc = pred.FixToMatch(spc) + fixed := pred.FixToMatch(spc.DeepCopy()) // then - assert.True(t, condition.IsFalse(spc.Status.Conditions, toolchainv1alpha1.ConditionReady)) + assert.NotNil(t, fixed.Status.ConsumedCapacity) + assert.Len(t, fixed.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole, 2) + assert.Equal(t, 5, fixed.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole["worker"]) + assert.Equal(t, 20, fixed.Status.ConsumedCapacity.MemoryUsagePercentPerNodeRole["master"]) }) } -func TestNotReadyWithReasonPredicate(t *testing.T) { - t.Run("matching", func(t *testing.T) { - // given - pred := ¬ReadyWithReason{expectedReason: "the right reason"} - spc := NewSpaceProvisionerConfig("spc", "default", WithReadyConditionInvalid("the right reason")) +func TestUnknownConsumedCapacityPredicate(t *testing.T) { + t.Run("matches", func(t *testing.T) { + pred := &unknownConsumedCapacity{} + spc := NewSpaceProvisionerConfig("spc", "default") - // when & then assert.True(t, pred.Matches(spc)) }) + t.Run("doesn't match with space count", func(t *testing.T) { + pred := &unknownConsumedCapacity{} + spc := NewSpaceProvisionerConfig("spc", "default", WithConsumedSpaceCount(5)) - t.Run("fixer with no conditions", func(t *testing.T) { - // given - pred := ¬ReadyWithReason{expectedReason: "the right reason"} - spc := NewSpaceProvisionerConfig("spc", "default") + assert.False(t, pred.Matches(spc)) + }) + t.Run("doesn't match with memory usage", func(t *testing.T) { + pred := &unknownConsumedCapacity{} + spc := NewSpaceProvisionerConfig("spc", "default", WithConsumedMemoryUsagePercentInNode("master", 5)) - // when - spc = pred.FixToMatch(spc) + assert.False(t, pred.Matches(spc)) + }) + t.Run("doesn't match with full consumed capacity", func(t *testing.T) { + pred := &unknownConsumedCapacity{} + spc := NewSpaceProvisionerConfig("spc", "default", WithConsumedSpaceCount(5), WithConsumedMemoryUsagePercentInNode("workter", 5)) - // then - assert.True(t, condition.IsFalseWithReason(spc.Status.Conditions, toolchainv1alpha1.ConditionReady, "the right reason")) + assert.False(t, pred.Matches(spc)) }) - t.Run("fixer with different conditions", func(t *testing.T) { + t.Run("fixer does nothing on matching SPC", func(t *testing.T) { // given - pred := ¬ReadyWithReason{expectedReason: "the right reason"} + pred := &unknownConsumedCapacity{} spc := NewSpaceProvisionerConfig("spc", "default") - spc.Status.Conditions = []toolchainv1alpha1.Condition{ - { - Type: toolchainv1alpha1.ConditionType("made up"), - Status: corev1.ConditionTrue, - }, - } // when - spc = pred.FixToMatch(spc) + fixed := pred.FixToMatch(spc.DeepCopy()) // then - assert.True(t, condition.IsFalseWithReason(spc.Status.Conditions, toolchainv1alpha1.ConditionReady, "the right reason")) - assert.Len(t, spc.Status.Conditions, 2) + assert.Equal(t, spc, fixed) }) - t.Run("fixer with wrong condition", func(t *testing.T) { + t.Run("fixes non-matching", func(t *testing.T) { // given - pred := ¬ReadyWithReason{expectedReason: "the right reason"} - spc := NewSpaceProvisionerConfig("spc", "default", WithReadyConditionInvalid("the wrong reason")) + pred := &unknownConsumedCapacity{} + spc := NewSpaceProvisionerConfig("spc", "default", WithConsumedSpaceCount(5)) // when - spc = pred.FixToMatch(spc) + fixed := pred.FixToMatch(spc.DeepCopy()) // then - assert.True(t, condition.IsFalseWithReason(spc.Status.Conditions, toolchainv1alpha1.ConditionReady, "the right reason")) + assert.NotEqual(t, spc, fixed) + assert.Nil(t, fixed.Status.ConsumedCapacity) }) }