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