summaryrefslogtreecommitdiffstats
path: root/cns-libs/cnslibs/common/heketi_libs.py
blob: 5101ae259c3e438b6266b1b9163e4b41e75d3c41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python

"""
    Description: Library for heketi BaseClass.
"""

from glusto.core import Glusto as g
import unittest
import datetime
from collections import OrderedDict
from cnslibs.common.exceptions import ExecutionError, ConfigError
from cnslibs.common.heketi_ops import (setup_heketi_ssh_key,
                                       modify_heketi_executor,
                                       export_heketi_cli_server,
                                       hello_heketi,
                                       heketi_volume_delete)
from cnslibs.common.openshift_ops import (oc_login, switch_oc_project,
                                          get_ocp_gluster_pod_names)


class HeketiBaseClass(unittest.TestCase):
    """
    This class initializes heketi config variables, constructs topology info
    dictionary and check if heketi server is alive.
    """

    @classmethod
    def setUpClass(cls):
        """
        setUpClass of HeketiBaseClass
        """

        super(HeketiBaseClass, cls).setUpClass()

        # Initializes heketi config variables
        cls.cns_username = g.config['cns']['setup']['cns_username']
        cls.cns_password = g.config['cns']['setup']['cns_password']
        cls.cns_project_name = g.config['cns']['setup']['cns_project_name']
        cls.ocp_master_nodes = g.config['ocp_servers']['master'].keys()
        cls.ocp_master_node = cls.ocp_master_nodes[0]

        cls.deployment_type = g.config['cns']['deployment_type']
        cls.executor = g.config['cns']['executor']
        cls.executor_user = g.config['cns']['executor_user']
        cls.executor_port = g.config['cns']['executor_port']

        heketi_config = g.config['cns']['heketi_config']
        cls.heketi_client_node = heketi_config['heketi_client_node']
        cls.heketi_server_url = heketi_config['heketi_server_url']
        cls.heketi_cli_user = heketi_config['heketi_cli_user']
        cls.heketi_cli_key = heketi_config['heketi_cli_key']
        cls.gluster_servers = g.config['gluster_servers'].keys()
        cls.gluster_servers_info = g.config['gluster_servers']
        cls.topo_info = g.config['cns']['trusted_storage_pool_list']
        cls.heketi_ssh_key = heketi_config['heketi_ssh_key']
        cls.heketi_config_file = heketi_config['heketi_config_file']
        cls.heketi_volume = {
            'size': g.config['cns']['heketi_volume']['size'],
            'name': g.config['cns']['heketi_volume']['name'],
            'expand_size': g.config['cns']['heketi_volume']['expand_size']}

        # Constructs topology info dictionary
        cls.topology_info = OrderedDict()
        for i in range(len(cls.topo_info)):
            cluster = 'cluster' + str(i + 1)
            cls.topology_info[cluster] = OrderedDict()
            for index, node in enumerate(cls.topo_info[i]):
                node_name = 'gluster_node' + str(index + 1)
                cls.topology_info[cluster][node_name] = {
                    'manage': cls.gluster_servers_info[node]['manage'],
                    'storage': cls.gluster_servers_info[node]['storage'],
                    'zone': cls.gluster_servers_info[node]['zone'],
                    'devices': cls.gluster_servers_info[node]['devices'],
                }

        # Checks if heketi server is alive
        if not hello_heketi(cls.heketi_client_node, cls.heketi_server_url):
            raise ConfigError("Heketi server %s is not alive"
                              % cls.heketi_server_url)

        if cls.deployment_type == "cns":
            if not oc_login(cls.ocp_master_node, cls.cns_username,
                            cls.cns_password):
                raise ExecutionError("Failed to do oc login on node %s"
                                     % cls.ocp_master_node)

            if not switch_oc_project(cls.ocp_master_node,
                                     cls.cns_project_name):
                raise ExecutionError("Failed to switch oc project on node %s"
                                     % cls.ocp_master_node)

            cls.gluster_pods = get_ocp_gluster_pod_names(cls.ocp_master_node)
            g.pod_name = cls.gluster_pods[0]

        # Have a unique string to recognize the test run for logging
        if 'glustotest_run_id' not in g.config:
            g.config['glustotest_run_id'] = (
                datetime.datetime.now().strftime('%H_%M_%d_%m_%Y'))
        cls.glustotest_run_id = g.config['glustotest_run_id']
        msg = "Setupclass: %s : %s" % (cls.__name__, cls.glustotest_run_id)
        g.log.info(msg)

    def setUp(self):
        super(HeketiBaseClass, self).setUp()
        msg = "Starting Test : %s : %s" % (self.id(), self.glustotest_run_id)
        g.log.info(msg)

    def delete_volumes(self, volume_ids):
        """
        Delete volumes by their IDs and raise error with list of failures
        Input: (volume_ids) It can be a single volume ID
        or a list of volume IDs
        """
        errored_ids = []

        if not isinstance(volume_ids, (list, set, tuple)):
            volume_ids = [volume_ids]

        for volume_id in volume_ids:
            out = heketi_volume_delete(
                self.heketi_client_node, self.heketi_server_url, volume_id)
            output_str = 'Volume %s deleted' % volume_id
            if output_str not in out:
                errored_ids.append(volume_id)

        if errored_ids:
            raise ExecutionError(
                "Failed to delete following heketi volumes: "
                "%s" % ',\n'.join(errored_ids))

    def tearDown(self):
        super(HeketiBaseClass, self).tearDown()
        msg = "Ending Test: %s : %s" % (self.id(), self.glustotest_run_id)
        g.log.info(msg)

    @classmethod
    def tearDownClass(cls):
        super(HeketiBaseClass, cls).tearDownClass()
        msg = "Teardownclass: %s : %s" % (cls.__name__, cls.glustotest_run_id)
        g.log.info(msg)


