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
|
# Copyright 2013 Mirantis, Inc.
# Copyright 2013 OpenStack Foundation
#
# 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.
from cinderclient import exceptions as cinder_exception
import mock
from nova import context
from nova import exception
from nova import test
from nova.volume import cinder
class FakeCinderClient(object):
class Volumes(object):
def get(self, volume_id):
return {'id': volume_id}
def list(self, detailed):
return [{'id': 'id1'}, {'id': 'id2'}]
def create(self, *args, **kwargs):
return {'id': 'created_id'}
def __getattr__(self, item):
return None
def __init__(self):
self.volumes = self.Volumes()
self.volume_snapshots = self.volumes
class FakeVolume(object):
def __init__(self, dict=dict()):
self.id = dict.get('id') or '1234'
self.status = dict.get('status') or 'available'
self.size = dict.get('size') or 1
self.availability_zone = dict.get('availability_zone') or 'cinder'
self.created_at = dict.get('created_at')
self.attach_time = dict.get('attach_time')
self.mountpoint = dict.get('mountpoint')
self.display_name = dict.get('display_name') or 'volume-' + self.id
self.display_description = dict.get('display_description') or 'fake'
self.volume_type_id = dict.get('volume_type_id')
self.snapshot_id = dict.get('snapshot_id')
self.metadata = dict.get('volume_metadata') or {}
class CinderApiTestCase(test.NoDBTestCase):
def setUp(self):
super(CinderApiTestCase, self).setUp()
self.api = cinder.API()
self.cinderclient = FakeCinderClient()
self.ctx = context.get_admin_context()
self.mox.StubOutWithMock(cinder, 'cinderclient')
self.mox.StubOutWithMock(cinder, '_untranslate_volume_summary_view')
self.mox.StubOutWithMock(cinder, '_untranslate_snapshot_summary_view')
self.mox.StubOutWithMock(cinder, 'get_cinder_client_version')
def test_get(self):
volume_id = 'volume_id1'
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
cinder._untranslate_volume_summary_view(self.ctx, {'id': 'volume_id1'})
self.mox.ReplayAll()
self.api.get(self.ctx, volume_id)
def test_get_failed(self):
volume_id = 'volume_id'
cinder.cinderclient(self.ctx).AndRaise(cinder_exception.NotFound(''))
cinder.cinderclient(self.ctx).AndRaise(cinder_exception.BadRequest(''))
cinder.cinderclient(self.ctx).AndRaise(
cinder_exception.ConnectionError(''))
self.mox.ReplayAll()
self.assertRaises(exception.VolumeNotFound,
self.api.get, self.ctx, volume_id)
self.assertRaises(exception.InvalidInput,
self.api.get, self.ctx, volume_id)
self.assertRaises(exception.CinderConnectionFailed,
self.api.get, self.ctx, volume_id)
def test_create(self):
cinder.get_cinder_client_version(self.ctx).AndReturn('2')
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
cinder._untranslate_volume_summary_view(self.ctx, {'id': 'created_id'})
self.mox.ReplayAll()
self.api.create(self.ctx, 1, '', '')
def test_create_failed(self):
cinder.get_cinder_client_version(self.ctx).AndReturn('2')
cinder.cinderclient(self.ctx).AndRaise(cinder_exception.BadRequest(''))
self.mox.ReplayAll()
self.assertRaises(exception.InvalidInput,
self.api.create, self.ctx, 1, '', '')
@mock.patch('nova.volume.cinder.get_cinder_client_version')
@mock.patch('nova.volume.cinder.cinderclient')
def test_create_over_quota_failed(self, mock_cinderclient,
mock_get_version):
mock_get_version.return_value = '2'
mock_cinderclient.return_value.volumes.create.side_effect = (
cinder_exception.OverLimit(413))
self.assertRaises(exception.OverQuota, self.api.create, self.ctx,
1, '', '')
mock_cinderclient.return_value.volumes.create.assert_called_once_with(
1, user_id=None, imageRef=None, availability_zone=None,
volume_type=None, description='', snapshot_id=None, name='',
project_id=None, metadata=None)
def test_get_all(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
cinder._untranslate_volume_summary_view(self.ctx,
{'id': 'id1'}).AndReturn('id1')
cinder._untranslate_volume_summary_view(self.ctx,
{'id': 'id2'}).AndReturn('id2')
self.mox.ReplayAll()
self.assertEqual(['id1', 'id2'], self.api.get_all(self.ctx))
def test_check_attach_volume_status_error(self):
volume = {'status': 'error'}
self.assertRaises(exception.InvalidVolume,
self.api.check_attach, self.ctx, volume)
def test_check_attach_volume_already_attached(self):
volume = {'status': 'available'}
volume['attach_status'] = "attached"
self.assertRaises(exception.InvalidVolume,
self.api.check_attach, self.ctx, volume)
def test_check_attach_availability_zone_differs(self):
volume = {'status': 'available'}
volume['attach_status'] = "detached"
instance = {'availability_zone': 'zone1', 'host': 'fakehost'}
with mock.patch.object(cinder.az, 'get_instance_availability_zone',
side_effect=lambda context,
instance: 'zone1') as mock_get_instance_az:
cinder.CONF.set_override('cross_az_attach', False, group='cinder')
volume['availability_zone'] = 'zone1'
self.assertIsNone(self.api.check_attach(self.ctx,
volume, instance))
mock_get_instance_az.assert_called_once_with(self.ctx, instance)
mock_get_instance_az.reset_mock()
volume['availability_zone'] = 'zone2'
self.assertRaises(exception.InvalidVolume,
self.api.check_attach, self.ctx, volume, instance)
mock_get_instance_az.assert_called_once_with(self.ctx, instance)
mock_get_instance_az.reset_mock()
del instance['host']
volume['availability_zone'] = 'zone1'
self.assertIsNone(self.api.check_attach(
self.ctx, volume, instance))
self.assertFalse(mock_get_instance_az.called)
volume['availability_zone'] = 'zone2'
self.assertRaises(exception.InvalidVolume,
self.api.check_attach, self.ctx, volume, instance)
self.assertFalse(mock_get_instance_az.called)
cinder.CONF.reset()
def test_check_attach(self):
volume = {'status': 'available'}
volume['attach_status'] = "detached"
volume['availability_zone'] = 'zone1'
instance = {'availability_zone': 'zone1', 'host': 'fakehost'}
cinder.CONF.set_override('cross_az_attach', False, group='cinder')
with mock.patch.object(cinder.az, 'get_instance_availability_zone',
side_effect=lambda context, instance: 'zone1'):
self.assertIsNone(self.api.check_attach(
self.ctx, volume, instance))
cinder.CONF.reset()
def test_check_detach(self):
volume = {'status': 'available'}
self.assertRaises(exception.InvalidVolume,
self.api.check_detach, self.ctx, volume)
volume['status'] = 'non-available'
self.assertIsNone(self.api.check_detach(self.ctx, volume))
def test_reserve_volume(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
self.mox.StubOutWithMock(self.cinderclient.volumes,
'reserve')
self.cinderclient.volumes.reserve('id1')
self.mox.ReplayAll()
self.api.reserve_volume(self.ctx, 'id1')
def test_unreserve_volume(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
self.mox.StubOutWithMock(self.cinderclient.volumes,
'unreserve')
self.cinderclient.volumes.unreserve('id1')
self.mox.ReplayAll()
self.api.unreserve_volume(self.ctx, 'id1')
def test_begin_detaching(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
self.mox.StubOutWithMock(self.cinderclient.volumes,
'begin_detaching')
self.cinderclient.volumes.begin_detaching('id1')
self.mox.ReplayAll()
self.api.begin_detaching(self.ctx, 'id1')
def test_roll_detaching(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
self.mox.StubOutWithMock(self.cinderclient.volumes,
'roll_detaching')
self.cinderclient.volumes.roll_detaching('id1')
self.mox.ReplayAll()
self.api.roll_detaching(self.ctx, 'id1')
@mock.patch('nova.volume.cinder.cinderclient')
def test_attach(self, mock_cinderclient):
mock_volumes = mock.MagicMock()
mock_cinderclient.return_value = mock.MagicMock(volumes=mock_volumes)
self.api.attach(self.ctx, 'id1', 'uuid', 'point')
mock_cinderclient.assert_called_once_with(self.ctx)
mock_volumes.attach.assert_called_once_with('id1', 'uuid', 'point',
mode='rw')
@mock.patch('nova.volume.cinder.cinderclient')
def test_attach_with_mode(self, mock_cinderclient):
mock_volumes = mock.MagicMock()
mock_cinderclient.return_value = mock.MagicMock(volumes=mock_volumes)
self.api.attach(self.ctx, 'id1', 'uuid', 'point', mode='ro')
mock_cinderclient.assert_called_once_with(self.ctx)
mock_volumes.attach.assert_called_once_with('id1', 'uuid', 'point',
mode='ro')
def test_detach(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
self.mox.StubOutWithMock(self.cinderclient.volumes,
'detach')
self.cinderclient.volumes.detach('id1')
self.mox.ReplayAll()
self.api.detach(self.ctx, 'id1')
def test_initialize_connection(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
self.mox.StubOutWithMock(self.cinderclient.volumes,
'initialize_connection')
self.cinderclient.volumes.initialize_connection('id1', 'connector')
self.mox.ReplayAll()
self.api.initialize_connection(self.ctx, 'id1', 'connector')
def test_terminate_connection(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
self.mox.StubOutWithMock(self.cinderclient.volumes,
'terminate_connection')
self.cinderclient.volumes.terminate_connection('id1', 'connector')
self.mox.ReplayAll()
self.api.terminate_connection(self.ctx, 'id1', 'connector')
def test_delete(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
self.mox.StubOutWithMock(self.cinderclient.volumes,
'delete')
self.cinderclient.volumes.delete('id1')
self.mox.ReplayAll()
self.api.delete(self.ctx, 'id1')
def test_update(self):
self.assertRaises(NotImplementedError,
self.api.update, self.ctx, '', '')
def test_get_snapshot(self):
snapshot_id = 'snapshot_id'
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
cinder._untranslate_snapshot_summary_view(self.ctx,
{'id': snapshot_id})
self.mox.ReplayAll()
self.api.get_snapshot(self.ctx, snapshot_id)
def test_get_snapshot_failed(self):
snapshot_id = 'snapshot_id'
cinder.cinderclient(self.ctx).AndRaise(cinder_exception.NotFound(''))
cinder.cinderclient(self.ctx).AndRaise(
cinder_exception.ConnectionError(''))
self.mox.ReplayAll()
self.assertRaises(exception.SnapshotNotFound,
self.api.get_snapshot, self.ctx, snapshot_id)
self.assertRaises(exception.CinderConnectionFailed,
self.api.get_snapshot, self.ctx, snapshot_id)
def test_get_all_snapshots(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
cinder._untranslate_snapshot_summary_view(self.ctx,
{'id': 'id1'}).AndReturn('id1')
cinder._untranslate_snapshot_summary_view(self.ctx,
{'id': 'id2'}).AndReturn('id2')
self.mox.ReplayAll()
self.assertEqual(['id1', 'id2'], self.api.get_all_snapshots(self.ctx))
def test_create_snapshot(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
cinder._untranslate_snapshot_summary_view(self.ctx,
{'id': 'created_id'})
self.mox.ReplayAll()
self.api.create_snapshot(self.ctx, {'id': 'id1'}, '', '')
def test_create_force(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
cinder._untranslate_snapshot_summary_view(self.ctx,
{'id': 'created_id'})
self.mox.ReplayAll()
self.api.create_snapshot_force(self.ctx, {'id': 'id1'}, '', '')
def test_delete_snapshot(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
self.mox.StubOutWithMock(self.cinderclient.volume_snapshots,
'delete')
self.cinderclient.volume_snapshots.delete('id1')
self.mox.ReplayAll()
self.api.delete_snapshot(self.ctx, 'id1')
@mock.patch('nova.volume.cinder.cinderclient')
def test_get_volume_metadata(self, mock_cinderclient):
volume_id = 'id1'
metadata = {'key1': 'value1', 'key2': 'value2'}
volume = FakeVolume({'id': volume_id, 'volume_metadata': metadata})
mock_volumes = mock.MagicMock()
mock_volumes.get.return_value = volume
mock_cinderclient.return_value = mock.MagicMock(volumes=mock_volumes)
results = self.api.get_volume_metadata(self.ctx, volume_id)
mock_cinderclient.assert_called_once_with(self.ctx)
mock_volumes.get.assert_called_once_with(volume_id)
self.assertEqual(results, metadata)
@mock.patch('nova.volume.cinder.cinderclient')
def test_get_volume_metadata_value(self, mock_cinderclient):
volume_id = 'id1'
metadata = {'key1': 'value1'}
volume = FakeVolume({'id': volume_id, 'volume_metadata': metadata})
mock_volumes = mock.MagicMock()
mock_volumes.get.return_value = volume
mock_cinderclient.return_value = mock.MagicMock(volumes=mock_volumes)
results = self.api.get_volume_metadata_value(self.ctx, volume_id,
'key1')
mock_cinderclient.assert_called_once_with(self.ctx)
mock_volumes.get.assert_called_once_with(volume_id)
self.assertEqual(results, 'value1')
@mock.patch('nova.volume.cinder.cinderclient')
def test_delete_volume_metadata(self, mock_cinderclient):
volume_id = 'id1'
keys = ['key1', 'key2', 'key3']
mock_volumes = mock.MagicMock()
mock_cinderclient.return_value = mock.MagicMock(volumes=mock_volumes)
self.api.delete_volume_metadata(self.ctx, volume_id, keys)
mock_cinderclient.assert_called_once_with(self.ctx)
mock_volumes.delete_metadata.assert_called_once_with(volume_id, keys)
@mock.patch('nova.volume.cinder.cinderclient')
def test_update_volume_metadata(self, mock_cinderclient):
volume_id = 'id1'
metadata = {'key1': 'value1'}
mock_volumes = mock.MagicMock()
mock_volumes.set_metadata.return_value = metadata
mock_cinderclient.return_value = mock.MagicMock(volumes=mock_volumes)
updated_meta = self.api.update_volume_metadata(self.ctx, volume_id,
metadata)
mock_cinderclient.assert_called_once_with(self.ctx)
self.assertFalse(mock_volumes.update_all_metadata.called)
mock_volumes.set_metadata.assert_called_once_with(volume_id, metadata)
self.assertEqual(metadata, updated_meta)
@mock.patch('nova.volume.cinder.cinderclient')
def test_update_volume_metadata_delete(self, mock_cinderclient):
volume_id = 'id1'
metadata = {'key1': 'value1', 'key2': 'value2'}
mock_volumes = mock.MagicMock()
mock_volumes.update_all_metadata.return_value = metadata
mock_cinderclient.return_value = mock.MagicMock(volumes=mock_volumes)
updated_meta = self.api.update_volume_metadata(self.ctx, volume_id,
metadata, delete=True)
mock_cinderclient.assert_called_once_with(self.ctx)
mock_volumes.update_all_metadata.assert_called_once_with(volume_id,
metadata)
self.assertFalse(mock_volumes.set_metadata.called)
self.assertEqual(metadata, updated_meta)
def test_update_snapshot_status(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
self.mox.StubOutWithMock(self.cinderclient.volume_snapshots,
'update_snapshot_status')
self.cinderclient.volume_snapshots.update_snapshot_status(
'id1', {'status': 'error', 'progress': '90%'})
self.mox.ReplayAll()
self.api.update_snapshot_status(self.ctx, 'id1', 'error')
def test_get_volume_encryption_metadata(self):
cinder.cinderclient(self.ctx).AndReturn(self.cinderclient)
self.mox.StubOutWithMock(self.cinderclient.volumes,
'get_encryption_metadata')
self.cinderclient.volumes.\
get_encryption_metadata({'encryption_key_id': 'fake_key'})
self.mox.ReplayAll()
self.api.get_volume_encryption_metadata(self.ctx,
{'encryption_key_id':
'fake_key'})
|