blob: f59ff6196660747df42d3ca1942f7b6872622e77 [file] [log] [blame]
Artem Panchenko0594cd72017-06-12 13:25:26 +03001# 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
15import pytest
Vladimir Jigulinee1faa52018-06-25 13:00:51 +040016import time
Artem Panchenko0594cd72017-06-12 13:25:26 +030017
18from tcp_tests.helpers import ext
Victor Ryzhenkin8ff3c3f2018-01-17 19:37:05 +040019from tcp_tests.helpers import utils
Artem Panchenko0594cd72017-06-12 13:25:26 +030020from tcp_tests import logger
21from tcp_tests.managers import k8smanager
22
23LOG = logger.logger
24
25
26@pytest.fixture(scope='function')
27def 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')
42def k8s_deployed(revert_snapshot, request, config, hardware, underlay,
Dennis Dmitrievb8115f52017-12-15 13:09:56 +020043 common_services_deployed, salt_deployed, k8s_actions):
Artem Panchenko0594cd72017-06-12 13:25:26 +030044 """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
52 :param common_services_deployed: fixture provides CommonServicesManager
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 Dmitrievb8115f52017-12-15 13:09:56 +020076 salt_deployed.sync_time()
Artem Panchenko0594cd72017-06-12 13:25:26 +030077
Artem Panchenko501e67e2017-06-14 14:59:18 +030078 # 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 Jigulinee1faa52018-06-25 13:00:51 +040083 # give some time to keepalived to enter in MASTER state
84 time.sleep(5)
Artem Panchenko501e67e2017-06-14 14:59:18 +030085 return k8s_actions
Victor Ryzhenkin8ff3c3f2018-01-17 19:37:05 +040086
87
Dennis Dmitriev63f9c952018-01-25 02:07:00 +020088@pytest.fixture(scope='function')
Victor Ryzhenkincf26c932018-03-29 20:08:21 +040089def k8s_logs(request, func_name, underlay, k8s_deployed):
90 """Finalizer to extract conformance logs
Victor Ryzhenkin8ff3c3f2018-01-17 19:37:05 +040091
Victor Ryzhenkincf26c932018-03-29 20:08:21 +040092 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 Ryzhenkin8ff3c3f2018-01-17 19:37:05 +0400123
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 Ryzhenkincf26c932018-03-29 20:08:21 +0400127 and grab_k8s_result:
128 files = utils.extract_name_from_mark(grab_k8s_result) \
Victor Ryzhenkin87a31422018-03-16 22:25:27 +0400129 or "{}".format(func_name)
Victor Ryzhenkincf26c932018-03-29 20:08:21 +0400130 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 Ryzhenkin87a31422018-03-16 22:25:27 +0400147 k8s_deployed.download_k8s_logs(files)
148
149 request.addfinalizer(test_fin)
150
151
152@pytest.fixture(scope='function')
153def 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 Ryzhenkin8ff3c3f2018-01-17 19:37:05 +0400171 request.addfinalizer(test_fin)
Vladimir Jigulin62bcf462018-05-28 18:17:01 +0400172
173
174@pytest.fixture(scope='function')
175def 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 Jigulin96bdcb02018-06-08 08:28:26 +0400184 report_path = "report_{}.xml".format(version)
Vladimir Jigulin62bcf462018-05-28 18:17:01 +0400185 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 Jigulin96bdcb02018-06-08 08:28:26 +0400191 k8s_deployed.combine_xunit(tmp_report_dir,
192 '/root/{}'.format(report_path))
Vladimir Jigulin62bcf462018-05-28 18:17:01 +0400193
194 k8s_deployed.download_k8s_logs(
195 [report_path, conformance_log_path])
196
197 request.addfinalizer(test_fin)