• 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';
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', 'coverage'];
91        for (const key of paramKeys) {
92            if (params[key] !== undefined && params[key] !== 'true' && params[key] !== 'false') {
93                this.filterValid.push(`${key}:${params[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 nameRule = /^[A-Za-z]{1}[\w#,.]*$/;
105        let paramClassKeys = ['class', 'notClass'];
106        for (const key of paramClassKeys) {
107            if (params[key] !== undefined && params[key] !== '' && params[key] !== null) {
108                let classArray = params[key].split(',');
109                classArray.forEach(item => !item.match(nameRule) ? this.filterValid.push(`${key}:${params[key]}`) : null);
110            }
111        }
112    }
113
114    setConfig(params) {
115        this.basicParamValidCheck(params);
116        this.filterParamValidCheck(params);
117        try {
118            this.class = params.class;
119            this.notClass = params.notClass;
120            this.flag = params.flag || {flag: false};
121            this.suite = params.suite;
122            this.itName = params.itName;
123            this.filter = params.filter;
124            this.testType = params.testType;
125            this.level = params.level;
126            this.size = params.size;
127            this.timeout = params.timeout;
128            this.dryRun = params.dryRun;
129            this.breakOnError = params.breakOnError;
130            this.random = params.random === 'true' ? true : false;
131            this.stress = params.stress;
132            this.coverage = params.coverage;
133            this.filterParam = {
134                testType: {
135                    'function': 1,
136                    'performance': 1 << 1,
137                    'power': 1 << 2,
138                    'reliability': 1 << 3,
139                    'security': 1 << 4,
140                    'global': 1 << 5,
141                    'compatibility': 1 << 6,
142                    'user': 1 << 7,
143                    'standard': 1 << 8,
144                    'safety': 1 << 9,
145                    'resilience': 1 << 10,
146                },
147                level: {
148                    '0': 1 << 24,
149                    '1': 1 << 25,
150                    '2': 1 << 26,
151                    '3': 1 << 27,
152                    '4': 1 << 28,
153                },
154                size: {
155                    'small': 1 << 16,
156                    'medium': 1 << 17,
157                    'large': 1 << 18,
158                }
159            };
160            this.parseParams();
161        } catch (err) {
162            console.info('setConfig error: ' + err.message);
163        }
164    }
165
166    parseParams() {
167        if (this.filter != null) {
168            return;
169        }
170        let testTypeFilter = 0;
171        let sizeFilter = 0;
172        let levelFilter = 0;
173        if (this.testType != null) {
174            testTypeFilter = this.testType.split(',')
175                .map(item => this.filterParam.testType[item] || 0)
176                .reduce((pre, cur) => pre | cur, 0);
177        }
178        if (this.level != null) {
179            levelFilter = this.level.split(',')
180                .map(item => this.filterParam.level[item] || 0)
181                .reduce((pre, cur) => pre | cur, 0);
182        }
183        if (this.size != null) {
184            sizeFilter = this.size.split(',')
185                .map(item => this.filterParam.size[item] || 0)
186                .reduce((pre, cur) => pre | cur, 0);
187        }
188        this.filter = testTypeFilter | sizeFilter | levelFilter;
189        console.info('filter params:' + this.filter);
190    }
191
192    isCurrentSuite(description) {
193        if (this.suite !== undefined && this.suite !== '' && this.suite !== null) {
194            let suiteArray = this.suite.split(',');
195            return suiteArray.indexOf(description) !== -1;
196        }
197        return false;
198    }
199
200    filterSuite(currentSuiteName) {
201        let filterArray = [];
202        if (this.suite !== undefined && this.suite !== '' && this.suite !== null) {
203            filterArray.push(new SuiteAndItNameFilter(currentSuiteName, '', this.suite));
204        }
205        if (this.class !== undefined && this.class !== '' && this.class !== null) {
206            filterArray.push(new ClassFilter(currentSuiteName, '', this.class));
207        }
208        if (this.notClass !== undefined && this.notClass !== '' && this.notClass !== null) {
209            filterArray.push(new NotClassFilter(currentSuiteName, '', this.notClass));
210        }
211
212        let result = filterArray.map(item => item.filterSuite()).reduce((pre, cur) => pre || cur, false);
213        return result;
214    }
215
216    filterDesc(currentSuiteName, desc, fi, coreContext) {
217        let filterArray = [];
218        if (this.itName !== undefined && this.itName !== '' && this.itName !== null) {
219            filterArray.push(new SuiteAndItNameFilter(currentSuiteName, desc, this.itName));
220        }
221        if (this.class !== undefined && this.class !== '' && this.class !== null) {
222            filterArray.push(new ClassFilter(currentSuiteName, desc, this.class));
223        }
224        if (this.notClass !== undefined && this.notClass !== '' && this.notClass !== null) {
225            filterArray.push(new NotClassFilter(currentSuiteName, desc, this.notClass));
226        }
227        if (typeof (this.filter) !== 'undefined' && this.filter !== 0 && fi !== 0) {
228            filterArray.push(new TestTypesFilter('', '', fi, this.filter));
229        }
230        let result = filterArray.map(item => item.filterIt()).reduce((pre, cur) => pre || cur, false);
231        return result;
232    }
233
234    isRandom() {
235        return this.random || false;
236    }
237
238    isBreakOnError() {
239        return this.breakOnError !== 'true' ? false : true;
240    }
241
242    setSupportAsync(value) {
243        this.supportAsync = value;
244    }
245
246    isSupportAsync() {
247        return this.supportAsync;
248    }
249
250    translateParams(parameters) {
251        const keySet = new Set([
252            '-s class', '-s notClass', '-s suite', '-s itName',
253            '-s level', '-s testType', '-s size', '-s timeout',
254            '-s dryRun', '-s random', '-s breakOnError', '-s stress',
255            '-s coverage', 'class', 'notClass', 'suite', 'itName',
256            'level', 'testType', 'size', 'timeout', 'dryRun', 'random',
257            'breakOnError', 'stress', 'coverage'
258        ]);
259        let targetParams = {};
260        for (const key in parameters) {
261            if (keySet.has(key)) {
262                var newKey = key.replace("-s ", "");
263                targetParams[newKey] = parameters[key];
264            }
265        }
266        return targetParams;
267    }
268    translateParamsToString(parameters) {
269        const keySet = new Set([
270            '-s class', '-s notClass', '-s suite', '-s itName',
271            '-s level', '-s testType', '-s size', '-s timeout',
272            '-s dryRun', '-s random', '-s breakOnError', '-s stress',
273            '-s coverage','class', 'notClass', 'suite', 'itName',
274            'level', 'testType', 'size', 'timeout', 'dryRun', 'random',
275            'breakOnError', 'stress', 'coverage'
276        ]);
277        let targetParams = '';
278        for (const key in parameters) {
279            if (keySet.has(key)) {
280                targetParams += ' ' + key + ' ' + parameters[key];
281            }
282        }
283        return targetParams.trim();
284    }
285
286    execute() {
287    }
288}
289
290export {
291    ConfigService
292};
293