Example #1
0
 def test_init_host_sync_provider_info_no_update(self, mock_update):
     vol0 = tests_utils.create_volume(
         self.context, size=1, host=CONF.host)
     vol1 = tests_utils.create_volume(
         self.context, size=1, host=CONF.host)
     snap0 = tests_utils.create_snapshot(self.context, vol0.id)
     snap1 = tests_utils.create_snapshot(self.context, vol1.id)
     mock_update.return_value = ([], [])
     # initialize
     self.volume.init_host(service_id=self.service_id)
     # Grab volume and snapshot objects
     vol0_obj = objects.Volume.get_by_id(context.get_admin_context(),
                                         vol0.id)
     vol1_obj = objects.Volume.get_by_id(context.get_admin_context(),
                                         vol1.id)
     snap0_obj = objects.Snapshot.get_by_id(self.context, snap0.id)
     snap1_obj = objects.Snapshot.get_by_id(self.context, snap1.id)
     # Check provider ids are not changed
     self.assertIsNone(vol0_obj.provider_id)
     self.assertIsNone(vol1_obj.provider_id)
     self.assertIsNone(snap0_obj.provider_id)
     self.assertIsNone(snap1_obj.provider_id)
     # Clean up
     self.volume.delete_snapshot(self.context, snap0_obj)
     self.volume.delete_snapshot(self.context, snap1_obj)
     self.volume.delete_volume(self.context, vol0)
     self.volume.delete_volume(self.context, vol1)
    def _test_failover_model_updates(self, in_volumes, in_snapshots,
                                     driver_volumes, driver_result,
                                     out_volumes, out_snapshots,
                                     in_groups=None, out_groups=None,
                                     driver_group_result=None,
                                     secondary_id=None):
        host = vol_utils.extract_host(self.manager.host)
        utils.create_service(self.context, {'host': host,
                                            'binary': 'cinder-volume'})
        for volume in in_volumes:
            utils.create_volume(self.context, self.manager.host, **volume)

        for snapshot in in_snapshots:
            utils.create_snapshot(self.context, **snapshot)

        for group in in_groups:
            utils.create_group(self.context, self.manager.host, **group)

        with mock.patch.object(
                self.manager.driver, 'failover_host',
                return_value=(secondary_id, driver_result,
                              driver_group_result)) as driver_mock:
            self.manager.failover_host(self.context, secondary_id)

            self.assertSetEqual(driver_volumes,
                                {v.id for v in driver_mock.call_args[0][1]})

        self._check_failover_db(objects.VolumeList, out_volumes)
        self._check_failover_db(objects.SnapshotList, out_snapshots)
        self._check_failover_db(objects.GroupList, out_groups)
Example #3
0
    def test_update_encryption_key_id(self,
                                      mock_barbican_client):
        vol = self.create_volume()

        snap_ids = [fake.SNAPSHOT_ID, fake.SNAPSHOT2_ID, fake.SNAPSHOT3_ID]
        for snap_id in snap_ids:
            tests_utils.create_snapshot(self.context, vol.id, id=snap_id)

        # Barbican's secret.store() returns a URI that contains the
        # secret's key ID at the end.
        secret_ref = 'http://some/path/' + fake.ENCRYPTION_KEY_ID
        mock_secret = mock.MagicMock()
        mock_secret.store.return_value = secret_ref

        mock_barbican_client.return_value.secrets.create.return_value \
            = mock_secret

        migration.migrate_fixed_key(self.my_vols, conf=self.conf)
        vol_db = db.volume_get(self.context, vol.id)
        self.assertEqual(fake.ENCRYPTION_KEY_ID, vol_db['encryption_key_id'])

        for snap_id in snap_ids:
            snap_db = db.snapshot_get(self.context, snap_id)
            self.assertEqual(fake.ENCRYPTION_KEY_ID,
                             snap_db['encryption_key_id'])
Example #4
0
    def test_show_host(self, mock_get_host):
        host = 'test_host'
        test_service = service.Service(id=1, host=host,
                                       binary=constants.VOLUME_BINARY,
                                       topic=constants.VOLUME_TOPIC)
        mock_get_host.return_value = test_service

        ctxt1 = context.RequestContext(project_id=fake_constants.PROJECT_ID,
                                       is_admin=True)
        ctxt2 = context.RequestContext(project_id=fake_constants.PROJECT2_ID,
                                       is_admin=True)
        # Create two volumes with different project.
        volume1 = test_utils.create_volume(ctxt1,
                                           host=host, size=1)
        test_utils.create_volume(ctxt2, host=host, size=1)
        # This volume is not on the same host. It should not be counted.
        test_utils.create_volume(ctxt2, host='fake_host', size=1)
        test_utils.create_snapshot(ctxt1, volume_id=volume1.id)

        resp = self.controller.show(self.req, host)

        host_resp = resp['host']
        # There are 3 resource list: total, project1, project2
        self.assertEqual(3, len(host_resp))
        expected = [
            {
                "resource": {
                    "volume_count": "2",
                    "total_volume_gb": "2",
                    "host": "test_host",
                    "total_snapshot_gb": "1",
                    "project": "(total)",
                    "snapshot_count": "1"}
            },
            {
                "resource": {
                    "volume_count": "1",
                    "total_volume_gb": "1",
                    "host": "test_host",
                    "project": fake_constants.PROJECT2_ID,
                    "total_snapshot_gb": "0",
                    "snapshot_count": "0"}
            },
            {
                "resource": {
                    "volume_count": "1",
                    "total_volume_gb": "1",
                    "host": "test_host",
                    "total_snapshot_gb": "1",
                    "project": fake_constants.PROJECT_ID,
                    "snapshot_count": "1"}
            }
        ]
        self.assertListEqual(expected, sorted(
            host_resp, key=lambda h: h['resource']['project']))
Example #5
0
 def test_transfer_accept_with_snapshots_invalid(self, mock_notify):
     svc = self.start_service('volume', host='test_host')
     self.addCleanup(svc.stop)
     tx_api = transfer_api.API()
     volume = utils.create_volume(self.ctxt,
                                  volume_type_id=fake.VOLUME_TYPE_ID,
                                  updated_at=self.updated_at)
     utils.create_volume_type(self.ctxt.elevated(),
                              id=fake.VOLUME_TYPE_ID, name="test_type")
     utils.create_snapshot(self.ctxt, volume.id, status='deleting')
     self.assertRaises(exception.InvalidSnapshot,
                       tx_api.create, self.ctxt, volume.id, 'Description')
Example #6
0
    def test_delete_group_delete_volumes_with_snapshots(self):
        self.group1.status = fields.GroupStatus.AVAILABLE
        self.group1.save()
        vol = utils.create_volume(self.ctxt, group_id=self.group1.id)
        utils.create_snapshot(self.ctxt, vol.id)
        req = fakes.HTTPRequest.blank('/v3/%s/groups/%s/action' %
                                      (fake.PROJECT_ID, self.group1.id),
                                      version=GROUP_MICRO_VERSION)
        body = {"delete": {"delete-volumes": True}}
        self.assertRaises(webob.exc.HTTPBadRequest,
                          self.controller.delete_group,
                          req, self.group1.id, body)

        vol.destroy()
Example #7
0
 def test_transfer_accept_with_snapshots(self):
     volume_id = utils.create_volume(self.ctxt)['id']
     snapshot_id1 = utils.create_snapshot(self.ctxt, volume_id,
                                          status='available')['id']
     snapshot_id2 = utils.create_snapshot(self.ctxt, volume_id,
                                          status='available')['id']
     xfer_id = self._create_transfer(volume_id)
     nctxt = context.RequestContext(user_id=fake.USER2_ID,
                                    project_id=fake.PROJECT2_ID)
     db.transfer_accept(nctxt.elevated(), xfer_id, fake.USER2_ID,
                        fake.PROJECT2_ID)
     self.assertEqual(fake.PROJECT2_ID,
                      db.snapshot_get(nctxt, snapshot_id1)['project_id'])
     self.assertEqual(fake.PROJECT2_ID,
                      db.snapshot_get(nctxt, snapshot_id2)['project_id'])
Example #8
0
    def test_create_snapshot_fails_with_creating_status(self):
        """Test init_host_with_service in case of snapshot.

        While the status of snapshot is 'creating', volume process
        down. After process restarting this 'creating' status is
        changed to 'error'.
        """
        volume = tests_utils.create_volume(self.context,
                                           **self.volume_params)
        snapshot = tests_utils.create_snapshot(
            self.context,
            volume.id,
            status=fields.SnapshotStatus.CREATING)
        db.worker_create(self.context, resource_type='Snapshot',
                         resource_id=snapshot.id, status=snapshot.status,
                         service_id=self.service_id)

        self.volume.init_host(service_id=service.Service.service_id)

        snapshot_obj = objects.Snapshot.get_by_id(self.context, snapshot.id)

        self.assertEqual(fields.SnapshotStatus.ERROR, snapshot_obj.status)
        self.assertEqual(service.Service.service_id,
                         self.volume.service_id)
        self._assert_workers_are_removed()

        self.volume.delete_snapshot(self.context, snapshot_obj)
        self.volume.delete_volume(self.context, volume)