class HeketiClientSetupBaseClass(HeketiBaseClass):
    """
    Class to setup heketi ssh keys, modify heketi executor and to export
    heketi cli server.
    """

    @classmethod
    def setUpClass(cls):
        """
        setUpClass of HeketiClientSetupBaseClass
        """

        super(HeketiClientSetupBaseClass, cls).setUpClass()

        if cls.deployment_type == "crs_heketi_outside_openshift":
            # setup heketi ssh keys, if setup is not done already
            conn = g.rpyc_get_connection(cls.heketi_client_node, user="root")
            if conn is None:
                raise ConfigError("Failed to get rpyc connection of heketi "
                                  "node %s" % cls.heket_client_node)

            if conn.modules.os.path.exists(cls.heketi_ssh_key):
                g.log.info("Heketi ssh key is already generated")
            else:
                if not setup_heketi_ssh_key(cls.heketi_client_node,
                                            cls.gluster_servers,
                                            heketi_ssh_key=cls.heketi_ssh_key):
                    raise ConfigError("heketi ssh key setup failed on %s"
                                      % cls.heketi_client_node)

            g.rpyc_close_connection(cls.heketi_client_node, user="root")

            # Modifies heketi executor
            if not modify_heketi_executor(cls.heketi_client_node, cls.executor,
                                          cls.heketi_ssh_key,
                                          cls.executor_user,
                                          int(cls.executor_port)):
                raise ExecutionError("Failed to modify heketi executor on %s"
                                     % cls.heketi_client_node)

            # Exports heketi cli server
            heketi_url = cls.heketi_server_url
            if not export_heketi_cli_server(
                    cls.heketi_client_node,
                    heketi_cli_server=heketi_url,
                    heketi_cli_user=cls.heketi_cli_user,
                    heketi_cli_key=cls.heketi_cli_key):
                raise ExecutionError("Failed to export heketi cli server on %s"
                                     % cls.heketi_client_node)