github.com/gocrane/crane@v0.11.0/pkg/recommendation/recommender/resource/resource_spec_test.go (about) 1 package resource 2 3 import ( 4 "reflect" 5 "testing" 6 7 "k8s.io/apimachinery/pkg/api/resource" 8 ) 9 10 func TestGetResourceSpecifications(t *testing.T) { 11 tests := []struct { 12 name string 13 input string 14 want []Specification 15 wantErr bool 16 }{ 17 { 18 name: "specs", 19 input: "2c4g,4c8g,2c5g,2c1g,0.25c0.25g,0.5c1g,4c16g", 20 want: []Specification{ 21 { 22 CPU: 0.25, 23 Memory: 0.25, 24 }, 25 { 26 CPU: 0.5, 27 Memory: 1, 28 }, 29 { 30 CPU: 2, 31 Memory: 1, 32 }, 33 { 34 CPU: 2, 35 Memory: 4, 36 }, 37 { 38 CPU: 2, 39 Memory: 5, 40 }, 41 { 42 CPU: 4, 43 Memory: 8, 44 }, 45 { 46 CPU: 4, 47 Memory: 16, 48 }, 49 }, 50 }, 51 { 52 name: "specs format error1", 53 input: "24c", 54 wantErr: true, 55 }, 56 { 57 name: "specs format error2", 58 input: "2c4g5c", 59 wantErr: true, 60 }, 61 } 62 for _, tt := range tests { 63 t.Run(tt.name, func(t *testing.T) { 64 got, err := GetResourceSpecifications(tt.input) 65 if tt.wantErr && err == nil { 66 t.Errorf("GetResourceSpecifications not return error ") 67 } 68 if !reflect.DeepEqual(tt.want, got) { 69 t.Errorf("GetResourceSpecifications %v, want %v", got, tt.want) 70 } 71 }) 72 } 73 } 74 75 func TestGetNormalizedResource(t *testing.T) { 76 tests := []struct { 77 name string 78 inputCpu string 79 inputMem string 80 wantCpu string 81 wantMem string 82 }{ 83 { 84 name: "specs1", 85 inputCpu: "100m", 86 inputMem: "125Mi", 87 wantCpu: "0.25", 88 wantMem: "256Mi", 89 }, 90 { 91 name: "specs2", 92 inputCpu: "100m", 93 inputMem: "325Mi", 94 wantCpu: "0.25", 95 wantMem: "512Mi", 96 }, 97 { 98 name: "specs3", 99 inputCpu: "500m", 100 inputMem: "1625Mi", 101 wantCpu: "1", 102 wantMem: "2Gi", 103 }, 104 } 105 106 specifications, _ := GetResourceSpecifications(DefaultSpecs) 107 108 for _, tt := range tests { 109 t.Run(tt.name, func(t *testing.T) { 110 cpu := resource.MustParse(tt.inputCpu) 111 mem := resource.MustParse(tt.inputMem) 112 normalizedCpu, normalizedMem := GetNormalizedResource(&cpu, &mem, specifications) 113 wantCpuQ := resource.MustParse(tt.wantCpu) 114 wantMemQ := resource.MustParse(tt.wantMem) 115 116 if normalizedCpu.Value() != wantCpuQ.Value() { 117 t.Errorf("got cpu %s, want %s", normalizedCpu.String(), wantCpuQ.String()) 118 } 119 120 if normalizedMem.Value() != wantMemQ.Value() { 121 t.Errorf("got memory %s, want %s", normalizedMem.String(), wantMemQ.String()) 122 } 123 }) 124 } 125 }