Example #9
0
 def test_name_id_snapshot_volume_name(self):
     """Make sure snapshot['volume_name'] is updated."""
     vol_ref = testutils.create_volume(self.ctxt, size=1)
     db.volume_update(self.ctxt, vol_ref['id'], {'name_id': 'fake'})
     snap_ref = testutils.create_snapshot(self.ctxt, vol_ref['id'])
     expected_name = CONF.volume_name_template % 'fake'
     self.assertEqual(snap_ref['volume_name'], expected_name)
Example #10
0
    def test_create_volume_from_snapshot_sparse(self):

        self.configuration.lvm_type = 'thin'
        lvm_driver = lvm.LVMVolumeDriver(configuration=self.configuration,
                                         db=db)

        with mock.patch.object(lvm_driver, 'vg'), \
                mock.patch.object(lvm_driver, '_create_volume'), \
                mock.patch.object(volutils, 'copy_volume') as mock_copy:

            # Test case for thin LVM
            lvm_driver._sparse_copy_volume = True
            src_volume = tests_utils.create_volume(self.context)
            snapshot_ref = tests_utils.create_snapshot(self.context,
                                                       src_volume['id'])
            dst_volume = tests_utils.create_volume(self.context)
            lvm_driver.create_volume_from_snapshot(dst_volume,
                                                   snapshot_ref)

            volume_path = lvm_driver.local_path(dst_volume)
            snapshot_path = lvm_driver.local_path(snapshot_ref)
            volume_size = 1024
            block_size = '1M'
            mock_copy.assert_called_with(snapshot_path,
                                         volume_path,
                                         volume_size,
                                         block_size,
                                         execute=lvm_driver._execute,
                                         sparse=True)
Example #11
0
    def test_create_consistencygroup_from_src_both_snap_cg(self):
        self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create)

        consistencygroup = utils.create_consistencygroup(self.ctxt)
        volume_id = utils.create_volume(self.ctxt, consistencygroup_id=consistencygroup.id)["id"]
        cgsnapshot_id = utils.create_cgsnapshot(self.ctxt, consistencygroup_id=consistencygroup.id)["id"]
        snapshot = utils.create_snapshot(self.ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status="available")

        test_cg_name = "test cg"
        body = {
            "consistencygroup-from-src": {
                "name": test_cg_name,
                "description": "Consistency Group 1",
                "cgsnapshot_id": cgsnapshot_id,
                "source_cgid": consistencygroup.id,
            }
        }
        req = webob.Request.blank("/v2/fake/consistencygroups/create_from_src")
        req.method = "POST"
        req.headers["Content-Type"] = "application/json"
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(fakes.wsgi_app())
        res_dict = jsonutils.loads(res.body)

        self.assertEqual(400, res.status_int)
        self.assertEqual(400, res_dict["badRequest"]["code"])
        self.assertIsNotNone(res_dict["badRequest"]["message"])

        snapshot.destroy()
        db.cgsnapshot_destroy(self.ctxt.elevated(), cgsnapshot_id)
        db.volume_destroy(self.ctxt.elevated(), volume_id)
        consistencygroup.destroy()
    def test_revert_snapshot(self):
        self._setup_stubs_for_manage_existing()
        self.configuration.lvm_type = 'auto'
        fake_volume = tests_utils.create_volume(self.context,
                                                display_name='fake_volume')
        fake_snapshot = tests_utils.create_snapshot(
            self.context, fake_volume.id)

        with mock.patch.object(self.volume.driver.vg,
                               'revert') as mock_revert,\
                mock.patch.object(self.volume.driver.vg,
                                  'create_lv_snapshot') as mock_create,\
                mock.patch.object(self.volume.driver.vg,
                                  'deactivate_lv') as mock_deactive,\
                mock.patch.object(self.volume.driver.vg,
                                  'activate_lv') as mock_active:
            self.volume.driver.revert_to_snapshot(self.context,
                                                  fake_volume,
                                                  fake_snapshot)
            mock_revert.assert_called_once_with(
                self.volume.driver._escape_snapshot(fake_snapshot.name))
            mock_deactive.assert_called_once_with(fake_volume.name)
            mock_active.assert_called_once_with(fake_volume.name)
            mock_create.assert_called_once_with(
                self.volume.driver._escape_snapshot(fake_snapshot.name),
                fake_volume.name, self.configuration.lvm_type)
Example #13
0
    def test_create_consistencygroup_from_src(self, mock_validate):
        self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create)

        consistencygroup = utils.create_consistencygroup(self.ctxt)
        volume_id = utils.create_volume(self.ctxt, consistencygroup_id=consistencygroup.id)["id"]
        cgsnapshot = utils.create_cgsnapshot(self.ctxt, consistencygroup_id=consistencygroup.id)
        snapshot = utils.create_snapshot(self.ctxt, volume_id, cgsnapshot_id=cgsnapshot.id, status="available")

        test_cg_name = "test cg"
        body = {
            "consistencygroup-from-src": {
                "name": test_cg_name,
                "description": "Consistency Group 1",
                "cgsnapshot_id": cgsnapshot.id,
            }
        }
        req = webob.Request.blank("/v2/fake/consistencygroups/create_from_src")
        req.method = "POST"
        req.headers["Content-Type"] = "application/json"
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(fakes.wsgi_app())
        res_dict = jsonutils.loads(res.body)

        self.assertEqual(202, res.status_int)
        self.assertIn("id", res_dict["consistencygroup"])
        self.assertEqual(test_cg_name, res_dict["consistencygroup"]["name"])
        self.assertTrue(mock_validate.called)

        cg_ref = objects.ConsistencyGroup.get_by_id(self.ctxt.elevated(), res_dict["consistencygroup"]["id"])

        cg_ref.destroy()
        snapshot.destroy()
        db.volume_destroy(self.ctxt.elevated(), volume_id)
        consistencygroup.destroy()
        cgsnapshot.destroy()
Example #14
0
 def test_name_id_snapshot_volume_name(self):
     """Make sure snapshot['volume_name'] is updated."""
     vol_ref = testutils.create_volume(self.ctxt, size=1,
                                       _name_id=fake.VOLUME2_ID)
     snap_ref = testutils.create_snapshot(self.ctxt, vol_ref['id'])
     expected_name = CONF.volume_name_template % fake.VOLUME2_ID
     self.assertEqual(expected_name, snap_ref['volume_name'])
Example #15
0
 def test_delete_snapshot_frozen(self):
     service = tests_utils.create_service(self.context, {'frozen': True})
     volume = tests_utils.create_volume(self.context, host=service.host)
     snapshot = tests_utils.create_snapshot(self.context, volume.id)
     self.assertRaises(exception.InvalidInput,
                       self.volume_api.delete_snapshot, self.context,
                       snapshot)
Example #16
0
    def test_create_consistencygroup_from_src_no_host(self):
        consistencygroup = utils.create_consistencygroup(self.ctxt, host=None)
        volume_id = utils.create_volume(self.ctxt, consistencygroup_id=consistencygroup.id)["id"]
        cgsnapshot = utils.create_cgsnapshot(self.ctxt, consistencygroup_id=consistencygroup.id)
        snapshot = utils.create_snapshot(self.ctxt, volume_id, cgsnapshot_id=cgsnapshot.id, status="available")

        test_cg_name = "test cg"
        body = {
            "consistencygroup-from-src": {
                "name": test_cg_name,
                "description": "Consistency Group 1",
                "cgsnapshot_id": cgsnapshot.id,
            }
        }
        req = webob.Request.blank("/v2/fake/consistencygroups/create_from_src")
        req.method = "POST"
        req.headers["Content-Type"] = "application/json"
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(fakes.wsgi_app())
        res_dict = jsonutils.loads(res.body)

        self.assertEqual(400, res.status_int)
        self.assertEqual(400, res_dict["badRequest"]["code"])
        msg = _("Invalid ConsistencyGroup: No host to create consistency " "group")
        self.assertIn(msg, res_dict["badRequest"]["message"])

        snapshot.destroy()
        db.volume_destroy(self.ctxt.elevated(), volume_id)
        consistencygroup.destroy()
        cgsnapshot.destroy()
