blob: 50f934e7f85a4cbd6d6f2aeede4fde91d2fb18e5 [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"),
90 },
91 'neutron': {
92 'client': {
93 'enabled': True
94 }
95 },
96 'runtest': {
97 'enabled': True,
98 'keystonerc_node': 'ctl01*',
99 'tempest': {
100 'enabled': True,
101 'cfg_dir': '${_param:runtest_tempest_cfg_dir}',
102 'cfg_name': '${_param:runtest_tempest_cfg_name}',
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300103 'put_keystone_rc_enabled': True,
104 'put_local_image_file_enabled': False,
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000105 'DEFAULT': {
106 'log_file': 'tempest.log'
107 },
108 'compute': {
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000109 'min_compute_nodes': 2,
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000110 },
111 'convert_to_uuid': {
112 'network': {
113 'public_network_id':
114 '${_param:runtest_tempest_public_net}'
115 }
116 },
Oleksii Butenko173e7702018-10-24 18:31:12 +0300117 'heat_plugin': {
118 'build_timeout': '600'
119 },
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000120 'share': {
121 'capability_snapshot_support': True,
122 'run_driver_assisted_migration_tests': False,
123 'run_manage_unmanage_snapshot_tests': False,
124 'run_manage_unmanage_tests': False,
125 'run_migration_with_preserve_snapshots_tests':
126 False,
127 'run_quota_tests': True,
128 'run_replication_tests': False,
129 'run_snapshot_tests': True,
130 }}}}}
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300131
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300132 def fetch_arficats(self, username=None, file_format='xml'):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000133 with self.underlay.remote(node_name=self.target_name,
134 username=None) as tgt:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300135 result = tgt.execute('find {} -name "report_*.{}"'.format(
136 TEMPEST_CFG_DIR, file_format))
137 LOG.debug("Find result {0}".format(result))
138 assert len(result['stdout']) > 0, ('No report found, please check'
139 ' if test run was successful.')
140 report = result['stdout'][0].rstrip()
141 LOG.debug("Found files {0}".format(report))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300142 tgt.download(
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300143 destination=report, # noqa
144 target=os.getcwd())
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300145
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000146 def store_runtest_model(self, runtest_pillar=None):
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300147 with self.underlay.yaml_editor(
148 file_path="/srv/salt/reclass/classes/cluster/"
149 "{cluster_name}/infra/"
150 "{class_name}.yml".format(
151 cluster_name=self.cluster_name,
152 class_name=self.class_name),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000153 node_name=self.master_name) as editor:
154 editor.content = runtest_pillar or self.runtest_pillar
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300155 with self.underlay.yaml_editor(
156 file_path="/srv/salt/reclass/nodes/_generated/"
157 "cfg01.{domain_name}.yml".format(
158 domain_name=self.domain_name),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000159 node_name=self.master_name) as editor:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300160 editor.content['classes'].append(
161 'cluster.{cluster_name}.infra.{class_name}'.format(
162 cluster_name=self.cluster_name,
163 class_name=self.class_name))
164
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300165 def save_runtime_logs(self, logs=None, inspect=None):
166 if logs:
167 with open("{path}/{target}_tempest_run.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000168 path=settings.LOGS_DIR,
169 target=self.target_name), 'w') as f:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300170 LOG.info("Save tempest console log")
171 container_log = logs
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300172 f.write(container_log.encode('ascii', 'ignore'))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300173
174 if inspect:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300175 with open("{path}/{target}_tempest_container_info.json.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000176 path=settings.LOGS_DIR,
177 target=self.target_name), 'w') as f:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300178 LOG.info("Save tempest container inspect data")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300179
180 container_inspect = json.dumps(inspect,
181 indent=4, sort_keys=True)
182 f.write(container_inspect)
183
sgudza5c901d2018-10-29 15:08:33 +0200184 def prepare(self):
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300185 self.store_runtest_model()
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300186 cirros_pillar = ("salt-call --out=newline_values_only "
187 "pillar.get "
188 "glance:client:identity:"
189 "admin_identity:image:cirros:location")
sgudza5c901d2018-10-29 15:08:33 +0200190 dpdk_pillar = "linux:network:dpdk:enabled"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000191 salt_cmd = "salt -l info --hard-crash --state-output=mixed "
192 salt_call_cmd = "salt-call -l info --hard-crash --state-output=mixed "
sgudza5c901d2018-10-29 15:08:33 +0200193
194 result = self.__salt_api.get_pillar(tgt=self.compute_name,
195 pillar=dpdk_pillar)
196
197 dpdk = result[0].get(self.compute_name, False)
198 LOG.info("DPDK enabled: {}".format(bool(dpdk)))
199
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000200 commands = [
201 {
202 'description': "Sync salt objects for runtest model",
203 'node_name': self.master_name,
204 'cmd': ("set -ex;" +
205 salt_cmd + "'*' saltutil.refresh_pillar && " +
206 salt_cmd + "'*' saltutil.sync_all")},
207 {
208 'description': ("Install docker.io package and "
209 "enable packets forwarding"),
210 'node_name': self.target_name,
211 'cmd': ("set -ex;" +
212 salt_call_cmd + " pkg.install docker.io && " +
213 " iptables --policy FORWARD ACCEPT")},
214 {
215 'description': "Install PyPI docker package",
216 'node_name': self.target_name,
217 'cmd': ("set -ex;" +
218 salt_call_cmd + " pip.install setuptools && " +
219 salt_call_cmd + " pip.install docker")},
220 {
221 'description': "Run salt.minion state for runtest formula",
222 'node_name': self.master_name,
223 'cmd': ("set -ex;" +
224 salt_call_cmd + " state.sls salt.minion && "
225 " sleep 20")},
226 {
227 'description': "Enforce keystone state for neutronv2",
228 'node_name': self.master_name,
229 'cmd': ("set -ex;" +
230 salt_call_cmd + " state.sls keystone.client")},
231 {
232 'description': "Create networks for Tempest tests",
233 'node_name': self.master_name,
234 'cmd': ("set -ex;" +
235 salt_call_cmd + " state.sls neutron.client")},
236 {
237 'description': "Create flavors for Tempest tests",
238 'node_name': self.master_name,
239 'cmd': ("set -ex;" +
240 salt_call_cmd + " state.sls nova.client")},
241 {
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300242 'description': "Upload images for Tempest",
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000243 'node_name': self.master_name,
244 'cmd': ("set -ex;" +
245 salt_call_cmd + " state.sls glance.client")},
246 {
247 'description': "Generate config for Tempest",
248 'node_name': self.master_name,
249 'cmd': ("set -ex;" +
250 salt_call_cmd + " state.sls runtest")},
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300251 {
252 'description': "Upload cirros image",
253 'node_name': self.master_name,
254 'cmd': ("set -ex;"
255 "cirros_url=$({}) && {} '{}' cmd.run "
256 "\"wget $cirros_url -O /tmp/TestCirros-0.3.5.img\""
257 .format(cirros_pillar, salt_cmd, self.target_name))},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000258 ]
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300259
Tatyana Leontovich61a821d2018-08-21 12:30:51 +0300260 if dpdk:
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000261 commands.append({
262 'description': "Configure flavor for DPDK",
263 'node_name': self.control_name,
264 'cmd': ("set -ex;" +
265 salt_call_cmd + " cmd.run "
266 " '. /root/keystonercv3;"
sgudz905ec692018-10-16 15:30:39 +0300267 " openstack flavor set m1.extra_tiny_test"
268 " --property hw:mem_page_size=any;"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000269 " openstack flavor set m1.tiny_test"
sgudz905ec692018-10-16 15:30:39 +0300270 " --property hw:mem_page_size=any'")},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000271 )
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300272
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000273 self.__salt_api.execute_commands(commands=commands,
274 label="Prepare for Tempest")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300275
276 def run_tempest(self, timeout=600):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000277 tgt = self.target_name
278 image_nameversion = "{}:{}".format(self.image_name, self.image_version)
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300279
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000280 docker_args = (
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300281 " -t "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000282 " --name {container_name} "
283 " -e ARGS=\"-r {tempest_pattern} -w {tempest_threads}\""
284 " -v {cfg_dir}/tempest.conf:/etc/tempest/tempest.conf"
285 " -v /tmp/:/tmp/"
286 " -v {cfg_dir}:/root/tempest"
287 " -v /etc/ssl/certs/:/etc/ssl/certs/"
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300288 " -d "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000289 " {image_nameversion} {run_cmd}"
290 .format(
291 container_name=self.container_name,
292 image_nameversion=image_nameversion,
293 tempest_pattern=self.tempest_pattern,
294 tempest_threads=self.tempest_threads,
295 cfg_dir=TEMPEST_CFG_DIR,
296 run_cmd=self.run_cmd,
297 ))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300298
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000299 commands = [
300 {
301 'description': "Run Tempest tests {0}".format(
302 image_nameversion),
303 'node_name': self.target_name,
304 'cmd': ("set -ex;" +
305 " docker rm --force {container_name} || true;"
306 " docker run {docker_args}"
307 .format(container_name=self.container_name,
308 docker_args=docker_args)),
309 'timeout': timeout},
310 ]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300311
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000312 self.__salt_api.execute_commands(commands=commands,
313 label="Run Tempest tests")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300314
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300315 def wait_status(s):
316 inspect_res = self.salt_api.local(tgt,
317 'dockerng.inspect',
318 self.container_name)
319 if 'return' in inspect_res:
320 inspect = inspect_res['return']
321 inspect = inspect[0]
322 inspect = next(inspect.iteritems())[1]
323 status = inspect['State']['Status']
324
325 return status.lower() == s.lower()
326
327 return False
328
329 helpers.wait(lambda: wait_status('exited'),
330 timeout=timeout,
331 timeout_msg=('Tempest run didnt finished '
332 'in {}'.format(timeout)))
333
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300334 inspect_res = self.salt_api.local(tgt,
335 'dockerng.inspect',
336 self.container_name)
337 inspect = inspect_res['return'][0]
338 inspect = next(inspect.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300339 logs_res = self.salt_api.local(tgt,
340 'dockerng.logs',
341 self.container_name)
342 logs = logs_res['return'][0]
343 logs = next(logs.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300344
345 res = self.salt_api.local(tgt, 'dockerng.rm', self.container_name)
346 LOG.info("Tempest container was removed".format(
347 json.dumps(res, indent=4)))
348
349 return {'inspect': inspect,
350 'logs': logs}
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300351
sgudza5c901d2018-10-29 15:08:33 +0200352 def prepare_and_run_tempest(self, username='root'):
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300353 """
354 Run tempest tests
355 """
356 tempest_timeout = settings.TEMPEST_TIMEOUT
sgudza5c901d2018-10-29 15:08:33 +0200357 self.prepare()
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300358 test_res = self.run_tempest(tempest_timeout)
359 self.fetch_arficats(username=username)
360 self.save_runtime_logs(**test_res)