/
test_nexenta.py
778 lines (668 loc) · 30.6 KB
/
test_nexenta.py
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2011 Nexenta Systems, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Unit tests for OpenStack Cinder volume driver
"""
import base64
import urllib2
import mox as mox_lib
from cinder import context
from cinder import db
from cinder import test
from cinder import units
from cinder.volume import configuration as conf
from cinder.volume.drivers import nexenta
from cinder.volume.drivers.nexenta import iscsi
from cinder.volume.drivers.nexenta import jsonrpc
from cinder.volume.drivers.nexenta import nfs
from cinder.volume.drivers.nexenta import utils
class TestNexentaISCSIDriver(test.TestCase):
TEST_VOLUME_NAME = 'volume1'
TEST_VOLUME_NAME2 = 'volume2'
TEST_SNAPSHOT_NAME = 'snapshot1'
TEST_VOLUME_REF = {
'name': TEST_VOLUME_NAME,
'size': 1,
'id': '1',
'status': 'available'
}
TEST_VOLUME_REF2 = {
'name': TEST_VOLUME_NAME2,
'size': 1,
'id': '2',
'status': 'in-use'
}
TEST_SNAPSHOT_REF = {
'name': TEST_SNAPSHOT_NAME,
'volume_name': TEST_VOLUME_NAME,
}
def __init__(self, method):
super(TestNexentaISCSIDriver, self).__init__(method)
def setUp(self):
super(TestNexentaISCSIDriver, self).setUp()
self.configuration = mox_lib.MockObject(conf.Configuration)
self.configuration.nexenta_host = '1.1.1.1'
self.configuration.nexenta_user = 'admin'
self.configuration.nexenta_password = 'nexenta'
self.configuration.nexenta_volume = 'cinder'
self.configuration.nexenta_rest_port = 2000
self.configuration.nexenta_rest_protocol = 'http'
self.configuration.nexenta_iscsi_target_portal_port = 3260
self.configuration.nexenta_target_prefix = 'iqn:'
self.configuration.nexenta_target_group_prefix = 'cinder/'
self.configuration.nexenta_blocksize = '8K'
self.configuration.nexenta_sparse = True
self.configuration.nexenta_rrmgr_compression = 1
self.configuration.nexenta_rrmgr_tcp_buf_size = 1024
self.configuration.nexenta_rrmgr_connections = 2
self.nms_mock = self.mox.CreateMockAnything()
for mod in ['volume', 'zvol', 'iscsitarget', 'appliance',
'stmf', 'scsidisk', 'snapshot']:
setattr(self.nms_mock, mod, self.mox.CreateMockAnything())
self.stubs.Set(jsonrpc, 'NexentaJSONProxy',
lambda *_, **__: self.nms_mock)
self.drv = iscsi.NexentaISCSIDriver(configuration=self.configuration)
self.drv.do_setup({})
def test_setup_error(self):
self.nms_mock.volume.object_exists('cinder').AndReturn(True)
self.mox.ReplayAll()
self.drv.check_for_setup_error()
def test_setup_error_fail(self):
self.nms_mock.volume.object_exists('cinder').AndReturn(False)
self.mox.ReplayAll()
self.assertRaises(LookupError, self.drv.check_for_setup_error)
def test_local_path(self):
self.assertRaises(NotImplementedError, self.drv.local_path, '')
def test_create_volume(self):
self.nms_mock.zvol.create('cinder/volume1', '1G', '8K', True)
self.nms_mock.stmf.list_targets()
self.nms_mock.iscsitarget.create_target({'target_name': 'iqn:volume1'})
self.nms_mock.stmf.list_targetgroups()
self.nms_mock.stmf.create_targetgroup('cinder/volume1')
self.nms_mock.stmf.list_targetgroup_members('cinder/volume1')
self.nms_mock.stmf.add_targetgroup_member('cinder/volume1',
'iqn:volume1')
self.nms_mock.scsidisk.lu_exists('cinder/volume1')
self.nms_mock.scsidisk.create_lu('cinder/volume1', {})
self.nms_mock.scsidisk.lu_shared('cinder/volume1')
self.nms_mock.scsidisk.add_lun_mapping_entry(
'cinder/volume1', {'target_group': 'cinder/volume1', 'lun': '0'})
self.mox.ReplayAll()
self.drv.create_volume(self.TEST_VOLUME_REF)
def test_delete_volume(self):
self.nms_mock.zvol.get_child_props('cinder/volume1',
'origin').AndReturn({})
self.nms_mock.zvol.destroy('cinder/volume1', '')
self.mox.ReplayAll()
self.drv.delete_volume(self.TEST_VOLUME_REF)
self.mox.ResetAll()
c = self.nms_mock.zvol.get_child_props('cinder/volume1', 'origin')
c.AndReturn({'origin': 'cinder/volume0@snapshot'})
self.nms_mock.zvol.destroy('cinder/volume1', '')
self.mox.ReplayAll()
self.drv.delete_volume(self.TEST_VOLUME_REF)
self.mox.ResetAll()
c = self.nms_mock.zvol.get_child_props('cinder/volume1', 'origin')
c.AndReturn({'origin': 'cinder/volume0@cinder-clone-snapshot-1'})
self.nms_mock.zvol.destroy('cinder/volume1', '')
self.nms_mock.snapshot.destroy(
'cinder/volume0@cinder-clone-snapshot-1', '')
self.mox.ReplayAll()
self.drv.delete_volume(self.TEST_VOLUME_REF)
self.mox.ResetAll()
def test_create_cloned_volume(self):
vol = self.TEST_VOLUME_REF2
src_vref = self.TEST_VOLUME_REF
snapshot = {
'volume_name': src_vref['name'],
'name': 'cinder-clone-snapshot-%s' % vol['id'],
}
self.nms_mock.zvol.create_snapshot('cinder/%s' % src_vref['name'],
snapshot['name'], '')
self.nms_mock.zvol.clone('cinder/%s@%s' % (src_vref['name'],
snapshot['name']),
'cinder/%s' % vol['name'])
self.mox.ReplayAll()
self.drv.create_cloned_volume(vol, src_vref)
def test_migrate_volume(self):
volume = self.TEST_VOLUME_REF
host = {
'capabilities': {
'vendor_name': 'Nexenta',
'location_info': 'NexentaISCSIDriver:1.1.1.1:cinder',
'free_capacity_gb': 1
}
}
snapshot = {
'volume_name': volume['name'],
'name': 'cinder-migrate-snapshot-%s' % volume['id'],
}
self.nms_mock.appliance.ssh_list_bindings().AndReturn([])
self.nms_mock.zvol.create_snapshot('cinder/%s' % volume['name'],
snapshot['name'], '')
src = '%(volume)s/%(zvol)s@%(snapshot)s' % {
'volume': 'cinder',
'zvol': volume['name'],
'snapshot': snapshot['name']}
dst = '1.1.1.1:cinder'
cmd = ' '.join(['rrmgr -s zfs -c 1 -q -e -w 1024 -n 2', src, dst])
self.nms_mock.appliance.execute(cmd)
self.nms_mock.snapshot.destroy('cinder/%(volume)s@%(snapshot)s' % {
'volume': volume['name'],
'snapshot': snapshot['name']}, '')
volume_name = 'cinder/%s' % volume['name']
self.nms_mock.zvol.get_child_props(volume_name,
'origin').AndReturn(None)
self.nms_mock.zvol.destroy(volume_name, '')
self.mox.ReplayAll()
self.drv.migrate_volume(None, volume, host)
def test_create_snapshot(self):
self.nms_mock.zvol.create_snapshot('cinder/volume1', 'snapshot1', '')
self.mox.ReplayAll()
self.drv.create_snapshot(self.TEST_SNAPSHOT_REF)
def test_create_volume_from_snapshot(self):
self.nms_mock.zvol.clone('cinder/volume1@snapshot1', 'cinder/volume2')
self.mox.ReplayAll()
self.drv.create_volume_from_snapshot(self.TEST_VOLUME_REF2,
self.TEST_SNAPSHOT_REF)
def test_delete_snapshot(self):
self.nms_mock.snapshot.destroy('cinder/volume1@snapshot1', '')
self.mox.ReplayAll()
self.drv.delete_snapshot(self.TEST_SNAPSHOT_REF)
self.mox.ResetAll()
# Check that exception not raised if snapshot does not exist
mock = self.nms_mock.snapshot.destroy('cinder/volume1@snapshot1', '')
mock.AndRaise(nexenta.NexentaException(
'Snapshot cinder/volume1@snapshot1 does not exist'))
self.mox.ReplayAll()
self.drv.delete_snapshot(self.TEST_SNAPSHOT_REF)
_CREATE_EXPORT_METHODS = [
('stmf', 'list_targets', tuple(), [], False, ),
('iscsitarget', 'create_target', ({'target_name': 'iqn:volume1'},),
u'Unable to create iscsi target\n'
u' iSCSI target iqn.1986-03.com.sun:02:cinder-volume1 already'
u' configured\n'
u' itadm create-target failed with error 17\n', True, ),
('stmf', 'list_targetgroups', tuple(), [], False, ),
('stmf', 'create_targetgroup', ('cinder/volume1',),
u'Unable to create targetgroup: stmfadm: cinder/volume1:'
u' already exists\n', True, ),
('stmf', 'list_targetgroup_members', ('cinder/volume1', ), [],
False, ),
('stmf', 'add_targetgroup_member', ('cinder/volume1', 'iqn:volume1'),
u'Unable to add member to targetgroup: stmfadm:'
u' iqn.1986-03.com.sun:02:cinder-volume1: already exists\n',
True, ),
('scsidisk', 'lu_exists', ('cinder/volume1', ), 0, False, ),
('scsidisk', 'create_lu', ('cinder/volume1', {}),
u"Unable to create lu with zvol 'cinder/volume1':\n"
u" sbdadm: filename /dev/zvol/rdsk/cinder/volume1: in use\n",
True, ),
('scsidisk', 'lu_shared', ('cinder/volume1', ), 0, False, ),
('scsidisk', 'add_lun_mapping_entry', ('cinder/volume1', {
'target_group': 'cinder/volume1', 'lun': '0'}),
u"Unable to add view to zvol 'cinder/volume1' (LUNs in use: ):\n"
u" stmfadm: view entry exists\n", True, ),
]
def _stub_export_method(self, module, method, args, error, raise_exception,
fail=False):
m = getattr(self.nms_mock, module)
m = getattr(m, method)
mock = m(*args)
if raise_exception and fail:
mock.AndRaise(nexenta.NexentaException(error))
else:
mock.AndReturn(error)
def _stub_all_export_methods(self, fail=False):
for params in self._CREATE_EXPORT_METHODS:
self._stub_export_method(*params, fail=fail)
def test_create_export(self):
self._stub_all_export_methods()
self.mox.ReplayAll()
retval = self.drv.create_export({}, self.TEST_VOLUME_REF)
location = '%(host)s:%(port)s,1 %(prefix)s%(volume)s 0' % {
'host': self.configuration.nexenta_host,
'port': self.configuration.nexenta_iscsi_target_portal_port,
'prefix': self.configuration.nexenta_target_prefix,
'volume': self.TEST_VOLUME_NAME
}
self.assertEqual(retval, {'provider_location': location})
def __get_test(i):
def _test_create_export_fail(self):
for params in self._CREATE_EXPORT_METHODS[:i]:
self._stub_export_method(*params)
self._stub_export_method(*self._CREATE_EXPORT_METHODS[i],
fail=True)
self.mox.ReplayAll()
self.assertRaises(nexenta.NexentaException,
self.drv.create_export,
{},
self.TEST_VOLUME_REF)
return _test_create_export_fail
for i in range(len(_CREATE_EXPORT_METHODS)):
if i % 2:
locals()['test_create_export_fail_%d' % i] = __get_test(i)
def test_ensure_export(self):
self._stub_all_export_methods(fail=True)
self.mox.ReplayAll()
self.drv.ensure_export({}, self.TEST_VOLUME_REF)
def test_remove_export(self):
self.nms_mock.scsidisk.delete_lu('cinder/volume1')
self.nms_mock.stmf.destroy_targetgroup('cinder/volume1')
self.nms_mock.iscsitarget.delete_target('iqn:volume1')
self.mox.ReplayAll()
self.drv.remove_export({}, self.TEST_VOLUME_REF)
def test_remove_export_fail_0(self):
self.nms_mock.scsidisk.delete_lu('cinder/volume1')
self.nms_mock.stmf.destroy_targetgroup(
'cinder/volume1').AndRaise(nexenta.NexentaException())
self.nms_mock.iscsitarget.delete_target('iqn:volume1')
self.mox.ReplayAll()
self.drv.remove_export({}, self.TEST_VOLUME_REF)
def test_remove_export_fail_1(self):
self.nms_mock.scsidisk.delete_lu('cinder/volume1')
self.nms_mock.stmf.destroy_targetgroup('cinder/volume1')
self.nms_mock.iscsitarget.delete_target(
'iqn:volume1').AndRaise(nexenta.NexentaException())
self.mox.ReplayAll()
self.drv.remove_export({}, self.TEST_VOLUME_REF)
def test_get_volume_stats(self):
stats = {'size': '5368709120G',
'used': '5368709120G',
'available': '5368709120G',
'health': 'ONLINE'}
self.nms_mock.volume.get_child_props(
self.configuration.nexenta_volume,
'health|size|used|available').AndReturn(stats)
self.mox.ReplayAll()
stats = self.drv.get_volume_stats(True)
self.assertEqual(stats['storage_protocol'], 'iSCSI')
self.assertEqual(stats['total_capacity_gb'], 5368709120.0)
self.assertEqual(stats['free_capacity_gb'], 5368709120.0)
self.assertEqual(stats['reserved_percentage'], 0)
self.assertEqual(stats['QoS_support'], False)
class TestNexentaJSONRPC(test.TestCase):
HOST = 'example.com'
URL = 'http://%s/' % HOST
URL_S = 'https://%s/' % HOST
USER = 'user'
PASSWORD = 'password'
HEADERS = {
'Authorization':
'Basic %s' % base64.b64encode('%s:%s' % (USER, PASSWORD)),
'Content-Type': 'application/json'
}
REQUEST = 'the request'
def setUp(self):
super(TestNexentaJSONRPC, self).setUp()
self.proxy = jsonrpc.NexentaJSONProxy(
'http', self.HOST, 2000, '/', self.USER, self.PASSWORD, auto=True)
self.mox.StubOutWithMock(urllib2, 'Request', True)
self.mox.StubOutWithMock(urllib2, 'urlopen')
self.resp_mock = self.mox.CreateMockAnything()
self.resp_info_mock = self.mox.CreateMockAnything()
self.resp_mock.info().AndReturn(self.resp_info_mock)
urllib2.urlopen(self.REQUEST).AndReturn(self.resp_mock)
def test_call(self):
urllib2.Request(
'http://%s:2000/' % self.HOST,
'{"object": null, "params": ["arg1", "arg2"], "method": null}',
self.HEADERS).AndReturn(self.REQUEST)
self.resp_info_mock.status = ''
self.resp_mock.read().AndReturn(
'{"error": null, "result": "the result"}')
self.mox.ReplayAll()
result = self.proxy('arg1', 'arg2')
self.assertEqual("the result", result)
def test_call_deep(self):
urllib2.Request(
'http://%s:2000/' % self.HOST,
'{"object": "obj1.subobj", "params": ["arg1", "arg2"],'
' "method": "meth"}',
self.HEADERS).AndReturn(self.REQUEST)
self.resp_info_mock.status = ''
self.resp_mock.read().AndReturn(
'{"error": null, "result": "the result"}')
self.mox.ReplayAll()
result = self.proxy.obj1.subobj.meth('arg1', 'arg2')
self.assertEqual("the result", result)
def test_call_auto(self):
urllib2.Request(
'http://%s:2000/' % self.HOST,
'{"object": null, "params": ["arg1", "arg2"], "method": null}',
self.HEADERS).AndReturn(self.REQUEST)
urllib2.Request(
'https://%s:2000/' % self.HOST,
'{"object": null, "params": ["arg1", "arg2"], "method": null}',
self.HEADERS).AndReturn(self.REQUEST)
self.resp_info_mock.status = 'EOF in headers'
self.resp_mock.read().AndReturn(
'{"error": null, "result": "the result"}')
urllib2.urlopen(self.REQUEST).AndReturn(self.resp_mock)
self.mox.ReplayAll()
result = self.proxy('arg1', 'arg2')
self.assertEqual("the result", result)
def test_call_error(self):
urllib2.Request(
'http://%s:2000/' % self.HOST,
'{"object": null, "params": ["arg1", "arg2"], "method": null}',
self.HEADERS).AndReturn(self.REQUEST)
self.resp_info_mock.status = ''
self.resp_mock.read().AndReturn(
'{"error": {"message": "the error"}, "result": "the result"}')
self.mox.ReplayAll()
self.assertRaises(jsonrpc.NexentaJSONException,
self.proxy, 'arg1', 'arg2')
def test_call_fail(self):
urllib2.Request(
'http://%s:2000/' % self.HOST,
'{"object": null, "params": ["arg1", "arg2"], "method": null}',
self.HEADERS).AndReturn(self.REQUEST)
self.resp_info_mock.status = 'EOF in headers'
self.proxy.auto = False
self.mox.ReplayAll()
self.assertRaises(jsonrpc.NexentaJSONException,
self.proxy, 'arg1', 'arg2')
class TestNexentaNfsDriver(test.TestCase):
TEST_EXPORT1 = 'host1:/volumes/stack/share'
TEST_NMS1 = 'http://admin:nexenta@host1:2000'
TEST_EXPORT2 = 'host2:/volumes/stack/share'
TEST_NMS2 = 'http://admin:nexenta@host2:2000'
TEST_EXPORT2_OPTIONS = '-o intr'
TEST_FILE_NAME = 'test.txt'
TEST_SHARES_CONFIG_FILE = '/etc/cinder/nexenta-shares.conf'
TEST_SHARE_SVC = 'svc:/network/nfs/server:default'
TEST_SHARE_OPTS = {
'read_only': '',
'read_write': '*',
'recursive': 'true',
'anonymous_rw': 'true',
'extra_options': 'anon=0',
'root': 'nobody'
}
def _create_volume_db_entry(self):
vol = {
'id': '1',
'size': 1,
'status': 'available',
'provider_location': self.TEST_EXPORT1
}
return db.volume_create(self.ctxt, vol)['id']
def setUp(self):
super(TestNexentaNfsDriver, self).setUp()
self.ctxt = context.get_admin_context()
self.configuration = mox_lib.MockObject(conf.Configuration)
self.configuration.nexenta_shares_config = None
self.configuration.nexenta_mount_point_base = '$state_path/mnt'
self.configuration.nexenta_sparsed_volumes = True
self.configuration.nexenta_volume_compression = 'on'
self.configuration.nfs_mount_point_base = '/mnt/test'
self.configuration.nfs_mount_options = None
self.configuration.nexenta_nms_cache_volroot = False
self.nms_mock = self.mox.CreateMockAnything()
for mod in ('appliance', 'folder', 'server', 'volume', 'netstorsvc',
'snapshot'):
setattr(self.nms_mock, mod, self.mox.CreateMockAnything())
self.nms_mock.__hash__ = lambda *_, **__: 1
self.stubs.Set(jsonrpc, 'NexentaJSONProxy',
lambda *_, **__: self.nms_mock)
self.drv = nfs.NexentaNfsDriver(configuration=self.configuration)
self.drv.shares = {}
self.drv.share2nms = {}
def test_check_for_setup_error(self):
self.drv.share2nms = {
'host1:/volumes/stack/share': self.nms_mock
}
self.nms_mock.server.get_prop('volroot').AndReturn('/volumes')
self.nms_mock.volume.object_exists('stack').AndReturn(True)
self.nms_mock.folder.object_exists('stack/share').AndReturn(True)
share_opts = {
'read_write': '*',
'read_only': '',
'root': 'nobody',
'extra_options': 'anon=0',
'recursive': 'true',
'anonymous_rw': 'true',
}
self.nms_mock.netstorsvc.share_folder(
'svc:/network/nfs/server:default', 'stack/share', share_opts)
self.mox.ReplayAll()
self.drv.check_for_setup_error()
self.mox.ResetAll()
self.nms_mock.server.get_prop('volroot').AndReturn('/volumes')
self.nms_mock.volume.object_exists('stack').AndReturn(False)
self.mox.ReplayAll()
self.assertRaises(LookupError, self.drv.check_for_setup_error)
self.mox.ResetAll()
self.nms_mock.server.get_prop('volroot').AndReturn('/volumes')
self.nms_mock.volume.object_exists('stack').AndReturn(True)
self.nms_mock.folder.object_exists('stack/share').AndReturn(False)
self.mox.ReplayAll()
self.assertRaises(LookupError, self.drv.check_for_setup_error)
def test_initialize_connection(self):
self.drv.shares = {
self.TEST_EXPORT1: None
}
volume = {
'provider_location': self.TEST_EXPORT1,
'name': 'volume'
}
result = self.drv.initialize_connection(volume, None)
self.assertEqual(result['data']['export'],
'%s/volume' % self.TEST_EXPORT1)
def test_do_create_volume(self):
volume = {
'provider_location': self.TEST_EXPORT1,
'size': 1,
'name': 'volume-1'
}
self.drv.shares = {self.TEST_EXPORT1: None}
self.drv.share2nms = {self.TEST_EXPORT1: self.nms_mock}
compression = self.configuration.nexenta_volume_compression
self.nms_mock.server.get_prop('volroot').AndReturn('/volumes')
self.nms_mock.folder.create_with_props(
'stack', 'share/volume-1', {'compression': compression})
self.nms_mock.netstorsvc.share_folder(self.TEST_SHARE_SVC,
'stack/share/volume-1',
self.TEST_SHARE_OPTS)
self.nms_mock.appliance.execute(
'dd if=/dev/zero of=/volumes/stack/share/volume-1/volume bs=1M '
'count=0 seek=1024'
)
self.nms_mock.appliance.execute('chmod ugo+rw '
'/volumes/stack/share/volume-1/volume')
self.mox.ReplayAll()
self.drv._do_create_volume(volume)
self.mox.ResetAll()
self.nms_mock.server.get_prop('volroot').AndReturn('/volumes')
self.nms_mock.folder.create_with_props(
'stack', 'share/volume-1', {'compression': compression})
self.nms_mock.netstorsvc.share_folder(
self.TEST_SHARE_SVC, 'stack/share/volume-1',
self.TEST_SHARE_OPTS).AndRaise(nexenta.NexentaException('-'))
self.nms_mock.folder.destroy('stack/share/volume-1')
self.mox.ReplayAll()
self.assertRaises(nexenta.NexentaException, self.drv._do_create_volume,
volume)
def test_create_sparsed_file(self):
self.nms_mock.appliance.execute('dd if=/dev/zero of=/tmp/path bs=1M '
'count=0 seek=1024')
self.mox.ReplayAll()
self.drv._create_sparsed_file(self.nms_mock, '/tmp/path', 1)
def test_create_regular_file(self):
self.nms_mock.appliance.execute('dd if=/dev/zero of=/tmp/path bs=1M '
'count=1024')
self.mox.ReplayAll()
self.drv._create_regular_file(self.nms_mock, '/tmp/path', 1)
def test_set_rw_permissions_for_all(self):
path = '/tmp/path'
self.nms_mock.appliance.execute('chmod ugo+rw %s' % path)
self.mox.ReplayAll()
self.drv._set_rw_permissions_for_all(self.nms_mock, path)
def test_local_path(self):
volume = {'provider_location': self.TEST_EXPORT1, 'name': 'volume-1'}
path = self.drv.local_path(volume)
self.assertEqual(
path,
'$state_path/mnt/b3f660847a52b29ac330d8555e4ad669/volume-1/volume'
)
def test_remote_path(self):
volume = {'provider_location': self.TEST_EXPORT1, 'name': 'volume-1'}
path = self.drv.remote_path(volume)
self.assertEqual(path, '/volumes/stack/share/volume-1/volume')
def test_share_folder(self):
path = 'stack/share/folder'
self.nms_mock.netstorsvc.share_folder(self.TEST_SHARE_SVC, path,
self.TEST_SHARE_OPTS)
self.mox.ReplayAll()
self.drv._share_folder(self.nms_mock, 'stack', 'share/folder')
def test_load_shares_config(self):
self.drv.configuration.nfs_shares_config = self.TEST_SHARES_CONFIG_FILE
self.mox.StubOutWithMock(self.drv, '_read_config_file')
config_data = [
'%s %s' % (self.TEST_EXPORT1, self.TEST_NMS1),
'# %s %s' % (self.TEST_EXPORT2, self.TEST_NMS2),
'',
'%s %s %s' % (self.TEST_EXPORT2, self.TEST_NMS2,
self.TEST_EXPORT2_OPTIONS)
]
self.drv._read_config_file(self.TEST_SHARES_CONFIG_FILE).\
AndReturn(config_data)
self.mox.ReplayAll()
self.drv._load_shares_config(self.drv.configuration.nfs_shares_config)
self.assertIn(self.TEST_EXPORT1, self.drv.shares)
self.assertIn(self.TEST_EXPORT2, self.drv.shares)
self.assertEqual(len(self.drv.shares), 2)
self.assertIn(self.TEST_EXPORT1, self.drv.share2nms)
self.assertIn(self.TEST_EXPORT2, self.drv.share2nms)
self.assertEqual(len(self.drv.share2nms.keys()), 2)
self.assertEqual(self.drv.shares[self.TEST_EXPORT2],
self.TEST_EXPORT2_OPTIONS)
self.mox.VerifyAll()
def test_get_capacity_info(self):
self.drv.share2nms = {self.TEST_EXPORT1: self.nms_mock}
self.nms_mock.server.get_prop('volroot').AndReturn('/volumes')
self.nms_mock.folder.get_child_props('stack/share', '').AndReturn({
'available': '1G',
'used': '2G'
})
self.mox.ReplayAll()
total, free, allocated = self.drv._get_capacity_info(self.TEST_EXPORT1)
self.assertEqual(total, 3 * units.GiB)
self.assertEqual(free, units.GiB)
self.assertEqual(allocated, 2 * units.GiB)
def test_get_share_datasets(self):
self.drv.share2nms = {self.TEST_EXPORT1: self.nms_mock}
self.nms_mock.server.get_prop('volroot').AndReturn('/volumes')
self.mox.ReplayAll()
volume_name, folder_name = \
self.drv._get_share_datasets(self.TEST_EXPORT1)
self.assertEqual(volume_name, 'stack')
self.assertEqual(folder_name, 'share')
def test_delete_snapshot(self):
self.drv.share2nms = {self.TEST_EXPORT1: self.nms_mock}
self._create_volume_db_entry()
self.nms_mock.server.get_prop('volroot').AndReturn('/volumes')
self.nms_mock.snapshot.destroy('stack/share/volume-1@snapshot1', '')
self.mox.ReplayAll()
self.drv.delete_snapshot({'volume_id': '1', 'name': 'snapshot1'})
self.mox.ResetAll()
# Check that exception not raised if snapshot does not exist on
# NexentaStor appliance.
self.nms_mock.server.get_prop('volroot').AndReturn('/volumes')
mock = self.nms_mock.snapshot.destroy('stack/share/volume-1@snapshot1',
'')
mock.AndRaise(nexenta.NexentaException("Snapshot does not exist"))
self.mox.ReplayAll()
self.drv.delete_snapshot({'volume_id': '1', 'name': 'snapshot1'})
self.mox.ResetAll()
def test_delete_volume(self):
self.drv.share2nms = {self.TEST_EXPORT1: self.nms_mock}
self._create_volume_db_entry()
self.drv._ensure_share_mounted = lambda *_, **__: 0
self.drv._execute = lambda *_, **__: 0
self.nms_mock.server.get_prop('volroot').AndReturn('/volumes')
self.nms_mock.folder.get_child_props('stack/share/volume-1',
'origin').AndReturn(None)
self.nms_mock.folder.destroy('stack/share/volume-1', '-r')
self.mox.ReplayAll()
self.drv.delete_volume({
'id': '1',
'name': 'volume-1',
'provider_location': self.TEST_EXPORT1
})
self.mox.ResetAll()
# Check that exception not raised if folder does not exist on
# NexentaStor appliance.
self.nms_mock.server.get_prop('volroot').AndReturn('/volumes')
self.nms_mock.folder.get_child_props('stack/share/volume-1',
'origin').AndReturn(None)
mock = self.nms_mock.folder.destroy('stack/share/volume-1', '-r')
mock.AndRaise(nexenta.NexentaException("Folder does not exist"))
self.mox.ReplayAll()
self.drv.delete_volume({
'id': '1',
'name': 'volume-1',
'provider_location': self.TEST_EXPORT1
})
self.mox.ResetAll()
class TestNexentaUtils(test.TestCase):
def test_str2size(self):
values_to_test = (
# Test empty value
(None, 0),
('', 0),
('0', 0),
('12', 12),
# Test int and long values
(10, 10),
(long(10), 10),
# Test bytes string
('1b', 1),
('1B', 1),
('1023b', 1023),
('0B', 0),
# Test other units
('1M', units.MiB),
('1.0M', units.MiB),
)
for value, result in values_to_test:
self.assertEquals(utils.str2size(value), result)
# Invalid format value
self.assertRaises(ValueError, utils.str2size, 'A')
def test_str2gib_size(self):
self.assertEqual(utils.str2gib_size('1024M'), 1)
self.assertEqual(utils.str2gib_size('300M'),
300 * units.MiB // units.GiB)
self.assertEqual(utils.str2gib_size('1.2T'),
1.2 * units.TiB // units.GiB)
self.assertRaises(ValueError, utils.str2gib_size, 'A')
def test_parse_nms_url(self):
urls = (
('auto://192.168.1.1/', (True, 'http', 'admin', 'nexenta',
'192.168.1.1', '2000', '/rest/nms/')),
('http://192.168.1.1/', (False, 'http', 'admin', 'nexenta',
'192.168.1.1', '2000', '/rest/nms/')),
('http://192.168.1.1:8080', (False, 'http', 'admin', 'nexenta',
'192.168.1.1', '8080', '/rest/nms/')),
('https://root:password@192.168.1.1:8080',
(False, 'https', 'root', 'password', '192.168.1.1', '8080',
'/rest/nms/')),
)
for url, result in urls:
self.assertEquals(utils.parse_nms_url(url), result)