summaryrefslogtreecommitdiffstats
path: root/tests/functional/common/heketi/test_volume_multi_req.py
blob: f6b0fcf66d24db78d3c66d94831029d1cce2f3ba (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
"""Test cases that create and delete multiple volumes.
"""

import contextlib
import random
import threading
import time

import ddt
import yaml

from glusto.core import Glusto as g

from cnslibs.common.baseclass import BaseClass
from cnslibs.common.heketi_ops import (
    heketi_volume_list)
from cnslibs.common.naming import (
    make_unique_label, extract_method_name)
from cnslibs.common.openshift_ops import (
    oc_create, oc_delete, oc_get_pvc, oc_get_pv, oc_get_all_pvs)
from cnslibs.common.waiter import Waiter


def build_storage_class(name, resturl, restuser='foo', restuserkey='foo'):
    """Build s simple structure for a storage class.
    """
    return {
        'apiVersion': 'storage.k8s.io/v1beta1',
        'kind': 'StorageClass',
        'provisioner': 'kubernetes.io/glusterfs',
        'metadata': {
            'name': name,
        },
        'parameters': {
            'resturl': resturl,
            'restuser': restuser,
            'restuserkey': restuserkey,
        }
    }


def build_pvc(name, storageclass, size, accessmodes=None):
    """Build a simple structture for a PVC defintion.
    """
    annotations = {
        'volume.beta.kubernetes.io/storage-class': storageclass,
    }
    accessmodes = accessmodes if accessmodes else ['ReadWriteOnce']
    if not isinstance(size, str):
        size = '%dGi' % size
    return {
        'apiVersion': 'v1',
        'kind': 'PersistentVolumeClaim',
        'metadata': {
            'name': name,
            'annotations': annotations,
        },
        'spec': {
            'accessModes': accessmodes,
            'resources': {
                'requests': {'storage': size},
            }
        }
    }


@contextlib.contextmanager
def temp_config(ocp_node, cfg):
    """Context manager to help define YAML files on the remote node
    that can be in turn fed to 'oc create'. Must be used as a context
    manager (with-statement).

    Example:
        >>> d = {'foo': True, 'bar': 22, 'baz': [1, 5, 9]}
        >>> with temp_config(node, d) as fpath:
        ...     func_that_takes_a_path(fpath)

        Here, the data dictionary `d` is serialized to YAML and written
        to a temporary file at `fpath`. Then, `fpath` can be used by
        a function that takes a file path. When the context manager exits
        the temporary file is automatically cleaned up.

    Args:
        ocp_node (str): The node to create the temp file on.
        cfg (dict): A data structure to be converted to YAML and
            saved in a tempfile on the node.
    Returns:
        str: A path to a temporary file.
    """
    conn = g.rpyc_get_connection(ocp_node, user="root")
    tmp = conn.modules.tempfile.NamedTemporaryFile()
    try:
        tmp.write(yaml.safe_dump(cfg))
        tmp.flush()
        filename = tmp.name
        yield filename
    finally:
        tmp.close()


def wait_for_claim(ocp_node, pvc_name, timeout=60, interval=2):
    """Wait for a claim to be created & bound up to the given timeout.
    """
    for w in Waiter(timeout, interval):
        sts = oc_get_pvc(ocp_node, pvc_name)
        if sts and sts.get('status', {}).get('phase') == 'Bound':
            return sts
    raise AssertionError('wait_for_claim on pvc %s timed out'
                         % (pvc_name,))


def wait_for_sc_unused(ocp_node, sc_name, timeout=60, interval=1):
    for w in Waiter(timeout, interval):
        sts = oc_get_all_pvs(ocp_node)
        items = (sts and sts.get('items')) or []
        if not any(i.get('spec', {}).get('storageClassName') == sc_name
                   for i in items):
            return
    raise AssertionError('wait_for_sc_unused on %s timed out'
                         % (sc_name,))


def delete_storageclass(ocp_node, sc_name, timeout=120):
    wait_for_sc_unused(ocp_node, sc_name, timeout)
    oc_delete(ocp_node, 'storageclass', sc_name)


class ClaimInfo(object):
    """Helper class to organize data as we go from PVC to PV to
    volume w/in heketi.
    """
    pvc_name = None
    vol_name = None
    vol_uuid = None
    sc_name = None
    req = None
    info = None
    pv_info = None

    def __init__(self, name, storageclass, size):
        self.pvc_name = name
        self.req = build_pvc(
            name=self.pvc_name,
            storageclass=storageclass,
            size=size)

    def create_pvc(self, ocp_node):
        assert self.req
        with temp_config(ocp_node, self.req) as tmpfn:
            oc_create(ocp_node, tmpfn)

    def update_pvc_info(self, ocp_node, timeout=60):
        self.info = wait_for_claim(ocp_node, self.pvc_name, timeout)

    def delete_pvc(self, ocp_node):
        oc_delete(ocp_node, 'pvc', self.pvc_name)

    def update_pv_info(self, ocp_node):
        self.pv_info = oc_get_pv(ocp_node, self.volumeName)

    @property
    def volumeName(self):
        return self.info.get('spec', {}).get('volumeName')

    @property
    def heketiVolumeName(self):
        return self.pv_info.get('spec', {}).get('glusterfs', {}).get('path')


def _heketi_vols(ocp_node, url):
    # Unfortunately, getting json from heketi-cli only gets the ids
    # To get a mapping of ids & volume names without a lot of
    # back and forth between the test and the ocp_node we end up having
    # to scrape the output of 'volume list'
    # TODO: This probably should be made into a utility function
    out = heketi_volume_list(ocp_node, url, json=False)
    res = []
    for line in out.splitlines():
        if not line.startswith('Id:'):
            continue
        row = {}
        for section in line.split():
            if ':' in section:
                key, value = section.split(':', 1)
                row[key.lower()] = value.strip()
        res.append(row)
    return res


def _heketi_name_id_map(vols):
    return {vol['name']: vol['id'] for vol in vols}


@ddt.ddt
class TestVolumeMultiReq(BaseClass):
    def setUp(self):
        super(TestVolumeMultiReq, self).setUp()
        self.volcount = self._count_vols()

    def wait_to_settle(self, timeout=120, interval=1):
        # This was originally going to be a tearDown, but oddly enough
        # tearDown is called *before* the cleanup functions, so it
        # could never succeed. This needs to be added as a cleanup
        # function first so that we run after our test's other cleanup
        # functions but before we go on to the next test in order
        # to prevent the async cleanups in kubernetes from steping
        # on the next test's "toes".
        for w in Waiter(timeout):
            nvols = self._count_vols()
            if nvols == self.volcount:
                return
        raise AssertionError(
            'wait for volume count to settle timed out')

    def _count_vols(self):
        ocp_node = g.config['ocp_servers']['master'].keys()[0]
        return len(_heketi_vols(ocp_node, self.heketi_server_url))

    def test_simple_serial_vol_create(self):
        """Test that serially creating PVCs causes heketi to add volumes.
        """
        self.addCleanup(self.wait_to_settle)
        # TODO A nice thing to add to this test would be to also verify
        # the gluster volumes also exist.
        tname = make_unique_label(extract_method_name(self.id()))
        ocp_node = g.config['ocp_servers']['master'].keys()[0]
        # deploy a temporary storage class
        sc = build_storage_class(
            name=tname,
            resturl=self.heketi_server_url,
            restuser=self.heketi_cli_user,
            restuserkey=self.heketi_cli_key)
        with temp_config(ocp_node, sc) as tmpfn:
            oc_create(ocp_node, tmpfn)
        self.addCleanup(delete_storageclass, ocp_node, tname)
        orig_vols = _heketi_name_id_map(
            _heketi_vols(ocp_node, self.heketi_server_url))

        # deploy a persistent volume claim
        c1 = ClaimInfo(
            name='-'.join((tname, 'pvc1')),
            storageclass=tname,
            size=2)
        c1.create_pvc(ocp_node)
        self.addCleanup(c1.delete_pvc, ocp_node)
        c1.update_pvc_info(ocp_node)
        # verify volume exists
        self.assertTrue(c1.volumeName)
        c1.update_pv_info(ocp_node)
        self.assertTrue(c1.heketiVolumeName)

        # verify this is a new volume to heketi
        now_vols = _heketi_name_id_map(
            _heketi_vols(ocp_node, self.heketi_server_url))
        self.assertEqual(len(orig_vols) + 1, len(now_vols))
        self.assertIn(c1.heketiVolumeName, now_vols)
        self.assertNotIn(c1.heketiVolumeName, orig_vols)

        # deploy a 2nd pvc
        c2 = ClaimInfo(
            name='-'.join((tname, 'pvc2')),
            storageclass=tname,
            size=2)
        c2.create_pvc(ocp_node)
        self.addCleanup(c2.delete_pvc, ocp_node)
        c2.update_pvc_info(ocp_node)
        # verify volume exists
        self.assertTrue(c2.volumeName)
        c2.update_pv_info(ocp_node)
        self.assertTrue(c2.heketiVolumeName)

        # verify this is a new volume to heketi
        now_vols = _heketi_name_id_map(
            _heketi_vols(ocp_node, self.heketi_server_url))
        self.assertEqual(len(orig_vols) + 2, len(now_vols))
        self.assertIn(c2.heketiVolumeName, now_vols)
        self.assertNotIn(c2.heketiVolumeName, orig_vols)

    def test_multiple_vol_create(self):
        """Test creating two volumes via PVCs with no waiting between
        the PVC requests.

        We do wait after all the PVCs are submitted to get statuses.
        """
        self.addCleanup(self.wait_to_settle)
        tname = make_unique_label(extract_method_name(self.id()))
        ocp_node = g.config['ocp_servers']['master'].keys()[0]
        # deploy a temporary storage class
        sc = build_storage_class(
            name=tname,
            resturl=self.heketi_server_url,
            restuser=self.heketi_cli_user,
            restuserkey=self.heketi_cli_key)
        with temp_config(ocp_node, sc) as tmpfn:
            oc_create(ocp_node, tmpfn)
        self.addCleanup(delete_storageclass, ocp_node, tname)

        # deploy two persistent volume claims
        c1 = ClaimInfo(
            name='-'.join((tname, 'pvc1')),
            storageclass=tname,
            size=2)
        c1.create_pvc(ocp_node)
        self.addCleanup(c1.delete_pvc, ocp_node)
        c2 = ClaimInfo(
            name='-'.join((tname, 'pvc2')),
            storageclass=tname,
            size=2)
        c2.create_pvc(ocp_node)
        self.addCleanup(c2.delete_pvc, ocp_node)

        # wait for pvcs/volumes to complete
        c1.update_pvc_info(ocp_node)
        c2.update_pvc_info(ocp_node)
        now_vols = _heketi_name_id_map(
            _heketi_vols(ocp_node, self.heketi_server_url))

        # verify first volume exists
        self.assertTrue(c1.volumeName)
        c1.update_pv_info(ocp_node)
        self.assertTrue(c1.heketiVolumeName)
        # verify this volume in heketi
        self.assertIn(c1.heketiVolumeName, now_vols)

        # verify second volume exists
        self.assertTrue(c2.volumeName)
        c2.update_pv_info(ocp_node)
        self.assertTrue(c2.heketiVolumeName)
        # verify this volume in heketi
        self.assertIn(c2.heketiVolumeName, now_vols)

    # NOTE(jjm): I've noticed that on the system I'm using (RHEL7).
    # with count=8 things start to back up a bit.
    # I needed to increase some timeouts to get this to pass.
    @ddt.data(2, 4, 8)
    def test_threaded_multi_request(self, count):
        """Test creating volumes via PVCs where the pvc create
        commands are launched in parallell via threads.
        """
        self.addCleanup(self.wait_to_settle)
        tname = make_unique_label(extract_method_name(self.id()))
        ocp_node = g.config['ocp_servers']['master'].keys()[0]
        # deploy a temporary storage class
        sc = build_storage_class(
            name=tname,
            resturl=self.heketi_server_url,
            restuser=self.heketi_cli_user,
            restuserkey=self.heketi_cli_key)
        with temp_config(ocp_node, sc) as tmpfn:
            oc_create(ocp_node, tmpfn)
        self.addCleanup(delete_storageclass, ocp_node, tname)

        # prepare the persistent volume claims
        claims = [
            ClaimInfo(name='-'.join((tname, ('pvc%d' % n))),
                      storageclass=tname,
                      size=2)
            for n in range(count)]

        # create a "bunch" of pvc all at once
        def create(ci):
            ci.create_pvc(ocp_node)
            self.addCleanup(ci.delete_pvc, ocp_node)
        threads = [
            threading.Thread(target=create, args=[c])
            for c in claims]
        for t in threads:
            t.start()
        for t in threads:
            t.join()

        for c in claims:
            c.update_pvc_info(ocp_node, timeout=120)
        now_vols = _heketi_name_id_map(
            _heketi_vols(ocp_node, self.heketi_server_url))
        for c in claims:
            c.update_pv_info(ocp_node)
            self.assertIn(c.heketiVolumeName, now_vols)

    def test_create_delete_volumes_concurrently(self):
        """Test creating volume when "other processes" are creating
        and deleting other volumes in the background.
        """
        self.addCleanup(self.wait_to_settle)
        tname = make_unique_label(extract_method_name(self.id()))
        ocp_node = g.config['ocp_servers']['master'].keys()[0]
        # deploy a temporary storage class
        sc = build_storage_class(
            name=tname,
            resturl=self.heketi_server_url,
            restuser=self.heketi_cli_user,
            restuserkey=self.heketi_cli_key)
        with temp_config(ocp_node, sc) as tmpfn:
            oc_create(ocp_node, tmpfn)
        self.addCleanup(delete_storageclass, ocp_node, tname)

        # make this a condition
        done = threading.Event()
        short_tc_name = "volumes-concurrently"

        def background_ops():
            subname = make_unique_label(short_tc_name)
            for i, w in enumerate(Waiter(60 * 60)):
                time.sleep(random.randint(1, 10) * 0.1)
                c = ClaimInfo(
                    name='{}-{}'.format(subname, i),
                    storageclass=tname,
                    size=2)
                c.create_pvc(ocp_node)
                time.sleep(1)
                c.update_pvc_info(ocp_node, timeout=120)
                c.update_pv_info(ocp_node)
                time.sleep(random.randint(1, 10) * 0.1)
                c.delete_pvc(ocp_node)
                if done.is_set():
                    break
        failures = []

        def checked_background_ops():
            try:
                background_ops()
            except Exception as e:
                failures.append(e)

        count = 4
        threads = [
            threading.Thread(target=checked_background_ops)
            for _ in range(count)]
        self.addCleanup(done.set)
        for t in threads:
            t.start()

        # let the threads start doing their own stuff
        time.sleep(10)

        # deploy two persistent volume claims
        c1 = ClaimInfo(
            name='-'.join((short_tc_name, 'pvc1')),
            storageclass=tname,
            size=2)
        c1.create_pvc(ocp_node)
        self.addCleanup(c1.delete_pvc, ocp_node)
        c2 = ClaimInfo(
            name='-'.join((short_tc_name, 'pvc2')),
            storageclass=tname,
            size=2)
        c2.create_pvc(ocp_node)
        self.addCleanup(c2.delete_pvc, ocp_node)

        # wait for pvcs/volumes to complete
        c1.update_pvc_info(ocp_node, timeout=120)
        c2.update_pvc_info(ocp_node, timeout=120)

        # verify first volume exists
        self.assertTrue(c1.volumeName)
        c1.update_pv_info(ocp_node)
        self.assertTrue(c1.heketiVolumeName)
        # verify this volume in heketi
        now_vols = _heketi_name_id_map(
            _heketi_vols(ocp_node, self.heketi_server_url))
        self.assertIn(c1.heketiVolumeName, now_vols)

        # verify second volume exists
        self.assertTrue(c2.volumeName)
        c2.update_pv_info(ocp_node)
        self.assertTrue(c2.heketiVolumeName)
        # verify this volume in heketi
        self.assertIn(c2.heketiVolumeName, now_vols)

        # clean up the background threads
        done.set()
        for t in threads:
            t.join()
        self.assertFalse(failures)