Example #17
0
 def test_name_id_snapshot_volume_name(self):
     """Make sure snapshot['volume_name'] is updated."""
     vol_ref = testutils.create_volume(self.ctxt, size=1)
     db.volume_update(self.ctxt, vol_ref["id"], {"name_id": "fake"})
     snap_ref = testutils.create_snapshot(self.ctxt, vol_ref["id"])
     expected_name = CONF.volume_name_template % "fake"
     self.assertEqual(expected_name, snap_ref["volume_name"])
Example #18
0
    def test_create_consistencygroup_from_src_no_host(self):
        consistencygroup = utils.create_consistencygroup(self.ctxt, host=None)
        volume_id = utils.create_volume(
            self.ctxt,
            consistencygroup_id=consistencygroup.id)['id']
        cgsnapshot = utils.create_cgsnapshot(
            self.ctxt, consistencygroup_id=consistencygroup.id)
        snapshot = utils.create_snapshot(
            self.ctxt,
            volume_id,
            cgsnapshot_id=cgsnapshot.id,
            status='available')

        test_cg_name = 'test cg'
        body = {"consistencygroup-from-src": {"name": test_cg_name,
                                              "description":
                                              "Consistency Group 1",
                                              "cgsnapshot_id": cgsnapshot.id}}
        req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src')
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.body = json.dumps(body)
        res = req.get_response(fakes.wsgi_app())
        res_dict = json.loads(res.body)

        self.assertEqual(400, res.status_int)
        self.assertEqual(400, res_dict['badRequest']['code'])
        msg = _('Invalid ConsistencyGroup: No host to create consistency '
                'group')
        self.assertIn(msg, res_dict['badRequest']['message'])

        snapshot.destroy()
        db.volume_destroy(self.ctxt.elevated(), volume_id)
        consistencygroup.destroy()
        cgsnapshot.destroy()
Example #19
0
    def test_create_consistencygroup_from_src_create_volume_failed(self, mock_create):
        ctxt = context.RequestContext("fake", "fake", auth_token=True)
        consistencygroup_id = utils.create_consistencygroup(ctxt)["id"]
        volume_id = utils.create_volume(ctxt, consistencygroup_id=consistencygroup_id)["id"]
        cgsnapshot_id = utils.create_cgsnapshot(ctxt, consistencygroup_id=consistencygroup_id)["id"]
        snapshot_id = utils.create_snapshot(ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status="available")["id"]

        test_cg_name = "test cg"
        body = {
            "consistencygroup-from-src": {
                "name": test_cg_name,
                "description": "Consistency Group 1",
                "cgsnapshot_id": cgsnapshot_id,
            }
        }
        req = webob.Request.blank("/v2/fake/consistencygroups/create_from_src")
        req.method = "POST"
        req.headers["Content-Type"] = "application/json"
        req.body = json.dumps(body)
        res = req.get_response(fakes.wsgi_app())
        res_dict = json.loads(res.body)

        self.assertEqual(400, res.status_int)
        self.assertEqual(400, res_dict["badRequest"]["code"])
        msg = _("Create volume failed.")
        self.assertEqual(msg, res_dict["badRequest"]["message"])

        db.snapshot_destroy(ctxt.elevated(), snapshot_id)
        db.cgsnapshot_destroy(ctxt.elevated(), cgsnapshot_id)
        db.volume_destroy(ctxt.elevated(), volume_id)
        db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id)
Example #20
0
 def _simple_snapshot(self, **kwargs):
     volume = kwargs.pop('volume', None) or self._simple_volume()
     ctxt = context.get_admin_context()
     updates = {'id': self._FAKE_SNAPSHOT_ID,
                'volume_id': volume.id}
     updates.update(kwargs)
     snapshot = test_utils.create_snapshot(ctxt, **updates)
     return snapshot
Example #21
0
 def test_unmanage_volume_with_snapshots(self):
     """Return 400 if the volume exists but has snapshots."""
     vol = utils.create_volume(self.ctxt)
     snap = utils.create_snapshot(self.ctxt, vol.id)
     res = self._get_resp(vol.id)
     self.assertEqual(400, res.status_int, res)
     db.volume_destroy(self.ctxt, vol.id)
     db.snapshot_destroy(self.ctxt, snap.id)
Example #22
0
    def test_update_volume_encryption_key_id(self,
                                             mock_barbican_client,
                                             mock_get_barbican_key_id):
        vol = self.create_volume()

        snap_ids = [fake.SNAPSHOT_ID, fake.SNAPSHOT2_ID, fake.SNAPSHOT3_ID]
        for snap_id in snap_ids:
            tests_utils.create_snapshot(self.context, vol.id, id=snap_id)

        mock_get_barbican_key_id.return_value = fake.ENCRYPTION_KEY_ID
        migration.migrate_fixed_key(self.my_vols, self.my_baks, conf=self.conf)
        vol_db = db.volume_get(self.context, vol.id)
        self.assertEqual(fake.ENCRYPTION_KEY_ID, vol_db['encryption_key_id'])

        for snap_id in snap_ids:
            snap_db = db.snapshot_get(self.context, snap_id)
            self.assertEqual(fake.ENCRYPTION_KEY_ID,
                             snap_db['encryption_key_id'])
Example #23
0
    def setUp(self):
        super(VolumeRpcAPITestCase, self).setUp()
        self.context = context.get_admin_context()
        vol = {}
        vol['host'] = 'fake_host'
        vol['availability_zone'] = CONF.storage_availability_zone
        vol['status'] = "available"
        vol['attach_status'] = "detached"
        vol['metadata'] = {"test_key": "test_val"}
        vol['size'] = 1
        volume = db.volume_create(self.context, vol)

        kwargs = {
            'status': fields.SnapshotStatus.CREATING,
            'progress': '0%',
            'display_name': 'fake_name',
            'display_description': 'fake_description'}
        snapshot = tests_utils.create_snapshot(self.context, vol['id'],
                                               **kwargs)

        source_group = tests_utils.create_consistencygroup(
            self.context,
            availability_zone=CONF.storage_availability_zone,
            volume_type='type1,type2',
            host='fakehost@fakedrv#fakepool')

        cgsnapshot = tests_utils.create_cgsnapshot(
            self.context,
            consistencygroup_id=source_group.id)

        group = tests_utils.create_consistencygroup(
            self.context,
            availability_zone=CONF.storage_availability_zone,
            volume_type='type1,type2',
            host='fakehost@fakedrv#fakepool',
            cgsnapshot_id=cgsnapshot.id)

        group2 = tests_utils.create_consistencygroup(
            self.context,
            availability_zone=CONF.storage_availability_zone,
            volume_type='type1,type2',
            host='fakehost@fakedrv#fakepool',
            source_cgid=source_group.id)

        group = objects.ConsistencyGroup.get_by_id(self.context, group.id)
        group2 = objects.ConsistencyGroup.get_by_id(self.context, group2.id)
        cgsnapshot = objects.CGSnapshot.get_by_id(self.context, cgsnapshot.id)
        self.fake_volume = jsonutils.to_primitive(volume)
        self.fake_volume_obj = fake_volume.fake_volume_obj(self.context, **vol)
        self.fake_volume_metadata = volume["volume_metadata"]
        self.fake_snapshot = snapshot
        self.fake_reservations = ["RESERVATION"]
        self.fake_cg = group
        self.fake_cg2 = group2
        self.fake_src_cg = jsonutils.to_primitive(source_group)
        self.fake_cgsnap = cgsnapshot
        self.fake_backup_obj = fake_backup.fake_backup_obj(self.context)
Example #24
0
    def test_create_vol_with_group_id_driver_exception(self,
                                                       mock_create_volume,
                                                       mock_create_from_snap,
                                                       mock_create_cloned_vol):
        """Test create a volume with group_id but driver exception."""
        # create_raw_volume with group id, but driver exception
        mock_create_volume.side_effect = exception.CinderException
        group = tests_utils.create_group(
            self.context,
            availability_zone=CONF.storage_availability_zone,
            volume_type_ids=[fake.VOLUME_TYPE_ID],
            group_type_id=fake.GROUP_TYPE_ID,
            host=CONF.host)
        self.volume.create_group(self.context, group)

        volume = tests_utils.create_volume(
            self.context,
            group_id=group.id,
            volume_type_id=fake.VOLUME_TYPE_ID,
            status='available',
            host=group.host)
        self.assertRaises(exception.CinderException,
                          self.volume.create_volume,
                          self.context,
                          volume)
        self.assertIsNone(volume.consistencygroup_id)

        # create volume from_snapshot with group id but driver exception
        mock_create_from_snap.side_effect = exception.CinderException
        snapshot = tests_utils.create_snapshot(self.context, volume.id)
        volume2 = tests_utils.create_volume(
            self.context,
            group_id=group.id,
            snapshot_id=snapshot.id,
            status='available',
            host=group.host,
            volume_type_id=fake.VOLUME_TYPE_ID)
        self.assertRaises(exception.CinderException,
                          self.volume.create_volume,
                          self.context,
                          volume2)
        self.assertIsNone(volume2.consistencygroup_id)

        # create cloned volume with group_id but driver exception
        mock_create_cloned_vol.side_effect = exception.CinderException
        volume3 = tests_utils.create_volume(
            self.context,
            group_id=group.id,
            source_volid=volume.id,
            status='available',
            host=group.host,
            volume_type_id=fake.VOLUME_TYPE_ID)
        self.assertRaises(exception.CinderException,
                          self.volume.create_volume,
                          self.context,
                          volume3)
        self.assertIsNone(volume3.consistencygroup_id)
