blob: fbfe644c8f0ef84f29553938a15b0f40e07d8fb7 [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
16
17from tcp_tests.helpers import ext
Victor Ryzhenkin8ff3c3f2018-01-17 19:37:05 +040018from tcp_tests.helpers import utils
Artem Panchenko0594cd72017-06-12 13:25:26 +030019from tcp_tests import logger
20from tcp_tests.managers import k8smanager
21
22LOG = logger.logger
23
24
25@pytest.fixture(scope='function')
26def 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')
41def k8s_deployed(revert_snapshot, request, config, hardware, underlay,
Dennis Dmitrievb8115f52017-12-15 13:09:56 +020042 common_services_deployed, salt_deployed, k8s_actions):
Artem Panchenko0594cd72017-06-12 13:25:26 +030043 """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 Dmitrievb8115f52017-12-15 13:09:56 +020075 salt_deployed.sync_time()
Artem Panchenko0594cd72017-06-12 13:25:26 +030076
Artem Panchenko501e67e2017-06-14 14:59:18 +030077 # 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 Ryzhenkin8ff3c3f2018-01-17 19:37:05 +040084
85
Dennis Dmitriev63f9c952018-01-25 02:07:00 +020086@pytest.fixture(scope='function')
Victor Ryzhenkincf26c932018-03-29 20:08:21 +040087def k8s_logs(request, func_name, underlay, k8s_deployed):
88 """Finalizer to extract conformance logs
Victor Ryzhenkin8ff3c3f2018-01-17 19:37:05 +040089
Victor Ryzhenkincf26c932018-03-29 20:08:21 +040090 Usage:
91 @pytest.mark.grab_k8s_result(name=['file1', 'file2'])
92 ^^^^^
93 This mark says tcp-qa to download files that counted in array as
94 parameter 'name'. Files should be located at ctl01. Files will be
95 downloaded to the host, where your test runs.
96
97 @pytest.mark.extract(container_system='docker', extract_from='conformance',
98 files_to_extract=['report'])
99 ^^^^^
100 This mark says tcp-qa to copy files from container. Docker or k8s system
101 supported.
102 container_system param says function what strategy should be
103 used.
104 extract_from param says what container should be used to copy. Note
105 that we are using grep to determine container ID, so if you have multiple
106 container with same substring to copy you may encounter unexpected issues.
107 files_to_extract param - this is array with paths of files/dirs to copy.
108
109 @pytest.mark.merge_xunit(path='/root/report',
110 output='/root/conformance_result.xml')
111 ^^^^^
112 This mark will help you to merge xunit results in case if you have
113 multiple reports because of multiple threads.
114 path param says where xml results stored
115 output param says where result will be saved
116 """
117
118 grab_k8s_result = request.keywords.get('grab_k8s_results', None)
119 extract = request.keywords.get('extract', None)
120 merge_xunit = request.keywords.get('merge_xunit', None)
Victor Ryzhenkin8ff3c3f2018-01-17 19:37:05 +0400121
122 def test_fin():
123 if hasattr(request.node, 'rep_call') and \
124 (request.node.rep_call.passed or request.node.rep_call.failed)\
Victor Ryzhenkincf26c932018-03-29 20:08:21 +0400125 and grab_k8s_result:
126 files = utils.extract_name_from_mark(grab_k8s_result) \
Victor Ryzhenkin87a31422018-03-16 22:25:27 +0400127 or "{}".format(func_name)
Victor Ryzhenkincf26c932018-03-29 20:08:21 +0400128 if extract:
129 container_system = utils.extract_name_from_mark(
130 extract, 'container_system')
131 extract_from = utils.extract_name_from_mark(
132 extract, 'extract_from')
133 files_to_extract = utils.extract_name_from_mark(
134 extract, 'files_to_extract')
135 for path in files_to_extract:
136 k8s_deployed.extract_file_to_node(
137 system=container_system, container=extract_from,
138 file_path=path)
139 else:
140 k8s_deployed.extract_file_to_node()
141 if merge_xunit:
142 path = utils.extract_name_from_mark(merge_xunit, 'path')
143 output = utils.extract_name_from_mark(merge_xunit, 'output')
144 k8s_deployed.combine_xunit(path, output)
Victor Ryzhenkin87a31422018-03-16 22:25:27 +0400145 k8s_deployed.download_k8s_logs(files)
146
147 request.addfinalizer(test_fin)
148
149
150@pytest.fixture(scope='function')
151def cncf_log_helper(request, func_name, underlay, k8s_deployed):
152 """Finalizer to prepare cncf tar.gz and save results from archive"""
153
154 cncf_publisher = request.keywords.get('cncf_publisher', None)
155
156 def test_fin():
157 if hasattr(request.node, 'rep_call') and \
158 (request.node.rep_call.passed or request.node.rep_call.failed)\
159 and cncf_publisher:
160 files = utils.extract_name_from_mark(cncf_publisher) \
161 or "{}".format(func_name)
162 k8s_deployed.extract_file_to_node(
163 system='k8s', file_path='tmp/sonobuoy',
164 pod_name='sonobuoy', pod_namespace='sonobuoy'
165 )
166 k8s_deployed.manage_cncf_archive()
167 k8s_deployed.download_k8s_logs(files)
168
Victor Ryzhenkin8ff3c3f2018-01-17 19:37:05 +0400169 request.addfinalizer(test_fin)