blob: e29f072c27b8caf97cf8e65b7d15bd9e1ca8d104 [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]
obutenko3e44b602018-11-13 12:50:22 +020060 self.barbican = False
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030061
62 @property
63 def salt_api(self):
64 return self.__salt_api
65
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000066 @property
67 def runtest_pillar(self):
68 public_net = self.__config.underlay.dhcp_ranges[
69 settings.EXTERNAL_ADDRESS_POOL_NAME]
70 public_gateway = public_net["gateway"].encode("ascii")
71 public_cidr = public_net["cidr"].encode("ascii")
72 public_allocation_start = public_net["start"].encode("ascii")
73 public_allocation_end = public_net["end"].encode("ascii")
Dennis Dmitrievcf52b8e2018-11-06 17:28:38 +020074 tempest_test_target = self.target_name.encode("ascii") + "*"
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030075
obutenko3e44b602018-11-13 12:50:22 +020076 pillar = {
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000077 'classes': ['service.runtest.tempest',
78 'service.runtest.tempest.public_net',
79 'service.runtest.tempest.services.manila.glance'],
80 'parameters': {
81 '_param': {
82 'runtest_tempest_cfg_dir': TEMPEST_CFG_DIR,
83 'runtest_tempest_cfg_name': 'tempest.conf',
84 'runtest_tempest_public_net': 'public',
85 'openstack_public_neutron_subnet_gateway': public_gateway,
86 'openstack_public_neutron_subnet_cidr': public_cidr,
87 'openstack_public_neutron_subnet_allocation_start':
88 public_allocation_start,
89 'openstack_public_neutron_subnet_allocation_end':
90 public_allocation_end,
Dennis Dmitrievcf52b8e2018-11-06 17:28:38 +020091 'tempest_test_target': tempest_test_target,
sgudz2f5aaf62018-10-30 13:20:04 +020092 'glance_image_cirros_location':
93 'http://cz8133.bud.mirantis.net:8099'
94 '/cirros-0.3.5-x86_64-disk.img',
95 'glance_image_fedora_location':
96 'http://cz8133.bud.mirantis.net:8099'
97 '/Fedora-Cloud-Base-27-1.6.x86_64.qcow2',
98 'glance_image_manila_location':
99 'http://cz8133.bud.mirantis.net:8099'
100 '/manila-service-image-master.qcow2',
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000101 },
102 'neutron': {
103 'client': {
104 'enabled': True
105 }
106 },
107 'runtest': {
108 'enabled': True,
109 'keystonerc_node': 'ctl01*',
110 'tempest': {
111 'enabled': True,
112 'cfg_dir': '${_param:runtest_tempest_cfg_dir}',
113 'cfg_name': '${_param:runtest_tempest_cfg_name}',
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300114 'put_keystone_rc_enabled': True,
115 'put_local_image_file_enabled': False,
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000116 'DEFAULT': {
117 'log_file': 'tempest.log'
118 },
119 'compute': {
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000120 'min_compute_nodes': 2,
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000121 },
122 'convert_to_uuid': {
123 'network': {
124 'public_network_id':
125 '${_param:runtest_tempest_public_net}'
126 }
127 },
Oleksii Butenko173e7702018-10-24 18:31:12 +0300128 'heat_plugin': {
129 'build_timeout': '600'
130 },
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000131 'share': {
132 'capability_snapshot_support': True,
133 'run_driver_assisted_migration_tests': False,
134 'run_manage_unmanage_snapshot_tests': False,
135 'run_manage_unmanage_tests': False,
136 'run_migration_with_preserve_snapshots_tests':
137 False,
138 'run_quota_tests': True,
139 'run_replication_tests': False,
140 'run_snapshot_tests': True,
141 }}}}}
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300142
obutenko3e44b602018-11-13 12:50:22 +0200143 if self.barbican:
144 pillar['classes'].append('service.runtest.tempest.barbican')
145
146 return pillar
147
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300148 def fetch_arficats(self, username=None, file_format='xml'):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000149 with self.underlay.remote(node_name=self.target_name,
150 username=None) as tgt:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300151 result = tgt.execute('find {} -name "report_*.{}"'.format(
152 TEMPEST_CFG_DIR, file_format))
153 LOG.debug("Find result {0}".format(result))
154 assert len(result['stdout']) > 0, ('No report found, please check'
155 ' if test run was successful.')
156 report = result['stdout'][0].rstrip()
157 LOG.debug("Found files {0}".format(report))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300158 tgt.download(
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300159 destination=report, # noqa
160 target=os.getcwd())
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300161
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000162 def store_runtest_model(self, runtest_pillar=None):
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300163 with self.underlay.yaml_editor(
164 file_path="/srv/salt/reclass/classes/cluster/"
165 "{cluster_name}/infra/"
166 "{class_name}.yml".format(
167 cluster_name=self.cluster_name,
168 class_name=self.class_name),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000169 node_name=self.master_name) as editor:
170 editor.content = runtest_pillar or self.runtest_pillar
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300171 with self.underlay.yaml_editor(
172 file_path="/srv/salt/reclass/nodes/_generated/"
173 "cfg01.{domain_name}.yml".format(
174 domain_name=self.domain_name),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000175 node_name=self.master_name) as editor:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300176 editor.content['classes'].append(
177 'cluster.{cluster_name}.infra.{class_name}'.format(
178 cluster_name=self.cluster_name,
179 class_name=self.class_name))
180
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300181 def save_runtime_logs(self, logs=None, inspect=None):
182 if logs:
183 with open("{path}/{target}_tempest_run.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000184 path=settings.LOGS_DIR,
185 target=self.target_name), 'w') as f:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300186 LOG.info("Save tempest console log")
187 container_log = logs
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300188 f.write(container_log.encode('ascii', 'ignore'))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300189
190 if inspect:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300191 with open("{path}/{target}_tempest_container_info.json.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000192 path=settings.LOGS_DIR,
193 target=self.target_name), 'w') as f:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300194 LOG.info("Save tempest container inspect data")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300195
196 container_inspect = json.dumps(inspect,
197 indent=4, sort_keys=True)
198 f.write(container_inspect)
199
Tatyana Leontovichdb185db2018-11-30 12:46:40 +0200200 def prepare(self, store_run_test_model):
obutenko3e44b602018-11-13 12:50:22 +0200201 barbican_pillar = "nova:controller:barbican:enabled"
202 result = self.__salt_api.get_pillar(tgt=self.control_name,
203 pillar=barbican_pillar)
204 self.barbican = result[0].get(self.control_name, False)
Tatyana Leontovichdb185db2018-11-30 12:46:40 +0200205 if store_run_test_model:
206 self.store_runtest_model()
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300207 cirros_pillar = ("salt-call --out=newline_values_only "
208 "pillar.get "
209 "glance:client:identity:"
210 "admin_identity:image:cirros:location")
sgudza5c901d2018-10-29 15:08:33 +0200211 dpdk_pillar = "linux:network:dpdk:enabled"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000212 salt_cmd = "salt -l info --hard-crash --state-output=mixed "
213 salt_call_cmd = "salt-call -l info --hard-crash --state-output=mixed "
sgudza5c901d2018-10-29 15:08:33 +0200214
215 result = self.__salt_api.get_pillar(tgt=self.compute_name,
216 pillar=dpdk_pillar)
217
218 dpdk = result[0].get(self.compute_name, False)
219 LOG.info("DPDK enabled: {}".format(bool(dpdk)))
220
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000221 commands = [
222 {
223 'description': "Sync salt objects for runtest model",
224 'node_name': self.master_name,
225 'cmd': ("set -ex;" +
226 salt_cmd + "'*' saltutil.refresh_pillar && " +
227 salt_cmd + "'*' saltutil.sync_all")},
228 {
Tatyana Leontovicheea03e92018-11-20 12:51:50 +0200229 'description': ("Install docker-ce package and "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000230 "enable packets forwarding"),
231 'node_name': self.target_name,
232 'cmd': ("set -ex;" +
Tatyana Leontovicheea03e92018-11-20 12:51:50 +0200233 salt_call_cmd + " pkg.install docker-ce && " +
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000234 " iptables --policy FORWARD ACCEPT")},
235 {
236 'description': "Install PyPI docker package",
237 'node_name': self.target_name,
238 'cmd': ("set -ex;" +
239 salt_call_cmd + " pip.install setuptools && " +
240 salt_call_cmd + " pip.install docker")},
241 {
242 'description': "Run salt.minion state for runtest formula",
243 'node_name': self.master_name,
244 'cmd': ("set -ex;" +
245 salt_call_cmd + " state.sls salt.minion && "
246 " sleep 20")},
247 {
248 'description': "Enforce keystone state for neutronv2",
249 'node_name': self.master_name,
250 'cmd': ("set -ex;" +
251 salt_call_cmd + " state.sls keystone.client")},
252 {
253 'description': "Create networks for Tempest tests",
254 'node_name': self.master_name,
255 'cmd': ("set -ex;" +
256 salt_call_cmd + " state.sls neutron.client")},
257 {
258 'description': "Create flavors for Tempest tests",
259 'node_name': self.master_name,
260 'cmd': ("set -ex;" +
261 salt_call_cmd + " state.sls nova.client")},
262 {
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300263 'description': "Upload images for Tempest",
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000264 'node_name': self.master_name,
265 'cmd': ("set -ex;" +
266 salt_call_cmd + " state.sls glance.client")},
267 {
268 'description': "Generate config for Tempest",
269 'node_name': self.master_name,
270 'cmd': ("set -ex;" +
271 salt_call_cmd + " state.sls runtest")},
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300272 {
273 'description': "Upload cirros image",
274 'node_name': self.master_name,
275 'cmd': ("set -ex;"
276 "cirros_url=$({}) && {} '{}' cmd.run "
277 "\"wget $cirros_url -O /tmp/TestCirros-0.3.5.img\""
278 .format(cirros_pillar, salt_cmd, self.target_name))},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000279 ]
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300280
Tatyana Leontovich61a821d2018-08-21 12:30:51 +0300281 if dpdk:
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000282 commands.append({
283 'description': "Configure flavor for DPDK",
284 'node_name': self.control_name,
285 'cmd': ("set -ex;" +
286 salt_call_cmd + " cmd.run "
287 " '. /root/keystonercv3;"
sgudz905ec692018-10-16 15:30:39 +0300288 " openstack flavor set m1.extra_tiny_test"
289 " --property hw:mem_page_size=any;"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000290 " openstack flavor set m1.tiny_test"
sgudz905ec692018-10-16 15:30:39 +0300291 " --property hw:mem_page_size=any'")},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000292 )
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300293
obutenko3e44b602018-11-13 12:50:22 +0200294 if self.barbican:
295 commands.append({
296 'description': "Configure barbican",
297 'node_name': self.master_name,
298 'cmd': ("set -ex;" +
299 salt_call_cmd +
300 " state.sls barbican.client && " +
301 salt_call_cmd +
302 " state.sls runtest.test_accounts && " +
303 salt_call_cmd +
304 " state.sls runtest.barbican_sign_image")},
305 )
306
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000307 self.__salt_api.execute_commands(commands=commands,
308 label="Prepare for Tempest")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300309
310 def run_tempest(self, timeout=600):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000311 tgt = self.target_name
312 image_nameversion = "{}:{}".format(self.image_name, self.image_version)
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300313
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000314 docker_args = (
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300315 " -t "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000316 " --name {container_name} "
317 " -e ARGS=\"-r {tempest_pattern} -w {tempest_threads}\""
318 " -v {cfg_dir}/tempest.conf:/etc/tempest/tempest.conf"
319 " -v /tmp/:/tmp/"
320 " -v {cfg_dir}:/root/tempest"
321 " -v /etc/ssl/certs/:/etc/ssl/certs/"
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300322 " -d "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000323 " {image_nameversion} {run_cmd}"
324 .format(
325 container_name=self.container_name,
326 image_nameversion=image_nameversion,
327 tempest_pattern=self.tempest_pattern,
328 tempest_threads=self.tempest_threads,
329 cfg_dir=TEMPEST_CFG_DIR,
330 run_cmd=self.run_cmd,
331 ))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300332
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000333 commands = [
334 {
335 'description': "Run Tempest tests {0}".format(
336 image_nameversion),
337 'node_name': self.target_name,
338 'cmd': ("set -ex;" +
339 " docker rm --force {container_name} || true;"
340 " docker run {docker_args}"
341 .format(container_name=self.container_name,
342 docker_args=docker_args)),
343 'timeout': timeout},
344 ]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300345
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000346 self.__salt_api.execute_commands(commands=commands,
347 label="Run Tempest tests")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300348
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300349 def wait_status(s):
350 inspect_res = self.salt_api.local(tgt,
351 'dockerng.inspect',
352 self.container_name)
353 if 'return' in inspect_res:
354 inspect = inspect_res['return']
355 inspect = inspect[0]
356 inspect = next(inspect.iteritems())[1]
357 status = inspect['State']['Status']
358
359 return status.lower() == s.lower()
360
361 return False
362
363 helpers.wait(lambda: wait_status('exited'),
364 timeout=timeout,
365 timeout_msg=('Tempest run didnt finished '
366 'in {}'.format(timeout)))
367
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300368 inspect_res = self.salt_api.local(tgt,
369 'dockerng.inspect',
370 self.container_name)
371 inspect = inspect_res['return'][0]
372 inspect = next(inspect.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300373 logs_res = self.salt_api.local(tgt,
374 'dockerng.logs',
375 self.container_name)
376 logs = logs_res['return'][0]
377 logs = next(logs.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300378
379 res = self.salt_api.local(tgt, 'dockerng.rm', self.container_name)
380 LOG.info("Tempest container was removed".format(
381 json.dumps(res, indent=4)))
382
383 return {'inspect': inspect,
384 'logs': logs}
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300385
Tatyana Leontovichdb185db2018-11-30 12:46:40 +0200386 def prepare_and_run_tempest(self, username='root',
387 store_run_test_model=True):
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300388 """
389 Run tempest tests
390 """
391 tempest_timeout = settings.TEMPEST_TIMEOUT
Tatyana Leontovichdb185db2018-11-30 12:46:40 +0200392 self.prepare(store_run_test_model=store_run_test_model)
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300393 test_res = self.run_tempest(tempest_timeout)
394 self.fetch_arficats(username=username)
395 self.save_runtime_logs(**test_res)