Example #25
0
    def test_delete_snapshot_driver_not_initialized(self):
        volume = tests_utils.create_volume(self.context, **self.volume_params)
        snapshot = tests_utils.create_snapshot(self.context, volume.id)

        self.volume.driver._initialized = False
        self.assertRaises(exception.DriverNotInitialized,
                          self.volume.delete_snapshot, self.context, snapshot)

        snapshot.refresh()
        self.assertEqual(fields.SnapshotStatus.ERROR_DELETING, snapshot.status)
Example #26
0
    def test_delete_group_delete_volumes_with_deleted_snapshots(self):
        self.group1.status = fields.GroupStatus.AVAILABLE
        self.group1.save()
        vol = utils.create_volume(self.ctxt, group_id=self.group1.id)
        utils.create_snapshot(self.ctxt,
                              vol.id,
                              status='deleted',
                              deleted=True)
        req = fakes.HTTPRequest.blank('/v3/%s/groups/%s/action' %
                                      (fake.PROJECT_ID, self.group1.id),
                                      version=GROUP_MICRO_VERSION)
        body = {"delete": {"delete-volumes": True}}
        res_dict = self.controller.delete_group(req, self.group1.id, body)

        group = objects.Group.get_by_id(self.ctxt, self.group1.id)
        self.assertEqual(202, res_dict.status_int)
        self.assertEqual('deleting', group.status)

        vol.destroy()
Example #27
0
 def test_existing_snapshot_failed_quota_reserve(self, mock_reserve):
     vol = tests_utils.create_volume(self.context)
     snap = tests_utils.create_snapshot(self.context, vol.id)
     with mock.patch.object(
             self.volume.driver,
             'manage_existing_snapshot_get_size') as mock_get_size:
         mock_get_size.return_value = 1
         self.assertRaises(exception.SnapshotLimitExceeded,
                           self.volume.manage_existing_snapshot,
                           self.context, snap)
    def _create_db_snapshots(self, num_snaps):
        volume = utils.create_volume(self.ctx)
        snaps = [utils.create_snapshot(self.ctx, volume.id, display_name="snap" + str(i)) for i in range(num_snaps)]

        self.addCleanup(db.volume_destroy, self.ctx, volume.id)
        for snap in snaps:
            self.addCleanup(db.snapshot_destroy, self.ctx, snap.id)

        snaps.reverse()
        return volume, snaps
Example #29
0
    def test_delete_group_delete_volumes_with_deleted_snapshots(self):
        self.group1.status = fields.GroupStatus.AVAILABLE
        self.group1.save()
        vol = utils.create_volume(self.ctxt, group_id=self.group1.id)
        utils.create_snapshot(self.ctxt, vol.id, status='deleted',
                              deleted=True)
        req = fakes.HTTPRequest.blank('/v3/%s/groups/%s/action' %
                                      (fake.PROJECT_ID, self.group1.id),
                                      version=GROUP_MICRO_VERSION)
        body = {"delete": {"delete-volumes": True}}
        res_dict = self.controller.delete_group(
            req, self.group1.id, body)

        group = objects.Group.get_by_id(
            self.ctxt, self.group1.id)
        self.assertEqual(202, res_dict.status_int)
        self.assertEqual('deleting', group.status)

        vol.destroy()
Example #30
0
 def test_transfer_accept_with_snapshots_invalid_status(self):
     volume_id = utils.create_volume(self.ctxt)['id']
     snapshot_id1 = utils.create_snapshot(self.ctxt, volume_id,
                                          status='available')['id']
     snapshot_id2 = utils.create_snapshot(self.ctxt, volume_id)['id']
     xfer_id = self._create_transfer(volume_id)
     nctxt = context.RequestContext(user_id=fake.USER2_ID,
                                    project_id=fake.PROJECT2_ID)
     self.assertRaises(exception.InvalidSnapshot, db.transfer_accept,
                       nctxt.elevated(), xfer_id, fake.USER2_ID,
                       fake.PROJECT2_ID)
     self.assertEqual(fake.PROJECT_ID,
                      db.snapshot_get(self.ctxt,
                                      snapshot_id1)['project_id'])
     self.assertEqual(fake.PROJECT_ID,
                      db.snapshot_get(self.ctxt,
                                      snapshot_id2)['project_id'])
     self.assertEqual('awaiting-transfer',
                      db.volume_get(self.ctxt, volume_id)['status'])
Example #31
0
 def test_transfer_accept_with_snapshots_invalid_status(self):
     volume_id = utils.create_volume(self.ctxt)['id']
     snapshot_id1 = utils.create_snapshot(self.ctxt, volume_id,
                                          status='available')['id']
     snapshot_id2 = utils.create_snapshot(self.ctxt, volume_id)['id']
     xfer_id = self._create_transfer(volume_id)
     nctxt = context.RequestContext(user_id=fake.USER2_ID,
                                    project_id=fake.PROJECT2_ID)
     self.assertRaises(exception.InvalidSnapshot, db.transfer_accept,
                       nctxt.elevated(), xfer_id, fake.USER2_ID,
                       fake.PROJECT2_ID)
     self.assertEqual(fake.PROJECT_ID,
                      db.snapshot_get(self.ctxt,
                                      snapshot_id1)['project_id'])
     self.assertEqual(fake.PROJECT_ID,
                      db.snapshot_get(self.ctxt,
                                      snapshot_id2)['project_id'])
     self.assertEqual('awaiting-transfer',
                      db.volume_get(self.ctxt, volume_id)['status'])
    def setUp(self):
        super(VolumeRpcAPITestCase, self).setUp()
        self.context = context.get_admin_context()
        vol = {}
        vol["host"] = "fake_host"
        vol["availability_zone"] = CONF.storage_availability_zone
        vol["status"] = "available"
        vol["attach_status"] = "detached"
        vol["metadata"] = {"test_key": "test_val"}
        vol["size"] = 1
        volume = db.volume_create(self.context, vol)

        kwargs = {
            "status": "creating",
            "progress": "0%",
            "display_name": "fake_name",
            "display_description": "fake_description",
        }
        snapshot = tests_utils.create_snapshot(self.context, vol["id"], **kwargs)

        source_group = tests_utils.create_consistencygroup(
            self.context,
            availability_zone=CONF.storage_availability_zone,
            volume_type="type1,type2",
            host="fakehost@fakedrv#fakepool",
        )

        cgsnapshot = tests_utils.create_cgsnapshot(self.context, consistencygroup_id=source_group["id"])

        group = tests_utils.create_consistencygroup(
            self.context,
            availability_zone=CONF.storage_availability_zone,
            volume_type="type1,type2",
            host="fakehost@fakedrv#fakepool",
            cgsnapshot_id=cgsnapshot["id"],
        )

        group2 = tests_utils.create_consistencygroup(
            self.context,
            availability_zone=CONF.storage_availability_zone,
            volume_type="type1,type2",
            host="fakehost@fakedrv#fakepool",
            source_cgid=source_group["id"],
        )

        group = objects.ConsistencyGroup.get_by_id(self.context, group.id)
        group2 = objects.ConsistencyGroup.get_by_id(self.context, group2.id)
        self.fake_volume = jsonutils.to_primitive(volume)
        self.fake_volume_metadata = volume["volume_metadata"]
        self.fake_snapshot = snapshot
        self.fake_reservations = ["RESERVATION"]
        self.fake_cg = group
        self.fake_cg2 = group2
        self.fake_src_cg = jsonutils.to_primitive(source_group)
        self.fake_cgsnap = jsonutils.to_primitive(cgsnapshot)
