def test_create_delete_share_error(self): """Test share can be created and deleted with error.""" def _raise_not_found(self, *args, **kwargs): raise exception.NotFound() self.stubs.Set(self.share_manager.driver, "create_share", mock.Mock(side_effect=_raise_not_found)) self.stubs.Set(self.share_manager.driver, "delete_share", mock.Mock(side_effect=_raise_not_found)) share = self._create_share() share_id = share['id'] self.assertRaises(exception.NotFound, self.share_manager.create_share, self.context, share_id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'error') self.assertRaises(exception.NotFound, self.share_manager.delete_share, self.context, share_id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'error_deleting') self.share_manager.driver.create_share.assert_called_once_with( utils.IsAMatcher(context.RequestContext), utils.IsAMatcher(models.Share), share_server=None) self.share_manager.driver.delete_share.assert_called_once_with( utils.IsAMatcher(context.RequestContext), utils.IsAMatcher(models.Share), share_server=None)
def test_create_share_with_server_created(self): """Test share can be created and share server is created.""" share_net = self._create_share_network() share = self._create_share(share_network_id=share_net['id']) self._create_share_server(share_network_id=share_net['id'], host=self.share_manager.host, state='ERROR') share_id = share['id'] fake_server = {'id': 'fake_srv_id'} self.stubs.Set(db, 'share_server_create', mock.Mock(return_value=fake_server)) self.stubs.Set(self.share_manager, '_setup_server', mock.Mock(return_value=fake_server)) self.share_manager.create_share(self.context, share_id) self.assertEqual( share_id, db.share_get(context.get_admin_context(), share_id).id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'available') self.assertEqual(shr['share_server_id'], 'fake_srv_id') db.share_server_create.assert_called_once_with( utils.IsAMatcher(context.RequestContext), mock.ANY) self.share_manager._setup_server.assert_called_once_with( utils.IsAMatcher(context.RequestContext), fake_server)
def test_create_delete_share_error(self): """Test share can be created and deleted with error.""" def _fake_create_export(self, context, share): raise exception.NotFound() def _fake_deallocate_container(self, context, share): raise exception.NotFound() self.stubs.Set(FakeShareDriver, "create_export", _fake_create_export) self.stubs.Set(FakeShareDriver, "deallocate_container", _fake_deallocate_container) share = self._create_share() share_id = share['id'] self.assertRaises(exception.NotFound, self.share.create_share, self.context, share_id) shr = db.share_get(self.context, share_id) self.assertEquals(shr['status'], 'error') self.assertRaises(exception.NotFound, self.share.delete_share, self.context, share_id) shr = db.share_get(self.context, share_id) self.assertEquals(shr['status'], 'error_deleting')
def test_create_delete_share_error(self): """Test share can be created and deleted with error.""" def _fake_create_share(self, *args, **kwargs): raise exception.NotFound() def _fake_delete_share(self, *args, **kwargs): raise exception.NotFound() self.stubs.Set(FakeShareDriver, "create_share", _fake_create_share) self.stubs.Set(FakeShareDriver, "delete_share", _fake_delete_share) share = self._create_share() share_id = share['id'] self.assertRaises(exception.NotFound, self.share_manager.create_share, self.context, share_id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'error') self.assertRaises(exception.NotFound, self.share_manager.delete_share, self.context, share_id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'error_deleting')
def test_create_share_with_server_created(self): """Test share can be created and share server is created.""" share_net = self._create_share_network() share = self._create_share(share_network_id=share_net['id']) self._create_share_server( share_network_id=share_net['id'], host=self.share_manager.host, state='ERROR') share_id = share['id'] fake_server = {'id': 'fake_srv_id'} self.stubs.Set(db, 'share_server_create', mock.Mock(return_value=fake_server)) self.stubs.Set(self.share_manager, '_setup_server', mock.Mock(return_value=fake_server)) self.share_manager.create_share(self.context, share_id) self.assertEqual(share_id, db.share_get(context.get_admin_context(), share_id).id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'available') self.assertEqual(shr['share_server_id'], 'fake_srv_id') db.share_server_create.assert_called_once_with( utils.IsAMatcher(context.RequestContext), mock.ANY) self.share_manager._setup_server.assert_called_once_with( utils.IsAMatcher(context.RequestContext), fake_server)
def test_get_and_update_access_rules_status_conditionally_change( self, initial_status, change_allowed): share = db_utils.create_share(access_rules_status=initial_status, status=constants.STATUS_AVAILABLE) share = db.share_get(self.context, share['id']) self.assertEqual(initial_status, share['access_rules_status']) conditionally_change = { constants.SHARE_INSTANCE_RULES_SYNCING: constants.STATUS_ACTIVE, } updated_instance = (self.access_helper. get_and_update_share_instance_access_rules_status( self.context, conditionally_change=conditionally_change, share_instance_id=share['instance']['id'])) share = db.share_get(self.context, share['id']) if change_allowed: self.assertEqual(constants.STATUS_ACTIVE, share['access_rules_status']) self.assertIsNotNone(updated_instance) else: self.assertEqual(initial_status, share['access_rules_status']) self.assertIsNone(updated_instance)
def test_get_and_update_access_rules_status_conditionally_change( self, initial_status, change_allowed): share = db_utils.create_share(access_rules_status=initial_status, status=constants.STATUS_AVAILABLE) share = db.share_get(self.context, share['id']) self.assertEqual(initial_status, share['access_rules_status']) conditionally_change = { constants.SHARE_INSTANCE_RULES_SYNCING: constants.STATUS_ACTIVE, } updated_instance = ( self.access_helper. get_and_update_share_instance_access_rules_status( self.context, conditionally_change=conditionally_change, share_instance_id=share['instance']['id']) ) share = db.share_get(self.context, share['id']) if change_allowed: self.assertEqual(constants.STATUS_ACTIVE, share['access_rules_status']) self.assertIsNotNone(updated_instance) else: self.assertEqual(initial_status, share['access_rules_status']) self.assertIsNone(updated_instance)
def test_get(self): self.mox.StubOutWithMock(db_driver, "share_get") db_driver.share_get(self.context, "fakeid").AndReturn("fakeshare") self.mox.StubOutWithMock(share_api.policy, "check_policy") share_api.policy.check_policy(self.context, "share", "get", "fakeshare") self.mox.ReplayAll() result = self.api.get(self.context, "fakeid") self.assertEqual(result, "fakeshare")
def test_get(self): self.mox.StubOutWithMock(db_driver, 'share_get') db_driver.share_get(self.context, 'fakeid').AndReturn('fakeshare') self.mox.StubOutWithMock(share_api, 'check_policy') share_api.check_policy(self.context, 'get', 'fakeshare') self.mox.ReplayAll() result = self.api.get(self.context, 'fakeid') self.assertEqual(result, 'fakeshare')
def test_delete_snapshot(self): date = datetime.datetime(1, 1, 1, 1, 1, 1) self.mock_utcnow.return_value = date share = fake_share("fakeid") snapshot = fake_snapshot("fakesnapshotid", share_id=share["id"], status="available") self.mox.StubOutWithMock(share_api.policy, "check_policy") share_api.policy.check_policy(self.context, "share", "delete_snapshot", snapshot) self.mox.StubOutWithMock(db_driver, "share_snapshot_update") db_driver.share_snapshot_update(self.context, snapshot["id"], {"status": "deleting"}) self.mox.StubOutWithMock(db_driver, "share_get") db_driver.share_get(self.context, snapshot["share_id"]).AndReturn(share) self.share_rpcapi.delete_snapshot(self.context, snapshot, share["host"]) self.mox.ReplayAll() self.api.delete_snapshot(self.context, snapshot)
def test_create_share_from_snapshot(self): """Test share can be created from snapshot.""" share = self._create_share() share_id = share['id'] snapshot = self._create_snapshot(share_id=share_id) snapshot_id = snapshot['id'] self.share_manager.create_share(self.context, share_id, snapshot_id=snapshot_id) self.assertEqual(share_id, db.share_get(context.get_admin_context(), share_id).id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'available')
def test_get_and_update_access_rules_status_force_status(self): share = db_utils.create_share( access_rule_status=constants.STATUS_ACTIVE, status=constants.STATUS_AVAILABLE) share = db.share_get(self.context, share['id']) self.assertEqual(constants.STATUS_ACTIVE, share['access_rules_status']) self.access_helper.get_and_update_share_instance_access_rules_status( self.context, status=constants.SHARE_INSTANCE_RULES_SYNCING, share_instance_id=share['instance']['id']) share = db.share_get(self.context, share['id']) self.assertEqual(constants.SHARE_INSTANCE_RULES_SYNCING, share['access_rules_status'])
def migrate_share_to_host(self, context, share_id, host, force_host_copy, request_spec, filter_properties=None): """Ensure that the host exists and can accept the share.""" def _migrate_share_set_error(self, context, ex, request_spec): self._set_share_state_and_notify( 'migrate_share_to_host', {'task_state': constants.STATUS_TASK_STATE_MIGRATION_ERROR}, context, ex, request_spec) try: tgt_host = self.driver.host_passes_filters(context, host, request_spec, filter_properties) except exception.NoValidHost as ex: _migrate_share_set_error(self, context, ex, request_spec) except Exception as ex: with excutils.save_and_reraise_exception(): _migrate_share_set_error(self, context, ex, request_spec) else: share_ref = db.share_get(context, share_id) try: share_rpcapi.ShareAPI().migrate_share(context, share_ref, tgt_host, force_host_copy) except Exception as ex: with excutils.save_and_reraise_exception(): _migrate_share_set_error(self, context, ex, request_spec)
def migrate_share_to_host(self, context, share_id, host, force_host_copy, notify, request_spec, filter_properties=None): """Ensure that the host exists and can accept the share.""" def _migrate_share_set_error(self, context, ex, request_spec): self._set_share_state_and_notify( 'migrate_share_to_host', {'task_state': constants.TASK_STATE_MIGRATION_ERROR}, context, ex, request_spec) try: tgt_host = self.driver.host_passes_filters(context, host, request_spec, filter_properties) except exception.NoValidHost as ex: with excutils.save_and_reraise_exception(): _migrate_share_set_error(self, context, ex, request_spec) except Exception as ex: with excutils.save_and_reraise_exception(): _migrate_share_set_error(self, context, ex, request_spec) else: share_ref = db.share_get(context, share_id) try: share_rpcapi.ShareAPI().migration_start( context, share_ref, tgt_host, force_host_copy, notify) except Exception as ex: with excutils.save_and_reraise_exception(): _migrate_share_set_error(self, context, ex, request_spec)
def manage_share(self, context, share_id, driver_options, request_spec, filter_properties=None): """Ensure that the host exists and can accept the share.""" def _manage_share_set_error(self, context, ex, request_spec): # NOTE(haixin) if failed to scheduler backend for manage share, # and we do not commit quota usages here, so we should set size 0 # because we don't know the real size of the size, and we will # skip quota cuts when unmanage share with manage_error status. self._set_share_state_and_notify('manage_share', { 'status': constants.STATUS_MANAGE_ERROR, 'size': 0 }, context, ex, request_spec) share_ref = db.share_get(context, share_id) try: self.driver.host_passes_filters(context, share_ref['host'], request_spec, filter_properties) except Exception as ex: with excutils.save_and_reraise_exception(): _manage_share_set_error(self, context, ex, request_spec) else: share_rpcapi.ShareAPI().manage_share(context, share_ref, driver_options)
def extend_share(self, context, share_id, new_size, reservations, request_spec=None, filter_properties=None): def _extend_share_set_error(self, context, ex, request_spec): share_state = {'status': constants.STATUS_AVAILABLE} self._set_share_state_and_notify('extend_share', share_state, context, ex, request_spec) share = db.share_get(context, share_id) try: target_host = self.driver.host_passes_filters( context, share['host'], request_spec, filter_properties) target_host.consume_from_share( {'size': int(new_size) - share['size']}) share_rpcapi.ShareAPI().extend_share(context, share, new_size, reservations) except exception.NoValidHost as ex: quota.QUOTAS.rollback(context, reservations, project_id=share['project_id'], user_id=share['user_id'], share_type_id=share['share_type_id']) _extend_share_set_error(self, context, ex, request_spec) self.message_api.create(context, message_field.Action.EXTEND, share['project_id'], resource_type=message_field.Resource.SHARE, resource_id=share['id'], exception=ex)
def manage_share(self, context, share_id, driver_options, request_spec, filter_properties=None): """Ensure that the host exists and can accept the share.""" def _manage_share_set_error(self, context, ex, request_spec): # NOTE(nidhimittalhada): set size as 1 because design expects size # to be set, it also will allow us to handle delete/unmanage # operations properly with this errored share according to quotas. self._set_share_state_and_notify('manage_share', { 'status': constants.STATUS_MANAGE_ERROR, 'size': 1 }, context, ex, request_spec) share_ref = db.share_get(context, share_id) try: self.driver.host_passes_filters(context, share_ref['host'], request_spec, filter_properties) except Exception as ex: with excutils.save_and_reraise_exception(): _manage_share_set_error(self, context, ex, request_spec) else: share_rpcapi.ShareAPI().manage_share(context, share_ref, driver_options)
def create(self, req, body): """Add a replica to an existing share.""" context = req.environ['manila.context'] if not self.is_valid_body(body, 'share_replica'): msg = _("Body does not contain 'share_replica' information.") raise exc.HTTPUnprocessableEntity(explanation=msg) share_id = body.get('share_replica').get('share_id') availability_zone = body.get('share_replica').get('availability_zone') share_network_id = body.get('share_replica').get('share_network_id') if not share_id: msg = _("Must provide Share ID to add replica.") raise exc.HTTPBadRequest(explanation=msg) try: share_ref = db.share_get(context, share_id) except exception.NotFound: msg = _("No share exists with ID %s.") raise exc.HTTPNotFound(explanation=msg % share_id) try: new_replica = self.share_api.create_share_replica( context, share_ref, availability_zone=availability_zone, share_network_id=share_network_id) except exception.AvailabilityZoneNotFound as e: raise exc.HTTPBadRequest(explanation=six.text_type(e)) except exception.ReplicationException as e: raise exc.HTTPBadRequest(explanation=six.text_type(e)) except exception.ShareBusyException as e: raise exc.HTTPBadRequest(explanation=six.text_type(e)) return self._view_builder.detail(req, new_replica)
def test_create_share_from_snapshot(self): """Test share can be created from snapshot.""" share = self._create_share() share_id = share['id'] snapshot = self._create_snapshot(share_id=share_id) snapshot_id = snapshot['id'] self.share_manager.create_share(self.context, share_id, snapshot_id=snapshot_id) self.assertEqual( share_id, db.share_get(context.get_admin_context(), share_id).id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'available')
def test_create_delete_share(self): """Test share can be created and deleted.""" share = self._create_share() share_id = share['id'] self._create_access(share_id=share_id) self.share.create_share(self.context, share_id) self.assertEqual(share_id, db.share_get(context.get_admin_context(), share_id).id) shr = db.share_get(self.context, share_id) self.assertEquals(shr['status'], 'available') self.share.delete_share(self.context, share_id) self.assertRaises(exception.NotFound, db.share_get, self.context, share_id)
def test_delete_snapshot(self): date = datetime.datetime(1, 1, 1, 1, 1, 1) timeutils.set_time_override(override_time=date) share = fake_share('fakeid') snapshot = fake_snapshot('fakesnapshotid', share_id=share['id'], status='available') self.mox.StubOutWithMock(share_api, 'check_policy') share_api.check_policy(self.context, 'delete_snapshot', snapshot) self.mox.StubOutWithMock(db_driver, 'share_snapshot_update') db_driver.share_snapshot_update(self.context, snapshot['id'], {'status': 'deleting'}) self.mox.StubOutWithMock(db_driver, 'share_get') db_driver.share_get(self.context, snapshot['share_id']).AndReturn(share) self.share_rpcapi.delete_snapshot(self.context, snapshot, share['host']) self.mox.ReplayAll() self.api.delete_snapshot(self.context, snapshot)
def test_create_share_from_snapshot_with_server(self): """Test share can be created from snapshot if server exists.""" server = self._create_share_server(share_network_id='net-id',) parent_share = self._create_share(share_network_id='net-id', share_server_id=server['id']) share = self._create_share() share_id = share['id'] snapshot = self._create_snapshot(share_id=parent_share['id']) snapshot_id = snapshot['id'] self.share_manager.create_share(self.context, share_id, snapshot_id=snapshot_id) self.assertEqual(share_id, db.share_get(context.get_admin_context(), share_id).id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'available') self.assertEqual(shr['share_server_id'], server['id'])
def test_create_share_with_share_network_server_exists(self): """Test share can be created with existing share server.""" share_net = self._create_share_network() share = self._create_share(share_network_id=share_net['id']) share_srv = self._create_share_server( share_network_id=share_net['id'], host=self.share_manager.host) share_id = share['id'] self.share_manager.driver = mock.Mock() self.share_manager.driver.create_share.return_value = "fake_location" self.share_manager.create_share(self.context, share_id) self.assertFalse(self.share_manager.driver.setup_network.called) self.assertEqual(share_id, db.share_get(context.get_admin_context(), share_id).id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'available') self.assertEqual(shr['share_server_id'], share_srv['id'])
def test_create_share_with_share_network_not_found(self): """Test creation fails if share network not found.""" share = self._create_share(share_network_id='fake-net-id') share_id = share['id'] self.assertRaises(exception.ShareNetworkNotFound, self.share_manager.create_share, self.context, share_id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'error')
def test_create_share_with_share_network_server_exists(self): """Test share can be created with existing share server.""" share_net = self._create_share_network() share = self._create_share(share_network_id=share_net['id']) share_srv = self._create_share_server(share_network_id=share_net['id'], host=self.share_manager.host) share_id = share['id'] self.share_manager.driver = mock.Mock() self.share_manager.driver.create_share.return_value = "fake_location" self.share_manager.create_share(self.context, share_id) self.assertFalse(self.share_manager.driver.setup_network.called) self.assertEqual( share_id, db.share_get(context.get_admin_context(), share_id).id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'available') self.assertEqual(shr['share_server_id'], share_srv['id'])
def test_create_share_from_snapshot_with_server(self): """Test share can be created from snapshot if server exists.""" server = self._create_share_server(share_network_id='net-id', ) parent_share = self._create_share(share_network_id='net-id', share_server_id=server['id']) share = self._create_share() share_id = share['id'] snapshot = self._create_snapshot(share_id=parent_share['id']) snapshot_id = snapshot['id'] self.share_manager.create_share(self.context, share_id, snapshot_id=snapshot_id) self.assertEqual( share_id, db.share_get(context.get_admin_context(), share_id).id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'available') self.assertEqual(shr['share_server_id'], server['id'])
def test_create_share_with_server_created(self): """Test share can be created and share server is created.""" share_net = self._create_share_network() share = self._create_share(share_network_id=share_net['id']) self._create_share_server( share_network_id=share_net['id'], host=self.share_manager.host, state='ERROR') share_id = share['id'] self.share_manager.driver = mock.Mock() self.share_manager._setup_server = mock.Mock( return_value={'id': 'fake_srv_id'}) self.share_manager.driver.create_share.return_value = "fake_location" self.share_manager.create_share(self.context, share_id) self.assertEqual(share_id, db.share_get(context.get_admin_context(), share_id).id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'available') self.assertEqual(shr['share_server_id'], 'fake_srv_id')
def test_create_share_with_share_network_not_found(self): """Test creation fails if share network not found.""" share = self._create_share(share_network_id='fake-net-id') share_id = share['id'] self.assertRaises( exception.ShareNetworkNotFound, self.share_manager.create_share, self.context, share_id ) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'error')
def migrate_share_to_host(self, context, share_id, host, force_host_assisted_migration, preserve_metadata, writable, nondisruptive, preserve_snapshots, new_share_network_id, new_share_type_id, request_spec, filter_properties=None): """Ensure that the host exists and can accept the share.""" share_ref = db.share_get(context, share_id) def _migrate_share_set_error(self, context, ex, request_spec): instance = next((x for x in share_ref.instances if x['status'] == constants.STATUS_MIGRATING), None) if instance: db.share_instance_update( context, instance['id'], {'status': constants.STATUS_AVAILABLE}) self._set_share_state_and_notify( 'migrate_share_to_host', {'task_state': constants.TASK_STATE_MIGRATION_ERROR}, context, ex, request_spec) try: tgt_host = self.driver.host_passes_filters(context, host, request_spec, filter_properties) except Exception as ex: with excutils.save_and_reraise_exception(): _migrate_share_set_error(self, context, ex, request_spec) else: try: share_rpcapi.ShareAPI().migration_start( context, share_ref, tgt_host.host, force_host_assisted_migration, preserve_metadata, writable, nondisruptive, preserve_snapshots, new_share_network_id, new_share_type_id) except Exception as ex: with excutils.save_and_reraise_exception(): _migrate_share_set_error(self, context, ex, request_spec)
def test_create_share_with_error_in_driver(self): """Test db updates if share creation fails in driver.""" share = self._create_share() share_id = share['id'] some_data = 'fake_location' self.share_manager.driver = mock.Mock() e = exception.ManilaException( detail_data={'export_location': some_data}) self.share_manager.driver.create_share.side_effect = e self.assertRaises(exception.ManilaException, self.share_manager.create_share, self.context, share_id) self.assertTrue(self.share_manager.driver.create_share.called) shr = db.share_get(self.context, share_id) self.assertEqual(some_data, shr['export_location'])
def test_reset_status_as_non_admin(self): # current status is 'error' share = db_utils.create_share(status=constants.STATUS_ERROR) req = webob.Request.blank("/v1/fake/shares/%s/action" % share["id"]) req.method = "POST" req.headers["content-type"] = "application/json" # request changing status to available req.body = six.b(jsonutils.dumps({"os-reset_status": {"status": constants.STATUS_AVAILABLE}})) # non-admin context req.environ["manila.context"] = self.member_context resp = req.get_response(app()) # request is not authorized self.assertEqual(resp.status_int, 403) share = db.share_get(context.get_admin_context(), share["id"]) # status is still 'error' self.assertEqual(share["status"], constants.STATUS_ERROR)
def test_malformed_reset_status_body(self): # current status is available share = db_utils.create_share(status=constants.STATUS_AVAILABLE) req = webob.Request.blank("/v1/fake/shares/%s/action" % share["id"]) req.method = "POST" req.headers["content-type"] = "application/json" # malformed request body req.body = six.b(jsonutils.dumps({"os-reset_status": {"x-status": "bad"}})) # attach admin context to request req.environ["manila.context"] = self.admin_context resp = req.get_response(app()) # bad request self.assertEqual(resp.status_int, 400) share = db.share_get(self.admin_context, share["id"]) # status is still 'available' self.assertEqual(share["status"], constants.STATUS_AVAILABLE)
def test_malformed_reset_status_body(self): # current status is available share = db.share_create(self.admin_context, {'status': 'available'}) req = webob.Request.blank('/v1/fake/shares/%s/action' % share['id']) req.method = 'POST' req.headers['content-type'] = 'application/json' # malformed request body req.body = jsonutils.dumps({'os-reset_status': {'x-status': 'bad'}}) # attach admin context to request req.environ['manila.context'] = self.admin_context resp = req.get_response(app()) # bad request self.assertEqual(resp.status_int, 400) share = db.share_get(self.admin_context, share['id']) # status is still 'available' self.assertEqual(share['status'], 'available')
def test_reset_status_as_admin(self): # current status is available share = db.share_create(self.admin_context, {'status': 'available'}) req = webob.Request.blank('/v1/fake/shares/%s/action' % share['id']) req.method = 'POST' req.headers['content-type'] = 'application/json' # request status of 'error' req.body = jsonutils.dumps({'os-reset_status': {'status': 'error'}}) # attach admin context to request req.environ['manila.context'] = self.admin_context resp = req.get_response(app()) # request is accepted self.assertEqual(resp.status_int, 202) share = db.share_get(self.admin_context, share['id']) # status changed to 'error' self.assertEqual(share['status'], 'error')
def test_reset_status_as_admin(self): # current status is available share = db_utils.create_share(status=constants.STATUS_AVAILABLE) req = webob.Request.blank("/v1/fake/shares/%s/action" % share["id"]) req.method = "POST" req.headers["content-type"] = "application/json" # request status of 'error' req.body = six.b(jsonutils.dumps({"os-reset_status": {"status": constants.STATUS_ERROR}})) # attach admin context to request req.environ["manila.context"] = self.admin_context resp = req.get_response(app()) # request is accepted self.assertEqual(resp.status_int, 202) share = db.share_get(self.admin_context, share["id"]) # status changed to 'error' self.assertEqual(share["status"], constants.STATUS_ERROR)
def test_create_share_from_snapshot_with_server_not_found(self): """Test creation from snapshot fails if server not found.""" parent_share = self._create_share(share_network_id='net-id', share_server_id='fake-id') share = self._create_share() share_id = share['id'] snapshot = self._create_snapshot(share_id=parent_share['id']) snapshot_id = snapshot['id'] self.assertRaises(exception.ShareServerNotFound, self.share_manager.create_share, self.context, share_id, snapshot_id=snapshot_id) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'error')
def test_invalid_status_for_share(self): # current status is available share = db.share_create( self.admin_context, {'status': constants.STATUS_AVAILABLE}) req = webob.Request.blank('/v1/fake/shares/%s/action' % share['id']) req.method = 'POST' req.headers['content-type'] = 'application/json' # 'invalid' is not a valid status req.body = jsonutils.dumps({'os-reset_status': {'status': 'invalid'}}) # attach admin context to request req.environ['manila.context'] = self.admin_context resp = req.get_response(app()) # bad request self.assertEqual(resp.status_int, 400) share = db.share_get(self.admin_context, share['id']) # status is still 'available' self.assertEqual(share['status'], constants.STATUS_AVAILABLE)
def test_create_share_from_snapshot_with_server_not_found(self): """Test creation from snapshot fails if server not found.""" parent_share = self._create_share(share_network_id='net-id', share_server_id='fake-id') share = self._create_share() share_id = share['id'] snapshot = self._create_snapshot(share_id=parent_share['id']) snapshot_id = snapshot['id'] self.assertRaises(exception.ShareServerNotFound, self.share_manager.create_share, self.context, share_id, snapshot_id=snapshot_id ) shr = db.share_get(self.context, share_id) self.assertEqual(shr['status'], 'error')
def test_reset_status_as_non_admin(self): # current status is 'error' share = db.share_create(context.get_admin_context(), {'status': 'error'}) req = webob.Request.blank('/v1/fake/shares/%s/action' % share['id']) req.method = 'POST' req.headers['content-type'] = 'application/json' # request changing status to available req.body = jsonutils.dumps({'os-reset_status': {'status': 'available'}}) # non-admin context req.environ['manila.context'] = self.member_context resp = req.get_response(app()) # request is not authorized self.assertEqual(resp.status_int, 403) share = db.share_get(context.get_admin_context(), share['id']) # status is still 'error' self.assertEqual(share['status'], 'error')
def test_invalid_status_for_share(self): # admin context ctx = context.RequestContext('admin', 'fake', True) # current status is available share = db.share_create(ctx, {'status': 'available'}) req = webob.Request.blank('/v2/fake/shares/%s/action' % share['id']) req.method = 'POST' req.headers['content-type'] = 'application/json' # 'invalid' is not a valid status req.body = jsonutils.dumps({'os-reset_status': {'status': 'invalid'}}) # attach admin context to request req.environ['manila.context'] = ctx resp = req.get_response(app()) # bad request self.assertEqual(resp.status_int, 400) share = db.share_get(ctx, share['id']) # status is still 'available' self.assertEqual(share['status'], 'available')
def test_reset_status_as_non_admin(self): # current status is 'error' share = db.share_create(context.get_admin_context(), {'status': constants.STATUS_ERROR}) req = webob.Request.blank('/v1/fake/shares/%s/action' % share['id']) req.method = 'POST' req.headers['content-type'] = 'application/json' # request changing status to available req.body = jsonutils.dumps( {'os-reset_status': {'status': constants.STATUS_AVAILABLE}}) # non-admin context req.environ['manila.context'] = self.member_context resp = req.get_response(app()) # request is not authorized self.assertEqual(resp.status_int, 403) share = db.share_get(context.get_admin_context(), share['id']) # status is still 'error' self.assertEqual(share['status'], constants.STATUS_ERROR)
def test_create_share_with_share_network_server_not_exists(self): """Test share can be created without share server.""" share_net = self._create_share_network() share = self._create_share(share_network_id=share_net['id']) share_id = share['id'] def fake_setup_server(context, share_network, *args, **kwargs): return self._create_share_server( share_network_id=share_network['id']) self.share_manager.driver.create_share = mock.Mock( return_value='fake_location') self.share_manager._setup_server = fake_setup_server self.share_manager.create_share(self.context, share_id) self.assertEqual(share_id, db.share_get(context.get_admin_context(), share_id).id)
def test_create_share_with_share_network_server_not_exists(self): """Test share can be created without share server.""" share_net = self._create_share_network() share = self._create_share(share_network_id=share_net['id']) share_id = share['id'] def fake_setup_server(context, share_network, *args, **kwargs): return self._create_share_server( share_network_id=share_network['id']) self.share_manager.driver.create_share = mock.Mock( return_value='fake_location') self.share_manager._setup_server = fake_setup_server self.share_manager.create_share(self.context, share_id) self.assertEqual( share_id, db.share_get(context.get_admin_context(), share_id).id)
def _create(self, req, body): """Add a replica to an existing share.""" context = req.environ['manila.context'] if not self.is_valid_body(body, 'share_replica'): msg = _("Body does not contain 'share_replica' information.") raise exc.HTTPUnprocessableEntity(explanation=msg) share_id = body.get('share_replica').get('share_id') availability_zone = body.get('share_replica').get('availability_zone') if not share_id: msg = _("Must provide Share ID to add replica.") raise exc.HTTPBadRequest(explanation=msg) try: share_ref = db.share_get(context, share_id) except exception.NotFound: msg = _("No share exists with ID %s.") raise exc.HTTPNotFound(explanation=msg % share_id) share_network_id = share_ref.get('share_network_id', None) if share_network_id: share_network = db.share_network_get(context, share_network_id) common.check_share_network_is_active(share_network) try: new_replica = self.share_api.create_share_replica( context, share_ref, availability_zone=availability_zone, share_network_id=share_network_id) except exception.AvailabilityZoneNotFound as e: raise exc.HTTPBadRequest(explanation=six.text_type(e)) except exception.ReplicationException as e: raise exc.HTTPBadRequest(explanation=six.text_type(e)) except exception.ShareBusyException as e: raise exc.HTTPBadRequest(explanation=six.text_type(e)) return self._view_builder.detail(req, new_replica)
def manage_share(self, context, share_id, driver_options, request_spec, filter_properties=None): """Ensure that the host exists and can accept the share.""" def _manage_share_set_error(self, context, ex, request_spec): self._set_share_state_and_notify( 'manage_share', {'status': constants.STATUS_MANAGE_ERROR}, context, ex, request_spec) share_ref = db.share_get(context, share_id) try: self.driver.host_passes_filters( context, share_ref['host'], request_spec, filter_properties) except Exception as ex: with excutils.save_and_reraise_exception(): _manage_share_set_error(self, context, ex, request_spec) else: share_rpcapi.ShareAPI().manage_share(context, share_ref, driver_options)
def migrate_share_to_host( self, context, share_id, host, force_host_assisted_migration, preserve_metadata, writable, nondisruptive, preserve_snapshots, new_share_network_id, new_share_type_id, request_spec, filter_properties=None): """Ensure that the host exists and can accept the share.""" share_ref = db.share_get(context, share_id) def _migrate_share_set_error(self, context, ex, request_spec): instance = next((x for x in share_ref.instances if x['status'] == constants.STATUS_MIGRATING), None) if instance: db.share_instance_update( context, instance['id'], {'status': constants.STATUS_AVAILABLE}) self._set_share_state_and_notify( 'migrate_share_to_host', {'task_state': constants.TASK_STATE_MIGRATION_ERROR}, context, ex, request_spec) try: tgt_host = self.driver.host_passes_filters( context, host, request_spec, filter_properties) except Exception as ex: with excutils.save_and_reraise_exception(): _migrate_share_set_error(self, context, ex, request_spec) else: try: share_rpcapi.ShareAPI().migration_start( context, share_ref, tgt_host.host, force_host_assisted_migration, preserve_metadata, writable, nondisruptive, preserve_snapshots, new_share_network_id, new_share_type_id) except Exception as ex: with excutils.save_and_reraise_exception(): _migrate_share_set_error(self, context, ex, request_spec)
def manage_share(self, context, share_id, driver_options, request_spec, filter_properties=None): """Ensure that the host exists and can accept the share.""" def _manage_share_set_error(self, context, ex, request_spec): self._set_share_state_and_notify( 'manage_share', {'status': constants.STATUS_MANAGE_ERROR}, context, ex, request_spec) share_ref = db.share_get(context, share_id) try: self.driver.host_passes_filters(context, share_ref['host'], request_spec, filter_properties) except Exception as ex: with excutils.save_and_reraise_exception(): _manage_share_set_error(self, context, ex, request_spec) else: share_rpcapi.ShareAPI().manage_share(context, share_ref, driver_options)