github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/class/types_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package class 21 22 import ( 23 "sort" 24 "testing" 25 26 "github.com/ghodss/yaml" 27 "github.com/stretchr/testify/assert" 28 corev1 "k8s.io/api/core/v1" 29 "k8s.io/apimachinery/pkg/api/resource" 30 31 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 32 ) 33 34 const resourceConstraintBytes = ` 35 # API scope: cluster 36 apiVersion: "apps.kubeblocks.io/v1alpha1" 37 kind: "ComponentResourceConstraint" 38 metadata: 39 name: kb-resource-constraint-general 40 spec: 41 rules: 42 - cpu: 43 min: 0.5 44 max: 128 45 step: 0.5 46 memory: 47 sizePerCPU: 4Gi 48 - cpu: 49 slots: [0.1, 0.2, 0.4, 0.6, 0.8, 1] 50 memory: 51 minPerCPU: 200Mi 52 - cpu: 53 min: 0.1 54 max: 64 55 step: 0.1 56 memory: 57 minPerCPU: 4Gi 58 maxPerCPU: 8Gi 59 ` 60 61 var buildClass = func(cpu string, memory string) *ComponentClassWithRef { 62 return &ComponentClassWithRef{ 63 ComponentClass: appsv1alpha1.ComponentClass{ 64 CPU: resource.MustParse(cpu), 65 Memory: resource.MustParse(memory), 66 }, 67 } 68 } 69 70 var buildResourceList = func(cpu string, memory string) corev1.ResourceList { 71 return corev1.ResourceList{ 72 corev1.ResourceCPU: resource.MustParse(cpu), 73 corev1.ResourceMemory: resource.MustParse(memory), 74 } 75 } 76 77 func TestComponentClass(t *testing.T) { 78 classes := []*ComponentClassWithRef{ 79 buildClass("1", "2Gi"), 80 buildClass("1", "1Gi"), 81 buildClass("2", "0.5Gi"), 82 buildClass("1", "1Gi"), 83 buildClass("0.5", "10Gi"), 84 } 85 sort.Sort(ByClassResource(classes)) 86 candidate := classes[0] 87 if !candidate.CPU.Equal(resource.MustParse("0.5")) || !candidate.Memory.Equal(resource.MustParse("10Gi")) { 88 t.Errorf("case failed") 89 } 90 } 91 92 func TestComponentResourceConstraint(t *testing.T) { 93 var cf appsv1alpha1.ComponentResourceConstraint 94 err := yaml.Unmarshal([]byte(resourceConstraintBytes), &cf) 95 if err != nil { 96 panic("Failed to unmarshal resource constraint: %v" + err.Error()) 97 } 98 var rules []appsv1alpha1.ResourceConstraintRule 99 rules = append(rules, cf.Spec.Rules...) 100 sort.Sort(ByRuleList(rules)) 101 resources := rules[0].GetMinimalResources() 102 assert.Equal(t, resources.Cpu().Cmp(resource.MustParse("0.1")) == 0, true) 103 assert.Equal(t, resources.Memory().Cmp(resource.MustParse("20Mi")) == 0, true) 104 } 105 106 func TestResourceList(t *testing.T) { 107 rl := []corev1.ResourceList{ 108 buildResourceList("1", "2Gi"), 109 buildResourceList("1", "1Gi"), 110 buildResourceList("2", "0.5Gi"), 111 buildResourceList("1", "1Gi"), 112 buildResourceList("0.5", "10Gi"), 113 } 114 sort.Sort(ByResourceList(rl)) 115 116 candidate := rl[0] 117 if !candidate.Cpu().Equal(resource.MustParse("0.5")) || !candidate.Memory().Equal(resource.MustParse("10Gi")) { 118 t.Errorf("case failed") 119 } 120 }