Example #33
0
    def test_create_from_src(self, mock_validate_host, mock_snap_get_all,
                             mock_group_snap_get, mock_update_quota,
                             mock_create_from_group,
                             mock_create_from_snap):
        name = "test_group"
        description = "this is a test group"
        grp = utils.create_group(self.ctxt, group_type_id=fake.GROUP_TYPE_ID,
                                 volume_type_ids=[fake.VOLUME_TYPE_ID],
                                 availability_zone='nova',
                                 name=name, description=description,
                                 status=fields.GroupStatus.AVAILABLE,)

        vol1 = utils.create_volume(
            self.ctxt,
            availability_zone='nova',
            volume_type_id=fake.VOLUME_TYPE_ID,
            group_id=grp.id)

        snap = utils.create_snapshot(self.ctxt, vol1.id,
                                     volume_type_id=fake.VOLUME_TYPE_ID,
                                     status=fields.SnapshotStatus.AVAILABLE)
        mock_snap_get_all.return_value = [snap]
        mock_validate_host.return_host = True

        grp_snap = utils.create_group_snapshot(
            self.ctxt, grp.id,
            group_type_id=fake.GROUP_TYPE_ID,
            status=fields.GroupStatus.AVAILABLE)
        mock_group_snap_get.return_value = grp_snap

        grp2 = utils.create_group(self.ctxt,
                                  group_type_id=fake.GROUP_TYPE_ID,
                                  volume_type_ids=[fake.VOLUME_TYPE_ID],
                                  availability_zone='nova',
                                  name=name, description=description,
                                  status=fields.GroupStatus.CREATING,
                                  group_snapshot_id=grp_snap.id)

        with mock.patch('cinder.objects.Group') as mock_group:
            mock_group.return_value = grp2
            with mock.patch('cinder.objects.group.Group.create'):
                ret_group = self.group_api.create_from_src(
                    self.ctxt, name, description,
                    group_snapshot_id=grp_snap.id,
                    source_group_id=None)
                self.assertEqual(grp2.obj_to_primitive(),
                                 ret_group.obj_to_primitive())
                mock_create_from_snap.assert_called_once_with(
                    self.ctxt, grp2, grp_snap.id)

        snap.destroy()
        grp_snap.destroy()
        vol1.destroy()
        grp.destroy()
        grp2.destroy()
Example #34
0
    def setUp(self):
        super(VolumeRpcAPITestCase, self).setUp()
        self.context = context.get_admin_context()
        vol = {}
        vol['host'] = 'fake_host'
        vol['availability_zone'] = CONF.storage_availability_zone
        vol['status'] = "available"
        vol['attach_status'] = "detached"
        vol['metadata'] = {"test_key": "test_val"}
        vol['size'] = 1
        volume = db.volume_create(self.context, vol)

        kwargs = {
            'status': "creating",
            'progress': '0%',
            'display_name': 'fake_name',
            'display_description': 'fake_description'}
        snapshot = tests_utils.create_snapshot(self.context, vol['id'],
                                               **kwargs)

        source_group = tests_utils.create_consistencygroup(
            self.context,
            availability_zone=CONF.storage_availability_zone,
            volume_type='type1,type2',
            host='fakehost@fakedrv#fakepool')

        cgsnapshot = tests_utils.create_cgsnapshot(
            self.context,
            consistencygroup_id=source_group.id)

        group = tests_utils.create_consistencygroup(
            self.context,
            availability_zone=CONF.storage_availability_zone,
            volume_type='type1,type2',
            host='fakehost@fakedrv#fakepool',
            cgsnapshot_id=cgsnapshot.id)

        group2 = tests_utils.create_consistencygroup(
            self.context,
            availability_zone=CONF.storage_availability_zone,
            volume_type='type1,type2',
            host='fakehost@fakedrv#fakepool',
            source_cgid=source_group.id)

        group = objects.ConsistencyGroup.get_by_id(self.context, group.id)
        group2 = objects.ConsistencyGroup.get_by_id(self.context, group2.id)
        cgsnapshot = objects.CGSnapshot.get_by_id(self.context, cgsnapshot.id)
        self.fake_volume = jsonutils.to_primitive(volume)
        self.fake_volume_metadata = volume["volume_metadata"]
        self.fake_snapshot = snapshot
        self.fake_reservations = ["RESERVATION"]
        self.fake_cg = group
        self.fake_cg2 = group2
        self.fake_src_cg = jsonutils.to_primitive(source_group)
        self.fake_cgsnap = cgsnapshot
Example #35
0
    def test_create_from_src(self, mock_validate_host, mock_snap_get_all,
                             mock_group_snap_get, mock_update_quota,
                             mock_create_from_group,
                             mock_create_from_snap):
        name = "test_group"
        description = "this is a test group"
        grp = utils.create_group(self.ctxt, group_type_id=fake.GROUP_TYPE_ID,
                                 volume_type_ids=[fake.VOLUME_TYPE_ID],
                                 availability_zone='nova',
                                 name=name, description=description,
                                 status=fields.GroupStatus.AVAILABLE,)

        vol1 = utils.create_volume(
            self.ctxt,
            availability_zone='nova',
            volume_type_id=fake.VOLUME_TYPE_ID,
            group_id=grp.id)

        snap = utils.create_snapshot(self.ctxt, vol1.id,
                                     volume_type_id=fake.VOLUME_TYPE_ID,
                                     status=fields.SnapshotStatus.AVAILABLE)
        mock_snap_get_all.return_value = [snap]
        mock_validate_host.return_host = True

        grp_snap = utils.create_group_snapshot(
            self.ctxt, grp.id,
            group_type_id=fake.GROUP_TYPE_ID,
            status=fields.GroupStatus.AVAILABLE)
        mock_group_snap_get.return_value = grp_snap

        grp2 = utils.create_group(self.ctxt,
                                  group_type_id=fake.GROUP_TYPE_ID,
                                  volume_type_ids=[fake.VOLUME_TYPE_ID],
                                  availability_zone='nova',
                                  name=name, description=description,
                                  status=fields.GroupStatus.CREATING,
                                  group_snapshot_id=grp_snap.id)

        with mock.patch('cinder.objects.Group') as mock_group:
            mock_group.return_value = grp2
            with mock.patch('cinder.objects.group.Group.create'):
                ret_group = self.group_api.create_from_src(
                    self.ctxt, name, description,
                    group_snapshot_id=grp_snap.id,
                    source_group_id=None)
                self.assertEqual(grp2.obj_to_primitive(),
                                 ret_group.obj_to_primitive())
                mock_create_from_snap.assert_called_once_with(
                    self.ctxt, grp2, grp_snap.id)

        snap.destroy()
        grp_snap.destroy()
        vol1.destroy()
        grp.destroy()
        grp2.destroy()
Example #36
0
    def test_transfer_accept_with_snapshots(self, mock_notify):
        svc = self.start_service('volume', host='test_host')
        self.addCleanup(svc.stop)
        tx_api = transfer_api.API()
        volume = utils.create_volume(self.ctxt,
                                     volume_type_id=fake.VOLUME_TYPE_ID,
                                     updated_at=self.updated_at)
        utils.create_volume_type(self.ctxt.elevated(),
                                 id=fake.VOLUME_TYPE_ID,
                                 name="test_type")
        utils.create_snapshot(self.ctxt, volume.id, status='available')
        transfer = tx_api.create(self.ctxt, volume.id, 'Description')

        # Get volume and snapshot quota before accept
        self.ctxt.user_id = fake.USER2_ID
        self.ctxt.project_id = fake.PROJECT2_ID
        usages = db.quota_usage_get_all_by_project(self.ctxt,
                                                   self.ctxt.project_id)
        self.assertEqual(0, usages.get('volumes', {}).get('in_use', 0))
        self.assertEqual(0, usages.get('snapshots', {}).get('in_use', 0))

        tx_api.accept(self.ctxt, transfer['id'], transfer['auth_key'])
        volume = objects.Volume.get_by_id(self.ctxt, volume.id)
        self.assertEqual(fake.PROJECT2_ID, volume.project_id)
        self.assertEqual(fake.USER2_ID, volume.user_id)

        calls = [
            mock.call(self.ctxt, mock.ANY, "transfer.accept.start"),
            mock.call(self.ctxt, mock.ANY, "transfer.accept.end")
        ]
        mock_notify.assert_has_calls(calls)
        # The notify_about_volume_usage is called twice at create(),
        # and twice at accept().
        self.assertEqual(4, mock_notify.call_count)

        # Get volume and snapshot quota after accept
        self.ctxt.user_id = fake.USER2_ID
        self.ctxt.project_id = fake.PROJECT2_ID
        usages = db.quota_usage_get_all_by_project(self.ctxt,
                                                   self.ctxt.project_id)
        self.assertEqual(1, usages.get('volumes', {}).get('in_use', 0))
        self.assertEqual(1, usages.get('snapshots', {}).get('in_use', 0))
