blob: 8957091285aea7a02690d0391d72ff45019198a7 [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 devops.helpers import helpers
19
20from tcp_tests import logger
21from tcp_tests import settings
22
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030023LOG = logger.logger
24
25TEMPEST_CFG_DIR = '/tmp/test'
26
27CONFIG = {
28 'classes': ['service.runtest.tempest'],
29 'parameters': {
30 '_param': {
31 'runtest_tempest_cfg_dir': TEMPEST_CFG_DIR,
32 'runtest_tempest_cfg_name': 'tempest.conf',
33 'runtest_tempest_public_net': 'net04_ext',
34 'tempest_test_target': 'gtw01*'
35 },
36 'neutron': {
37 'client': {
38 'enabled': True
39 }
40 },
41 'runtest': {
42 'enabled': True,
43 'keystonerc_node': 'ctl01*',
44 'tempest': {
45 'enabled': True,
46 'cfg_dir': '${_param:runtest_tempest_cfg_dir}',
47 'cfg_name': '${_param:runtest_tempest_cfg_name}',
48 'DEFAULT': {
49 'log_file': 'tempest.log'
50 },
51 'compute': {
52 'build_timeout': 600,
53 'max_microversion': 2.53,
54 'min_compute_nodes': 2,
55 'min_microversion': 2.1,
56 'volume_device_name': 'vdc'
57 },
58 'convert_to_uuid': {
59 'network': {
60 'public_network_id':
61 '${_param:runtest_tempest_public_net}'
62 }
63 },
64 'dns_feature_enabled': {
65 'api_admin': False,
66 'api_v1': False,
67 'api_v2': True,
68 'api_v2_quotas': True,
69 'api_v2_root_recordsets': True,
70 'bug_1573141_fixed': True
71 },
72 'heat_plugin': {
73 'floating_network_name':
74 '${_param:runtest_tempest_public_net}'
75 },
76 'network': {
77 'floating_network_name':
78 '${_param:runtest_tempest_public_net}'
79 },
80 'share': {
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030081 'capability_snapshot_support': True,
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030082 'run_driver_assisted_migration_tests': False,
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030083 'run_manage_unmanage_snapshot_tests': False,
84 'run_manage_unmanage_tests': False,
85 'run_migration_with_preserve_snapshots_tests': False,
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030086 'run_quota_tests': True,
87 'run_replication_tests': False,
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030088 'run_snapshot_tests': True,
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030089 }}}}}
90
91
92class RuntestManager(object):
93 """Helper manager for execution tempest via runtest-formula"""
94
95 image_name = settings.TEMPEST_IMAGE
96 image_version = settings.TEMPEST_IMAGE_VERSION
97 container_name = 'run-tempest-ci'
98 master_host = "cfg01"
99 master_tgt = "{}*".format(master_host)
100 class_name = "runtest"
101 run_cmd = '/bin/bash -c "run-tempest"'
102
103 def __init__(self, underlay, salt_api, cluster_name, domain_name,
104 tempest_threads, tempest_exclude_test_args,
105 tempest_pattern=settings.TEMPEST_PATTERN,
106 run_cmd=None, target='gtw01'):
107 self.underlay = underlay
108 self.__salt_api = salt_api
109 self.target = target
110 self.cluster_name = cluster_name
111 self.domain_name = domain_name
112 self.tempest_threads = tempest_threads
113 self.tempest_exclude_test_args = tempest_exclude_test_args
114 self.tempest_pattern = tempest_pattern
115 self.run_cmd = run_cmd or self.run_cmd
116
117 @property
118 def salt_api(self):
119 return self.__salt_api
120
121 def install_python_lib(self):
122 return self.salt_api.local(
123 "{}*".format(self.target),
124 'pip.install', 'docker'), None
125
126 def install_formula(self):
127 return self.salt_api.local(
128 self.master_tgt,
129 'pkg.install', 'salt-formula-runtest'), None
130
131 def create_networks(self):
132 return self.salt_api.enforce_state(self.master_tgt, 'neutron.client')
133
134 def create_flavors(self):
135 return self.salt_api.enforce_state(self.master_tgt, 'nova.client')
136
137 def create_cirros(self):
138 return self.salt_api.enforce_state(self.master_tgt, 'glance.client')
139
140 def generate_config(self):
141 return self.salt_api.enforce_state(self.master_tgt, 'runtest')
142
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300143 def fetch_arficats(self, username=None, file_format='xml'):
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300144 target_name = next(node_name for node_name
145 in self.underlay.node_names() if
146 self.target in node_name)
147 with self.underlay.remote(node_name=target_name, username=None) as tgt:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300148 result = tgt.execute('find {} -name "report_*.{}"'.format(
149 TEMPEST_CFG_DIR, file_format))
150 LOG.debug("Find result {0}".format(result))
151 assert len(result['stdout']) > 0, ('No report found, please check'
152 ' if test run was successful.')
153 report = result['stdout'][0].rstrip()
154 LOG.debug("Found files {0}".format(report))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300155 tgt.download(
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300156 destination=report, # noqa
157 target=os.getcwd())
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300158
159 def store_runtest_model(self, config=CONFIG):
160 master_name = next(node_name for node_name
161 in self.underlay.node_names() if
162 self.master_host in node_name)
163 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),
169 node_name=master_name) as editor:
170 editor.content = config
171
172 with self.underlay.yaml_editor(
173 file_path="/srv/salt/reclass/nodes/_generated/"
174 "cfg01.{domain_name}.yml".format(
175 domain_name=self.domain_name),
176 node_name=master_name) as editor:
177 editor.content['classes'].append(
178 'cluster.{cluster_name}.infra.{class_name}'.format(
179 cluster_name=self.cluster_name,
180 class_name=self.class_name))
181
182 self.salt_api.local('*', 'saltutil.refresh_pillar')
183 self.salt_api.local('*', 'saltutil.sync_all')
184
185 def save_runtime_logs(self, logs=None, inspect=None):
186 if logs:
187 with open("{path}/{target}_tempest_run.log".format(
188 path=settings.LOGS_DIR, target=self.target), 'w') as f:
189 LOG.info("Save tempest console log")
190 container_log = logs
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300191 f.write(container_log.encode('ascii', 'ignore'))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300192
193 if inspect:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300194 with open("{path}/{target}_tempest_container_info.json.log".format(
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300195 path=settings.LOGS_DIR, target=self.target), 'w') as f:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300196 LOG.info("Save tempest container inspect data")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300197
198 container_inspect = json.dumps(inspect,
199 indent=4, sort_keys=True)
200 f.write(container_inspect)
201
202 def prepare(self):
203 self.store_runtest_model()
204 res = self.install_formula()
205 LOG.info(json.dumps(res, indent=4))
206 res = self.install_python_lib()
207 LOG.info(json.dumps(res, indent=4))
208 res = self.create_networks()
209 LOG.info(json.dumps(res, indent=4))
210 res = self.create_flavors()
211 LOG.info(json.dumps(res, indent=4))
212 res = self.create_cirros()
213 LOG.info(json.dumps(res, indent=4))
214 res = self.generate_config()
215 LOG.info(json.dumps(res, indent=4))
216
217 def run_tempest(self, timeout=600):
218 tgt = "{}*".format(self.target)
219 params = {
220 "name": self.container_name,
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300221 "image": "{}:{}".format(self.image_name, self.image_version),
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300222 "environment": {
223 "ARGS": "-r {tempest_pattern} -w "
224 "{tempest_threads} "
225 "{tempest_exclude_test_args}".format(
226 tempest_pattern=self.tempest_pattern,
227 tempest_threads=self.tempest_threads,
228 tempest_exclude_test_args=self.tempest_exclude_test_args) # noqa
229 },
230 "binds": [
231 "{cfg_dir}/tempest.conf:/etc/tempest/tempest.conf".format(cfg_dir=TEMPEST_CFG_DIR), # noqa
232 "/tmp/:/tmp/",
233 "{cfg_dir}:/root/tempest".format(cfg_dir=TEMPEST_CFG_DIR),
234 "/etc/ssl/certs/:/etc/ssl/certs/"
235 ],
236 "auto_remove": False,
237 "cmd": self.run_cmd
238 }
239
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300240 res = self.salt_api.local(tgt, 'dockerng.pull', "{}:{}".format(
241 self.image_name, self.image_version))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300242 LOG.info("Tempest image has beed pulled- \n{}".format(
243 json.dumps(res, indent=4)))
244
245 res = self.salt_api.local(tgt, 'dockerng.create', kwargs=params)
246 LOG.info("Tempest container has been created - \n{}".format(
247 json.dumps(res, indent=4)))
248
249 res = self.salt_api.local(tgt, 'dockerng.start', self.container_name)
250 LOG.info("Tempest container has been started - \n{}".format(
251 json.dumps(res, indent=4)))
252
253 def wait_status(s):
254 inspect_res = self.salt_api.local(tgt,
255 'dockerng.inspect',
256 self.container_name)
257 if 'return' in inspect_res:
258 inspect = inspect_res['return']
259 inspect = inspect[0]
260 inspect = next(inspect.iteritems())[1]
261 status = inspect['State']['Status']
262
263 return status.lower() == s.lower()
264
265 return False
266
267 helpers.wait(lambda: wait_status('exited'),
268 timeout=timeout,
269 timeout_msg=('Tempest run didnt finished '
270 'in {}'.format(timeout)))
271
272 inspect_res = self.salt_api.local(tgt,
273 'dockerng.inspect',
274 self.container_name)
275 inspect = inspect_res['return'][0]
276 inspect = next(inspect.iteritems())[1]
277 if inspect['State']['ExitCode'] != 0:
278 LOG.error("Tempest running failed")
279 LOG.info("Tempest tests have been finished - \n{}".format(
280 json.dumps(res, indent=4)))
281
282 logs_res = self.salt_api.local(tgt,
283 'dockerng.logs',
284 self.container_name)
285 logs = logs_res['return'][0]
286 logs = next(logs.iteritems())[1]
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300287 LOG.info("Tempest result - \n{}".format(
288 logs.encode('ascii', 'ignore')))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300289
290 res = self.salt_api.local(tgt, 'dockerng.rm', self.container_name)
291 LOG.info("Tempest container was removed".format(
292 json.dumps(res, indent=4)))
293
294 return {'inspect': inspect,
295 'logs': logs}
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300296
297 def prepare_and_run_tempest(self, username='root'):
298 """
299 Run tempest tests
300 """
301 tempest_timeout = settings.TEMPEST_TIMEOUT
302 self.prepare()
303 test_res = self.run_tempest(tempest_timeout)
304 self.fetch_arficats(username=username)
305 self.save_runtime_logs(**test_res)