blob: dbffa588d5c20a75e5a282002c2f36b29d4e0d4a [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 = {}
Dennis Dmitriev8ea1f652017-09-25 18:12:52 +030025 parse_regex = re.compile(r'^(?P<title>(.+\n)+)'
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030026 r'\s*Scenario:\s*\n(?P<scenario>(.+\n)+)'
Dennis Dmitriev8ea1f652017-09-25 18:12:52 +030027 r'(\s*Duration:\s*(?P<duration>\d+).*\n)?')
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030028 doc_match = re.match(parse_regex, docstring)
29
30 if not doc_match:
31 LOG.error("Can't parse test docstring, unknown format!")
32 return test_case
33
34 test_case['title'] = re.sub(r'[\n\s]+', # replace multiple spaces and
35 ' ', # line breaks by single space
36 doc_match.group('title')
37 ).strip()
38
39 test_case['steps'] = []
40 for raw_step in re.split(r'\s+\d+\.\s*', doc_match.group('scenario')):
41 if not raw_step:
42 # start or end of the string
43 continue
44 test_case['steps'].append(
45 re.sub(r'[\n\s]+', # replace multiple spaces and
46 ' ', # line breaks by single space
47 raw_step
48 ).strip()
49 )
50
51 # TODO(apanchenko): now it works only with 'seconds'
52 duration = doc_match.group('duration') or 1000
53 test_case['duration'] = int(duration)
54 return test_case
55
56
57def log_step(func, step_num):
58 if not func.__doc__:
59 LOG.error("Can't show step #{0}: docstring for method {1} not "
60 "found!".format(step_num, func.__name__))
61 test_case_steps = parse_test_doc(func.__doc__)['steps']
62 try:
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +020063 LOG.info("\n\n*** [STEP#{0}] {1} ***".format(
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030064 step_num,
65 test_case_steps[step_num - 1]))
66 except IndexError:
67 LOG.error("Can't show step #{0}: docstring for method {1} does't "
68 "contain it!".format(step_num, func.__name__))