Example #37
0
    def test_snapshot_list_with_filter(self, filter_name, filter_value,
                                       is_admin_user):
        volume1 = test_utils.create_volume(self.ctx, host='test_host1',
                                           cluster_name='cluster1',
                                           availability_zone='nova1')
        volume2 = test_utils.create_volume(self.ctx, host='test_host2',
                                           cluster_name='cluster2',
                                           availability_zone='nova2')
        snapshot1 = test_utils.create_snapshot(self.ctx, volume1.id)
        test_utils.create_snapshot(self.ctx, volume2.id)

        url = '/v3/snapshots?%s=%s' % (filter_name, filter_value)
        # Generic filtering is introduced since '3,31' and we add
        # 'availability_zone' support by using generic filtering.
        req = fakes.HTTPRequest.blank(url, use_admin_context=is_admin_user,
                                      version=mv.RESOURCE_FILTER)
        res_dict = self.controller.detail(req)

        self.assertEqual(1, len(res_dict['snapshots']))
        self.assertEqual(snapshot1.id, res_dict['snapshots'][0]['id'])
Example #38
0
 def test_existing_snapshot_failed_quota_reserve(self, mock_reserve):
     vol = tests_utils.create_volume(self.context)
     snap = tests_utils.create_snapshot(self.context, vol.id)
     with mock.patch.object(
             self.volume.driver,
             'manage_existing_snapshot_get_size') as mock_get_size:
         mock_get_size.return_value = 1
         self.assertRaises(exception.SnapshotLimitExceeded,
                           self.volume.manage_existing_snapshot,
                           self.context,
                           snap)
Example #39
0
    def test_revert_thin_snapshot(self):

        configuration = conf.Configuration(fake_opt, 'fake_group')
        configuration.lvm_type = 'thin'
        lvm_driver = lvm.LVMVolumeDriver(configuration=configuration, db=db)
        fake_volume = tests_utils.create_volume(self.context,
                                                display_name='fake_volume')
        fake_snapshot = tests_utils.create_snapshot(self.context,
                                                    fake_volume.id)

        self.assertRaises(NotImplementedError, lvm_driver.revert_to_snapshot,
                          self.context, fake_volume, fake_snapshot)
Example #40
0
    def _create_snapshot(self, **kwargs):
        vol_type = test_utils.create_volume_type(self.project_admin_context,
                                                 name='fake_vol_type',
                                                 testcase_instance=self)

        volume = test_utils.create_volume(self.project_member_context,
                                          volume_type_id=vol_type.id,
                                          testcase_instance=self)

        snapshot = test_utils.create_snapshot(self.project_member_context,
                                              volume_id=volume.id,
                                              testcase_instance=self, **kwargs)
        return snapshot
    def _test_failover_model_updates(self,
                                     in_volumes,
                                     in_snapshots,
                                     driver_volumes,
                                     driver_result,
                                     out_volumes,
                                     out_snapshots,
                                     in_groups=None,
                                     out_groups=None,
                                     driver_group_result=None,
                                     secondary_id=None):
        host = volume_utils.extract_host(self.manager.host)
        utils.create_service(self.context, {
            'host': host,
            'binary': constants.VOLUME_BINARY
        })
        for volume in in_volumes:
            utils.create_volume(self.context, self.manager.host, **volume)

        for snapshot in in_snapshots:
            utils.create_snapshot(self.context, **snapshot)

        for group in in_groups:
            utils.create_group(self.context, self.manager.host, **group)

        with mock.patch.object(
                self.manager.driver,
                'failover_host',
                return_value=(secondary_id, driver_result,
                              driver_group_result)) as driver_mock:
            self.manager.failover_host(self.context, secondary_id)

            self.assertSetEqual(driver_volumes,
                                {v.id
                                 for v in driver_mock.call_args[0][1]})

        self._check_failover_db(objects.VolumeList, out_volumes)
        self._check_failover_db(objects.SnapshotList, out_snapshots)
        self._check_failover_db(objects.GroupList, out_groups)
Example #42
0
    def _create_db_snapshots(self, num_snaps):
        volume = utils.create_volume(self.ctx)
        snaps = [utils.create_snapshot(self.ctx,
                                       volume.id,
                                       display_name='snap' + str(i))
                 for i in range(num_snaps)]

        self.addCleanup(db.volume_destroy, self.ctx, volume.id)
        for snap in snaps:
            self.addCleanup(db.snapshot_destroy, self.ctx, snap.id)

        snaps.reverse()
        return volume, snaps
Example #43
0
    def test_init_host_clears_deleting_snapshots(self):
        """Test that init_host will delete a snapshot stuck in deleting."""
        volume = tests_utils.create_volume(self.context, status='deleting',
                                           size=1, host=CONF.host)
        snapshot = tests_utils.create_snapshot(self.context,
                                               volume.id, status='deleting')

        db.worker_create(self.context, resource_type='Volume',
                         resource_id=volume.id, status=volume.status,
                         service_id=self.service_id)

        self.volume.init_host(service_id=self.service_id)
        self.assertRaises(exception.VolumeNotFound, volume.refresh)
        self.assertRaises(exception.SnapshotNotFound, snapshot.refresh)
Example #44
0
 def test_delete_snapshot_in_error_deleting_status(self):
     volume_type = volume_types.get_default_volume_type()
     volume = tests_utils.create_volume(self.context,
                                        volume_type_id=volume_type['id'])
     snapshot = tests_utils.create_snapshot(self.context,
                                            volume['id'],
                                            status='error_deleting')
     # explicitly set volume_type_id in stub
     snapshot['volume_type_id'] = volume_type['id']
     api = lunr_api.API()
     api.delete_snapshot(self.context, snapshot)
     post_delete = db.snapshot_get(self.context, snapshot['id'])
     self.assertIsNot(snapshot['status'], post_delete['status'])
     self.assertEqual(post_delete['status'], 'deleting')
Example #45
0
    def test_create_snapshot(self):
        self.driver.do_setup(None)

        vol_type = test_utils.create_volume_type(self.ctxt,
                                                 self,
                                                 name='my_vol_type')
        volume = test_utils.create_volume(self.ctxt,
                                          size=4,
                                          volume_type_id=vol_type.id)
        snapshot = test_utils.create_snapshot(self.ctxt, volume_id=volume.id)

        self.driver.create_volume(volume)
        self.driver.create_snapshot(snapshot)
        self.driver.delete_volume(volume)
        db.volume_destroy(self.ctxt, volume.id)
Example #46
0
    def test_create_volume_from_snapshot_sparse(self):

        self.configuration.lvm_type = 'thin'
        lvm_driver = lvm.LVMVolumeDriver(configuration=self.configuration,
                                         db=db)

        with mock.patch.object(lvm_driver, 'vg'):

            # Test case for thin LVM
            lvm_driver._sparse_copy_volume = True
            src_volume = tests_utils.create_volume(self.context)
            snapshot_ref = tests_utils.create_snapshot(self.context,
                                                       src_volume['id'])
            dst_volume = tests_utils.create_volume(self.context)
            lvm_driver.create_volume_from_snapshot(dst_volume, snapshot_ref)
Example #47
0
    def test_update_encryption_key_id(self, mock_barbican_client):
        vol = self.create_volume()

        snap_ids = [fake.SNAPSHOT_ID, fake.SNAPSHOT2_ID, fake.SNAPSHOT3_ID]
        for snap_id in snap_ids:
            tests_utils.create_snapshot(self.context, vol.id, id=snap_id)

        # Barbican's secret.store() returns a URI that contains the
        # secret's key ID at the end.
        secret_ref = 'http://some/path/' + fake.ENCRYPTION_KEY_ID
        mock_secret = mock.MagicMock()
        mock_secret.store.return_value = secret_ref

        mock_barbican_client.return_value.secrets.create.return_value \
            = mock_secret

        migration.migrate_fixed_key(self.my_vols, conf=self.conf)
        vol_db = db.volume_get(self.context, vol.id)
        self.assertEqual(fake.ENCRYPTION_KEY_ID, vol_db['encryption_key_id'])

        for snap_id in snap_ids:
            snap_db = db.snapshot_get(self.context, snap_id)
            self.assertEqual(fake.ENCRYPTION_KEY_ID,
                             snap_db['encryption_key_id'])
