blob: dcf43092b8bac28ddb3242c3fab613f5a8278b9c [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
17from junit_xml import TestSuite, TestCase
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030018
19from tcp_tests import logger
20from tcp_tests import settings
21
22
23LOG = logger.logger
24
25
26class RallyManager(object):
27 """docstring for RallyManager"""
28
29 image_name = 'rallyforge/rally'
Dennis Dmitriev5e81a4b2017-04-27 03:32:01 +030030 image_version = '0.9.1'
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030031
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +020032 def __init__(self, underlay, admin_host):
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030033 super(RallyManager, self).__init__()
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +020034 self._admin_host = admin_host
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030035 self._underlay = underlay
36
37 def prepare(self):
38 content = """
39sed -i 's|#swift_operator_role = Member|swift_operator_role=SwiftOperator|g' /etc/rally/rally.conf # noqa
40source /home/rally/openrc
41rally-manage db recreate
42rally deployment create --fromenv --name=tempest
Dennis Dmitriev5e81a4b2017-04-27 03:32:01 +030043rally verify create-verifier --type tempest --name tempest-verifier
44rally verify configure-verifier
45rally verify configure-verifier --show
46"""
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030047 cmd = "cat > {path} << EOF\n{content}\nEOF".format(
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +020048 path='/root/rally/install_tempest.sh', content=content)
49 cmd1 = "chmod +x /root/rally/install_tempest.sh"
Oleksiy Butenkod7045a12016-11-04 13:26:56 +020050 cmd2 = "scp ctl01:/root/keystonercv3 /root/rally/openrc"
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030051
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +020052 with self._underlay.remote(host=self._admin_host) as remote:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030053 LOG.info("Create rally workdir")
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +020054 remote.check_call('mkdir -p /root/rally')
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030055 LOG.info("Create install_tempest.sh")
56 remote.check_call(cmd)
57 LOG.info("Chmod +x install_tempest.sh")
58 remote.check_call(cmd1)
59 LOG.info("Copy openstackrc")
60 remote.check_call(cmd2)
61
62 def pull_image(self, version=None):
63 version = version or self.image_version
64 image = self.image_name
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +020065 cmd = ("apt-get -y install docker.io &&"
66 " docker pull {image}:{version}".format(image=image,
67 version=version))
68 with self._underlay.remote(host=self._admin_host) as remote:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030069 LOG.info("Pull {image}:{version}".format(image=image,
70 version=version))
71 remote.check_call(cmd)
72
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +020073 with self._underlay.remote(host=self._admin_host) as remote:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030074 LOG.info("Getting image id")
Dennis Dmitriev5e81a4b2017-04-27 03:32:01 +030075 cmd = "docker images | grep {0}| awk '{print $3}'".format(
76 self.image_version)
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030077 res = remote.check_call(cmd)
78 self.image_id = res['stdout'][0].strip()
79 LOG.info("Image ID is {}".format(self.image_id))
80
81 def run(self):
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +020082 with self._underlay.remote(host=self._admin_host) as remote:
83 cmd = ("docker run --net host -v /root/rally:/home/rally "
84 "-tid -u root {image_id}".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
88 cmd = ("docker ps | grep {image_id} | "
89 "awk '{{print $1}}'| head -1").format(
90 image_id=self.image_id)
91 LOG.info("Getting container id")
92 res = remote.check_call(cmd)
93 self.docker_id = res['stdout'][0].strip()
94 LOG.info("Container ID is {}".format(self.docker_id))
95
96 def run_tempest(self, test=''):
Oleksiy Butenkod7045a12016-11-04 13:26:56 +020097 docker_exec = ('docker exec -i {docker_id} bash -c "{cmd}"')
Dennis Dmitriev6f59add2016-10-18 13:45:27 +030098 commands = [
99 docker_exec.format(cmd="./install_tempest.sh",
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300100 docker_id=self.docker_id),
101 docker_exec.format(
102 cmd="source /home/rally/openrc && "
103 "rally verify start {test}".format(test=test),
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300104 docker_id=self.docker_id),
105 docker_exec.format(
Dennis Dmitriev5e81a4b2017-04-27 03:32:01 +0300106 cmd="rally verify report --type json --to result.json",
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300107 docker_id=self.docker_id),
108 docker_exec.format(
Dennis Dmitriev5e81a4b2017-04-27 03:32:01 +0300109 cmd="rally verify report --type html --to result.html",
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300110 docker_id=self.docker_id),
111 ]
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +0200112 with self._underlay.remote(host=self._admin_host) as remote:
Dennis Dmitriev6f59add2016-10-18 13:45:27 +0300113 LOG.info("Run tempest inside Rally container")
114 for cmd in commands:
115 remote.check_call(cmd, verbose=True)
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +0200116
117 def get_results(self, store=True, store_file='tempest.xml'):
118 LOG.info('Storing tests results...')
119 res_file_name = 'result.json'
120 file_prefix = 'results_' + datetime.datetime.now().strftime(
121 '%Y%m%d_%H%M%S') + '_'
Dennis Dmitrievd6807ab2017-04-28 09:07:43 +0300122 file_dst = '{0}/{1}{2}'.format(
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +0200123 settings.LOGS_DIR, file_prefix, res_file_name)
124 with self._underlay.remote(host=self._admin_host) as remote:
125 remote.download(
dis129285a2016-12-05 10:35:14 +0200126 '/root/rally/{0}'.format(res_file_name),
Dennis Dmitriev9cc4ca32016-11-03 13:50:45 +0200127 file_dst)
128 res = json.load(remote.open('/root/rally/result.json'))
129 if not store:
130 return res
131
132 formatted_tc = []
133 failed_cases = [res['test_cases'][case]
134 for case in res['test_cases']
135 if res['test_cases'][case]['status']
136 in 'fail']
137 for case in failed_cases:
138 if case:
139 tc = TestCase(case['name'])
140 tc.add_failure_info(case['traceback'])
141 formatted_tc.append(tc)
142
143 skipped_cases = [res['test_cases'][case]
144 for case in res['test_cases']
145 if res['test_cases'][case]['status'] in 'skip']
146 for case in skipped_cases:
147 if case:
148 tc = TestCase(case['name'])
149 tc.add_skipped_info(case['reason'])
150 formatted_tc.append(tc)
151
152 error_cases = [res['test_cases'][case] for case in res['test_cases']
153 if res['test_cases'][case]['status'] in 'error']
154
155 for case in error_cases:
156 if case:
157 tc = TestCase(case['name'])
158 tc.add_error_info(case['traceback'])
159 formatted_tc.append(tc)
160
161 success = [res['test_cases'][case] for case in res['test_cases']
162 if res['test_cases'][case]['status'] in 'success']
163 for case in success:
164 if case:
165 tc = TestCase(case['name'])
166 formatted_tc.append(tc)
167
168 ts = TestSuite("tempest", formatted_tc)
169 with open(store_file, 'w') as f:
170 ts.to_file(f, [ts], prettyprint=False)
171
172 return res