Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 1 | # Copyright 2017 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 pytest |
| 16 | |
| 17 | from tcp_tests.helpers import ext |
Victor Ryzhenkin | 8ff3c3f | 2018-01-17 19:37:05 +0400 | [diff] [blame] | 18 | from tcp_tests.helpers import utils |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 19 | from tcp_tests import logger |
| 20 | from tcp_tests.managers import k8smanager |
| 21 | |
| 22 | LOG = logger.logger |
| 23 | |
| 24 | |
| 25 | @pytest.fixture(scope='function') |
| 26 | def k8s_actions(config, underlay, salt_deployed): |
| 27 | """Fixture that provides various actions for K8S |
| 28 | |
| 29 | :param config: fixture provides oslo.config |
| 30 | :param underlay: fixture provides underlay manager |
| 31 | :param salt_deployed: fixture provides salt manager |
| 32 | :rtype: K8SManager |
| 33 | |
| 34 | For use in tests or fixtures to deploy a custom K8S |
| 35 | """ |
| 36 | return k8smanager.K8SManager(config, underlay, salt_deployed) |
| 37 | |
| 38 | |
| 39 | @pytest.mark.revert_snapshot(ext.SNAPSHOT.k8s_deployed) |
| 40 | @pytest.fixture(scope='function') |
| 41 | def k8s_deployed(revert_snapshot, request, config, hardware, underlay, |
Dennis Dmitriev | b8115f5 | 2017-12-15 13:09:56 +0200 | [diff] [blame] | 42 | common_services_deployed, salt_deployed, k8s_actions): |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 43 | """Fixture to get or install k8s on environment |
| 44 | |
| 45 | :param revert_snapshot: fixture that reverts snapshot that is specified |
| 46 | in test with @pytest.mark.revert_snapshot(<name>) |
| 47 | :param request: fixture provides pytest data |
| 48 | :param config: fixture provides oslo.config |
| 49 | :param hardware: fixture provides enviromnet manager |
| 50 | :param underlay: fixture provides underlay manager |
| 51 | :param common_services_deployed: fixture provides CommonServicesManager |
| 52 | :param k8s_actions: fixture provides K8SManager instance |
| 53 | :rtype: K8SManager |
| 54 | |
| 55 | If config.k8s.k8s_installed is not set, this fixture assumes |
| 56 | that the k8s services were not installed, and do the following: |
| 57 | - install k8s services |
| 58 | - make snapshot with name 'k8s_deployed' |
| 59 | - return K8SManager instance |
| 60 | |
| 61 | If config.k8s.k8s_installed was set, this fixture assumes that |
| 62 | the k8s services were already installed, and do the following: |
| 63 | - return K8SManager instance |
| 64 | |
| 65 | If you want to revert 'k8s_deployed' snapshot, please use mark: |
| 66 | @pytest.mark.revert_snapshot("k8s_deployed") |
| 67 | """ |
| 68 | |
| 69 | # Deploy Kubernetes cluster |
| 70 | if not config.k8s.k8s_installed: |
| 71 | steps_path = config.k8s_deploy.k8s_steps_path |
| 72 | commands = underlay.read_template(steps_path) |
| 73 | k8s_actions.install(commands) |
| 74 | hardware.create_snapshot(ext.SNAPSHOT.k8s_deployed) |
Dennis Dmitriev | b8115f5 | 2017-12-15 13:09:56 +0200 | [diff] [blame] | 75 | salt_deployed.sync_time() |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 76 | |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 77 | # Workaround for keepalived hang issue after env revert from snapshot |
| 78 | # see https://mirantis.jira.com/browse/PROD-12038 |
| 79 | LOG.warning('Restarting keepalived service on controllers...') |
| 80 | k8s_actions._salt.local(tgt='ctl*', fun='cmd.run', |
| 81 | args='systemctl restart keepalived.service') |
| 82 | |
| 83 | return k8s_actions |
Victor Ryzhenkin | 8ff3c3f | 2018-01-17 19:37:05 +0400 | [diff] [blame] | 84 | |
| 85 | |
| 86 | @pytest.fixture(scope='function', autouse=True) |
| 87 | def grab_virtlet_results(request, func_name, underlay, k8s_deployed): |
| 88 | """Finalizer to extract virtlet conformance logs""" |
| 89 | |
| 90 | grab_virtlet_result = request.keywords.get('grab_virtlet_results', None) |
| 91 | |
| 92 | def test_fin(): |
| 93 | if hasattr(request.node, 'rep_call') and \ |
| 94 | (request.node.rep_call.passed or request.node.rep_call.failed)\ |
| 95 | and grab_virtlet_result: |
| 96 | artifact_name = utils.extract_name_from_mark(grab_virtlet_result) \ |
| 97 | or "{}".format(func_name) |
| 98 | k8s_deployed.download_virtlet_conformance_log(artifact_name) |
| 99 | request.addfinalizer(test_fin) |