Example #48
0
    def test_create_vol_with_group_id_driver_exception(self,
                                                       mock_create_volume,
                                                       mock_create_from_snap,
                                                       mock_create_cloned_vol):
        """Test create a volume with group_id but driver exception."""
        # create_raw_volume with group id, but driver exception
        mock_create_volume.side_effect = exception.CinderException
        group = tests_utils.create_group(
            self.context,
            availability_zone=CONF.storage_availability_zone,
            volume_type_ids=[fake.VOLUME_TYPE_ID],
            group_type_id=fake.GROUP_TYPE_ID,
            host=CONF.host)
        self.volume.create_group(self.context, group)

        volume = tests_utils.create_volume(self.context,
                                           group_id=group.id,
                                           volume_type_id=fake.VOLUME_TYPE_ID,
                                           status='available',
                                           host=group.host)
        self.assertRaises(exception.CinderException, self.volume.create_volume,
                          self.context, volume)
        self.assertIsNone(volume.consistencygroup_id)

        # create volume from_snapshot with group id but driver exception
        mock_create_from_snap.side_effect = exception.CinderException
        snapshot = tests_utils.create_snapshot(self.context, volume.id)
        volume2 = tests_utils.create_volume(self.context,
                                            group_id=group.id,
                                            snapshot_id=snapshot.id,
                                            status='available',
                                            host=group.host,
                                            volume_type_id=fake.VOLUME_TYPE_ID)
        self.assertRaises(exception.CinderException, self.volume.create_volume,
                          self.context, volume2)
        self.assertIsNone(volume2.consistencygroup_id)

        # create cloned volume with group_id but driver exception
        mock_create_cloned_vol.side_effect = exception.CinderException
        volume3 = tests_utils.create_volume(self.context,
                                            group_id=group.id,
                                            source_volid=volume.id,
                                            status='available',
                                            host=group.host,
                                            volume_type_id=fake.VOLUME_TYPE_ID)
        self.assertRaises(exception.CinderException, self.volume.create_volume,
                          self.context, volume3)
        self.assertIsNone(volume3.consistencygroup_id)
Example #49
0
    def test_snapshot_show_with_use_quota(self, present, value, microversion):
        volume = test_utils.create_volume(self.ctx,
                                          host='test_host1',
                                          cluster_name='cluster1',
                                          availability_zone='nova1')
        snapshot = test_utils.create_snapshot(self.ctx,
                                              volume.id,
                                              use_quota=value)

        url = '/v3/snapshots?%s' % snapshot.id
        req = fakes.HTTPRequest.blank(url, version=microversion)
        res_dict = self.controller.show(req, snapshot.id)['snapshot']
        if present:
            self.assertIs(value, res_dict['consumes_quota'])
        else:
            self.assertNotIn('consumes_quota', res_dict)
Example #50
0
 def test_create_volume_from_snapshot(self):
     self.driver.do_setup(None)
     vol_type = test_utils.create_volume_type(self.ctxt,
                                              self,
                                              name='my_vol_type')
     volume = test_utils.create_volume(self.ctxt,
                                       size=4,
                                       volume_type_id=vol_type.id)
     snapshot = test_utils.create_snapshot(self.ctxt, volume_id=volume.id)
     self.driver.create_volume_from_snapshot(volume, snapshot)
     proj = self.db.data["projects"][lightos.LIGHTOS_DEFAULT_PROJECT_NAME]
     actual_volumes = proj["volumes"]
     self.assertEqual(1, len(actual_volumes))
     self.driver.delete_snapshot(snapshot)
     self.driver.delete_volume(volume)
     db.volume_destroy(self.ctxt, volume.id)
     db.snapshot_destroy(self.ctxt, snapshot.id)
Example #51
0
 def _create_multiple_snapshots_with_different_project(self):
     volume1 = test_utils.create_volume(self.ctx, project=fake.PROJECT_ID)
     volume2 = test_utils.create_volume(self.ctx, project=fake.PROJECT2_ID)
     test_utils.create_snapshot(
         context.RequestContext(fake.USER_ID, fake.PROJECT_ID, True),
         volume1.id)
     test_utils.create_snapshot(
         context.RequestContext(fake.USER_ID, fake.PROJECT_ID, True),
         volume1.id)
     test_utils.create_snapshot(
         context.RequestContext(fake.USER_ID, fake.PROJECT2_ID, True),
         volume2.id)
Example #52
0
    def test_create_volume_from_snapshot_sparse_extend(self):

        self.configuration.lvm_type = 'thin'
        lvm_driver = lvm.LVMVolumeDriver(configuration=self.configuration,
                                         db=db)

        with mock.patch.object(lvm_driver, 'vg'), \
                mock.patch.object(lvm_driver, 'extend_volume') as mock_extend:

            # Test case for thin LVM
            lvm_driver._sparse_copy_volume = True
            src_volume = tests_utils.create_volume(self.context)
            snapshot_ref = tests_utils.create_snapshot(self.context,
                                                       src_volume['id'])
            dst_volume = tests_utils.create_volume(self.context)
            dst_volume['size'] = snapshot_ref['volume_size'] + 1
            lvm_driver.create_volume_from_snapshot(dst_volume, snapshot_ref)
            mock_extend.assert_called_with(dst_volume, dst_volume['size'])
Example #53
0
    def setUp(self):
        super(VolumeRPCAPITestCase, self).setUp()
        self.rpcapi = volume_rpcapi.VolumeAPI
        self.base_version = '3.0'
        vol = {}
        vol['host'] = 'fake_host'
        vol['availability_zone'] = CONF.storage_availability_zone
        vol['status'] = "available"
        vol['attach_status'] = "detached"
        vol['metadata'] = {"test_key": "test_val"}
        vol['size'] = 1
        vol['volume_type_id'] = fake.VOLUME_TYPE_ID
        volume = db.volume_create(self.context, vol)

        kwargs = {
            'status': fields.SnapshotStatus.CREATING,
            'progress': '0%',
            'display_name': 'fake_name',
            'display_description': 'fake_description'
        }
        snapshot = tests_utils.create_snapshot(self.context, vol['id'],
                                               **kwargs)

        generic_group = tests_utils.create_group(
            self.context,
            availability_zone=CONF.storage_availability_zone,
            group_type_id='group_type1',
            host='fakehost@fakedrv#fakepool')

        group_snapshot = tests_utils.create_group_snapshot(
            self.context,
            group_id=generic_group.id,
            group_type_id=fake.GROUP_TYPE_ID)

        self.fake_volume = jsonutils.to_primitive(volume)
        self.fake_volume_obj = fake_volume.fake_volume_obj(self.context, **vol)
        self.fake_snapshot = snapshot
        self.fake_reservations = ["RESERVATION"]
        self.fake_backup_obj = fake_backup.fake_backup_obj(self.context)
        self.fake_group = generic_group
        self.fake_group_snapshot = group_snapshot

        self.can_send_version_mock = self.patch(
            'oslo_messaging.RPCClient.can_send_version', return_value=True)
Example #54
0
    def test_force_delete_snapshot(self):
        """Test snapshot can be forced to delete."""
        fake_volume = tests_utils.create_volume(self.context)
        fake_snapshot = tests_utils.create_snapshot(self.context,
                                                    fake_volume.id,
                                                    status='error_deleting')
        # 'error_deleting' snapshot can't be deleted
        self.assertRaises(exception.InvalidSnapshot,
                          self.volume_api.delete_snapshot, self.context,
                          fake_snapshot)

        # delete with force
        self.volume_api.delete_snapshot(self.context,
                                        fake_snapshot,
                                        force=True)

        # status is deleting
        fake_snapshot.refresh()
        self.assertEqual(fields.SnapshotStatus.DELETING, fake_snapshot.status)
Example #55
0
    def test_create_group_from_src_snap(self, mock_validate):
        self.mock_object(volume_api.API, "create", v3_fakes.fake_volume_create)

        group = utils.create_group(self.ctxt,
                                   group_type_id=fake.GROUP_TYPE_ID,
                                   volume_type_ids=[fake.VOLUME_TYPE_ID])
        volume = utils.create_volume(self.ctxt,
                                     group_id=group.id,
                                     volume_type_id=fake.VOLUME_TYPE_ID)
        group_snapshot = utils.create_group_snapshot(
            self.ctxt, group_id=group.id, group_type_id=group.group_type_id)
        snapshot = utils.create_snapshot(
            self.ctxt,
            volume.id,
            group_snapshot_id=group_snapshot.id,
            status=fields.SnapshotStatus.AVAILABLE,
            volume_type_id=volume.volume_type_id)

        test_grp_name = 'test grp'
        body = {
            "create-from-src": {
                "name": test_grp_name,
                "description": "Group 1",
                "group_snapshot_id": group_snapshot.id
            }
        }
        req = fakes.HTTPRequest.blank('/v3/%s/groups/action' % fake.PROJECT_ID,
                                      version=GROUP_FROM_SRC_MICRO_VERSION)
        res_dict = self.controller.create_from_src(req, body)

        self.assertIn('id', res_dict['group'])
        self.assertEqual(test_grp_name, res_dict['group']['name'])
        self.assertTrue(mock_validate.called)

        grp_ref = objects.Group.get_by_id(self.ctxt.elevated(),
                                          res_dict['group']['id'])

        grp_ref.destroy()
        snapshot.destroy()
        volume.destroy()
        group.destroy()
        group_snapshot.destroy()
