blob: 135a467c8874b3809b0b5736d2fd4827410df2c9 [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
18from tcp_tests import logger
19from tcp_tests import settings
20
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030021LOG = logger.logger
22
23TEMPEST_CFG_DIR = '/tmp/test'
24
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030025
26class RuntestManager(object):
27 """Helper manager for execution tempest via runtest-formula"""
28
29 image_name = settings.TEMPEST_IMAGE
30 image_version = settings.TEMPEST_IMAGE_VERSION
31 container_name = 'run-tempest-ci'
32 master_host = "cfg01"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000033 control_host = "ctl01"
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030034 class_name = "runtest"
35 run_cmd = '/bin/bash -c "run-tempest"'
36
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000037 def __init__(self, config, underlay, salt_api, cluster_name,
Oleksii Butenko74566102018-09-11 11:55:13 +030038 domain_name, tempest_threads,
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030039 tempest_pattern=settings.TEMPEST_PATTERN,
40 run_cmd=None, target='gtw01'):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000041 self.__config = config
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030042 self.underlay = underlay
43 self.__salt_api = salt_api
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030044 self.cluster_name = cluster_name
45 self.domain_name = domain_name
46 self.tempest_threads = tempest_threads
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030047 self.tempest_pattern = tempest_pattern
48 self.run_cmd = run_cmd or self.run_cmd
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000049 self.target_name = self.underlay.get_target_node_names(target)[0]
50 self.master_name = self.underlay.get_target_node_names(
51 self.master_host)[0]
52 self.control_name = self.underlay.get_target_node_names(
53 self.control_host)[0]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030054
55 @property
56 def salt_api(self):
57 return self.__salt_api
58
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000059 @property
60 def runtest_pillar(self):
61 public_net = self.__config.underlay.dhcp_ranges[
62 settings.EXTERNAL_ADDRESS_POOL_NAME]
63 public_gateway = public_net["gateway"].encode("ascii")
64 public_cidr = public_net["cidr"].encode("ascii")
65 public_allocation_start = public_net["start"].encode("ascii")
66 public_allocation_end = public_net["end"].encode("ascii")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030067
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000068 return {
69 'classes': ['service.runtest.tempest',
70 'service.runtest.tempest.public_net',
71 'service.runtest.tempest.services.manila.glance'],
72 'parameters': {
73 '_param': {
74 'runtest_tempest_cfg_dir': TEMPEST_CFG_DIR,
75 'runtest_tempest_cfg_name': 'tempest.conf',
76 'runtest_tempest_public_net': 'public',
77 'openstack_public_neutron_subnet_gateway': public_gateway,
78 'openstack_public_neutron_subnet_cidr': public_cidr,
79 'openstack_public_neutron_subnet_allocation_start':
80 public_allocation_start,
81 'openstack_public_neutron_subnet_allocation_end':
82 public_allocation_end,
83 'tempest_test_target': self.target_name.encode("ascii"),
84 },
85 'neutron': {
86 'client': {
87 'enabled': True
88 }
89 },
90 'runtest': {
91 'enabled': True,
92 'keystonerc_node': 'ctl01*',
93 'tempest': {
94 'enabled': True,
95 'cfg_dir': '${_param:runtest_tempest_cfg_dir}',
96 'cfg_name': '${_param:runtest_tempest_cfg_name}',
97 'DEFAULT': {
98 'log_file': 'tempest.log'
99 },
100 'compute': {
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000101 'min_compute_nodes': 2,
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000102 },
103 'convert_to_uuid': {
104 'network': {
105 'public_network_id':
106 '${_param:runtest_tempest_public_net}'
107 }
108 },
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000109 'share': {
110 'capability_snapshot_support': True,
111 'run_driver_assisted_migration_tests': False,
112 'run_manage_unmanage_snapshot_tests': False,
113 'run_manage_unmanage_tests': False,
114 'run_migration_with_preserve_snapshots_tests':
115 False,
116 'run_quota_tests': True,
117 'run_replication_tests': False,
118 'run_snapshot_tests': True,
119 }}}}}
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300120
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300121 def fetch_arficats(self, username=None, file_format='xml'):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000122 with self.underlay.remote(node_name=self.target_name,
123 username=None) as tgt:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300124 result = tgt.execute('find {} -name "report_*.{}"'.format(
125 TEMPEST_CFG_DIR, file_format))
126 LOG.debug("Find result {0}".format(result))
127 assert len(result['stdout']) > 0, ('No report found, please check'
128 ' if test run was successful.')
129 report = result['stdout'][0].rstrip()
130 LOG.debug("Found files {0}".format(report))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300131 tgt.download(
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300132 destination=report, # noqa
133 target=os.getcwd())
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300134
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000135 def store_runtest_model(self, runtest_pillar=None):
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300136 with self.underlay.yaml_editor(
137 file_path="/srv/salt/reclass/classes/cluster/"
138 "{cluster_name}/infra/"
139 "{class_name}.yml".format(
140 cluster_name=self.cluster_name,
141 class_name=self.class_name),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000142 node_name=self.master_name) as editor:
143 editor.content = runtest_pillar or self.runtest_pillar
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300144 with self.underlay.yaml_editor(
145 file_path="/srv/salt/reclass/nodes/_generated/"
146 "cfg01.{domain_name}.yml".format(
147 domain_name=self.domain_name),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000148 node_name=self.master_name) as editor:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300149 editor.content['classes'].append(
150 'cluster.{cluster_name}.infra.{class_name}'.format(
151 cluster_name=self.cluster_name,
152 class_name=self.class_name))
153
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300154 def save_runtime_logs(self, logs=None, inspect=None):
155 if logs:
156 with open("{path}/{target}_tempest_run.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000157 path=settings.LOGS_DIR,
158 target=self.target_name), 'w') as f:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300159 LOG.info("Save tempest console log")
160 container_log = logs
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300161 f.write(container_log.encode('ascii', 'ignore'))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300162
163 if inspect:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300164 with open("{path}/{target}_tempest_container_info.json.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000165 path=settings.LOGS_DIR,
166 target=self.target_name), 'w') as f:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300167 LOG.info("Save tempest container inspect data")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300168
169 container_inspect = json.dumps(inspect,
170 indent=4, sort_keys=True)
171 f.write(container_inspect)
172
Tatyana Leontovich61a821d2018-08-21 12:30:51 +0300173 def prepare(self, dpdk=None):
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300174 self.store_runtest_model()
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300175
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000176 salt_cmd = "salt -l info --hard-crash --state-output=mixed "
177 salt_call_cmd = "salt-call -l info --hard-crash --state-output=mixed "
178 commands = [
179 {
180 'description': "Sync salt objects for runtest model",
181 'node_name': self.master_name,
182 'cmd': ("set -ex;" +
183 salt_cmd + "'*' saltutil.refresh_pillar && " +
184 salt_cmd + "'*' saltutil.sync_all")},
185 {
186 'description': ("Install docker.io package and "
187 "enable packets forwarding"),
188 'node_name': self.target_name,
189 'cmd': ("set -ex;" +
190 salt_call_cmd + " pkg.install docker.io && " +
191 " iptables --policy FORWARD ACCEPT")},
192 {
193 'description': "Install PyPI docker package",
194 'node_name': self.target_name,
195 'cmd': ("set -ex;" +
196 salt_call_cmd + " pip.install setuptools && " +
197 salt_call_cmd + " pip.install docker")},
198 {
199 'description': "Run salt.minion state for runtest formula",
200 'node_name': self.master_name,
201 'cmd': ("set -ex;" +
202 salt_call_cmd + " state.sls salt.minion && "
203 " sleep 20")},
204 {
205 'description': "Enforce keystone state for neutronv2",
206 'node_name': self.master_name,
207 'cmd': ("set -ex;" +
208 salt_call_cmd + " state.sls keystone.client")},
209 {
210 'description': "Create networks for Tempest tests",
211 'node_name': self.master_name,
212 'cmd': ("set -ex;" +
213 salt_call_cmd + " state.sls neutron.client")},
214 {
215 'description': "Create flavors for Tempest tests",
216 'node_name': self.master_name,
217 'cmd': ("set -ex;" +
218 salt_call_cmd + " state.sls nova.client")},
219 {
220 'description': "Create cirros image for Tempest",
221 'node_name': self.master_name,
222 'cmd': ("set -ex;" +
223 salt_call_cmd + " state.sls glance.client")},
224 {
225 'description': "Generate config for Tempest",
226 'node_name': self.master_name,
227 'cmd': ("set -ex;" +
228 salt_call_cmd + " state.sls runtest")},
229 ]
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300230
Tatyana Leontovich61a821d2018-08-21 12:30:51 +0300231 if dpdk:
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000232 commands.append({
233 'description': "Configure flavor for DPDK",
234 'node_name': self.control_name,
235 'cmd': ("set -ex;" +
236 salt_call_cmd + " cmd.run "
237 " '. /root/keystonercv3;"
238 " openstack flavor set m1.tiny_test"
239 " --property hw:mem_page_size=small'")},
240 )
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300241
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000242 self.__salt_api.execute_commands(commands=commands,
243 label="Prepare for Tempest")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300244
245 def run_tempest(self, timeout=600):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000246 tgt = self.target_name
247 image_nameversion = "{}:{}".format(self.image_name, self.image_version)
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300248
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000249 docker_args = (
250 " --name {container_name} "
251 " -e ARGS=\"-r {tempest_pattern} -w {tempest_threads}\""
252 " -v {cfg_dir}/tempest.conf:/etc/tempest/tempest.conf"
253 " -v /tmp/:/tmp/"
254 " -v {cfg_dir}:/root/tempest"
255 " -v /etc/ssl/certs/:/etc/ssl/certs/"
256 " --rm"
257 " {image_nameversion} {run_cmd}"
258 .format(
259 container_name=self.container_name,
260 image_nameversion=image_nameversion,
261 tempest_pattern=self.tempest_pattern,
262 tempest_threads=self.tempest_threads,
263 cfg_dir=TEMPEST_CFG_DIR,
264 run_cmd=self.run_cmd,
265 ))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300266
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000267 commands = [
268 {
269 'description': "Run Tempest tests {0}".format(
270 image_nameversion),
271 'node_name': self.target_name,
272 'cmd': ("set -ex;" +
273 " docker rm --force {container_name} || true;"
274 " docker run {docker_args}"
275 .format(container_name=self.container_name,
276 docker_args=docker_args)),
277 'timeout': timeout},
278 ]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300279
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000280 self.__salt_api.execute_commands(commands=commands,
281 label="Run Tempest tests")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300282
283 inspect_res = self.salt_api.local(tgt,
284 'dockerng.inspect',
285 self.container_name)
286 inspect = inspect_res['return'][0]
287 inspect = next(inspect.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300288 logs_res = self.salt_api.local(tgt,
289 'dockerng.logs',
290 self.container_name)
291 logs = logs_res['return'][0]
292 logs = next(logs.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300293
294 res = self.salt_api.local(tgt, 'dockerng.rm', self.container_name)
295 LOG.info("Tempest container was removed".format(
296 json.dumps(res, indent=4)))
297
298 return {'inspect': inspect,
299 'logs': logs}
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300300
Tatyana Leontovich61a821d2018-08-21 12:30:51 +0300301 def prepare_and_run_tempest(self, username='root', dpdk=None):
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300302 """
303 Run tempest tests
304 """
305 tempest_timeout = settings.TEMPEST_TIMEOUT
Tatyana Leontovich61a821d2018-08-21 12:30:51 +0300306 self.prepare(dpdk=dpdk)
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300307 test_res = self.run_tempest(tempest_timeout)
308 self.fetch_arficats(username=username)
309 self.save_runtime_logs(**test_res)