volcano.sh/volcano@v1.9.0/pkg/scheduler/framework/arguments_test.go (about) 1 /* 2 Copyright 2019 The Kubernetes 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 framework 18 19 import ( 20 "reflect" 21 "testing" 22 23 "volcano.sh/volcano/pkg/scheduler/conf" 24 ) 25 26 type GetIntTestCases struct { 27 arg Arguments 28 key string 29 baseValue int 30 expectValue int 31 } 32 33 func TestArgumentsGetInt(t *testing.T) { 34 key1 := "intkey" 35 36 cases := []GetIntTestCases{ 37 { 38 arg: Arguments{ 39 key1: 15, 40 }, 41 key: key1, 42 baseValue: 10, 43 expectValue: 15, 44 }, 45 { 46 arg: Arguments{ 47 key1: "errorvalue", 48 }, 49 key: key1, 50 baseValue: 11, 51 expectValue: 11, 52 }, 53 { 54 arg: Arguments{ 55 key1: "", 56 }, 57 key: key1, 58 baseValue: 0, 59 expectValue: 0, 60 }, 61 } 62 63 for index, c := range cases { 64 baseValue := c.baseValue 65 c.arg.GetInt(nil, c.key) 66 c.arg.GetInt(&baseValue, c.key) 67 if baseValue != c.expectValue { 68 t.Errorf("index %d, value should be %v, but not %v", index, c.expectValue, baseValue) 69 } 70 } 71 } 72 73 func TestArgumentsGetFloat64(t *testing.T) { 74 key1 := "float64key" 75 76 cases := []struct { 77 name string 78 arg Arguments 79 key string 80 baseValue float64 81 expectValue float64 82 }{ 83 { 84 name: "key not exist", 85 arg: Arguments{ 86 "anotherKey": "12", 87 }, 88 key: key1, 89 baseValue: 1.2, 90 expectValue: 1.2, 91 }, 92 { 93 name: "key exist", 94 arg: Arguments{ 95 key1: 1.5, 96 }, 97 key: key1, 98 baseValue: 1.2, 99 expectValue: 1.5, 100 }, 101 { 102 name: "value of key invalid", 103 arg: Arguments{ 104 key1: "errorValue", 105 }, 106 key: key1, 107 baseValue: 1.2, 108 expectValue: 1.2, 109 }, 110 { 111 name: "value of key null", 112 arg: Arguments{ 113 key1: "", 114 }, 115 key: key1, 116 baseValue: 1.2, 117 expectValue: 1.2, 118 }, 119 { 120 name: "int value", 121 arg: Arguments{ 122 key1: 15, 123 }, 124 key: key1, 125 baseValue: 1.2, 126 expectValue: 15, 127 }, 128 } 129 130 for index, c := range cases { 131 baseValue := c.baseValue 132 c.arg.GetFloat64(&baseValue, c.key) 133 if baseValue != c.expectValue { 134 t.Errorf("index %d, case %s, value should be %v, but not %v", index, c.name, c.expectValue, baseValue) 135 } 136 } 137 } 138 139 func TestGetArgOfActionFromConf(t *testing.T) { 140 cases := []struct { 141 name string 142 configurations []conf.Configuration 143 action string 144 expectedArguments Arguments 145 }{ 146 { 147 name: "action exist in configurations", 148 configurations: []conf.Configuration{ 149 { 150 Name: "enqueue", 151 Arguments: map[string]interface{}{ 152 "overCommitFactor": "1.5", 153 }, 154 }, 155 { 156 Name: "allocate", 157 Arguments: map[string]interface{}{ 158 "placeholde": "placeholde", 159 }, 160 }, 161 }, 162 action: "enqueue", 163 expectedArguments: map[string]interface{}{ 164 "overCommitFactor": "1.5", 165 }, 166 }, 167 { 168 name: "action not exist in configurations", 169 configurations: []conf.Configuration{ 170 { 171 Name: "enqueue", 172 Arguments: map[string]interface{}{ 173 "overCommitFactor": "1.5", 174 }, 175 }, 176 }, 177 action: "allocate", 178 expectedArguments: nil, 179 }, 180 } 181 182 for index, c := range cases { 183 arg := GetArgOfActionFromConf(c.configurations, c.action) 184 if false == reflect.DeepEqual(arg, c.expectedArguments) { 185 t.Errorf("index %d, case %s,expected %v, but got %v", index, c.name, c.expectedArguments, arg) 186 } 187 } 188 }