summaryrefslogtreecommitdiffstats
path: root/tests/functional/heketi/test_heketi_cluster_operations.py
blob: 682399651b5d0c4a73c9e39211e8b65a0c6b8380 (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
import ddt
from glusto.core import Glusto as g
import pytest

from openshiftstoragelibs import baseclass
from openshiftstoragelibs import exceptions
from openshiftstoragelibs import heketi_ops
from openshiftstoragelibs.waiter import Waiter


@ddt.ddt
class TestClusterOperationsTestCases(baseclass.BaseClass):
    """Class for heketi cluster creation related test cases"""

    @pytest.mark.tier1
    @ddt.data("", "block", "file")
    def test_heketi_cluster_create(self, disable_volume_type):
        """Test heketi cluster creation"""
        kwargs = {"json": True}
        if disable_volume_type:
            kwargs.update({disable_volume_type: False})

        # Create heketi cluster
        cluster_info = heketi_ops.heketi_cluster_create(
            self.heketi_client_node, self.heketi_server_url, **kwargs)
        self.addCleanup(
            heketi_ops.heketi_cluster_delete, self.heketi_client_node,
            self.heketi_server_url, cluster_info["id"])

        # Validate block and file options
        err_msg = "Cluster with %s option, unexpectedly set to %s"
        if disable_volume_type:
            self.assertFalse(
                cluster_info[disable_volume_type],
                err_msg % (disable_volume_type, "True"))
        else:
            self.assertTrue(
                cluster_info["block"], err_msg % ("block", "False"))
            self.assertTrue(cluster_info["file"], err_msg % ("file", "False"))

    @pytest.mark.tier1
    def test_heketi_cluster_list(self):
        """Test and validateheketi cluster list operation"""
        # Create heketi cluster
        cluster_info = heketi_ops.heketi_cluster_create(
            self.heketi_client_node, self.heketi_server_url, json=True)
        self.addCleanup(
            heketi_ops.heketi_cluster_delete, self.heketi_client_node,
            self.heketi_server_url, cluster_info["id"])

        # Get heketi cluster list and validate presence of newly
        # created cluster
        cluster_list = heketi_ops.heketi_cluster_list(
            self.heketi_client_node, self.heketi_server_url, json=True)
        err_msg = (
            "Cluster id %s not found in cluster list %s"
            % (cluster_info["id"], cluster_list["clusters"]))
        self.assertIn(cluster_info["id"], cluster_list["clusters"], err_msg)

    @pytest.mark.tier1
    def test_heketi_cluster_info(self):
        """Test and validateheketi cluster info operation"""
        # Create heketi cluster
        cluster_info = heketi_ops.heketi_cluster_create(
            self.heketi_client_node, self.heketi_server_url, json=True)
        self.addCleanup(
            heketi_ops.heketi_cluster_delete, self.heketi_client_node,
            self.heketi_server_url, cluster_info["id"])

        # Get newly created heketi cluster info
        get_cluster_info = heketi_ops.heketi_cluster_info(
            self.heketi_client_node, self.heketi_server_url,
            cluster_info["id"], json=True)

        # Validate newly created heketi cluster info
        params = (
            ("id", cluster_info["id"]),
            ("block", True),
            ("file", True),
            ("blockvolumes", []),
            ("volumes", []),
            ("nodes", []))
        for param, value in params:
            self.assertEqual(get_cluster_info[param], value)

    @pytest.mark.tier1
    def test_heketi_cluster_delete(self):
        """Test and validateheketi cluster delete operation"""
        # Create heketi cluster
        cluster_info = heketi_ops.heketi_cluster_create(
            self.heketi_client_node, self.heketi_server_url, json=True)

        # Delete newly created cluster
        heketi_ops.heketi_cluster_delete(
            self.heketi_client_node, self.heketi_server_url,
            cluster_info["id"])

        # Get heketi cluster list and check for absence of deleted cluster
        cluster_list = heketi_ops.heketi_cluster_list(
            self.heketi_client_node, self.heketi_server_url, json=True)
        err_msg = (
            "Cluster id %s was not expected in cluster list %s"
            % (cluster_info["id"], cluster_list["clusters"]))
        self.assertNotIn(cluster_info["id"], cluster_list["clusters"], err_msg)

    @pytest.mark.tier1
    def test_create_heketi_cluster_and_add_node(self):
        """Test heketi node add to a newly created cluster"""
        storage_host_info = g.config.get("additional_gluster_servers")
        if not storage_host_info:
            self.skipTest(
                "Skip test case as 'additional_gluster_servers' option is "
                "not provided in config file")

        storage_host_info = list(storage_host_info.values())[0]
        try:
            storage_hostname = storage_host_info["manage"]
            storage_ip = storage_host_info["storage"]
            storage_device = storage_host_info["devices"][0]
        except KeyError:
            msg = ("Config options 'additional_gluster_servers.manage' "
                   "'additional_gluster_servers.storage' and "
                   "'additional_gluster_servers.devices' "
                   "must be set.")
            g.log.error(msg)
            raise exceptions.ConfigError(msg)

        h_client, h_server = self.heketi_client_node, self.heketi_server_url
        storage_zone = 1

        cluster_id = heketi_ops.heketi_cluster_create(
            self.heketi_client_node, self.heketi_server_url, json=True)["id"]
        self.addCleanup(
            heketi_ops.heketi_cluster_delete, self.heketi_client_node,
            self.heketi_server_url, cluster_id)

        self.configure_node_to_run_gluster(storage_hostname)

        heketi_node_info = heketi_ops.heketi_node_add(
            h_client, h_server, storage_zone, cluster_id,
            storage_hostname, storage_ip, json=True)
        heketi_node_id = heketi_node_info["id"]
        self.addCleanup(
            heketi_ops.heketi_node_delete, h_client, h_server, heketi_node_id)
        self.addCleanup(
            heketi_ops.heketi_node_remove, h_client, h_server, heketi_node_id)
        self.addCleanup(
            heketi_ops.heketi_node_disable, h_client, h_server, heketi_node_id)
        self.assertEqual(
            heketi_node_info["cluster"], cluster_id,
            "Node got added in unexpected cluster exp: %s, act: %s" % (
                cluster_id, heketi_node_info["cluster"]))

        heketi_ops.heketi_device_add(
            h_client, h_server, storage_device, heketi_node_id)
        heketi_node_info = heketi_ops.heketi_node_info(
            h_client, h_server, heketi_node_id, json=True)
        device_id = None
        for device in heketi_node_info["devices"]:
            if device["name"] == storage_device:
                device_id = device["id"]
                break
        err_msg = ("Failed to add device %s on node %s" % (
            storage_device, heketi_node_id))
        self.assertTrue(device_id, err_msg)

        self.addCleanup(
            heketi_ops.heketi_device_delete, h_client, h_server, device_id)
        self.addCleanup(
            heketi_ops.heketi_device_remove, h_client, h_server, device_id)
        self.addCleanup(
            heketi_ops.heketi_device_disable, h_client, h_server, device_id)

        cluster_info = heketi_ops.heketi_cluster_info(
            h_client, h_server, cluster_id, json=True)
        self.assertIn(
            heketi_node_info["id"], cluster_info["nodes"],
            "Newly added node %s not found in cluster %s, cluster info %s" % (
                heketi_node_info["id"], cluster_id, cluster_info))

        topology_info = heketi_ops.heketi_topology_info(
            h_client, h_server, json=True)

        cluster_details = [
            cluster for cluster in topology_info["clusters"]
            if cluster["id"] == cluster_id]
        err_msg = "Cluster details for id '%s' not found" % cluster_id
        self.assertTrue(cluster_details, err_msg)
        err_msg = ("Multiple clusters with same id '%s' found %s" % (
                   cluster_id, cluster_details))
        self.assertEqual(len(cluster_details), 1, err_msg)

        node_details = [
            node for node in cluster_details[0]["nodes"]
            if node["id"] == heketi_node_id]
        err_msg = "Node details for id '%s' not found" % heketi_node_id
        self.assertTrue(node_details, err_msg)
        err_msg = ("Multiple nodes with same id '%s' found %s" % (
                   heketi_node_id, node_details))
        self.assertEqual(len(node_details), 1, err_msg)

        err_msg = "Unexpected %s found '%s', expected '%s'"
        exp_storage_hostname = node_details[0]["hostnames"]["manage"][0]
        self.assertEqual(
            exp_storage_hostname, storage_hostname,
            err_msg % ("hostname", exp_storage_hostname, storage_hostname,))
        exp_storage_ip = node_details[0]["hostnames"]["storage"][0]
        self.assertEqual(
            exp_storage_ip, storage_ip,
            err_msg % ("IP address", exp_storage_ip, storage_ip))
        zone = node_details[0]["zone"]
        self.assertEqual(
            zone, storage_zone, err_msg % ("zone", zone, storage_zone))

    @pytest.mark.tier1
    def test_heketi_server_operations_cleanup_on_idle_setup(self):
        """Run heketi db clean up on an idle setup"""
        h_node, h_url = self.heketi_client_node, self.heketi_server_url
        err_msg = "There should not be any pending operations list {}"

        # Verify the server operations
        for waiter_add in Waiter(300, 20):
            initial_ops = heketi_ops.heketi_server_operations_list(
                h_node, h_url)
            if not initial_ops:
                break
        if waiter_add.expired and initial_ops:
            self.skipTest(err_msg.format(initial_ops))

        # Run cleanup
        cleanup = heketi_ops.heketi_server_operation_cleanup(h_node, h_url)
        self.assertFalse(
            cleanup, "Cleanup command failed with message {}".format(cleanup))

        # Verify the server operations
        final_ops = heketi_ops.heketi_server_operations_list(h_node, h_url)
        self.assertFalse(final_ops, err_msg.format(final_ops))