Skip to content

Commit

Permalink
Utility methods for SPC's consumed capacity tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
metlos committed Nov 19, 2024
1 parent 0964795 commit 266b4a1
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 134 deletions.
27 changes: 25 additions & 2 deletions pkg/test/spaceprovisionerconfig/spaceprovisionerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
}
149 changes: 100 additions & 49 deletions pkg/test/spaceprovisionerconfig/spaceprovisionerconfig_assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 &notReady{}
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 &notReadyWithReason{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{}
}
Loading

0 comments on commit 266b4a1

Please sign in to comment.