1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 Josh Durgin
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
19
from nova import exception
20
from nova.openstack.common import log as logging
21
from nova.openstack.common import timeutils
23
from nova.tests.image import fake as fake_image
24
from nova.tests.test_volume import DriverTestCase
25
from nova.volume.driver import RBDDriver
27
LOG = logging.getLogger(__name__)
30
class RBDTestCase(test.TestCase):
33
super(RBDTestCase, self).setUp()
35
def fake_execute(*args):
37
self.driver = RBDDriver(execute=fake_execute)
39
def test_good_locations(self):
41
'rbd://fsid/pool/image/snap',
42
'rbd://%2F/%2F/%2F/%2F',
44
map(self.driver._parse_location, locations)
46
def test_bad_locations(self):
49
'http://path/to/somewhere/else',
52
'rbd://fsid/pool/image/',
53
'rbd://fsid/pool/image/snap/',
57
self.assertRaises(exception.ImageUnacceptable,
58
self.driver._parse_location,
60
self.assertFalse(self.driver._is_cloneable(loc))
62
def test_cloneable(self):
63
self.stubs.Set(self.driver, '_get_fsid', lambda: 'abc')
64
location = 'rbd://abc/pool/image/snap'
65
self.assertTrue(self.driver._is_cloneable(location))
67
def test_uncloneable_different_fsid(self):
68
self.stubs.Set(self.driver, '_get_fsid', lambda: 'abc')
69
location = 'rbd://def/pool/image/snap'
70
self.assertFalse(self.driver._is_cloneable(location))
72
def test_uncloneable_unreadable(self):
74
raise exception.ProcessExecutionError()
75
self.stubs.Set(self.driver, '_get_fsid', lambda: 'abc')
76
self.stubs.Set(self.driver, '_execute', fake_exc)
77
location = 'rbd://abc/pool/image/snap'
78
self.assertFalse(self.driver._is_cloneable(location))
81
class FakeRBDDriver(RBDDriver):
90
class ManagedRBDTestCase(DriverTestCase):
91
driver_name = "nova.tests.test_rbd.FakeRBDDriver"
94
super(ManagedRBDTestCase, self).setUp()
95
fake_image.stub_out_image_service(self.stubs)
97
def _clone_volume_from_image(self, expected_status,
99
"""Try to clone a volume from an image, and check the status
101
def fake_clone_image(volume, image_location):
104
def fake_clone_error(volume, image_location):
105
raise exception.NovaException()
107
self.stubs.Set(self.volume.driver, '_is_cloneable', lambda x: True)
109
self.stubs.Set(self.volume.driver, 'clone_image', fake_clone_image)
111
self.stubs.Set(self.volume.driver, 'clone_image', fake_clone_error)
113
image_id = 'c905cedb-7281-47e4-8a62-f26bc5fc4c77'
115
# creating volume testdata
116
db.volume_create(self.context, {'id': volume_id,
117
'updated_at': timeutils.utcnow(),
118
'display_description': 'Test Desc',
120
'status': 'creating',
121
'instance_uuid': None,
125
self.volume.create_volume(self.context,
129
self.assertRaises(exception.NovaException,
130
self.volume.create_volume,
135
volume = db.volume_get(self.context, volume_id)
136
self.assertEqual(volume['status'], expected_status)
139
db.volume_destroy(self.context, volume_id)
141
def test_clone_image_status_available(self):
142
"""Verify that before cloning, an image is in the available state."""
143
self._clone_volume_from_image('available', True)
145
def test_clone_image_status_error(self):
146
"""Verify that before cloning, an image is in the available state."""
147
self._clone_volume_from_image('error', False)
149
def test_clone_success(self):
150
self.stubs.Set(self.volume.driver, '_is_cloneable', lambda x: True)
151
self.stubs.Set(self.volume.driver, 'clone_image', lambda a, b: True)
152
image_id = 'c905cedb-7281-47e4-8a62-f26bc5fc4c77'
153
self.assertTrue(self.volume.driver.clone_image({}, image_id))
155
def test_clone_bad_image_id(self):
156
self.stubs.Set(self.volume.driver, '_is_cloneable', lambda x: True)
157
self.assertFalse(self.volume.driver.clone_image({}, None))
159
def test_clone_uncloneable(self):
160
self.stubs.Set(self.volume.driver, '_is_cloneable', lambda x: False)
161
self.assertFalse(self.volume.driver.clone_image({}, 'dne'))