blob: 07810ba481cd86954100d9d5e6e2bcf3741d0c65 [file] [log] [blame]
Dennis Dmitriev010f4cd2016-11-01 20:43:51 +02001# 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 Leontovich53bd1f92017-09-08 13:04:42 +030014import os
Tatyana Leontovichc72604d2018-01-04 17:58:00 +020015import requests
Dennis Dmitriev010f4cd2016-11-01 20:43:51 +020016
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030017from tcp_tests.managers.execute_commands import ExecuteCommandsMixin
Tatyana Leontovich53bd1f92017-09-08 13:04:42 +030018from tcp_tests import logger
Tatyana Leontovich8b70b902017-11-10 20:44:08 +020019from tcp_tests import settings
Tatyana Leontovich53bd1f92017-09-08 13:04:42 +030020
21LOG = logger.logger
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030022
23
24class OpenstackManager(ExecuteCommandsMixin):
Dennis Dmitriev010f4cd2016-11-01 20:43:51 +020025 """docstring for OpenstackManager"""
26
Dmitry Tyzhnenkobc0f8262017-04-28 15:39:26 +030027 __config = None
28 __underlay = None
Dennis Dmitriev010f4cd2016-11-01 20:43:51 +020029
Vladimir Jigulinee1faa52018-06-25 13:00:51 +040030 def __init__(self, config, underlay, salt):
Dmitry Tyzhnenkobc0f8262017-04-28 15:39:26 +030031 self.__config = config
32 self.__underlay = underlay
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030033 self._salt = salt
Dmitry Tyzhnenkobc0f8262017-04-28 15:39:26 +030034 super(OpenstackManager, self).__init__(
35 config=config, underlay=underlay)
Dennis Dmitriev010f4cd2016-11-01 20:43:51 +020036
37 def install(self, commands):
Dmitry Tyzhnenko2b730a02017-04-07 19:31:32 +030038 self.execute_commands(commands,
39 label='Install OpenStack services')
vrovachev700a7b02017-05-23 18:36:48 +040040 self.__config.openstack.openstack_installed = True
Tatyana Leontovichc72604d2018-01-04 17:58:00 +020041 h_data = self.get_horizon_data()
42 self.__config.openstack.horizon_host = h_data['horizon_host']
43 self.__config.openstack.horizon_port = h_data['horizon_port']
44 self.__config.openstack.horizon_user = h_data['horizon_user']
45 self.__config.openstack.horizon_password = h_data['horizon_password']
Tatyana Leontovichc08a54b2018-04-19 02:21:25 +030046 if self.__config.openstack.horizon_check:
47 self.auth_in_horizon(
48 h_data['horizon_host'],
49 h_data['horizon_port'],
50 h_data['horizon_user'],
51 h_data['horizon_password'])
Tatyana Leontovichc72604d2018-01-04 17:58:00 +020052
53 def get_horizon_data(self):
54 horizon_data = {}
55 tgt = 'I@nginx:server and not cfg*'
56 pillar_host = ('nginx:server:site:nginx_ssl_redirect'
57 '_openstack_web:host:name')
58 pillar_port = ('nginx:server:site:nginx_ssl_redirect'
59 '_openstack_web:host:port')
60 hosts = self._salt.get_pillar(tgt=tgt, pillar=pillar_host)
61 host = set([ip for item in hosts for node, ip
62 in item.items() if ip])
63 if host:
64 host = host.pop()
65 ports = self._salt.get_pillar(tgt=tgt, pillar=pillar_port)
66
67 port = set([port for item in ports for node, port
68 in item.items() if port])
69 if port:
70 port = port.pop()
71 tgt = 'I@keystone:server and ctl01*'
72 pillar_user = 'keystone:server:admin_name'
73 pillar_password = 'keystone:server:admin_password'
74 users = self._salt.get_pillar(tgt=tgt, pillar=pillar_user)
75 user = set([user for item in users for node, user
76 in item.items() if user])
77 if user:
78 user = user.pop()
79 passwords = self._salt.get_pillar(tgt=tgt, pillar=pillar_password)
80 pwd = set([pwd for item in passwords for node, pwd
81 in item.items() if pwd])
82 if pwd:
83 pwd = pwd.pop()
84 horizon_data.update({'horizon_host': host})
85 horizon_data.update({'horizon_port': port})
86 horizon_data.update({'horizon_user': user})
87 horizon_data.update({'horizon_password': pwd})
88 LOG.info("Data from pillars {}".format(horizon_data))
89
90 return horizon_data
Tatyana Leontovich53bd1f92017-09-08 13:04:42 +030091
92 def run_tempest(
93 self,
Tatyana Leontovich53bd1f92017-09-08 13:04:42 +030094 target='gtw01', pattern=None,
95 conf_name='lvm_mcp.conf',
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +020096 registry=None, node_name=None):
Tatyana Leontovich8b70b902017-11-10 20:44:08 +020097 if not registry:
ibumarskoved91a0d2017-11-21 10:17:53 +030098 registry = ('{0}/{1}'.format(settings.DOCKER_REGISTRY,
99 settings.DOCKER_NAME))
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200100 if node_name is None and target is not None:
101 target_name = next(
102 name for name in self.__underlay.node_names()
103 if target in name)
104 else:
105 target_name = node_name
Tatyana Leontovich53bd1f92017-09-08 13:04:42 +0300106
Tatyana Leontovicha0cb2702018-07-17 12:30:18 +0300107 cmd = ("apt-get -y install docker-ce")
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200108 with self.__underlay.remote(node_name=target_name) as node_remote:
Dennis Dmitrieve3ee28d2018-02-20 16:51:52 +0200109 result = node_remote.execute(cmd, verbose=True)
110
Tatyana Leontovichd6bcbc92018-03-23 15:02:28 +0200111 cmd_iptables = "iptables --policy FORWARD ACCEPT"
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +0200112 with self.__underlay.remote(node_name=target_name) as node_remote:
Tatyana Leontovichd6bcbc92018-03-23 15:02:28 +0200113 result = node_remote.execute(cmd_iptables, verbose=True)
114
Dennis Dmitriev3e61ffb2018-02-20 19:16:46 +0200115 with self.__underlay.remote(
116 host=self.__config.salt.salt_master_host) as node_remote:
117 result = node_remote.execute(
Dennis Dmitriev8ccf6b52018-02-20 19:18:25 +0200118 ("scp ctl01:/root/keystonercv3 /root;"
119 "scp /root/keystonercv3 gtw01:/root;"),
Dennis Dmitriev3e61ffb2018-02-20 19:16:46 +0200120 verbose=True)
121
Tatyana Leontovich53bd1f92017-09-08 13:04:42 +0300122 if pattern:
123 cmd = ("docker run --rm --net=host "
124 "-e TEMPEST_CONF={0} "
125 "-e SKIP_LIST=mcp_skip.list "
126 "-e SOURCE_FILE=keystonercv3 "
Oleksii Butenko66385572018-04-06 16:01:47 +0300127 "-e CUSTOM='--pattern {1} --concurrency 2' "
Tatyana Leontovich5b2c5b22018-02-19 19:48:34 +0200128 "-v /root/:/home/rally "
Tatyana Leontovichc447b122018-02-22 12:30:42 +0200129 "-v /var/log/:/home/rally/rally_reports/ "
130 "-v /etc/ssl/certs/:/etc/ssl/certs/ {2} >> image.output"
Tatyana Leontovich8b70b902017-11-10 20:44:08 +0200131 .format(conf_name, pattern, registry))
Tatyana Leontovich53bd1f92017-09-08 13:04:42 +0300132 else:
133 cmd = ("docker run --rm --net=host "
134 "-e TEMPEST_CONF={0} "
135 "-e SKIP_LIST=mcp_skip.list "
Tatyana Leontovich60518172017-11-14 17:39:58 +0200136 "-e SET=full "
Oleksii Butenko5187ea92018-04-05 17:16:26 +0300137 "-e CONCURRENCY=2 "
Tatyana Leontovich53bd1f92017-09-08 13:04:42 +0300138 "-e SOURCE_FILE=keystonercv3 "
Tatyana Leontovich5b2c5b22018-02-19 19:48:34 +0200139 "-v /root/:/home/rally "
Tatyana Leontovichc447b122018-02-22 12:30:42 +0200140 "-v /var/log/:/home/rally/rally_reports/ "
Pavel Glazov794b1a92022-10-03 16:33:04 +0400141 "-v /etc/ssl/certs/:/etc/ssl/certs/ {1} >> image.output"
142 .format(conf_name, registry))
Leontii Istomin05d00352017-11-10 17:12:11 +0400143 LOG.info("Running tempest testing on node {0} using the following "
ibumarskovd3feedf2018-05-21 14:19:10 +0400144 "command:\n{1}".format(target_name, cmd))
sgudz5d44f3d2017-09-25 19:27:46 +0300145
ibumarskovd3feedf2018-05-21 14:19:10 +0400146 with self.__underlay.remote(node_name=target_name) as node_remote:
sgudz5d44f3d2017-09-25 19:27:46 +0300147 result = node_remote.execute(cmd, verbose=True)
Tatyana Leontovich53bd1f92017-09-08 13:04:42 +0300148 LOG.debug("Test execution result is {}".format(result))
149 return result
150
151 def download_tempest_report(self, file_fromat='xml', stored_node='gtw01'):
152 target_node_name = [node_name for node_name
153 in self.__underlay.node_names()
154 if stored_node in node_name]
155 with self.__underlay.remote(node_name=target_node_name[0]) as r:
Tatyana Leontovich5b2c5b22018-02-19 19:48:34 +0200156 result = r.execute('find /var/log/ -name "report_*.{}"'.format(
Tatyana Leontovich53bd1f92017-09-08 13:04:42 +0300157 file_fromat))
158 LOG.debug("Find result {0}".format(result))
Dennis Dmitriev3e61ffb2018-02-20 19:16:46 +0200159 assert len(result['stdout']) > 0, ('No report found, please check'
Tatyana Leontovich62d11582017-11-09 14:05:52 +0200160 ' if test run was successful.')
Tatyana Leontovich53bd1f92017-09-08 13:04:42 +0300161 file_name = result['stdout'][0].rstrip()
sgudz5d44f3d2017-09-25 19:27:46 +0300162 LOG.debug("Found files {0}".format(file_name))
Tatyana Leontovich53bd1f92017-09-08 13:04:42 +0300163 r.download(destination=file_name, target=os.getcwd())
Tatyana Leontoviche5ccdb32017-10-09 20:10:43 +0300164
Tatyana Leontovichc72604d2018-01-04 17:58:00 +0200165 def auth_in_horizon(self, host, port, user, password):
166 client = requests.session()
167 url = "http://{0}:{1}".format(
168 self.__config.openstack.horizon_host,
169 self.__config.openstack.horizon_port)
170 # Retrieve the CSRF token first
171 client.get(url, verify=False) # sets cookie
172 if not len(client.cookies):
173 login_data = dict(
174 username=self.__config.openstack.horizon_user,
175 password=self.__config.openstack.horizon_password,
176 next='/')
177 resp = client.post(url, data=login_data,
178 headers=dict(Referer=url), verify=False)
179 LOG.debug("Horizon resp {}".format(resp))
180 assert 200 == resp.status_code, ("Failed to auth in "
181 "horizon. Response "
182 "{0}".format(resp.status_code))
183 else:
184 login_data = dict(
185 username=self.__config.openstack.horizon_user,
186 password=self.__config.openstack.horizon_password,
187 next='/')
188 csrftoken = client.cookies.get('csrftoken', None)
189 if csrftoken:
190 login_data['csrfmiddlewaretoken'] = csrftoken
191
192 resp = client.post(url, data=login_data,
193 headers=dict(Referer=url), verify=False)
194 LOG.debug("Horizon resp {}".format(resp))
195 assert 200 == resp.status_code, ("Failed to auth in "
196 "horizon. Response "
197 "{0}".format(resp.status_code))