Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 1 | # 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 Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 14 | import datetime |
| 15 | import json |
| 16 | |
| 17 | from junit_xml import TestSuite, TestCase |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 18 | |
| 19 | from tcp_tests import logger |
| 20 | from tcp_tests import settings |
| 21 | |
| 22 | |
| 23 | LOG = logger.logger |
| 24 | |
| 25 | |
| 26 | class RallyManager(object): |
| 27 | """docstring for RallyManager""" |
| 28 | |
| 29 | image_name = 'rallyforge/rally' |
Dennis Dmitriev | 5e81a4b | 2017-04-27 03:32:01 +0300 | [diff] [blame] | 30 | image_version = '0.9.1' |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 31 | |
Dennis Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 32 | def __init__(self, underlay, admin_host): |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 33 | super(RallyManager, self).__init__() |
Dennis Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 34 | self._admin_host = admin_host |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 35 | self._underlay = underlay |
| 36 | |
| 37 | def prepare(self): |
| 38 | content = """ |
| 39 | sed -i 's|#swift_operator_role = Member|swift_operator_role=SwiftOperator|g' /etc/rally/rally.conf # noqa |
| 40 | source /home/rally/openrc |
| 41 | rally-manage db recreate |
| 42 | rally deployment create --fromenv --name=tempest |
Dennis Dmitriev | 5e81a4b | 2017-04-27 03:32:01 +0300 | [diff] [blame] | 43 | rally verify create-verifier --type tempest --name tempest-verifier |
| 44 | rally verify configure-verifier |
| 45 | rally verify configure-verifier --show |
| 46 | """ |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 47 | cmd = "cat > {path} << EOF\n{content}\nEOF".format( |
Dennis Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 48 | path='/root/rally/install_tempest.sh', content=content) |
| 49 | cmd1 = "chmod +x /root/rally/install_tempest.sh" |
Oleksiy Butenko | d7045a1 | 2016-11-04 13:26:56 +0200 | [diff] [blame] | 50 | cmd2 = "scp ctl01:/root/keystonercv3 /root/rally/openrc" |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 51 | |
Dennis Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 52 | with self._underlay.remote(host=self._admin_host) as remote: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 53 | LOG.info("Create rally workdir") |
Dennis Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 54 | remote.check_call('mkdir -p /root/rally') |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 55 | 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 Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 65 | 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 Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 69 | LOG.info("Pull {image}:{version}".format(image=image, |
| 70 | version=version)) |
| 71 | remote.check_call(cmd) |
| 72 | |
Dennis Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 73 | with self._underlay.remote(host=self._admin_host) as remote: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 74 | LOG.info("Getting image id") |
Dennis Dmitriev | 5e81a4b | 2017-04-27 03:32:01 +0300 | [diff] [blame] | 75 | cmd = "docker images | grep {0}| awk '{print $3}'".format( |
| 76 | self.image_version) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 77 | 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 Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 82 | 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 Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 85 | 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 Butenko | d7045a1 | 2016-11-04 13:26:56 +0200 | [diff] [blame] | 97 | docker_exec = ('docker exec -i {docker_id} bash -c "{cmd}"') |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 98 | commands = [ |
| 99 | docker_exec.format(cmd="./install_tempest.sh", |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 100 | docker_id=self.docker_id), |
| 101 | docker_exec.format( |
| 102 | cmd="source /home/rally/openrc && " |
| 103 | "rally verify start {test}".format(test=test), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 104 | docker_id=self.docker_id), |
| 105 | docker_exec.format( |
Dennis Dmitriev | 5e81a4b | 2017-04-27 03:32:01 +0300 | [diff] [blame] | 106 | cmd="rally verify report --type json --to result.json", |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 107 | docker_id=self.docker_id), |
| 108 | docker_exec.format( |
Dennis Dmitriev | 5e81a4b | 2017-04-27 03:32:01 +0300 | [diff] [blame] | 109 | cmd="rally verify report --type html --to result.html", |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 110 | docker_id=self.docker_id), |
| 111 | ] |
Dennis Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 112 | with self._underlay.remote(host=self._admin_host) as remote: |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 113 | LOG.info("Run tempest inside Rally container") |
| 114 | for cmd in commands: |
| 115 | remote.check_call(cmd, verbose=True) |
Dennis Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 116 | |
| 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 Dmitriev | d6807ab | 2017-04-28 09:07:43 +0300 | [diff] [blame] | 122 | file_dst = '{0}/{1}{2}'.format( |
Dennis Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 123 | settings.LOGS_DIR, file_prefix, res_file_name) |
| 124 | with self._underlay.remote(host=self._admin_host) as remote: |
| 125 | remote.download( |
dis | 129285a | 2016-12-05 10:35:14 +0200 | [diff] [blame] | 126 | '/root/rally/{0}'.format(res_file_name), |
Dennis Dmitriev | 9cc4ca3 | 2016-11-03 13:50:45 +0200 | [diff] [blame] | 127 | 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 |