~nottrobin/charms/precise/block-storage-broker/ensure-python-apt

« back to all changes in this revision

Viewing changes to hooks/test_hooks.py

Merge bsb-ec2-support [f=1298496] [r=dpb,fcorrea]

Add EC2 support to block-storage-broker to create, attach, label and detach ec2 volumes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
import mocker
5
5
import os
6
6
from testing import TestHookenv
 
7
from util import StorageServiceUtil
7
8
 
8
9
 
9
10
class TestHooks(mocker.MockerTestCase):
10
11
 
11
12
    def setUp(self):
 
13
        super(TestHooks, self).setUp()
12
14
        self.maxDiff = None
13
15
        hooks.hookenv = TestHookenv(
14
16
            {"key": "myusername", "tenant": "myusername_project",
15
 
             "secret": "password", "region": "region1",
 
17
             "secret": "password", "region": "region1", "provider": "nova",
16
18
             "endpoint": "https://keystone_url:443/v2.0/",
17
19
             "default_volume_size": 11})
18
20
 
157
159
        self.addCleanup(
158
160
            setattr, hooks.hookenv, "_config", hooks.hookenv._config)
159
161
        hooks.hookenv._config = (
160
 
            ("key", "myusername"), ("tenant", ""),
 
162
            ("key", "myusername"), ("tenant", ""), ("provider", "nova"),
161
163
            ("secret", "password"), ("region", ""),
162
164
            ("endpoint", "https://keystone_url:443/v2.0/"))
163
 
        self.mocker.replay()
164
165
 
165
166
        result = self.assertRaises(SystemExit, hooks.config_changed)
166
167
        self.assertEqual(result.code, 0)
176
177
        configured credentials when all mandatory configuration options are
177
178
        set.
178
179
        """
179
 
        load_environment = self.mocker.replace("nova_util.load_environment")
180
 
        load_environment()
 
180
        self.storage_util = self.mocker.patch(StorageServiceUtil)
 
181
        self.storage_util.load_environment()
181
182
        self.mocker.replay()
182
183
        hooks.config_changed()
183
184
 
214
215
 
215
216
    def test_block_storage_relation_changed_with_instance_id(self):
216
217
        """
217
 
        L{block_storage_relation_changed} calls L{attach_nova_volume} when
 
218
        L{block_storage_relation_changed} calls L{attach_volume} when
218
219
        C{instance-id} is available in the relation data. To report
219
220
        a successful device attach, it sets the relation data
220
221
        C{block-device-path} to the attached volume's device path from nova.
228
229
        hooks.hookenv._incoming_relation_data = (("instance-id", "i-123"),)
229
230
 
230
231
        persist = self.mocker.replace(hooks._persist_data)
231
 
        persist("storage/0", "i-123")
232
 
        nova_attach = self.mocker.replace("nova_util.attach_nova_volume")
233
 
        nova_attach(
 
232
        persist("storage/0", "storage/0 unit volume")
 
233
        self.storage_util = self.mocker.patch(StorageServiceUtil)
 
234
        self.storage_util.attach_volume(
234
235
            instance_id="i-123", volume_id=None, size=None, volume_label=None)
235
236
        self.mocker.result(device_path)   # The attached device path from nova
236
237
        self.mocker.replay()
247
248
    def test_block_storage_relation_changed_with_instance_id_volume_id(self):
248
249
        """
249
250
        When C{volume-id} and C{instance-id} are both present in the relation
250
 
        data, they will both be passed to L{attach_nova_volume}. To report a
 
251
        data, they will both be passed to L{attach_volume}. To report a
251
252
        successful device attach, it sets the relation data
252
253
        C{block-device-path} to the attached volume's device path from nova.
253
254
        """
262
263
            ("instance-id", "i-123"), ("volume-id", volume_id))
263
264
 
264
265
        persist = self.mocker.replace(hooks._persist_data)
265
 
        persist("storage/0", "i-123")
266
 
        nova_attach = self.mocker.replace("nova_util.attach_nova_volume")
267
 
        nova_attach(
 
266
        persist("storage/0", "storage/0 unit volume")
 
267
        self.storage_util = self.mocker.patch(StorageServiceUtil)
 
268
        self.storage_util.attach_volume(
268
269
            instance_id="i-123", volume_id=volume_id, size=None,
269
270
            volume_label=None)
270
271
        self.mocker.result(device_path)   # The attached device path from nova
282
283
    def test_block_storage_relation_changed_with_instance_id_size(self):
283
284
        """
284
285
        When C{size} and C{instance-id} are both present in the relation data,
285
 
        they will be passed to L{attach_nova_volume}. To report a successful
 
286
        they will be passed to L{attach_volume}. To report a successful
286
287
        device attach, it sets the relation data C{block-device-path} to the
287
288
        attached volume's device path from nova.
288
289
        """
296
297
        hooks.hookenv._incoming_relation_data = (
297
298
            ("instance-id", "i-123"), ("size", size))
298
299
        persist = self.mocker.replace(hooks._persist_data)
299
 
        persist("storage/0", "i-123")
300
 
        nova_attach = self.mocker.replace("nova_util.attach_nova_volume")
301
 
        nova_attach(
 
300
        persist("storage/0", "storage/0 unit volume")
 
301
        self.storage_util = self.mocker.patch(StorageServiceUtil)
 
302
        self.storage_util.attach_volume(
302
303
            instance_id="i-123", volume_id=None, size=size, volume_label=None)
303
304
        self.mocker.result(device_path)   # The attached device path from nova
304
305
        self.mocker.replay()
325
326
        result = self.assertRaises(
326
327
            SystemExit, hooks.block_storage_relation_departed)
327
328
        self.assertEqual(result.code, 1)
328
 
        message = "Cannot detach nova volume from instance without instance-id"
 
329
        message = "Cannot detach volume from instance without volume_label"
329
330
        self.assertIn(
330
331
            message, hooks.hookenv._log_ERROR, "Not logged- %s" % message)
331
332
 
343
344
        with open(persist_path, "w") as outfile:
344
345
            outfile.write(unicode(json.dumps(data, ensure_ascii=False)))
345
346
 
346
 
        nova_detach = self.mocker.replace("nova_util.detach_nova_volume")
347
 
        nova_detach("i-123")
 
347
        self.storage_util = self.mocker.patch(StorageServiceUtil)
 
348
        self.storage_util.detach_volume("i-123")
348
349
        self.mocker.replay()
349
350
 
350
351
        hooks.block_storage_relation_departed()