1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import {ClassFilter, NotClassFilter, SuiteAndItNameFilter, TestTypesFilter} from './Filter'; 17const STRESS_RULE = /^[1-9]\d*$/; 18 19class ConfigService { 20 constructor(attr) { 21 this.id = attr.id; 22 this.supportAsync = false; 23 this.random = false; 24 this.filterValid = []; 25 this.filter = 0; 26 this.flag = false; 27 this.suite = null; 28 this.itName = null; 29 this.testType = null; 30 this.level = null; 31 this.size = null; 32 this.class = null; 33 this.notClass = null; 34 this.timeout = null; 35 // 遇错即停模式配置 36 this.breakOnError = false; 37 // 压力测试配置 38 this.stress = null; 39 } 40 41 init(coreContext) { 42 this.coreContext = coreContext; 43 } 44 45 isNormalInteger(str) { 46 const n = Math.floor(Number(str)); 47 return n !== Infinity && String(n) === String(str) && n >= 0; 48 } 49 50 getStress() { 51 if(this.stress === undefined || this.stress === '' || this.stress === null) { 52 return 1; 53 } 54 return !this.stress.match(STRESS_RULE) ? 1 : Number.parseInt(this.stress); 55 } 56 57 basicParamValidCheck(params) { 58 let size = params.size; 59 if (size !== undefined && size !== '' && size !== null) { 60 let sizeArray = ['small', 'medium', 'large']; 61 if (sizeArray.indexOf(size) === -1) { 62 this.filterValid.push('size:' + size); 63 } 64 } 65 let level = params.level; 66 if (level !== undefined && level !== '' && level !== null) { 67 let levelArray = ['0', '1', '2', '3', '4']; 68 if (levelArray.indexOf(level) === -1) { 69 this.filterValid.push('level:' + level); 70 } 71 } 72 let testType = params.testType; 73 if (testType !== undefined && testType !== '' && testType !== null) { 74 let testTypeArray = ['function', 'performance', 'power', 'reliability', 'security', 75 'global', 'compatibility', 'user', 'standard', 'safety', 'resilience']; 76 if (testTypeArray.indexOf(testType) === -1) { 77 this.filterValid.push('testType:' + testType); 78 } 79 } 80 } 81 82 filterParamValidCheck(params) { 83 let timeout = params.timeout; 84 if (timeout !== undefined && timeout !== '' && timeout !== null) { 85 if (!this.isNormalInteger(timeout)) { 86 this.filterValid.push('timeout:' + timeout); 87 } 88 } 89 90 let paramKeys = ['dryRun', 'random', 'breakOnError']; 91 for (const key of paramKeys) { 92 if (paramKeys[key] !== undefined && paramKeys[key] !== 'true' && paramKeys[key] !== 'false') { 93 this.filterValid.push(`${key}:${paramKeys[key]}`); 94 } 95 } 96 97 // 压力测试参数验证,正整数 98 if(params.stress !== undefined && params.stress !== '' && params.stress !== null) { 99 if(!params.stress.match(STRESS_RULE)){ 100 this.filterValid.push('stress:' + params.stress); 101 } 102 } 103 104 let classes = params.class; 105 let nameRule = /^[A-Za-z]{1}[\w#,.]*$/; 106 if (classes !== undefined && classes !== '' && classes !== null) { 107 let classArray = classes.split(','); 108 for (let className of classArray) { 109 if (!className.match(nameRule)) { 110 this.filterValid.push('class:' + classes); 111 break; 112 } 113 } 114 } 115 let notClasses = params.notClass; 116 if (notClasses !== undefined && notClasses !== '' && notClasses !== null) { 117 let notClassArray = notClasses.split(','); 118 for (let notClassName of notClassArray) { 119 if (!notClassName.match(nameRule)) { 120 this.filterValid.push('notClass:' + notClasses); 121 break; 122 } 123 } 124 } 125 } 126 127 setConfig(params) { 128 this.basicParamValidCheck(params); 129 this.filterParamValidCheck(params); 130 try { 131 this.class = params.class; 132 this.notClass = params.notClass; 133 this.flag = params.flag || {flag: false}; 134 this.suite = params.suite; 135 this.itName = params.itName; 136 this.filter = params.filter; 137 this.testType = params.testType; 138 this.level = params.level; 139 this.size = params.size; 140 this.timeout = params.timeout; 141 this.dryRun = params.dryRun; 142 this.breakOnError = params.breakOnError 143 this.random = params.random === 'true' ? true : false; 144 this.stress = params.stress 145 this.filterParam = { 146 testType: { 147 'function': 1, 148 'performance': 1 << 1, 149 'power': 1 << 2, 150 'reliability': 1 << 3, 151 'security': 1 << 4, 152 'global': 1 << 5, 153 'compatibility': 1 << 6, 154 'user': 1 << 7, 155 'standard': 1 << 8, 156 'safety': 1 << 9, 157 'resilience': 1 << 10, 158 }, 159 level: { 160 '0': 1 << 24, 161 '1': 1 << 25, 162 '2': 1 << 26, 163 '3': 1 << 27, 164 '4': 1 << 28, 165 }, 166 size: { 167 'small': 1 << 16, 168 'medium': 1 << 17, 169 'large': 1 << 18, 170 } 171 }; 172 this.parseParams(); 173 } catch (err) { 174 console.info('setConfig error: ' + err.message); 175 } 176 } 177 178 parseParams() { 179 if (this.filter != null) { 180 return; 181 } 182 let testTypeFilter = 0; 183 let sizeFilter = 0; 184 let levelFilter = 0; 185 if (this.testType != null) { 186 testTypeFilter = this.testType.split(',') 187 .map(item => this.filterParam.testType[item] || 0) 188 .reduce((pre, cur) => pre | cur, 0); 189 } 190 if (this.level != null) { 191 levelFilter = this.level.split(',') 192 .map(item => this.filterParam.level[item] || 0) 193 .reduce((pre, cur) => pre | cur, 0); 194 } 195 if (this.size != null) { 196 sizeFilter = this.size.split(',') 197 .map(item => this.filterParam.size[item] || 0) 198 .reduce((pre, cur) => pre | cur, 0); 199 } 200 this.filter = testTypeFilter | sizeFilter | levelFilter; 201 console.info('filter params:' + this.filter); 202 } 203 204 isCurrentSuite(description) { 205 if (this.suite !== undefined && this.suite !== '' && this.suite !== null) { 206 let suiteArray = this.suite.split(','); 207 return suiteArray.indexOf(description) !== -1; 208 } 209 return false; 210 } 211 212 filterSuite(currentSuiteName) { 213 let filterArray = []; 214 if (this.suite !== undefined && this.suite !== '' && this.suite !== null) { 215 filterArray.push(new SuiteAndItNameFilter(currentSuiteName, '', this.suite)); 216 } 217 if (this.class !== undefined && this.class !== '' && this.class !== null) { 218 filterArray.push(new ClassFilter(currentSuiteName, '', this.class)); 219 } 220 if (this.notClass !== undefined && this.notClass !== '' && this.notClass !== null) { 221 filterArray.push(new NotClassFilter(currentSuiteName, '', this.notClass)); 222 } 223 224 let result = filterArray.map(item => item.filterSuite()).reduce((pre, cur) => pre || cur, false); 225 return result; 226 } 227 228 filterDesc(currentSuiteName, desc, fi, coreContext) { 229 let filterArray = []; 230 if (this.itName !== undefined && this.itName !== '' && this.itName !== null) { 231 filterArray.push(new SuiteAndItNameFilter(currentSuiteName, desc, this.itName)); 232 } 233 if (this.class !== undefined && this.class !== '' && this.class !== null) { 234 filterArray.push(new ClassFilter(currentSuiteName, desc, this.class)); 235 } 236 if (this.notClass !== undefined && this.notClass !== '' && this.notClass !== null) { 237 filterArray.push(new NotClassFilter(currentSuiteName, desc, this.notClass)); 238 } 239 if (typeof (this.filter) !== 'undefined' && this.filter !== 0 && fi !== 0) { 240 filterArray.push(new TestTypesFilter('', '', fi, this.filter)); 241 } 242 let result = filterArray.map(item => item.filterIt()).reduce((pre, cur) => pre || cur, false); 243 return result; 244 } 245 246 isRandom() { 247 return this.random || false; 248 } 249 250 isBreakOnError() { 251 return this.breakOnError !== 'true' ? false : true; 252 } 253 254 setSupportAsync(value) { 255 this.supportAsync = value; 256 } 257 258 isSupportAsync() { 259 return this.supportAsync; 260 } 261 262 translateParams(parameters) { 263 const keySet = new Set([ 264 '-s class', '-s notClass', '-s suite', '-s itName', 265 '-s level', '-s testType', '-s size', '-s timeout', 266 '-s dryRun', '-s random', '-s breakOnError', '-s stress', 'class', 'notClass', 'suite', 'itName', 267 'level', 'testType', 'size', 'timeout', 'dryRun', 'random', 'breakOnError', 'stress' 268 ]); 269 let targetParams = {}; 270 for (const key in parameters) { 271 if (keySet.has(key)) { 272 var newKey = key.replace("-s ", ""); 273 targetParams[newKey] = parameters[key]; 274 } 275 } 276 return targetParams; 277 } 278 translateParamsToString(parameters) { 279 const keySet = new Set([ 280 '-s class', '-s notClass', '-s suite', '-s itName', 281 '-s level', '-s testType', '-s size', '-s timeout', 282 '-s dryRun', '-s random', '-s breakOnError', '-s stress', 'class', 'notClass', 'suite', 'itName', 283 'level', 'testType', 'size', 'timeout', 'dryRun', 'random', 'breakOnError', 'stress' 284 ]); 285 let targetParams = ''; 286 for (const key in parameters) { 287 if (keySet.has(key)) { 288 targetParams += ' ' + key + ' ' + parameters[key]; 289 } 290 } 291 return targetParams.trim(); 292 } 293 294 execute() { 295 } 296} 297 298export { 299 ConfigService 300}; 301