blob: 5abb84eeb700446a2a44a90b5b661280f9967828 [file] [log] [blame]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +03001# Copyright 2018 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
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030015import json
Oleksii Butenko71d76f32018-06-05 17:46:34 +030016import os
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030017
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +030018from devops.helpers import helpers
19
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030020from tcp_tests import logger
21from tcp_tests import settings
22
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +030023
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030024LOG = logger.logger
25
26TEMPEST_CFG_DIR = '/tmp/test'
27
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030028
29class RuntestManager(object):
30 """Helper manager for execution tempest via runtest-formula"""
31
32 image_name = settings.TEMPEST_IMAGE
33 image_version = settings.TEMPEST_IMAGE_VERSION
34 container_name = 'run-tempest-ci'
35 master_host = "cfg01"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000036 control_host = "ctl01"
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030037 class_name = "runtest"
38 run_cmd = '/bin/bash -c "run-tempest"'
39
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000040 def __init__(self, config, underlay, salt_api, cluster_name,
Oleksii Butenko74566102018-09-11 11:55:13 +030041 domain_name, tempest_threads,
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030042 tempest_pattern=settings.TEMPEST_PATTERN,
43 run_cmd=None, target='gtw01'):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000044 self.__config = config
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030045 self.underlay = underlay
46 self.__salt_api = salt_api
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030047 self.cluster_name = cluster_name
48 self.domain_name = domain_name
49 self.tempest_threads = tempest_threads
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030050 self.tempest_pattern = tempest_pattern
51 self.run_cmd = run_cmd or self.run_cmd
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000052 self.target_name = self.underlay.get_target_node_names(target)[0]
53 self.master_name = self.underlay.get_target_node_names(
54 self.master_host)[0]
55 self.control_name = self.underlay.get_target_node_names(
56 self.control_host)[0]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030057
58 @property
59 def salt_api(self):
60 return self.__salt_api
61
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000062 @property
63 def runtest_pillar(self):
64 public_net = self.__config.underlay.dhcp_ranges[
65 settings.EXTERNAL_ADDRESS_POOL_NAME]
66 public_gateway = public_net["gateway"].encode("ascii")
67 public_cidr = public_net["cidr"].encode("ascii")
68 public_allocation_start = public_net["start"].encode("ascii")
69 public_allocation_end = public_net["end"].encode("ascii")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030070
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000071 return {
72 'classes': ['service.runtest.tempest',
73 'service.runtest.tempest.public_net',
74 'service.runtest.tempest.services.manila.glance'],
75 'parameters': {
76 '_param': {
77 'runtest_tempest_cfg_dir': TEMPEST_CFG_DIR,
78 'runtest_tempest_cfg_name': 'tempest.conf',
79 'runtest_tempest_public_net': 'public',
80 'openstack_public_neutron_subnet_gateway': public_gateway,
81 'openstack_public_neutron_subnet_cidr': public_cidr,
82 'openstack_public_neutron_subnet_allocation_start':
83 public_allocation_start,
84 'openstack_public_neutron_subnet_allocation_end':
85 public_allocation_end,
86 'tempest_test_target': self.target_name.encode("ascii"),
87 },
88 'neutron': {
89 'client': {
90 'enabled': True
91 }
92 },
93 'runtest': {
94 'enabled': True,
95 'keystonerc_node': 'ctl01*',
96 'tempest': {
97 'enabled': True,
98 'cfg_dir': '${_param:runtest_tempest_cfg_dir}',
99 'cfg_name': '${_param:runtest_tempest_cfg_name}',
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300100 'put_keystone_rc_enabled': True,
101 'put_local_image_file_enabled': False,
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000102 'DEFAULT': {
103 'log_file': 'tempest.log'
104 },
105 'compute': {
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000106 'min_compute_nodes': 2,
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000107 },
108 'convert_to_uuid': {
109 'network': {
110 'public_network_id':
111 '${_param:runtest_tempest_public_net}'
112 }
113 },
Oleksii Butenko173e7702018-10-24 18:31:12 +0300114 'heat_plugin': {
115 'build_timeout': '600'
116 },
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000117 'share': {
118 'capability_snapshot_support': True,
119 'run_driver_assisted_migration_tests': False,
120 'run_manage_unmanage_snapshot_tests': False,
121 'run_manage_unmanage_tests': False,
122 'run_migration_with_preserve_snapshots_tests':
123 False,
124 'run_quota_tests': True,
125 'run_replication_tests': False,
126 'run_snapshot_tests': True,
127 }}}}}
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300128
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300129 def fetch_arficats(self, username=None, file_format='xml'):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000130 with self.underlay.remote(node_name=self.target_name,
131 username=None) as tgt:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300132 result = tgt.execute('find {} -name "report_*.{}"'.format(
133 TEMPEST_CFG_DIR, file_format))
134 LOG.debug("Find result {0}".format(result))
135 assert len(result['stdout']) > 0, ('No report found, please check'
136 ' if test run was successful.')
137 report = result['stdout'][0].rstrip()
138 LOG.debug("Found files {0}".format(report))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300139 tgt.download(
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300140 destination=report, # noqa
141 target=os.getcwd())
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300142
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000143 def store_runtest_model(self, runtest_pillar=None):
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300144 with self.underlay.yaml_editor(
145 file_path="/srv/salt/reclass/classes/cluster/"
146 "{cluster_name}/infra/"
147 "{class_name}.yml".format(
148 cluster_name=self.cluster_name,
149 class_name=self.class_name),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000150 node_name=self.master_name) as editor:
151 editor.content = runtest_pillar or self.runtest_pillar
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300152 with self.underlay.yaml_editor(
153 file_path="/srv/salt/reclass/nodes/_generated/"
154 "cfg01.{domain_name}.yml".format(
155 domain_name=self.domain_name),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000156 node_name=self.master_name) as editor:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300157 editor.content['classes'].append(
158 'cluster.{cluster_name}.infra.{class_name}'.format(
159 cluster_name=self.cluster_name,
160 class_name=self.class_name))
161
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300162 def save_runtime_logs(self, logs=None, inspect=None):
163 if logs:
164 with open("{path}/{target}_tempest_run.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000165 path=settings.LOGS_DIR,
166 target=self.target_name), 'w') as f:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300167 LOG.info("Save tempest console log")
168 container_log = logs
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300169 f.write(container_log.encode('ascii', 'ignore'))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300170
171 if inspect:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300172 with open("{path}/{target}_tempest_container_info.json.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000173 path=settings.LOGS_DIR,
174 target=self.target_name), 'w') as f:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300175 LOG.info("Save tempest container inspect data")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300176
177 container_inspect = json.dumps(inspect,
178 indent=4, sort_keys=True)
179 f.write(container_inspect)
180
Tatyana Leontovich61a821d2018-08-21 12:30:51 +0300181 def prepare(self, dpdk=None):
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300182 self.store_runtest_model()
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300183 cirros_pillar = ("salt-call --out=newline_values_only "
184 "pillar.get "
185 "glance:client:identity:"
186 "admin_identity:image:cirros:location")
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000187 salt_cmd = "salt -l info --hard-crash --state-output=mixed "
188 salt_call_cmd = "salt-call -l info --hard-crash --state-output=mixed "
189 commands = [
190 {
191 'description': "Sync salt objects for runtest model",
192 'node_name': self.master_name,
193 'cmd': ("set -ex;" +
194 salt_cmd + "'*' saltutil.refresh_pillar && " +
195 salt_cmd + "'*' saltutil.sync_all")},
196 {
197 'description': ("Install docker.io package and "
198 "enable packets forwarding"),
199 'node_name': self.target_name,
200 'cmd': ("set -ex;" +
201 salt_call_cmd + " pkg.install docker.io && " +
202 " iptables --policy FORWARD ACCEPT")},
203 {
204 'description': "Install PyPI docker package",
205 'node_name': self.target_name,
206 'cmd': ("set -ex;" +
207 salt_call_cmd + " pip.install setuptools && " +
208 salt_call_cmd + " pip.install docker")},
209 {
210 'description': "Run salt.minion state for runtest formula",
211 'node_name': self.master_name,
212 'cmd': ("set -ex;" +
213 salt_call_cmd + " state.sls salt.minion && "
214 " sleep 20")},
215 {
216 'description': "Enforce keystone state for neutronv2",
217 'node_name': self.master_name,
218 'cmd': ("set -ex;" +
219 salt_call_cmd + " state.sls keystone.client")},
220 {
221 'description': "Create networks for Tempest tests",
222 'node_name': self.master_name,
223 'cmd': ("set -ex;" +
224 salt_call_cmd + " state.sls neutron.client")},
225 {
226 'description': "Create flavors for Tempest tests",
227 'node_name': self.master_name,
228 'cmd': ("set -ex;" +
229 salt_call_cmd + " state.sls nova.client")},
230 {
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300231 'description': "Upload images for Tempest",
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000232 'node_name': self.master_name,
233 'cmd': ("set -ex;" +
234 salt_call_cmd + " state.sls glance.client")},
235 {
236 'description': "Generate config for Tempest",
237 'node_name': self.master_name,
238 'cmd': ("set -ex;" +
239 salt_call_cmd + " state.sls runtest")},
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300240 {
241 'description': "Upload cirros image",
242 'node_name': self.master_name,
243 'cmd': ("set -ex;"
244 "cirros_url=$({}) && {} '{}' cmd.run "
245 "\"wget $cirros_url -O /tmp/TestCirros-0.3.5.img\""
246 .format(cirros_pillar, salt_cmd, self.target_name))},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000247 ]
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300248
Tatyana Leontovich61a821d2018-08-21 12:30:51 +0300249 if dpdk:
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000250 commands.append({
251 'description': "Configure flavor for DPDK",
252 'node_name': self.control_name,
253 'cmd': ("set -ex;" +
254 salt_call_cmd + " cmd.run "
255 " '. /root/keystonercv3;"
sgudz905ec692018-10-16 15:30:39 +0300256 " openstack flavor set m1.extra_tiny_test"
257 " --property hw:mem_page_size=any;"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000258 " openstack flavor set m1.tiny_test"
sgudz905ec692018-10-16 15:30:39 +0300259 " --property hw:mem_page_size=any'")},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000260 )
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300261
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000262 self.__salt_api.execute_commands(commands=commands,
263 label="Prepare for Tempest")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300264
265 def run_tempest(self, timeout=600):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000266 tgt = self.target_name
267 image_nameversion = "{}:{}".format(self.image_name, self.image_version)
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300268
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000269 docker_args = (
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300270 " -t "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000271 " --name {container_name} "
272 " -e ARGS=\"-r {tempest_pattern} -w {tempest_threads}\""
273 " -v {cfg_dir}/tempest.conf:/etc/tempest/tempest.conf"
274 " -v /tmp/:/tmp/"
275 " -v {cfg_dir}:/root/tempest"
276 " -v /etc/ssl/certs/:/etc/ssl/certs/"
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300277 " -d "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000278 " {image_nameversion} {run_cmd}"
279 .format(
280 container_name=self.container_name,
281 image_nameversion=image_nameversion,
282 tempest_pattern=self.tempest_pattern,
283 tempest_threads=self.tempest_threads,
284 cfg_dir=TEMPEST_CFG_DIR,
285 run_cmd=self.run_cmd,
286 ))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300287
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000288 commands = [
289 {
290 'description': "Run Tempest tests {0}".format(
291 image_nameversion),
292 'node_name': self.target_name,
293 'cmd': ("set -ex;" +
294 " docker rm --force {container_name} || true;"
295 " docker run {docker_args}"
296 .format(container_name=self.container_name,
297 docker_args=docker_args)),
298 'timeout': timeout},
299 ]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300300
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000301 self.__salt_api.execute_commands(commands=commands,
302 label="Run Tempest tests")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300303
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300304 def wait_status(s):
305 inspect_res = self.salt_api.local(tgt,
306 'dockerng.inspect',
307 self.container_name)
308 if 'return' in inspect_res:
309 inspect = inspect_res['return']
310 inspect = inspect[0]
311 inspect = next(inspect.iteritems())[1]
312 status = inspect['State']['Status']
313
314 return status.lower() == s.lower()
315
316 return False
317
318 helpers.wait(lambda: wait_status('exited'),
319 timeout=timeout,
320 timeout_msg=('Tempest run didnt finished '
321 'in {}'.format(timeout)))
322
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300323 inspect_res = self.salt_api.local(tgt,
324 'dockerng.inspect',
325 self.container_name)
326 inspect = inspect_res['return'][0]
327 inspect = next(inspect.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300328 logs_res = self.salt_api.local(tgt,
329 'dockerng.logs',
330 self.container_name)
331 logs = logs_res['return'][0]
332 logs = next(logs.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300333
334 res = self.salt_api.local(tgt, 'dockerng.rm', self.container_name)
335 LOG.info("Tempest container was removed".format(
336 json.dumps(res, indent=4)))
337
338 return {'inspect': inspect,
339 'logs': logs}
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300340
Tatyana Leontovich61a821d2018-08-21 12:30:51 +0300341 def prepare_and_run_tempest(self, username='root', dpdk=None):
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300342 """
343 Run tempest tests
344 """
345 tempest_timeout = settings.TEMPEST_TIMEOUT
Tatyana Leontovich61a821d2018-08-21 12:30:51 +0300346 self.prepare(dpdk=dpdk)
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300347 test_res = self.run_tempest(tempest_timeout)
348 self.fetch_arficats(username=username)
349 self.save_runtime_logs(**test_res)