blob: 8e70e58ec4f8e78670f5edbafaea14df37b38834 [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"
sgudza5c901d2018-10-29 15:08:33 +020037 compute_host = "cmp"
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030038 class_name = "runtest"
39 run_cmd = '/bin/bash -c "run-tempest"'
40
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000041 def __init__(self, config, underlay, salt_api, cluster_name,
Oleksii Butenko74566102018-09-11 11:55:13 +030042 domain_name, tempest_threads,
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030043 tempest_pattern=settings.TEMPEST_PATTERN,
44 run_cmd=None, target='gtw01'):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000045 self.__config = config
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030046 self.underlay = underlay
47 self.__salt_api = salt_api
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030048 self.cluster_name = cluster_name
49 self.domain_name = domain_name
50 self.tempest_threads = tempest_threads
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030051 self.tempest_pattern = tempest_pattern
52 self.run_cmd = run_cmd or self.run_cmd
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000053 self.target_name = self.underlay.get_target_node_names(target)[0]
54 self.master_name = self.underlay.get_target_node_names(
55 self.master_host)[0]
56 self.control_name = self.underlay.get_target_node_names(
57 self.control_host)[0]
sgudza5c901d2018-10-29 15:08:33 +020058 self.compute_name = self.underlay.get_target_node_names(
59 self.compute_host)[0]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030060
61 @property
62 def salt_api(self):
63 return self.__salt_api
64
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000065 @property
66 def runtest_pillar(self):
67 public_net = self.__config.underlay.dhcp_ranges[
68 settings.EXTERNAL_ADDRESS_POOL_NAME]
69 public_gateway = public_net["gateway"].encode("ascii")
70 public_cidr = public_net["cidr"].encode("ascii")
71 public_allocation_start = public_net["start"].encode("ascii")
72 public_allocation_end = public_net["end"].encode("ascii")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030073
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000074 return {
75 'classes': ['service.runtest.tempest',
76 'service.runtest.tempest.public_net',
77 'service.runtest.tempest.services.manila.glance'],
78 'parameters': {
79 '_param': {
80 'runtest_tempest_cfg_dir': TEMPEST_CFG_DIR,
81 'runtest_tempest_cfg_name': 'tempest.conf',
82 'runtest_tempest_public_net': 'public',
83 'openstack_public_neutron_subnet_gateway': public_gateway,
84 'openstack_public_neutron_subnet_cidr': public_cidr,
85 'openstack_public_neutron_subnet_allocation_start':
86 public_allocation_start,
87 'openstack_public_neutron_subnet_allocation_end':
88 public_allocation_end,
89 'tempest_test_target': self.target_name.encode("ascii"),
sgudz2f5aaf62018-10-30 13:20:04 +020090 'glance_image_cirros_location':
91 'http://cz8133.bud.mirantis.net:8099'
92 '/cirros-0.3.5-x86_64-disk.img',
93 'glance_image_fedora_location':
94 'http://cz8133.bud.mirantis.net:8099'
95 '/Fedora-Cloud-Base-27-1.6.x86_64.qcow2',
96 'glance_image_manila_location':
97 'http://cz8133.bud.mirantis.net:8099'
98 '/manila-service-image-master.qcow2',
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000099 },
100 'neutron': {
101 'client': {
102 'enabled': True
103 }
104 },
105 'runtest': {
106 'enabled': True,
107 'keystonerc_node': 'ctl01*',
108 'tempest': {
109 'enabled': True,
110 'cfg_dir': '${_param:runtest_tempest_cfg_dir}',
111 'cfg_name': '${_param:runtest_tempest_cfg_name}',
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300112 'put_keystone_rc_enabled': True,
113 'put_local_image_file_enabled': False,
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000114 'DEFAULT': {
115 'log_file': 'tempest.log'
116 },
117 'compute': {
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000118 'min_compute_nodes': 2,
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000119 },
120 'convert_to_uuid': {
121 'network': {
122 'public_network_id':
123 '${_param:runtest_tempest_public_net}'
124 }
125 },
Oleksii Butenko173e7702018-10-24 18:31:12 +0300126 'heat_plugin': {
127 'build_timeout': '600'
128 },
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000129 'share': {
130 'capability_snapshot_support': True,
131 'run_driver_assisted_migration_tests': False,
132 'run_manage_unmanage_snapshot_tests': False,
133 'run_manage_unmanage_tests': False,
134 'run_migration_with_preserve_snapshots_tests':
135 False,
136 'run_quota_tests': True,
137 'run_replication_tests': False,
138 'run_snapshot_tests': True,
139 }}}}}
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300140
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300141 def fetch_arficats(self, username=None, file_format='xml'):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000142 with self.underlay.remote(node_name=self.target_name,
143 username=None) as tgt:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300144 result = tgt.execute('find {} -name "report_*.{}"'.format(
145 TEMPEST_CFG_DIR, file_format))
146 LOG.debug("Find result {0}".format(result))
147 assert len(result['stdout']) > 0, ('No report found, please check'
148 ' if test run was successful.')
149 report = result['stdout'][0].rstrip()
150 LOG.debug("Found files {0}".format(report))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300151 tgt.download(
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300152 destination=report, # noqa
153 target=os.getcwd())
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300154
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000155 def store_runtest_model(self, runtest_pillar=None):
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300156 with self.underlay.yaml_editor(
157 file_path="/srv/salt/reclass/classes/cluster/"
158 "{cluster_name}/infra/"
159 "{class_name}.yml".format(
160 cluster_name=self.cluster_name,
161 class_name=self.class_name),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000162 node_name=self.master_name) as editor:
163 editor.content = runtest_pillar or self.runtest_pillar
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300164 with self.underlay.yaml_editor(
165 file_path="/srv/salt/reclass/nodes/_generated/"
166 "cfg01.{domain_name}.yml".format(
167 domain_name=self.domain_name),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000168 node_name=self.master_name) as editor:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300169 editor.content['classes'].append(
170 'cluster.{cluster_name}.infra.{class_name}'.format(
171 cluster_name=self.cluster_name,
172 class_name=self.class_name))
173
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300174 def save_runtime_logs(self, logs=None, inspect=None):
175 if logs:
176 with open("{path}/{target}_tempest_run.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000177 path=settings.LOGS_DIR,
178 target=self.target_name), 'w') as f:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300179 LOG.info("Save tempest console log")
180 container_log = logs
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300181 f.write(container_log.encode('ascii', 'ignore'))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300182
183 if inspect:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300184 with open("{path}/{target}_tempest_container_info.json.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000185 path=settings.LOGS_DIR,
186 target=self.target_name), 'w') as f:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300187 LOG.info("Save tempest container inspect data")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300188
189 container_inspect = json.dumps(inspect,
190 indent=4, sort_keys=True)
191 f.write(container_inspect)
192
sgudza5c901d2018-10-29 15:08:33 +0200193 def prepare(self):
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300194 self.store_runtest_model()
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300195 cirros_pillar = ("salt-call --out=newline_values_only "
196 "pillar.get "
197 "glance:client:identity:"
198 "admin_identity:image:cirros:location")
sgudza5c901d2018-10-29 15:08:33 +0200199 dpdk_pillar = "linux:network:dpdk:enabled"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000200 salt_cmd = "salt -l info --hard-crash --state-output=mixed "
201 salt_call_cmd = "salt-call -l info --hard-crash --state-output=mixed "
sgudza5c901d2018-10-29 15:08:33 +0200202
203 result = self.__salt_api.get_pillar(tgt=self.compute_name,
204 pillar=dpdk_pillar)
205
206 dpdk = result[0].get(self.compute_name, False)
207 LOG.info("DPDK enabled: {}".format(bool(dpdk)))
208
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000209 commands = [
210 {
211 'description': "Sync salt objects for runtest model",
212 'node_name': self.master_name,
213 'cmd': ("set -ex;" +
214 salt_cmd + "'*' saltutil.refresh_pillar && " +
215 salt_cmd + "'*' saltutil.sync_all")},
216 {
217 'description': ("Install docker.io package and "
218 "enable packets forwarding"),
219 'node_name': self.target_name,
220 'cmd': ("set -ex;" +
221 salt_call_cmd + " pkg.install docker.io && " +
222 " iptables --policy FORWARD ACCEPT")},
223 {
224 'description': "Install PyPI docker package",
225 'node_name': self.target_name,
226 'cmd': ("set -ex;" +
227 salt_call_cmd + " pip.install setuptools && " +
228 salt_call_cmd + " pip.install docker")},
229 {
230 'description': "Run salt.minion state for runtest formula",
231 'node_name': self.master_name,
232 'cmd': ("set -ex;" +
233 salt_call_cmd + " state.sls salt.minion && "
234 " sleep 20")},
235 {
236 'description': "Enforce keystone state for neutronv2",
237 'node_name': self.master_name,
238 'cmd': ("set -ex;" +
239 salt_call_cmd + " state.sls keystone.client")},
240 {
241 'description': "Create networks for Tempest tests",
242 'node_name': self.master_name,
243 'cmd': ("set -ex;" +
244 salt_call_cmd + " state.sls neutron.client")},
245 {
246 'description': "Create flavors for Tempest tests",
247 'node_name': self.master_name,
248 'cmd': ("set -ex;" +
249 salt_call_cmd + " state.sls nova.client")},
250 {
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300251 'description': "Upload images for Tempest",
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000252 'node_name': self.master_name,
253 'cmd': ("set -ex;" +
254 salt_call_cmd + " state.sls glance.client")},
255 {
256 'description': "Generate config for Tempest",
257 'node_name': self.master_name,
258 'cmd': ("set -ex;" +
259 salt_call_cmd + " state.sls runtest")},
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300260 {
261 'description': "Upload cirros image",
262 'node_name': self.master_name,
263 'cmd': ("set -ex;"
264 "cirros_url=$({}) && {} '{}' cmd.run "
265 "\"wget $cirros_url -O /tmp/TestCirros-0.3.5.img\""
266 .format(cirros_pillar, salt_cmd, self.target_name))},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000267 ]
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300268
Tatyana Leontovich61a821d2018-08-21 12:30:51 +0300269 if dpdk:
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000270 commands.append({
271 'description': "Configure flavor for DPDK",
272 'node_name': self.control_name,
273 'cmd': ("set -ex;" +
274 salt_call_cmd + " cmd.run "
275 " '. /root/keystonercv3;"
sgudz905ec692018-10-16 15:30:39 +0300276 " openstack flavor set m1.extra_tiny_test"
277 " --property hw:mem_page_size=any;"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000278 " openstack flavor set m1.tiny_test"
sgudz905ec692018-10-16 15:30:39 +0300279 " --property hw:mem_page_size=any'")},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000280 )
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300281
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000282 self.__salt_api.execute_commands(commands=commands,
283 label="Prepare for Tempest")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300284
285 def run_tempest(self, timeout=600):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000286 tgt = self.target_name
287 image_nameversion = "{}:{}".format(self.image_name, self.image_version)
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300288
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000289 docker_args = (
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300290 " -t "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000291 " --name {container_name} "
292 " -e ARGS=\"-r {tempest_pattern} -w {tempest_threads}\""
293 " -v {cfg_dir}/tempest.conf:/etc/tempest/tempest.conf"
294 " -v /tmp/:/tmp/"
295 " -v {cfg_dir}:/root/tempest"
296 " -v /etc/ssl/certs/:/etc/ssl/certs/"
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300297 " -d "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000298 " {image_nameversion} {run_cmd}"
299 .format(
300 container_name=self.container_name,
301 image_nameversion=image_nameversion,
302 tempest_pattern=self.tempest_pattern,
303 tempest_threads=self.tempest_threads,
304 cfg_dir=TEMPEST_CFG_DIR,
305 run_cmd=self.run_cmd,
306 ))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300307
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000308 commands = [
309 {
310 'description': "Run Tempest tests {0}".format(
311 image_nameversion),
312 'node_name': self.target_name,
313 'cmd': ("set -ex;" +
314 " docker rm --force {container_name} || true;"
315 " docker run {docker_args}"
316 .format(container_name=self.container_name,
317 docker_args=docker_args)),
318 'timeout': timeout},
319 ]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300320
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000321 self.__salt_api.execute_commands(commands=commands,
322 label="Run Tempest tests")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300323
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300324 def wait_status(s):
325 inspect_res = self.salt_api.local(tgt,
326 'dockerng.inspect',
327 self.container_name)
328 if 'return' in inspect_res:
329 inspect = inspect_res['return']
330 inspect = inspect[0]
331 inspect = next(inspect.iteritems())[1]
332 status = inspect['State']['Status']
333
334 return status.lower() == s.lower()
335
336 return False
337
338 helpers.wait(lambda: wait_status('exited'),
339 timeout=timeout,
340 timeout_msg=('Tempest run didnt finished '
341 'in {}'.format(timeout)))
342
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300343 inspect_res = self.salt_api.local(tgt,
344 'dockerng.inspect',
345 self.container_name)
346 inspect = inspect_res['return'][0]
347 inspect = next(inspect.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300348 logs_res = self.salt_api.local(tgt,
349 'dockerng.logs',
350 self.container_name)
351 logs = logs_res['return'][0]
352 logs = next(logs.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300353
354 res = self.salt_api.local(tgt, 'dockerng.rm', self.container_name)
355 LOG.info("Tempest container was removed".format(
356 json.dumps(res, indent=4)))
357
358 return {'inspect': inspect,
359 'logs': logs}
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300360
sgudza5c901d2018-10-29 15:08:33 +0200361 def prepare_and_run_tempest(self, username='root'):
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300362 """
363 Run tempest tests
364 """
365 tempest_timeout = settings.TEMPEST_TIMEOUT
sgudza5c901d2018-10-29 15:08:33 +0200366 self.prepare()
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300367 test_res = self.run_tempest(tempest_timeout)
368 self.fetch_arficats(username=username)
369 self.save_runtime_logs(**test_res)