blob: 64ec7aa1f4a889757a28a94716b61960d41a1b73 [file] [log] [blame]
Dennis Dmitriev6f59add2016-10-18 13:45:27 +03001# Copyright 2016 Mirantis, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# 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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import re
16
17from tcp_tests import logger
18
19
20LOG = logger.logger
21
22
23def parse_test_doc(docstring):
24 test_case = {}
25 parse_regex = re.compile(r'(?P<title>^(.*\S.*\n)+)+'
26 r'(?P<empty_line1>\s*\n)'
27 r'\s*Scenario:\s*\n(?P<scenario>(.+\n)+)'
28 r'(?P<empty_line2>\s*(\n|$))?'
29 r'(\s*Duration:\s+(?P<duration>\d+).*\n)?')
30 doc_match = re.match(parse_regex, docstring)
31
32 if not doc_match:
33 LOG.error("Can't parse test docstring, unknown format!")
34 return test_case
35
36 test_case['title'] = re.sub(r'[\n\s]+', # replace multiple spaces and
37 ' ', # line breaks by single space
38 doc_match.group('title')
39 ).strip()
40
41 test_case['steps'] = []
42 for raw_step in re.split(r'\s+\d+\.\s*', doc_match.group('scenario')):
43 if not raw_step:
44 # start or end of the string
45 continue
46 test_case['steps'].append(
47 re.sub(r'[\n\s]+', # replace multiple spaces and
48 ' ', # line breaks by single space
49 raw_step
50 ).strip()
51 )
52
53 # TODO(apanchenko): now it works only with 'seconds'
54 duration = doc_match.group('duration') or 1000
55 test_case['duration'] = int(duration)
56 return test_case
57
58
59def log_step(func, step_num):
60 if not func.__doc__:
61 LOG.error("Can't show step #{0}: docstring for method {1} not "
62 "found!".format(step_num, func.__name__))
63 test_case_steps = parse_test_doc(func.__doc__)['steps']
64 try:
65 LOG.info(" *** [STEP#{0}] {1} ***".format(
66 step_num,
67 test_case_steps[step_num - 1]))
68 except IndexError:
69 LOG.error("Can't show step #{0}: docstring for method {1} does't "
70 "contain it!".format(step_num, func.__name__))