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 |
Vladimir Jigulin | ee1faa5 | 2018-06-25 13:00:51 +0400 | [diff] [blame] | 16 | import time |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 17 | |
| 18 | from tcp_tests.helpers import ext |
Victor Ryzhenkin | 8ff3c3f | 2018-01-17 19:37:05 +0400 | [diff] [blame] | 19 | from tcp_tests.helpers import utils |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 20 | from tcp_tests import logger |
| 21 | from tcp_tests.managers import k8smanager |
| 22 | |
| 23 | LOG = logger.logger |
| 24 | |
| 25 | |
| 26 | @pytest.fixture(scope='function') |
| 27 | def k8s_actions(config, underlay, salt_deployed): |
| 28 | """Fixture that provides various actions for K8S |
| 29 | |
| 30 | :param config: fixture provides oslo.config |
| 31 | :param underlay: fixture provides underlay manager |
| 32 | :param salt_deployed: fixture provides salt manager |
| 33 | :rtype: K8SManager |
| 34 | |
| 35 | For use in tests or fixtures to deploy a custom K8S |
| 36 | """ |
| 37 | return k8smanager.K8SManager(config, underlay, salt_deployed) |
| 38 | |
| 39 | |
| 40 | @pytest.mark.revert_snapshot(ext.SNAPSHOT.k8s_deployed) |
| 41 | @pytest.fixture(scope='function') |
| 42 | def k8s_deployed(revert_snapshot, request, config, hardware, underlay, |
Dennis Dmitriev | ea48cf5 | 2018-07-18 18:04:39 +0300 | [diff] [blame] | 43 | core_deployed, salt_deployed, k8s_actions): |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 44 | """Fixture to get or install k8s on environment |
| 45 | |
| 46 | :param revert_snapshot: fixture that reverts snapshot that is specified |
| 47 | in test with @pytest.mark.revert_snapshot(<name>) |
| 48 | :param request: fixture provides pytest data |
| 49 | :param config: fixture provides oslo.config |
| 50 | :param hardware: fixture provides enviromnet manager |
| 51 | :param underlay: fixture provides underlay manager |
Dennis Dmitriev | ea48cf5 | 2018-07-18 18:04:39 +0300 | [diff] [blame] | 52 | :param core_deployed: fixture provides CoreManager |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 53 | :param k8s_actions: fixture provides K8SManager instance |
| 54 | :rtype: K8SManager |
| 55 | |
| 56 | If config.k8s.k8s_installed is not set, this fixture assumes |
| 57 | that the k8s services were not installed, and do the following: |
| 58 | - install k8s services |
| 59 | - make snapshot with name 'k8s_deployed' |
| 60 | - return K8SManager instance |
| 61 | |
| 62 | If config.k8s.k8s_installed was set, this fixture assumes that |
| 63 | the k8s services were already installed, and do the following: |
| 64 | - return K8SManager instance |
| 65 | |
| 66 | If you want to revert 'k8s_deployed' snapshot, please use mark: |
| 67 | @pytest.mark.revert_snapshot("k8s_deployed") |
| 68 | """ |
| 69 | |
| 70 | # Deploy Kubernetes cluster |
| 71 | if not config.k8s.k8s_installed: |
| 72 | steps_path = config.k8s_deploy.k8s_steps_path |
| 73 | commands = underlay.read_template(steps_path) |
| 74 | k8s_actions.install(commands) |
| 75 | hardware.create_snapshot(ext.SNAPSHOT.k8s_deployed) |
Dennis Dmitriev | b8115f5 | 2017-12-15 13:09:56 +0200 | [diff] [blame] | 76 | salt_deployed.sync_time() |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 77 | |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 78 | # Workaround for keepalived hang issue after env revert from snapshot |
| 79 | # see https://mirantis.jira.com/browse/PROD-12038 |
| 80 | LOG.warning('Restarting keepalived service on controllers...') |
| 81 | k8s_actions._salt.local(tgt='ctl*', fun='cmd.run', |
| 82 | args='systemctl restart keepalived.service') |
Vladimir Jigulin | ee1faa5 | 2018-06-25 13:00:51 +0400 | [diff] [blame] | 83 | # give some time to keepalived to enter in MASTER state |
| 84 | time.sleep(5) |
Artem Panchenko | 501e67e | 2017-06-14 14:59:18 +0300 | [diff] [blame] | 85 | return k8s_actions |
Victor Ryzhenkin | 8ff3c3f | 2018-01-17 19:37:05 +0400 | [diff] [blame] | 86 | |
| 87 | |
Dennis Dmitriev | 63f9c95 | 2018-01-25 02:07:00 +0200 | [diff] [blame] | 88 | @pytest.fixture(scope='function') |
Victor Ryzhenkin | cf26c93 | 2018-03-29 20:08:21 +0400 | [diff] [blame] | 89 | def k8s_logs(request, func_name, underlay, k8s_deployed): |
| 90 | """Finalizer to extract conformance logs |
Victor Ryzhenkin | 8ff3c3f | 2018-01-17 19:37:05 +0400 | [diff] [blame] | 91 | |
Victor Ryzhenkin | cf26c93 | 2018-03-29 20:08:21 +0400 | [diff] [blame] | 92 | Usage: |
| 93 | @pytest.mark.grab_k8s_result(name=['file1', 'file2']) |
| 94 | ^^^^^ |
| 95 | This mark says tcp-qa to download files that counted in array as |
| 96 | parameter 'name'. Files should be located at ctl01. Files will be |
| 97 | downloaded to the host, where your test runs. |
| 98 | |
| 99 | @pytest.mark.extract(container_system='docker', extract_from='conformance', |
| 100 | files_to_extract=['report']) |
| 101 | ^^^^^ |
| 102 | This mark says tcp-qa to copy files from container. Docker or k8s system |
| 103 | supported. |
| 104 | container_system param says function what strategy should be |
| 105 | used. |
| 106 | extract_from param says what container should be used to copy. Note |
| 107 | that we are using grep to determine container ID, so if you have multiple |
| 108 | container with same substring to copy you may encounter unexpected issues. |
| 109 | files_to_extract param - this is array with paths of files/dirs to copy. |
| 110 | |
| 111 | @pytest.mark.merge_xunit(path='/root/report', |
| 112 | output='/root/conformance_result.xml') |
| 113 | ^^^^^ |
| 114 | This mark will help you to merge xunit results in case if you have |
| 115 | multiple reports because of multiple threads. |
| 116 | path param says where xml results stored |
| 117 | output param says where result will be saved |
| 118 | """ |
| 119 | |
| 120 | grab_k8s_result = request.keywords.get('grab_k8s_results', None) |
| 121 | extract = request.keywords.get('extract', None) |
| 122 | merge_xunit = request.keywords.get('merge_xunit', None) |
Victor Ryzhenkin | 8ff3c3f | 2018-01-17 19:37:05 +0400 | [diff] [blame] | 123 | |
| 124 | def test_fin(): |
| 125 | if hasattr(request.node, 'rep_call') and \ |
| 126 | (request.node.rep_call.passed or request.node.rep_call.failed)\ |
Victor Ryzhenkin | cf26c93 | 2018-03-29 20:08:21 +0400 | [diff] [blame] | 127 | and grab_k8s_result: |
| 128 | files = utils.extract_name_from_mark(grab_k8s_result) \ |
Victor Ryzhenkin | 87a3142 | 2018-03-16 22:25:27 +0400 | [diff] [blame] | 129 | or "{}".format(func_name) |
Victor Ryzhenkin | cf26c93 | 2018-03-29 20:08:21 +0400 | [diff] [blame] | 130 | if extract: |
| 131 | container_system = utils.extract_name_from_mark( |
| 132 | extract, 'container_system') |
| 133 | extract_from = utils.extract_name_from_mark( |
| 134 | extract, 'extract_from') |
| 135 | files_to_extract = utils.extract_name_from_mark( |
| 136 | extract, 'files_to_extract') |
| 137 | for path in files_to_extract: |
| 138 | k8s_deployed.extract_file_to_node( |
| 139 | system=container_system, container=extract_from, |
| 140 | file_path=path) |
| 141 | else: |
| 142 | k8s_deployed.extract_file_to_node() |
| 143 | if merge_xunit: |
| 144 | path = utils.extract_name_from_mark(merge_xunit, 'path') |
| 145 | output = utils.extract_name_from_mark(merge_xunit, 'output') |
| 146 | k8s_deployed.combine_xunit(path, output) |
Victor Ryzhenkin | 87a3142 | 2018-03-16 22:25:27 +0400 | [diff] [blame] | 147 | k8s_deployed.download_k8s_logs(files) |
| 148 | |
| 149 | request.addfinalizer(test_fin) |
| 150 | |
| 151 | |
| 152 | @pytest.fixture(scope='function') |
| 153 | def cncf_log_helper(request, func_name, underlay, k8s_deployed): |
| 154 | """Finalizer to prepare cncf tar.gz and save results from archive""" |
| 155 | |
| 156 | cncf_publisher = request.keywords.get('cncf_publisher', None) |
| 157 | |
| 158 | def test_fin(): |
| 159 | if hasattr(request.node, 'rep_call') and \ |
| 160 | (request.node.rep_call.passed or request.node.rep_call.failed)\ |
| 161 | and cncf_publisher: |
| 162 | files = utils.extract_name_from_mark(cncf_publisher) \ |
| 163 | or "{}".format(func_name) |
| 164 | k8s_deployed.extract_file_to_node( |
| 165 | system='k8s', file_path='tmp/sonobuoy', |
| 166 | pod_name='sonobuoy', pod_namespace='sonobuoy' |
| 167 | ) |
| 168 | k8s_deployed.manage_cncf_archive() |
| 169 | k8s_deployed.download_k8s_logs(files) |
| 170 | |
Victor Ryzhenkin | 8ff3c3f | 2018-01-17 19:37:05 +0400 | [diff] [blame] | 171 | request.addfinalizer(test_fin) |
Vladimir Jigulin | 62bcf46 | 2018-05-28 18:17:01 +0400 | [diff] [blame] | 172 | |
| 173 | |
| 174 | @pytest.fixture(scope='function') |
| 175 | def k8s_chain_update_log_helper(request, config, k8s_deployed): |
| 176 | def test_fin(): |
| 177 | if hasattr(request.node, 'rep_call') and \ |
| 178 | (request.node.rep_call.passed or request.node.rep_call.failed): |
| 179 | |
| 180 | chain_versions = config.k8s.k8s_update_chain.split(" ") |
| 181 | for version in chain_versions: |
| 182 | container_name = "k8s-conformance:{}".format(version) |
| 183 | tmp_report_dir = "/root/report_{}".format(version) |
Vladimir Jigulin | 96bdcb0 | 2018-06-08 08:28:26 +0400 | [diff] [blame] | 184 | report_path = "report_{}.xml".format(version) |
Vladimir Jigulin | 62bcf46 | 2018-05-28 18:17:01 +0400 | [diff] [blame] | 185 | conformance_log_path = "k8s_conformance_{}.log".format(version) |
| 186 | |
| 187 | k8s_deployed.extract_file_to_node( |
| 188 | system='docker', container=container_name, |
| 189 | out_dir=tmp_report_dir, file_path='report' |
| 190 | ) |
Vladimir Jigulin | 96bdcb0 | 2018-06-08 08:28:26 +0400 | [diff] [blame] | 191 | k8s_deployed.combine_xunit(tmp_report_dir, |
| 192 | '/root/{}'.format(report_path)) |
Vladimir Jigulin | 62bcf46 | 2018-05-28 18:17:01 +0400 | [diff] [blame] | 193 | |
| 194 | k8s_deployed.download_k8s_logs( |
| 195 | [report_path, conformance_log_path]) |
| 196 | |
| 197 | request.addfinalizer(test_fin) |