Ceph failover HA

Change-Id: I41945fa48f5de97c472392b22e0efc6319b5aefb
diff --git a/tcp_tests/managers/openstack_manager.py b/tcp_tests/managers/openstack_manager.py
index 2780d9b..bac459a 100644
--- a/tcp_tests/managers/openstack_manager.py
+++ b/tcp_tests/managers/openstack_manager.py
@@ -12,6 +12,7 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 import os
+import requests
 
 from tcp_tests.managers.execute_commands import ExecuteCommandsMixin
 from tcp_tests import logger
@@ -39,6 +40,55 @@
         self.execute_commands(commands,
                               label='Install OpenStack services')
         self.__config.openstack.openstack_installed = True
+        h_data = self.get_horizon_data()
+        self.__config.openstack.horizon_host = h_data['horizon_host']
+        self.__config.openstack.horizon_port = h_data['horizon_port']
+        self.__config.openstack.horizon_user = h_data['horizon_user']
+        self.__config.openstack.horizon_password = h_data['horizon_password']
+        self.auth_in_horizon(
+            h_data['horizon_host'],
+            h_data['horizon_port'],
+            h_data['horizon_user'],
+            h_data['horizon_password'])
+
+    def get_horizon_data(self):
+        horizon_data = {}
+        tgt = 'I@nginx:server and not cfg*'
+        pillar_host = ('nginx:server:site:nginx_ssl_redirect'
+                       '_openstack_web:host:name')
+        pillar_port = ('nginx:server:site:nginx_ssl_redirect'
+                       '_openstack_web:host:port')
+        hosts = self._salt.get_pillar(tgt=tgt, pillar=pillar_host)
+        host = set([ip for item in hosts for node, ip
+                    in item.items() if ip])
+        if host:
+            host = host.pop()
+        ports = self._salt.get_pillar(tgt=tgt, pillar=pillar_port)
+
+        port = set([port for item in ports for node, port
+                    in item.items() if port])
+        if port:
+            port = port.pop()
+        tgt = 'I@keystone:server and ctl01*'
+        pillar_user = 'keystone:server:admin_name'
+        pillar_password = 'keystone:server:admin_password'
+        users = self._salt.get_pillar(tgt=tgt, pillar=pillar_user)
+        user = set([user for item in users for node, user
+                    in item.items() if user])
+        if user:
+            user = user.pop()
+        passwords = self._salt.get_pillar(tgt=tgt, pillar=pillar_password)
+        pwd = set([pwd for item in passwords for node, pwd
+                   in item.items() if pwd])
+        if pwd:
+            pwd = pwd.pop()
+        horizon_data.update({'horizon_host': host})
+        horizon_data.update({'horizon_port': port})
+        horizon_data.update({'horizon_user': user})
+        horizon_data.update({'horizon_password': pwd})
+        LOG.info("Data from pillars {}".format(horizon_data))
+
+        return horizon_data
 
     def run_tempest(
             self,
@@ -121,3 +171,37 @@
         LOG.info('Reboot (warm restart) nodes {0}'.format(node_names))
         self.warm_shutdown_openstack_nodes(node_names, timeout=timeout)
         self.warm_start_nodes(node_names)
+
+    def auth_in_horizon(self, host, port, user, password):
+        client = requests.session()
+        url = "http://{0}:{1}".format(
+            self.__config.openstack.horizon_host,
+            self.__config.openstack.horizon_port)
+        # Retrieve the CSRF token first
+        client.get(url, verify=False)  # sets cookie
+        if not len(client.cookies):
+            login_data = dict(
+                username=self.__config.openstack.horizon_user,
+                password=self.__config.openstack.horizon_password,
+                next='/')
+            resp = client.post(url, data=login_data,
+                               headers=dict(Referer=url), verify=False)
+            LOG.debug("Horizon resp {}".format(resp))
+            assert 200 == resp.status_code, ("Failed to auth in "
+                                             "horizon. Response "
+                                             "{0}".format(resp.status_code))
+        else:
+            login_data = dict(
+                username=self.__config.openstack.horizon_user,
+                password=self.__config.openstack.horizon_password,
+                next='/')
+            csrftoken = client.cookies.get('csrftoken', None)
+            if csrftoken:
+                login_data['csrfmiddlewaretoken'] = csrftoken
+
+            resp = client.post(url, data=login_data,
+                               headers=dict(Referer=url), verify=False)
+            LOG.debug("Horizon resp {}".format(resp))
+            assert 200 == resp.status_code, ("Failed to auth in "
+                                             "horizon. Response "
+                                             "{0}".format(resp.status_code))