github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/general/flags_test.go (about) 1 /* 2 Copyright 2022 The Katalyst Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package general 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 v1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/api/resource" 25 ) 26 27 func TestResourceList_Set(t *testing.T) { 28 t.Parallel() 29 30 type args struct { 31 value string 32 } 33 tests := []struct { 34 name string 35 args args 36 wantResource ResourceList 37 wantErr bool 38 }{ 39 { 40 name: "string to cpu and memory", 41 args: args{ 42 value: "cpu=10,memory=10Gi", 43 }, 44 wantResource: ResourceList{ 45 v1.ResourceCPU: resource.MustParse("10"), 46 v1.ResourceMemory: resource.MustParse("10Gi"), 47 }, 48 wantErr: false, 49 }, 50 } 51 for _, tt := range tests { 52 r := ResourceList{} 53 tt := tt 54 t.Run(tt.name, func(t *testing.T) { 55 t.Parallel() 56 57 err := r.Set(tt.args.value) 58 assert.Equalf(t, tt.wantErr, err != nil, "Set(%v) return err", tt.args.value) 59 assert.Equal(t, tt.wantResource, r) 60 }) 61 } 62 } 63 64 func TestResourceList_String(t *testing.T) { 65 t.Parallel() 66 67 tests := []struct { 68 name string 69 r ResourceList 70 want string 71 }{ 72 { 73 name: "cpu and memory to string", 74 r: ResourceList{ 75 v1.ResourceCPU: resource.MustParse("10"), 76 v1.ResourceMemory: resource.MustParse("10Gi"), 77 }, 78 want: "cpu=10,memory=10Gi", 79 }, 80 { 81 name: "cpu and memory to string", 82 r: ResourceList{ 83 v1.ResourceCPU: resource.MustParse("10m"), 84 v1.ResourceMemory: resource.MustParse("15Gi"), 85 }, 86 want: "cpu=10m,memory=15Gi", 87 }, 88 } 89 for _, tt := range tests { 90 tt := tt 91 t.Run(tt.name, func(t *testing.T) { 92 t.Parallel() 93 assert.Equalf(t, tt.want, tt.r.String(), "String()") 94 }) 95 } 96 }