Skip to content

Commit

Permalink
feat(spacetemplates): added unit tests for space/utils.go
Browse files Browse the repository at this point in the history
  • Loading branch information
abelhoula committed Oct 28, 2023
1 parent 0d87580 commit ff1bc3f
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions controllers/space/utlis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2022-2023 Edixos
// SPDX-License-Identifier: Apache-2.0
package space

import (
nauticusiov1alpha1 "github.com/edixos/nauticus/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"reflect"
"testing"
)

func TestMergeResourceQuotas(t *testing.T) {
testCases := []struct {
name string
space *nauticusiov1alpha1.Space
spaceTemplate *nauticusiov1alpha1.SpaceTemplate
expected *corev1.ResourceQuotaSpec
expectedErr error
}{
{
name: "Both space and spaceTemplate ResourceQuotas provided",
space: &nauticusiov1alpha1.Space{
Spec: nauticusiov1alpha1.SpaceSpec{
ResourceQuota: corev1.ResourceQuotaSpec{
Hard: corev1.ResourceList{
corev1.ResourceLimitsCPU: resource.MustParse("8"),
corev1.ResourceLimitsMemory: resource.MustParse("16Gi"),
corev1.ResourceRequestsCPU: resource.MustParse("4"),
corev1.ResourceRequestsMemory: resource.MustParse("8Gi"),
},
},
},
},
spaceTemplate: &nauticusiov1alpha1.SpaceTemplate{
Spec: nauticusiov1alpha1.SpaceTemplateSpec{
ResourceQuota: corev1.ResourceQuotaSpec{
Hard: corev1.ResourceList{
corev1.ResourceLimitsCPU: resource.MustParse("2"),
corev1.ResourceLimitsMemory: resource.MustParse("2Gi"),
corev1.ResourceRequestsCPU: resource.MustParse("1"),
corev1.ResourceRequestsMemory: resource.MustParse("1Gi"),
},
},
},
},
expected: &corev1.ResourceQuotaSpec{
Hard: corev1.ResourceList{
corev1.ResourceLimitsCPU: resource.MustParse("8"),
corev1.ResourceLimitsMemory: resource.MustParse("16Gi"),
corev1.ResourceRequestsCPU: resource.MustParse("4"),
corev1.ResourceRequestsMemory: resource.MustParse("8Gi"),
},
},
expectedErr: nil,
},
{
name: "Both space and spaceTemplate ResourceQuotas provided (CPU)",
space: &nauticusiov1alpha1.Space{
Spec: nauticusiov1alpha1.SpaceSpec{
ResourceQuota: corev1.ResourceQuotaSpec{
Hard: corev1.ResourceList{
corev1.ResourceLimitsCPU: resource.MustParse("8"),
corev1.ResourceRequestsCPU: resource.MustParse("4"),
corev1.ResourceLimitsMemory: resource.MustParse("1Gi"),
corev1.ResourceRequestsMemory: resource.MustParse("500Mi"),
},
},
},
},
spaceTemplate: &nauticusiov1alpha1.SpaceTemplate{
Spec: nauticusiov1alpha1.SpaceTemplateSpec{
ResourceQuota: corev1.ResourceQuotaSpec{
Hard: corev1.ResourceList{
corev1.ResourceLimitsCPU: resource.MustParse("2"),
corev1.ResourceRequestsCPU: resource.MustParse("1"),
corev1.ResourceLimitsMemory: resource.MustParse("1Gi"),
corev1.ResourceRequestsMemory: resource.MustParse("500Mi"),
},
},
},
},
expected: &corev1.ResourceQuotaSpec{
Hard: corev1.ResourceList{
corev1.ResourceLimitsCPU: resource.MustParse("8"),
corev1.ResourceRequestsCPU: resource.MustParse("4"),
corev1.ResourceLimitsMemory: resource.MustParse("1Gi"),
corev1.ResourceRequestsMemory: resource.MustParse("500Mi"),
},
},
expectedErr: nil,
},
{
name: "Only spaceTemplate ResourceQuotas (limits) provided",
space: &nauticusiov1alpha1.Space{},
spaceTemplate: &nauticusiov1alpha1.SpaceTemplate{
Spec: nauticusiov1alpha1.SpaceTemplateSpec{
ResourceQuota: corev1.ResourceQuotaSpec{
Hard: corev1.ResourceList{
corev1.ResourceLimitsCPU: resource.MustParse("3"),
corev1.ResourceLimitsMemory: resource.MustParse("3Gi"),
},
},
},
},
expected: &corev1.ResourceQuotaSpec{
Hard: corev1.ResourceList{
corev1.ResourceLimitsCPU: resource.MustParse("3"),
corev1.ResourceLimitsMemory: resource.MustParse("3Gi"),
},
},
expectedErr: nil,
},
/*{
name: "Both space and spaceTemplate ResourceQuotas are empty",
space: &nauticusiov1alpha1.Space{},
spaceTemplate: &nauticusiov1alpha1.SpaceTemplate{},
expected: &corev1.ResourceQuotaSpec{},
expectedErr: errors.New("both space and spacetpl resource quotas are empty. No merge required"),
},*/
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := MergeResourceQuotas(tc.space, tc.spaceTemplate)

if !reflect.DeepEqual(tc.expected, result) {
t.Errorf("Expected: %v, Got: %v", tc.expected, result)
}
if !reflect.DeepEqual(tc.expectedErr, err) {
t.Errorf("Expected error: %v, Got error: %v", tc.expectedErr, err)
}
})
}
}

0 comments on commit ff1bc3f

Please sign in to comment.