summaryrefslogtreecommitdiffstats
path: root/tests/functional/common/heketi/test_heketi_volume_operations.py
blob: ac7df139266ad7e59955b9f4556b9ad6f066c497 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
from unittest import skip

from glusto.core import Glusto as g
from cnslibs.common.heketi_ops import (heketi_volume_delete,
                                       heketi_volume_create,
                                       heketi_volume_expand,
                                       heketi_volume_info,
                                       heketi_topology_info,
                                       heketi_device_add,
                                       heketi_device_enable,
                                       heketi_device_disable,
                                       heketi_device_remove,
                                       heketi_device_delete,
                                       heketi_node_info,
                                       heketi_node_list)
from cnslibs.common.heketi_libs import HeketiBaseClass
from cnslibs.common.exceptions import ExecutionError


class TestHeketiVolumeOperations(HeketiBaseClass):
    """
    Class to test heketi volume operations - create, expand
    """

    @classmethod
    def setUpClass(cls):
        super(TestHeketiVolumeOperations, cls).setUpClass()
        cls.volume_id = None
        cls.volume_size = 1

    def volume_cleanup(self, volume_id):
        """
        Method to cleanup volume in self.addCleanup()
        """
        if volume_id is not None:
            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:
                raise ExecutionError("Failed to delete heketi volume of"
                                     "id %s" % volume_id)

    def add_device(self, device_name, node_id):
        """
        Adds a device through heketi-cli
        """
        ret = heketi_device_add(self.heketi_client_node,
                                self.heketi_server_url,
                                device_name,
                                node_id)

        self.assertTrue(ret, ("Failed to add a device %s" % device_name))

    def detach_devices_attached(self, device_id_list):
        """
        All the devices attached are gracefully
        detached in this function
        """
        if not isinstance(device_id_list, (list, set, tuple)):
            device_id_list = [device_id_list]

        for device_id in device_id_list:
            device_disable = heketi_device_disable(
                self.heketi_client_node, self.heketi_server_url, device_id)
            self.assertNotEqual(
                device_disable, False,
                "Device %s could not be disabled" % device_id)
            device_remove = heketi_device_remove(
                self.heketi_client_node, self.heketi_server_url, device_id)
            self.assertNotEqual(
                device_remove, False,
                "Device %s could not be removed" % device_id)
            device_delete = heketi_device_delete(
                self.heketi_client_node, self.heketi_server_url, device_id)
            self.assertNotEqual(
                device_delete, False,
                "Device %s could not be deleted" % device_id)

    def test_heketi_with_default_options(self):
        """
        Test to create volume with default options.
        """

        vol_info = heketi_volume_create(self.heketi_client_node,
                                        self.heketi_server_url,
                                        self.volume_size, json=True)
        self.assertTrue(vol_info, ("Failed to create heketi volume of size %s"
                                   % self.volume_size))
        self.addCleanup(self.volume_cleanup, vol_info['id'])

        self.assertEqual(vol_info['size'], self.volume_size,
                         ("Failed to create volume with default options."
                          "Expected Size: %s, Actual Size: %s"
                          % (self.volume_size, vol_info['size'])))

    def test_heketi_with_expand_volume(self):
        """
        Test volume expand and size if updated correctly in heketi-cli info
        """

        vol_info = heketi_volume_create(self.heketi_client_node,
                                        self.heketi_server_url,
                                        self.volume_size, json=True)
        self.assertTrue(vol_info, ("Failed to create heketi volume of size %s"
                                   % self.volume_size))
        self.addCleanup(self.volume_cleanup, vol_info['id'])
        self.assertEqual(vol_info['size'], self.volume_size,
                         ("Failed to create volume."
                          "Expected Size: %s, Actual Size: %s"
                          % (self.volume_size, vol_info['size'])))
        volume_id = vol_info["id"]
        expand_size = 2
        ret = heketi_volume_expand(self.heketi_client_node,
                                   self.heketi_server_url, volume_id,
                                   expand_size)
        self.assertTrue(ret, ("Failed to expand heketi volume of id %s"
                              % volume_id))
        volume_info = heketi_volume_info(self.heketi_client_node,
                                         self.heketi_server_url,
                                         volume_id, json=True)
        expected_size = self.volume_size + expand_size
        self.assertEqual(volume_info['size'], expected_size,
                         ("Volume Expansion failed Expected Size: %s, Actual "
                          "Size: %s" % (str(expected_size),
                                        str(volume_info['size']))))

    @skip("Blocked by BZ-1629889")
    def test_heketi_with_device_removal_insuff_space(self):
        """
        Test to create volume consuming all space and then adding new device
        and then trying to remove an existing device. We should get an error
        saying insufficient space when removing device.
        """
        device_id_list = []

        vol_info = heketi_volume_create(self.heketi_client_node,
                                        self.heketi_server_url,
                                        650,
                                        json=True)

        self.assertNotEqual(vol_info, False, "Failed to create heketi volume")
        self.addCleanup(self.volume_cleanup, vol_info["id"])

        node_id_list = heketi_node_list(
            self.heketi_client_node, self.heketi_server_url)

        for node_id in node_id_list[:2]:
            device_present = False
            node_info = heketi_node_info(
                self.heketi_client_node, self.heketi_server_url,
                node_id, json=True)

            self.assertNotEqual(
                node_info, False,
                "Heketi node info on node %s failed" % node_id)

            node_ip = node_info["hostnames"]["storage"][0]

            for gluster_server in g.config["gluster_servers"].keys():
                gluster_server_ip = (g.config["gluster_servers"]
                                     [gluster_server]["storage"])
                if gluster_server_ip == node_ip:
                    device_name = (g.config["gluster_servers"][gluster_server]
                                   ["additional_devices"][0])
                    break
            device_addition_info = heketi_device_add(
                self.heketi_client_node, self.heketi_server_url,
                device_name, node_id, json=True)

            self.assertNotEqual(device_addition_info, False,
                                "Device %s addition failed" % device_name)

            node_info_after_addition = heketi_node_info(
                self.heketi_client_node, self.heketi_server_url,
                node_id, json=True)

            self.assertNotEqual(node_info_after_addition, False,
                                "Node info failed for node %s" % node_id)

            self.assertNotEqual(
                node_info_after_addition["devices"], [],
                "No devices in node %s" % node_id)

            for device in node_info_after_addition["devices"]:
                if device["name"] == device_name:
                    device_present = True
                    device_id_list.append(device["id"])
                    break

            self.assertEqual(device_present, True,
                             "device %s not present" % device["id"])

        self.addCleanup(self.detach_devices_attached, device_id_list)

        node_1_id = node_id_list[0]

        node_1_info = heketi_node_info(
                self.heketi_client_node, self.heketi_server_url,
                node_1_id, json=True)

        self.assertNotEqual(node_1_info, False,
                            "Node info failed for node %s" % node_1_id)
        self.assertNotEqual(
            node_1_info["devices"], [],
            "No devices in node %s" % node_1_id)
        device = any([d for d in node_1_info["devices"]
                      if device["id"] != device_id_list[0]])
        device_disable = heketi_device_disable(
            self.heketi_client_node, self.heketi_server_url,
            device["id"])
        self.assertNotEqual(
            device_disable, False,
            "Device %s could not be disabled" % device["id"])
        ret, out, err = heketi_device_remove(
            self.heketi_client_node, self.heketi_server_url,
            device["id"],
            raw_cli_output=True)
        self.assertNotEqual(ret, 0, "Device %s removal successfull")
        msg = "Error: Failed to remove device, error: No " +\
              "Replacement was found for resource requested to be " +\
              "removed"
        self.assertEqual(
            msg, err.strip(),
            "Device %s removal failed due to invalid reason")
        device_enable = heketi_device_enable(
            self.heketi_client_node, self.heketi_server_url,
            device["id"])
        self.assertNotEqual(
            device_enable, False,
            "Device %s could not be enabled" % device["id"])

    @skip("Blocked by BZ-1629889")
    def test_device_remove_basic_validation(self):
        """
        Test to create volume after a device removal and with new device added.
        """

        vol_info = heketi_volume_create(self.heketi_client_node,
                                        self.heketi_server_url,
                                        self.volume_size, json=True)
        self.assertTrue(vol_info, ("Failed to create heketi volume of size %s"
                                   % self.volume_size))
        self.addCleanup(self.volume_cleanup, vol_info['id'])

        self.assertEqual(vol_info['size'], self.volume_size,
                         ("Failed to create volume with default options."
                          "Expected Size: %s, Actual Size: %s"
                          % (self.volume_size, vol_info['size'])))

        # 1. Device addition
        gluster_srvrs = self.gluster_servers

        device_name = (g.config["gluster_servers"][gluster_srvrs[0]]
                       ["additional_devices"][0])
        manage_hostname = (g.config["gluster_servers"]
                           [gluster_srvrs[0]]["manage"])
        # Now, get node id of corresponding hostname
        topo_info = heketi_topology_info(self.heketi_client_node,
                                         self.heketi_server_url,
                                         json=True)

        self.assertNotEqual(
            topo_info["clusters"][0]["nodes"], [],
            "Nodes don't exist, empty cluster")

        node_id = None
        for node in topo_info["clusters"][0]["nodes"]:
            if manage_hostname == node['hostnames']["manage"][0]:
                node_id = node["id"]
                break
        self.assertNotEqual(
            node_id, None,
            "No information about node_id for %s" % manage_hostname)
        self.add_device(device_name, node_id)

        # get other device id for deletion
        topo_info = heketi_topology_info(self.heketi_client_node,
                                         self.heketi_server_url,
                                         json=True)

        self.assertNotEqual(
            topo_info["clusters"][0]["nodes"], [],
            "No information about nodes")

        device_id_flag1 = False
        device_id_flag2 = False

        for node in topo_info["clusters"][0]["nodes"]:
            if node["id"] != node_id:
                continue
            self.assertNotEqual(
                node["devices"], [],
                "Device list empty for node %s" % node_id)
            for device in node["devices"]:
                device_id = device["id"]
                if device_name != device["name"]:
                    device_id_flag1 = True
                else:
                    self.addCleanup(self.detach_devices_attached, device_id)
                    device_id_flag2 = True
                if device_id_flag1 and device_id_flag2:
                    break
            break

        self.detach_devices_attached(device_id)

        # Check whether deleted device removed from topology info.
        topo_info = heketi_topology_info(self.heketi_client_node,
                                         self.heketi_server_url,
                                         json=True)
        match = False
        for device in topo_info["clusters"][0]["nodes"][0]["devices"]:
            if device_id == device["id"]:
                match = True
                break
        self.assertFalse(match, "Device:%s still present in topology"
                         % device_id)

        # Volume creation after device update
        vol_info = heketi_volume_create(self.heketi_client_node,
                                        self.heketi_server_url,
                                        self.volume_size, json=True)
        self.assertTrue(vol_info, ("Failed to create heketi volume of size %s"
                                   % self.volume_size))
        self.addCleanup(self.volume_cleanup, vol_info['id'])