Example #56
0
    def test_create_consistencygroup_from_src(self, mock_validate):
        self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create)

        consistencygroup = utils.create_consistencygroup(self.ctxt)
        volume_id = utils.create_volume(
            self.ctxt, consistencygroup_id=consistencygroup.id)['id']
        cgsnapshot = utils.create_cgsnapshot(
            self.ctxt, consistencygroup_id=consistencygroup.id)
        snapshot = utils.create_snapshot(self.ctxt,
                                         volume_id,
                                         cgsnapshot_id=cgsnapshot.id,
                                         status='available')

        test_cg_name = 'test cg'
        body = {
            "consistencygroup-from-src": {
                "name": test_cg_name,
                "description": "Consistency Group 1",
                "cgsnapshot_id": cgsnapshot.id
            }
        }
        req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src')
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.body = json.dumps(body)
        res = req.get_response(fakes.wsgi_app())
        res_dict = json.loads(res.body)

        self.assertEqual(202, res.status_int)
        self.assertIn('id', res_dict['consistencygroup'])
        self.assertEqual(test_cg_name, res_dict['consistencygroup']['name'])
        self.assertTrue(mock_validate.called)

        cg_ref = objects.ConsistencyGroup.get_by_id(
            self.ctxt.elevated(), res_dict['consistencygroup']['id'])

        cg_ref.destroy()
        snapshot.destroy()
        db.volume_destroy(self.ctxt.elevated(), volume_id)
        consistencygroup.destroy()
        cgsnapshot.destroy()
Example #57
0
    def test_backup_volume_inuse(self, mock_volume_get,
                                 mock_get_connector_properties,
                                 mock_file_open,
                                 mock_temporary_chown):

        vol = tests_utils.create_volume(self.context,
                                        status='backing-up',
                                        previous_status='in-use')
        self.context.user_id = fake.USER_ID
        self.context.project_id = fake.PROJECT_ID

        mock_volume_get.return_value = vol
        temp_snapshot = tests_utils.create_snapshot(self.context, vol['id'])
        backup = tests_utils.create_backup(self.context,
                                           vol['id'])
        backup_obj = objects.Backup.get_by_id(self.context, backup.id)
        properties = {}
        attach_info = {'device': {'path': '/dev/null'}}
        backup_service = mock.Mock()

        self.volume.driver._detach_volume = mock.MagicMock()
        self.volume.driver._attach_volume = mock.MagicMock()
        self.volume.driver.terminate_connection = mock.MagicMock()
        self.volume.driver._create_temp_snapshot = mock.MagicMock()
        self.volume.driver._delete_temp_snapshot = mock.MagicMock()

        mock_get_connector_properties.return_value = properties
        f = mock_file_open.return_value = open('/dev/null', 'rb')

        backup_service.backup(backup_obj, f, None)
        self.volume.driver._attach_volume.return_value = attach_info
        self.volume.driver._create_temp_snapshot.return_value = temp_snapshot

        self.volume.driver.backup_volume(self.context, backup_obj,
                                         backup_service)

        mock_volume_get.assert_called_with(self.context, vol['id'])
        self.volume.driver._create_temp_snapshot.assert_called_once_with(
            self.context, vol)
        self.volume.driver._delete_temp_snapshot.assert_called_once_with(
            self.context, temp_snapshot)
    def test_show_cgsnapshot(self):
        vol_type = utils.create_volume_type(context.get_admin_context(),
                                            self, name='my_vol_type')
        consistencygroup = utils.create_group(
            self.context,
            group_type_id=fake.GROUP_TYPE_ID,
            volume_type_ids=[vol_type['id']])
        volume_id = utils.create_volume(self.context,
                                        volume_type_id=vol_type['id'],
                                        group_id=
                                        consistencygroup.id)['id']
        cgsnapshot = utils.create_group_snapshot(
            self.context, group_id=consistencygroup.id,
            group_type_id=fake.GROUP_TYPE_ID,)
        snapshot_id = utils.create_snapshot(
            self.context,
            volume_type_id=vol_type['id'],
            volume_id=volume_id,
            group_snapshot_id=cgsnapshot.id)['id']

        req = webob.Request.blank('/v2/%s/cgsnapshots/%s' % (
            fake.PROJECT_ID, cgsnapshot.id))
        req.method = 'GET'
        req.headers['Content-Type'] = 'application/json'
        res = req.get_response(fakes.wsgi_app(
            fake_auth_context=self.user_ctxt))
        res_dict = jsonutils.loads(res.body)

        self.assertEqual(http_client.OK, res.status_int)
        self.assertEqual('this is a test group snapshot',
                         res_dict['cgsnapshot']['description'])

        self.assertEqual('test_group_snapshot',
                         res_dict['cgsnapshot']['name'])
        self.assertEqual('creating', res_dict['cgsnapshot']['status'])

        db.snapshot_destroy(context.get_admin_context(), snapshot_id)
        cgsnapshot.destroy()
        db.volume_destroy(context.get_admin_context(), volume_id)
        consistencygroup.destroy()
Example #59
0
    def test_init_host_sync_provider_info_no_update_cluster(self, mock_update):
        cluster_name = 'mycluster'
        self.volume.cluster = cluster_name
        vol0 = tests_utils.create_volume(self.context,
                                         size=1,
                                         host=CONF.host,
                                         cluster_name=cluster_name)
        vol1 = tests_utils.create_volume(self.context,
                                         size=1,
                                         host=CONF.host + '2',
                                         cluster_name=cluster_name)
        vol2 = tests_utils.create_volume(self.context, size=1, host=CONF.host)
        vol3 = tests_utils.create_volume(self.context,
                                         size=1,
                                         host=CONF.host,
                                         cluster_name=cluster_name + '2')
        snap0 = tests_utils.create_snapshot(self.context, vol0.id)
        snap1 = tests_utils.create_snapshot(self.context, vol1.id)
        tests_utils.create_snapshot(self.context, vol2.id)
        tests_utils.create_snapshot(self.context, vol3.id)
        mock_update.return_value = ([], [])
        # initialize
        self.volume.init_host(service_id=self.service_id)
        # Grab volume and snapshot objects
        vol0_obj = objects.Volume.get_by_id(context.get_admin_context(),
                                            vol0.id)
        vol1_obj = objects.Volume.get_by_id(context.get_admin_context(),
                                            vol1.id)
        snap0_obj = objects.Snapshot.get_by_id(self.context, snap0.id)
        snap1_obj = objects.Snapshot.get_by_id(self.context, snap1.id)

        self.assertSetEqual({vol0.id, vol1.id},
                            {vol.id
                             for vol in mock_update.call_args[0][0]})
        self.assertSetEqual({snap0.id, snap1.id},
                            {snap.id
                             for snap in mock_update.call_args[0][1]})
        # Check provider ids are not changed
        self.assertIsNone(vol0_obj.provider_id)
        self.assertIsNone(vol1_obj.provider_id)
        self.assertIsNone(snap0_obj.provider_id)
        self.assertIsNone(snap1_obj.provider_id)
        # Clean up
        self.volume.delete_snapshot(self.context, snap0_obj)
        self.volume.delete_snapshot(self.context, snap1_obj)
        self.volume.delete_volume(self.context, vol0)
        self.volume.delete_volume(self.context, vol1)
Example #60
0
    def test_create_consistencygroup_from_src_cgsnapshot(self):
        self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create)

        ctxt = context.RequestContext('fake', 'fake', auth_token=True)
        consistencygroup_id = utils.create_consistencygroup(ctxt)['id']
        volume_id = utils.create_volume(
            ctxt, consistencygroup_id=consistencygroup_id)['id']
        cgsnapshot_id = utils.create_cgsnapshot(
            ctxt, consistencygroup_id=consistencygroup_id)['id']
        snapshot_id = utils.create_snapshot(ctxt,
                                            volume_id,
                                            cgsnapshot_id=cgsnapshot_id,
                                            status='available')['id']

        test_cg_name = 'test cg'
        body = {
            "consistencygroup-from-src": {
                "name": test_cg_name,
                "description": "Consistency Group 1",
                "cgsnapshot_id": cgsnapshot_id
            }
        }
        req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src')
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.body = json.dumps(body)
        res = req.get_response(fakes.wsgi_app())
        res_dict = json.loads(res.body)

        self.assertEqual(202, res.status_int)
        self.assertIn('id', res_dict['consistencygroup'])
        self.assertEqual(test_cg_name, res_dict['consistencygroup']['name'])

        db.consistencygroup_destroy(ctxt.elevated(),
                                    res_dict['consistencygroup']['id'])
        db.snapshot_destroy(ctxt.elevated(), snapshot_id)
        db.cgsnapshot_destroy(ctxt.elevated(), cgsnapshot_id)
        db.volume_destroy(ctxt.elevated(), volume_id)
        db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id)