blob: 36174dbc0aff19c685ee824e31ec8104cd186a6f [file] [log] [blame]
Dennis Dmitriev6f59add2016-10-18 13:45:27 +03001# Copyright 2016 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.
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +020014import datetime
15import json
16
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +020017from devops import error
18from functools32 import lru_cache
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030019
20from tcp_tests import logger
21from tcp_tests import settings
22
23
24LOG = logger.logger
25
26
27class RallyManager(object):
28 """docstring for RallyManager"""
29
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +020030 image_name = (
31 'docker-prod-virtual.docker.mirantis.net/'
32 'mirantis/oscore/rally-tempest')
33 image_version = 'latest'
34 tempest_tag = "16.0.0"
35 designate_tag = "0.2.0"
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030036
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +020037 def __init__(self, underlay, rally_node='gtw01.'):
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030038 super(RallyManager, self).__init__()
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030039 self._underlay = underlay
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +020040 self._node_name = self.get_target_node(target=rally_node)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030041
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +020042 @property
43 @lru_cache(maxsize=None)
44 def image_id(self):
45 LOG.info("Getting image id")
46 cmd = ("docker images | grep {0}| grep {1}| awk '{{print $3}}'"
47 .format(self.image_name, self.image_version))
48 res = self._underlay.check_call(cmd, node_name=self._node_name)
49 image_id = res['stdout'][0].strip()
50 LOG.info("Image ID is {}".format(image_id))
51 return image_id
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030052
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +020053 @property
54 @lru_cache(maxsize=None)
55 def docker_id(self):
56 cmd = ("docker ps | grep {image_id} | "
57 "awk '{{print $1}}'| head -1").format(
58 image_id=self.image_id)
59 LOG.info("Getting container id")
60 res = self._underlay.check_call(cmd, node_name=self._node_name)
61 docker_id = res['stdout'][0].strip()
62 LOG.info("Container ID is {}".format(docker_id))
63 return docker_id
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030064
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +020065 # Move method to underlay
66 def get_target_node(self, target='gtw01.'):
67 return [node_name for node_name
68 in self._underlay.node_names()
69 if node_name.startswith(target)][0]
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030070
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +020071 def _docker_exec(self, cmd, timeout=None, verbose=False):
72 docker_cmd = ('docker exec -i {docker_id} bash -c "{cmd}"'
73 .format(cmd=cmd, docker_id=self.docker_id))
74 LOG.info("Executing: {docker_cmd}".format(docker_cmd=docker_cmd))
Dennis Dmitrievb8115f52017-12-15 13:09:56 +020075 return self._underlay.check_call(docker_cmd, node_name=self._node_name,
76 verbose=verbose, timeout=timeout)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030077
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +020078 def _run(self):
79 """Start the rally container in the background"""
80 with self._underlay.remote(node_name=self._node_name) as remote:
81 cmd = ("docker run --net host -v /root/rally:/home/rally/.rally "
82 "-v /etc/ssl/certs/:/etc/ssl/certs/ "
83 "-tid -u root --entrypoint /bin/bash {image_id}"
84 .format(image_id=self.image_id))
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030085 LOG.info("Run Rally container")
86 remote.check_call(cmd)
87
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +020088 def run_container(self, version=None):
89 """Install docker, configure and run rally container"""
90 version = version or self.image_version
91 image = self.image_name
92 LOG.info("Pull {image}:{version}".format(image=image,
93 version=version))
94 cmd = ("apt-get -y install docker.io &&"
95 " docker pull {image}:{version}".format(image=image,
96 version=version))
97 self._underlay.check_call(cmd, node_name=self._node_name)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030098
Tatyana Leontovichd6bcbc92018-03-23 15:02:28 +020099 cmd_iptables = "iptables --policy FORWARD ACCEPT"
100 self._underlay.check_call(cmd_iptables, node_name=self._node_name)
101
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200102 LOG.info("Create rally workdir")
103 cmd = 'mkdir -p /root/rally; chown 65500 /root/rally'
104 self._underlay.check_call(cmd, node_name=self._node_name)
105
106 LOG.info("Copy keystonercv3")
Dmitry Tyzhnenko6d77ce42018-06-06 18:39:31 +0300107 tgt = self._node_name.split('.')[0]
108 cmd = "scp -3 ctl01:/root/keystonercv3 " \
109 "{tgt}:/root/rally/keystonercv3".format(
110 tgt=tgt)
111 domain = '.'.join(self._node_name.split('.')[1:])
112 self._underlay.check_call(cmd, node_name="cfg01.{domain}".format(
113 domain=domain))
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200114 self._run()
115
116 LOG.info("Create rally deployment")
117 self._docker_exec("rally-manage db recreate")
118 self._docker_exec("source /home/rally/.rally/keystonercv3;"
119 "rally deployment create --fromenv --name=Abathur")
120 self._docker_exec("rally deployment list")
121
122 def prepare_rally_task(self, target_node='ctl01.'):
123 """Prepare cirros image and private network for rally task"""
124 ctl_node_name = self._underlay.get_target_node_names(
125 target=target_node)[0]
126 cmds = [
127 ". keystonercv3 ; openstack flavor create --public m1.tiny",
128 ("wget http://download.cirros-cloud.net/0.3.4/"
129 "cirros-0.3.4-i386-disk.img"),
130 (". /root/keystonercv3; glance --timeout 120 image-create "
131 "--name cirros-disk --visibility public --disk-format qcow2 "
132 "--container-format bare --progress "
133 "< /root/cirros-0.3.4-i386-disk.img"),
134 ". /root/keystonercv3; neutron net-create net04",
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300135 ]
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +0200136
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200137 for cmd in cmds:
138 self._underlay.check_call(cmd, node_name=ctl_node_name)
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +0200139
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200140 def prepare_tempest_task(self):
141 """Configure rally.conf for tempest tests"""
142 pass
143# LOG.info("Modify rally.conf")
144# cmd = ("sed -i 's|#swift_operator_role = Member|"
145# "swift_operator_role=SwiftOperator|g' "
146# "/etc/rally/rally.conf")
147# self._docker_exec(cmd)
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +0200148
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200149 def create_rally_task(self, task_path, task_content):
150 """Create a file with rally task definition
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +0200151
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200152 :param task_path: path to JSON or YAML file on target node
153 :task_content: string with json or yaml content to store in file
154 """
Dmitry Tyzhnenkobc1133a2017-12-20 14:23:53 +0200155 cmd = ("mkdir -p $(dirname {task_path}) && "
156 "cat > {task_path} << EOF\n{task_content}\nEOF").format(
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200157 task_path=task_path, task_content=task_content)
158 self._underlay.check_call(cmd, node_name=self._node_name)
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +0200159
Dennis Dmitrievb8115f52017-12-15 13:09:56 +0200160 def run_task(self, task='', timeout=None, raise_on_timeout=True,
161 verbose=False):
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200162 """Run rally task
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +0200163
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200164 :param taks: path to json or yaml file with the task definition
165 :param raise_on_timeout: bool, ignore TimeoutError if False
Dennis Dmitrievb8115f52017-12-15 13:09:56 +0200166 :param verbose: show rally output to console if True
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200167 """
168 try:
Dennis Dmitrievb8115f52017-12-15 13:09:56 +0200169 res = self._docker_exec(
170 "rally task start {task}".format(task=task),
171 timeout=timeout,
172 verbose=verbose)
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200173 except error.TimeoutError:
174 if raise_on_timeout:
175 raise
176 else:
Dennis Dmitrievb8115f52017-12-15 13:09:56 +0200177 res = None
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200178 pass
Dennis Dmitrievb8115f52017-12-15 13:09:56 +0200179 return res
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +0200180
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200181 # Updated to replace the OpenStackManager method run_tempest
182 def run_tempest(self, conf_name='/var/lib/lvm_mcp.conf',
183 pattern='set=smoke', concurrency=0, timeout=None,
Tatyana Leontovichc72604d2018-01-04 17:58:00 +0200184 report_prefix='', report_types=None,
185 designate_plugin=True):
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200186 """Run tempest tests
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +0200187
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200188 :param conf_name: tempest config placed in the rally container
189 :param pattern: tempest testcase name or one of existing 'set=...'
190 :param concurrency: how many threads to use in parallel. 0 means
191 to take the amount of the cores on the node
192 <self._node_name>.
193 :param timeout: stop tempest tests after specified timeout.
Tatyana Leontovichc72604d2018-01-04 17:58:00 +0200194 :param designate_plugin: enabled by default plugin for designate
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200195 :param report_prefix: str, prefix for report filenames. Usually the
196 output of the fixture 'func_name'
197 :param report_types: list of the report types that need to download
198 from the environment: ['html', 'xml', 'json'].
199 None by default.
200 """
201 report_types = report_types or []
Tatyana Leontovichc72604d2018-01-04 17:58:00 +0200202 if not designate_plugin:
203 cmd = (
204 "cat > /root/rally/install_tempest.sh << EOF\n"
205 "rally verify create-verifier"
206 " --type tempest "
207 " --name tempest-verifier"
208 " --source /var/lib/tempest"
209 " --version {tempest_tag}"
210 " --system-wide\n"
211 "rally verify configure-verifier --extend {tempest_conf}\n"
212 "rally verify configure-verifier --show\n"
213 "EOF".format(tempest_tag=self.tempest_tag,
214 tempest_conf=conf_name))
215 else:
216 cmd = (
217 "cat > /root/rally/install_tempest.sh << EOF\n"
218 "rally verify create-verifier"
219 " --type tempest "
220 " --name tempest-verifier"
221 " --source /var/lib/tempest"
222 " --version {tempest_tag}"
223 " --system-wide\n"
224 "rally verify add-verifier-ext"
225 " --source /var/lib/designate-tempest-plugin"
226 " --version {designate_tag}\n"
227 "rally verify configure-verifier --extend {tempest_conf}\n"
228 "rally verify configure-verifier --show\n"
229 "EOF".format(tempest_tag=self.tempest_tag,
230 designate_tag=self.designate_tag,
231 tempest_conf=conf_name))
Dennis Dmitriev2d643bc2017-12-04 12:23:47 +0200232 with self._underlay.remote(node_name=self._node_name) as remote:
233 LOG.info("Create install_tempest.sh")
234 remote.check_call(cmd)
235 remote.check_call("chmod +x /root/rally/install_tempest.sh")
236
237 LOG.info("Run tempest inside Rally container")
238 self._docker_exec("/home/rally/.rally/install_tempest.sh")
239 self._docker_exec(
240 ("source /home/rally/.rally/keystonercv3 && "
241 "rally verify start --skip-list /var/lib/mcp_skip.list "
242 " --concurrency {concurrency} --pattern {pattern}"
243 .format(concurrency=concurrency, pattern=pattern)),
244 timeout=timeout, verbose=True)
245 if report_prefix:
246 report_filename = '{0}_report_{1}'.format(
247 report_prefix,
248 datetime.datetime.now().strftime('%Y%m%d_%H%M%S'))
249 else:
250 report_filename = 'report_{1}'.format(
251 datetime.datetime.now().strftime('%Y%m%d_%H%M%S'))
252 docker_file_prefix = '/home/rally/.rally/' + report_filename
253
254 # Create reports
255 if 'xml' in report_types:
256 self._docker_exec(
257 "rally verify report --type junit-xml --to {0}.xml"
258 .format(docker_file_prefix))
259 if 'html' in report_types:
260 self._docker_exec(
261 "rally verify report --type html --to {0}.html"
262 .format(docker_file_prefix))
263 # Always create report in JSON to return results into test case
264 # However, it won't be downloaded until ('json' in report_prefix)
265 self._docker_exec("rally verify report --type json --to {0}.json"
266 .format(docker_file_prefix))
267
268 # Download reports to the settings.LOGS_DIR
269 file_src_prefix = '/root/rally/{0}'.format(report_filename)
270 file_dst_prefix = '{0}/{1}'.format(settings.LOGS_DIR, report_filename)
271 with self._underlay.remote(node_name=self._node_name) as remote:
272 for suffix in report_types:
273 remote.download(file_src_prefix + '.' + suffix,
274 file_dst_prefix + '.' + suffix)
275 res = json.load(remote.open(file_src_prefix + '.json'))
276
277 # Get latest verification ID to find the lates testcases in the report
278 vtime = {vdata['finished_at']: vid
279 for vid, vdata in res['verifications'].items()}
280 vlatest_id = vtime[max(vtime.keys())]
281
282 # Each status has the dict with pairs:
283 # <status>: {
284 # <case_name>: <case_details>,
285 # }
286 formatted_tc = {
287 'success': {},
288 'fail': {},
289 'xfail': {},
290 'skip': {}
291 }
292
293 for tname, tdata in res['tests'].items():
294 status = tdata['by_verification'][vlatest_id]['status']
295 details = tdata['by_verification'][vlatest_id].get('details', '')
296 if status not in formatted_tc:
297 # Fail if tempest return a new status that may be
298 # necessary to take into account in test cases
299 raise Exception("Unknown testcase {0} status: {1} "
300 .format(tname, status))
301 formatted_tc[status][tdata['name']] = details
302 LOG.debug("Formatted testcases: {0}".format(formatted_tc))
303 return formatted_tc