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