Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 1 | # 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 | |
| 15 | import re |
| 16 | |
| 17 | from tcp_tests import logger |
| 18 | |
| 19 | |
| 20 | LOG = logger.logger |
| 21 | |
| 22 | |
| 23 | def parse_test_doc(docstring): |
| 24 | test_case = {} |
Dennis Dmitriev | 8ea1f65 | 2017-09-25 18:12:52 +0300 | [diff] [blame] | 25 | parse_regex = re.compile(r'^(?P<title>(.+\n)+)' |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 26 | r'\s*Scenario:\s*\n(?P<scenario>(.+\n)+)' |
Dennis Dmitriev | 8ea1f65 | 2017-09-25 18:12:52 +0300 | [diff] [blame] | 27 | r'(\s*Duration:\s*(?P<duration>\d+).*\n)?') |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 28 | 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 | |
| 57 | def 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 Dmitriev | 2d643bc | 2017-12-04 12:23:47 +0200 | [diff] [blame] | 63 | LOG.info("\n\n*** [STEP#{0}] {1} ***".format( |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 64 | 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__)) |