Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [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. |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 14 | import os |
Tatyana Leontovich | c72604d | 2018-01-04 17:58:00 +0200 | [diff] [blame] | 15 | import requests |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 16 | |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 17 | from tcp_tests.managers.execute_commands import ExecuteCommandsMixin |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 18 | from tcp_tests import logger |
Tatyana Leontovich | 8b70b90 | 2017-11-10 20:44:08 +0200 | [diff] [blame] | 19 | from tcp_tests import settings |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 20 | |
| 21 | LOG = logger.logger |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 22 | |
| 23 | |
| 24 | class OpenstackManager(ExecuteCommandsMixin): |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 25 | """docstring for OpenstackManager""" |
| 26 | |
Dmitry Tyzhnenko | bc0f826 | 2017-04-28 15:39:26 +0300 | [diff] [blame] | 27 | __config = None |
| 28 | __underlay = None |
Tatyana Leontovich | e5ccdb3 | 2017-10-09 20:10:43 +0300 | [diff] [blame] | 29 | __hardware = None |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 30 | |
Tatyana Leontovich | e5ccdb3 | 2017-10-09 20:10:43 +0300 | [diff] [blame] | 31 | def __init__(self, config, underlay, hardware, salt): |
Dmitry Tyzhnenko | bc0f826 | 2017-04-28 15:39:26 +0300 | [diff] [blame] | 32 | self.__config = config |
| 33 | self.__underlay = underlay |
Tatyana Leontovich | e5ccdb3 | 2017-10-09 20:10:43 +0300 | [diff] [blame] | 34 | self.__hardware = hardware |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 35 | self._salt = salt |
Dmitry Tyzhnenko | bc0f826 | 2017-04-28 15:39:26 +0300 | [diff] [blame] | 36 | super(OpenstackManager, self).__init__( |
| 37 | config=config, underlay=underlay) |
Dennis Dmitriev | 010f4cd | 2016-11-01 20:43:51 +0200 | [diff] [blame] | 38 | |
| 39 | def install(self, commands): |
Dmitry Tyzhnenko | 2b730a0 | 2017-04-07 19:31:32 +0300 | [diff] [blame] | 40 | self.execute_commands(commands, |
| 41 | label='Install OpenStack services') |
vrovachev | 700a7b0 | 2017-05-23 18:36:48 +0400 | [diff] [blame] | 42 | self.__config.openstack.openstack_installed = True |
Tatyana Leontovich | c72604d | 2018-01-04 17:58:00 +0200 | [diff] [blame] | 43 | h_data = self.get_horizon_data() |
| 44 | self.__config.openstack.horizon_host = h_data['horizon_host'] |
| 45 | self.__config.openstack.horizon_port = h_data['horizon_port'] |
| 46 | self.__config.openstack.horizon_user = h_data['horizon_user'] |
| 47 | self.__config.openstack.horizon_password = h_data['horizon_password'] |
Tatyana Leontovich | c08a54b | 2018-04-19 02:21:25 +0300 | [diff] [blame] | 48 | if self.__config.openstack.horizon_check: |
| 49 | self.auth_in_horizon( |
| 50 | h_data['horizon_host'], |
| 51 | h_data['horizon_port'], |
| 52 | h_data['horizon_user'], |
| 53 | h_data['horizon_password']) |
Tatyana Leontovich | c72604d | 2018-01-04 17:58:00 +0200 | [diff] [blame] | 54 | |
| 55 | def get_horizon_data(self): |
| 56 | horizon_data = {} |
| 57 | tgt = 'I@nginx:server and not cfg*' |
| 58 | pillar_host = ('nginx:server:site:nginx_ssl_redirect' |
| 59 | '_openstack_web:host:name') |
| 60 | pillar_port = ('nginx:server:site:nginx_ssl_redirect' |
| 61 | '_openstack_web:host:port') |
| 62 | hosts = self._salt.get_pillar(tgt=tgt, pillar=pillar_host) |
| 63 | host = set([ip for item in hosts for node, ip |
| 64 | in item.items() if ip]) |
| 65 | if host: |
| 66 | host = host.pop() |
| 67 | ports = self._salt.get_pillar(tgt=tgt, pillar=pillar_port) |
| 68 | |
| 69 | port = set([port for item in ports for node, port |
| 70 | in item.items() if port]) |
| 71 | if port: |
| 72 | port = port.pop() |
| 73 | tgt = 'I@keystone:server and ctl01*' |
| 74 | pillar_user = 'keystone:server:admin_name' |
| 75 | pillar_password = 'keystone:server:admin_password' |
| 76 | users = self._salt.get_pillar(tgt=tgt, pillar=pillar_user) |
| 77 | user = set([user for item in users for node, user |
| 78 | in item.items() if user]) |
| 79 | if user: |
| 80 | user = user.pop() |
| 81 | passwords = self._salt.get_pillar(tgt=tgt, pillar=pillar_password) |
| 82 | pwd = set([pwd for item in passwords for node, pwd |
| 83 | in item.items() if pwd]) |
| 84 | if pwd: |
| 85 | pwd = pwd.pop() |
| 86 | horizon_data.update({'horizon_host': host}) |
| 87 | horizon_data.update({'horizon_port': port}) |
| 88 | horizon_data.update({'horizon_user': user}) |
| 89 | horizon_data.update({'horizon_password': pwd}) |
| 90 | LOG.info("Data from pillars {}".format(horizon_data)) |
| 91 | |
| 92 | return horizon_data |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 93 | |
| 94 | def run_tempest( |
| 95 | self, |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 96 | target='gtw01', pattern=None, |
| 97 | conf_name='lvm_mcp.conf', |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 98 | registry=None, node_name=None): |
Tatyana Leontovich | 8b70b90 | 2017-11-10 20:44:08 +0200 | [diff] [blame] | 99 | if not registry: |
ibumarskov | ed91a0d | 2017-11-21 10:17:53 +0300 | [diff] [blame] | 100 | registry = ('{0}/{1}'.format(settings.DOCKER_REGISTRY, |
| 101 | settings.DOCKER_NAME)) |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 102 | if node_name is None and target is not None: |
| 103 | target_name = next( |
| 104 | name for name in self.__underlay.node_names() |
| 105 | if target in name) |
| 106 | else: |
| 107 | target_name = node_name |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 108 | |
Dennis Dmitriev | e3ee28d | 2018-02-20 16:51:52 +0200 | [diff] [blame] | 109 | cmd = ("apt-get -y install docker.io") |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 110 | with self.__underlay.remote(node_name=target_name) as node_remote: |
Dennis Dmitriev | e3ee28d | 2018-02-20 16:51:52 +0200 | [diff] [blame] | 111 | result = node_remote.execute(cmd, verbose=True) |
| 112 | |
Tatyana Leontovich | d6bcbc9 | 2018-03-23 15:02:28 +0200 | [diff] [blame] | 113 | cmd_iptables = "iptables --policy FORWARD ACCEPT" |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 114 | with self.__underlay.remote(node_name=target_name) as node_remote: |
Tatyana Leontovich | d6bcbc9 | 2018-03-23 15:02:28 +0200 | [diff] [blame] | 115 | result = node_remote.execute(cmd_iptables, verbose=True) |
| 116 | |
Dennis Dmitriev | 3e61ffb | 2018-02-20 19:16:46 +0200 | [diff] [blame] | 117 | with self.__underlay.remote( |
| 118 | host=self.__config.salt.salt_master_host) as node_remote: |
| 119 | result = node_remote.execute( |
Dennis Dmitriev | 8ccf6b5 | 2018-02-20 19:18:25 +0200 | [diff] [blame] | 120 | ("scp ctl01:/root/keystonercv3 /root;" |
| 121 | "scp /root/keystonercv3 gtw01:/root;"), |
Dennis Dmitriev | 3e61ffb | 2018-02-20 19:16:46 +0200 | [diff] [blame] | 122 | verbose=True) |
| 123 | |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 124 | if pattern: |
| 125 | cmd = ("docker run --rm --net=host " |
| 126 | "-e TEMPEST_CONF={0} " |
| 127 | "-e SKIP_LIST=mcp_skip.list " |
| 128 | "-e SOURCE_FILE=keystonercv3 " |
Oleksii Butenko | 6638557 | 2018-04-06 16:01:47 +0300 | [diff] [blame] | 129 | "-e CUSTOM='--pattern {1} --concurrency 2' " |
Tatyana Leontovich | 5b2c5b2 | 2018-02-19 19:48:34 +0200 | [diff] [blame] | 130 | "-v /root/:/home/rally " |
Tatyana Leontovich | c447b12 | 2018-02-22 12:30:42 +0200 | [diff] [blame] | 131 | "-v /var/log/:/home/rally/rally_reports/ " |
| 132 | "-v /etc/ssl/certs/:/etc/ssl/certs/ {2} >> image.output" |
Tatyana Leontovich | 8b70b90 | 2017-11-10 20:44:08 +0200 | [diff] [blame] | 133 | .format(conf_name, pattern, registry)) |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 134 | else: |
| 135 | cmd = ("docker run --rm --net=host " |
| 136 | "-e TEMPEST_CONF={0} " |
| 137 | "-e SKIP_LIST=mcp_skip.list " |
Tatyana Leontovich | 6051817 | 2017-11-14 17:39:58 +0200 | [diff] [blame] | 138 | "-e SET=full " |
Oleksii Butenko | 5187ea9 | 2018-04-05 17:16:26 +0300 | [diff] [blame] | 139 | "-e CONCURRENCY=2 " |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 140 | "-e SOURCE_FILE=keystonercv3 " |
Tatyana Leontovich | 5b2c5b2 | 2018-02-19 19:48:34 +0200 | [diff] [blame] | 141 | "-v /root/:/home/rally " |
Tatyana Leontovich | c447b12 | 2018-02-22 12:30:42 +0200 | [diff] [blame] | 142 | "-v /var/log/:/home/rally/rally_reports/ " |
| 143 | "-v /etc/ssl/certs/:/etc/ssl/certs/ {2} >> image.output" |
Tatyana Leontovich | 8b70b90 | 2017-11-10 20:44:08 +0200 | [diff] [blame] | 144 | .format(conf_name, pattern, registry)) |
Leontii Istomin | 05d0035 | 2017-11-10 17:12:11 +0400 | [diff] [blame] | 145 | LOG.info("Running tempest testing on node {0} using the following " |
ibumarskov | d3feedf | 2018-05-21 14:19:10 +0400 | [diff] [blame] | 146 | "command:\n{1}".format(target_name, cmd)) |
sgudz | 5d44f3d | 2017-09-25 19:27:46 +0300 | [diff] [blame] | 147 | |
ibumarskov | d3feedf | 2018-05-21 14:19:10 +0400 | [diff] [blame] | 148 | with self.__underlay.remote(node_name=target_name) as node_remote: |
sgudz | 5d44f3d | 2017-09-25 19:27:46 +0300 | [diff] [blame] | 149 | result = node_remote.execute(cmd, verbose=True) |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 150 | LOG.debug("Test execution result is {}".format(result)) |
| 151 | return result |
| 152 | |
| 153 | def download_tempest_report(self, file_fromat='xml', stored_node='gtw01'): |
| 154 | target_node_name = [node_name for node_name |
| 155 | in self.__underlay.node_names() |
| 156 | if stored_node in node_name] |
| 157 | with self.__underlay.remote(node_name=target_node_name[0]) as r: |
Tatyana Leontovich | 5b2c5b2 | 2018-02-19 19:48:34 +0200 | [diff] [blame] | 158 | result = r.execute('find /var/log/ -name "report_*.{}"'.format( |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 159 | file_fromat)) |
| 160 | LOG.debug("Find result {0}".format(result)) |
Dennis Dmitriev | 3e61ffb | 2018-02-20 19:16:46 +0200 | [diff] [blame] | 161 | assert len(result['stdout']) > 0, ('No report found, please check' |
Tatyana Leontovich | 62d1158 | 2017-11-09 14:05:52 +0200 | [diff] [blame] | 162 | ' if test run was successful.') |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 163 | file_name = result['stdout'][0].rstrip() |
sgudz | 5d44f3d | 2017-09-25 19:27:46 +0300 | [diff] [blame] | 164 | LOG.debug("Found files {0}".format(file_name)) |
Tatyana Leontovich | 53bd1f9 | 2017-09-08 13:04:42 +0300 | [diff] [blame] | 165 | r.download(destination=file_name, target=os.getcwd()) |
Tatyana Leontovich | e5ccdb3 | 2017-10-09 20:10:43 +0300 | [diff] [blame] | 166 | |
| 167 | def get_node_name_by_subname(self, node_sub_name): |
| 168 | return [node_name for node_name |
| 169 | in self.__underlay.node_names() |
| 170 | if node_sub_name in node_name] |
| 171 | |
| 172 | def warm_shutdown_openstack_nodes(self, node_sub_name, timeout=10 * 60): |
| 173 | """Gracefully shutting down the node """ |
| 174 | node_names = self.get_node_name_by_subname(node_sub_name) |
| 175 | LOG.info('Shutting down nodes {}'.format(node_names)) |
| 176 | for node in node_names: |
| 177 | LOG.debug('Shutdown node {0}'.format(node)) |
| 178 | self.__underlay.check_call(cmd="shutdown +1", node_name=node) |
| 179 | for node in node_names: |
| 180 | LOG.info('Destroy node {}'.format(node)) |
| 181 | self.__hardware.destroy_node(node) |
| 182 | self.__hardware.wait_for_node_state( |
| 183 | node, state='offline', timeout=timeout) |
| 184 | |
| 185 | def warm_start_nodes(self, node_sub_name, timeout=10 * 60): |
| 186 | node_names = self.get_node_name_by_subname(node_sub_name) |
| 187 | LOG.info('Starting nodes {}'.format(node_names)) |
| 188 | for node in node_names: |
| 189 | self.__hardware.start_node(node) |
| 190 | self.__hardware.wait_for_node_state( |
| 191 | node, state='active', timeout=timeout) |
| 192 | |
| 193 | def warm_restart_nodes(self, node_names, timeout=10 * 60): |
| 194 | LOG.info('Reboot (warm restart) nodes {0}'.format(node_names)) |
| 195 | self.warm_shutdown_openstack_nodes(node_names, timeout=timeout) |
| 196 | self.warm_start_nodes(node_names) |
Tatyana Leontovich | c72604d | 2018-01-04 17:58:00 +0200 | [diff] [blame] | 197 | |
| 198 | def auth_in_horizon(self, host, port, user, password): |
| 199 | client = requests.session() |
| 200 | url = "http://{0}:{1}".format( |
| 201 | self.__config.openstack.horizon_host, |
| 202 | self.__config.openstack.horizon_port) |
| 203 | # Retrieve the CSRF token first |
| 204 | client.get(url, verify=False) # sets cookie |
| 205 | if not len(client.cookies): |
| 206 | login_data = dict( |
| 207 | username=self.__config.openstack.horizon_user, |
| 208 | password=self.__config.openstack.horizon_password, |
| 209 | next='/') |
| 210 | resp = client.post(url, data=login_data, |
| 211 | headers=dict(Referer=url), verify=False) |
| 212 | LOG.debug("Horizon resp {}".format(resp)) |
| 213 | assert 200 == resp.status_code, ("Failed to auth in " |
| 214 | "horizon. Response " |
| 215 | "{0}".format(resp.status_code)) |
| 216 | else: |
| 217 | login_data = dict( |
| 218 | username=self.__config.openstack.horizon_user, |
| 219 | password=self.__config.openstack.horizon_password, |
| 220 | next='/') |
| 221 | csrftoken = client.cookies.get('csrftoken', None) |
| 222 | if csrftoken: |
| 223 | login_data['csrfmiddlewaretoken'] = csrftoken |
| 224 | |
| 225 | resp = client.post(url, data=login_data, |
| 226 | headers=dict(Referer=url), verify=False) |
| 227 | LOG.debug("Horizon resp {}".format(resp)) |
| 228 | assert 200 == resp.status_code, ("Failed to auth in " |
| 229 | "horizon. Response " |
| 230 | "{0}".format(resp.status_code)) |