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 | from __future__ import division |
| 16 | import time |
| 17 | |
| 18 | import pytest |
| 19 | |
| 20 | from tcp_tests import logger |
| 21 | from tcp_tests.helpers import log_step |
| 22 | from tcp_tests.helpers import utils |
| 23 | |
| 24 | |
| 25 | LOG = logger.logger |
| 26 | |
| 27 | |
| 28 | @pytest.yield_fixture(scope='session') |
| 29 | def ssh_keys_dir(request): |
| 30 | ssh_keys_dir = utils.generate_keys() |
| 31 | LOG.info("SSH keys were generated in {}".format(ssh_keys_dir)) |
| 32 | yield ssh_keys_dir |
| 33 | utils.clean_dir(ssh_keys_dir) |
| 34 | LOG.info("Tmp dir {} with generated ssh keys was cleaned".format( |
| 35 | ssh_keys_dir)) |
| 36 | |
| 37 | |
| 38 | @pytest.hookimpl(tryfirst=True, hookwrapper=True) |
| 39 | def pytest_runtest_makereport(item, call): |
| 40 | outcome = yield |
| 41 | rep = outcome.get_result() |
| 42 | setattr(item, "rep_" + rep.when, rep) |
| 43 | |
| 44 | |
| 45 | def pytest_runtest_setup(item): |
| 46 | if item.cls is not None: |
| 47 | item.cls._current_test = item.function |
| 48 | item._start_time = time.time() |
| 49 | head = "<" * 5 + "#" * 30 + "[ {} ]" + "#" * 30 + ">" * 5 |
| 50 | head = head.format(item.function.__name__) |
| 51 | start_step = "\n{head}".format(head=head) |
| 52 | LOG.info(start_step) |
| 53 | |
| 54 | |
| 55 | def pytest_runtest_teardown(item): |
| 56 | step_name = item.function.__name__ |
| 57 | if hasattr(item, '_start_time'): |
| 58 | spent_time = time.time() - item._start_time |
| 59 | else: |
| 60 | spent_time = 0 |
| 61 | minutes = spent_time // 60 |
| 62 | seconds = int(round(spent_time)) % 60 |
| 63 | finish_step = "FINISH {} TEST. TOOK {} min {} sec".format( |
| 64 | step_name, minutes, seconds |
| 65 | ) |
| 66 | foot = "\n" + "<" * 5 + "#" * 30 + "[ {} ]" + "#" * 30 + ">" * 5 |
| 67 | foot = foot.format(finish_step) |
| 68 | LOG.info(foot) |
| 69 | |
| 70 | |
| 71 | @pytest.fixture(scope='function') |
| 72 | def show_step(request): |
| 73 | def _show_step(step_number): |
| 74 | return log_step.log_step(request.function, step_number) |
| 75 | return _show_step |
Dennis Dmitriev | 474e3f7 | 2016-10-21 16:46:09 +0300 | [diff] [blame] | 76 | |
| 77 | @pytest.fixture(scope='function') |
| 78 | def steps(request): |
| 79 | steps_mark = request.keywords.get('steps', None) |
| 80 | steps = steps_mark.args[0] |
| 81 | return steps |