def test_add_security_service_association_error_status_active(self): security_dict1 = {'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type'} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_network_update(self.fake_context, self.share_nw_dict['id'], {'status': constants.STATUS_ACTIVE}) db_api.security_service_create(self.fake_context, security_dict1) self.assertRaises( exception.ShareNetworkSecurityServiceAssociationError, db_api.share_network_add_security_service, self.fake_context, self.share_nw_dict['id'], security_dict1['id']) assoc_ref = sqlalchemy_api.model_query( self.fake_context, models.ShareNetworkSecurityServiceAssociation).\ filter_by(security_service_id=security_dict1['id']).\ filter_by(share_network_id=self.share_nw_dict['id']).first() self.assertTrue(assoc_ref is None)
def test_create_with_duplicated_id(self): db_api.share_network_create(self.fake_context, self.share_nw_dict) self.assertRaises(db_exception.DBDuplicateEntry, db_api.share_network_create, self.fake_context, self.share_nw_dict)
def test_get_with_two_security_services(self): security_dict1 = { 'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type' } security_dict2 = { 'id': 'fake security service id2', 'project_id': self.fake_context.project_id, 'type': 'fake type' } db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) db_api.security_service_create(self.fake_context, security_dict2) db_api.share_network_add_security_service(self.fake_context, self.share_nw_dict['id'], security_dict1['id']) db_api.share_network_add_security_service(self.fake_context, self.share_nw_dict['id'], security_dict2['id']) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(result['security_services']), 2)
def test_remove_security_service(self): security_dict1 = {'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type'} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) db_api.share_network_add_security_service(self.fake_context, self.share_nw_dict['id'], security_dict1['id']) db_api.share_network_remove_security_service(self.fake_context, self.share_nw_dict['id'], security_dict1['id']) result = sqlalchemy_api.model_query( self.fake_context, models.ShareNetworkSecurityServiceAssociation).\ filter_by(security_service_id=security_dict1['id']).\ filter_by(share_network_id=self.share_nw_dict['id']).first() self.assertTrue(result is None) share_nw_ref = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(share_nw_ref['security_services']), 0)
def test_delete(self): db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_network_delete(self.fake_context, self.share_nw_dict["id"]) self.assertRaises( exception.ShareNetworkNotFound, db_api.share_network_get, self.fake_context, self.share_nw_dict["id"] )
def test_add_security_service_association_error_status_active(self): security_dict1 = { "id": "fake security service id1", "project_id": self.fake_context.project_id, "type": "fake type", } db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_network_update(self.fake_context, self.share_nw_dict["id"], {"status": constants.STATUS_ACTIVE}) db_api.security_service_create(self.fake_context, security_dict1) self.assertRaises( exception.ShareNetworkSecurityServiceAssociationError, db_api.share_network_add_security_service, self.fake_context, self.share_nw_dict["id"], security_dict1["id"], ) assoc_ref = ( sqlalchemy_api.model_query(self.fake_context, models.ShareNetworkSecurityServiceAssociation) .filter_by(security_service_id=security_dict1["id"]) .filter_by(share_network_id=self.share_nw_dict["id"]) .first() ) self.assertTrue(assoc_ref is None)
def test_remove_security_service(self): security_dict1 = { 'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type' } db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) db_api.share_network_add_security_service(self.fake_context, self.share_nw_dict['id'], security_dict1['id']) db_api.share_network_remove_security_service(self.fake_context, self.share_nw_dict['id'], security_dict1['id']) result = sqlalchemy_api.model_query( self.fake_context, models.ShareNetworkSecurityServiceAssociation).\ filter_by(security_service_id=security_dict1['id']).\ filter_by(share_network_id=self.share_nw_dict['id']).first() self.assertTrue(result is None) share_nw_ref = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(share_nw_ref['security_services']), 0)
def test_get(self): db_api.share_network_create(self.fake_context, self.share_nw_dict) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self._check_fields(expected=self.share_nw_dict, actual=result) self.assertEqual(len(result['shares']), 0) self.assertEqual(len(result['security_services']), 0)
def test_delete(self): db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_network_delete(self.fake_context, self.share_nw_dict['id']) self.assertRaises(exception.ShareNetworkNotFound, db_api.share_network_get, self.fake_context, self.share_nw_dict['id'])
def test_update(self): new_status = constants.STATUS_ERROR db_api.share_network_create(self.fake_context, self.share_nw_dict) result_update = db_api.share_network_update(self.fake_context, self.share_nw_dict["id"], {"status": new_status}) result_get = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(result_update["status"], new_status) self._check_fields(expected=dict(result_update.iteritems()), actual=dict(result_get.iteritems()))
def test_get_with_one_allocation(self): db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.network_allocation_create(self.fake_context, self.allocation_dict) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["network_allocations"]), 1) self._check_fields(expected=self.allocation_dict, actual=result["network_allocations"][0])
def test_remove_security_service_not_found_01(self): security_service_id = 'unknown security service' db_api.share_network_create(self.fake_context, self.share_nw_dict) self.assertRaises(exception.SecurityServiceNotFound, db_api.share_network_remove_security_service, self.fake_context, self.share_nw_dict['id'], security_service_id)
def test_network_allocations_relation(self): db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.network_allocation_create(self.fake_context, self.allocation_dict) db_api.network_allocation_delete(self.fake_context, self.allocation_dict["id"]) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["network_allocations"]), 0)
def test_create_two_networks_in_one_tenant(self): share_nw_dict2 = self.share_nw_dict.copy() share_nw_dict2['id'] = share_nw_dict2['id'] + "suffix" result1 = db_api.share_network_create(self.fake_context, self.share_nw_dict) result2 = db_api.share_network_create(self.fake_context, share_nw_dict2) self._check_fields(expected=self.share_nw_dict, actual=result1) self._check_fields(expected=share_nw_dict2, actual=result2)
def test_create_two_networks(self): share_nw_dict2 = self.share_nw_dict.copy() share_nw_dict2["id"] = None share_nw_dict2["project_id"] = "fake project 2" result1 = db_api.share_network_create(self.fake_context, self.share_nw_dict) result2 = db_api.share_network_create(self.fake_context, share_nw_dict2) self._check_fields(expected=self.share_nw_dict, actual=result1) self._check_fields(expected=share_nw_dict2, actual=result2)
def test_shares_relation(self): share_dict = {"id": "fake share id1"} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_create(self.fake_context, share_dict) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["shares"]), 0)
def test_shares_relation(self): share_dict = {'id': 'fake share id1'} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_create(self.fake_context, share_dict) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(result['shares']), 0)
def test_get_with_two_allocations(self): allocation_dict2 = dict(self.allocation_dict) allocation_dict2["id"] = "fake port id2" db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.network_allocation_create(self.fake_context, self.allocation_dict) db_api.network_allocation_create(self.fake_context, allocation_dict2) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["network_allocations"]), 2)
def test_get_all_two_records(self): share_nw_dict2 = dict(self.share_nw_dict) share_nw_dict2['id'] = 'fake subnet id2' share_nw_dict2['neutron_subnet_id'] = 'fake subnet id2' db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_network_create(self.fake_context, share_nw_dict2) result = db_api.share_network_get_all(self.fake_context) self.assertEqual(len(result), 2)
def test_get_with_two_shares(self): share_dict1 = {"id": "fake share id1", "share_network_id": self.share_nw_dict["id"]} share_dict2 = {"id": "fake share id2", "share_network_id": self.share_nw_dict["id"]} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_create(self.fake_context, share_dict1) db_api.share_create(self.fake_context, share_dict2) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["shares"]), 2)
def test_get_with_one_share(self): share_dict1 = {"id": "fake share id1", "share_network_id": self.share_nw_dict["id"]} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_create(self.fake_context, share_dict1) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["shares"]), 1) self._check_fields(expected=share_dict1, actual=result["shares"][0])
def test_create_two_networks_in_different_tenants(self): share_nw_dict2 = self.share_nw_dict.copy() share_nw_dict2['id'] = None share_nw_dict2['project_id'] = 'fake project 2' result1 = db_api.share_network_create(self.fake_context, self.share_nw_dict) result2 = db_api.share_network_create(self.fake_context, share_nw_dict2) self._check_fields(expected=self.share_nw_dict, actual=result1) self._check_fields(expected=share_nw_dict2, actual=result2)
def test_add_security_service_not_found_01(self): security_service_id = "unknown security service" db_api.share_network_create(self.fake_context, self.share_nw_dict) self.assertRaises( exception.SecurityServiceNotFound, db_api.share_network_add_security_service, self.fake_context, self.share_nw_dict["id"], security_service_id, )
def test_security_services_relation(self): security_dict1 = {'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type'} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(result['security_services']), 0)
def test_update(self): new_name = 'fake_new_name' db_api.share_network_create(self.fake_context, self.share_nw_dict) result_update = db_api.share_network_update(self.fake_context, self.share_nw_dict['id'], {'name': new_name}) result_get = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(result_update['name'], new_name) self._check_fields(expected=dict(six.iteritems(result_update)), actual=dict(six.iteritems(result_get)))
def test_get_all_by_project(self): share_nw_dict2 = dict(self.share_nw_dict) share_nw_dict2["id"] = "fake share nw id2" share_nw_dict2["project_id"] = "fake project 2" share_nw_dict2["neutron_subnet_id"] = "fake subnet id2" db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_network_create(self.fake_context, share_nw_dict2) result = db_api.share_network_get_all_by_project(self.fake_context, share_nw_dict2["project_id"]) self.assertEqual(len(result), 1) self._check_fields(expected=share_nw_dict2, actual=result[0])
def test_get_with_one_share(self): share_dict1 = {'id': 'fake share id1', 'share_network_id': self.share_nw_dict['id']} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_create(self.fake_context, share_dict1) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(result['shares']), 1) self._check_fields(expected=share_dict1, actual=result['shares'][0])
def test_get_with_two_shares(self): share_dict1 = {'id': 'fake share id1', 'share_network_id': self.share_nw_dict['id']} share_dict2 = {'id': 'fake share id2', 'share_network_id': self.share_nw_dict['id']} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_create(self.fake_context, share_dict1) db_api.share_create(self.fake_context, share_dict2) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(result['shares']), 2)
def test_get_all_by_project(self): share_nw_dict2 = dict(self.share_nw_dict) share_nw_dict2['id'] = 'fake share nw id2' share_nw_dict2['project_id'] = 'fake project 2' share_nw_dict2['neutron_subnet_id'] = 'fake subnet id2' db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.share_network_create(self.fake_context, share_nw_dict2) result = db_api.share_network_get_all_by_project( self.fake_context, share_nw_dict2['project_id']) self.assertEqual(len(result), 1) self._check_fields(expected=share_nw_dict2, actual=result[0])
def test_security_services_relation(self): security_dict1 = { "id": "fake security service id1", "project_id": self.fake_context.project_id, "type": "fake type", } db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["security_services"]), 0)
def test_remove_security_service_dissociation_error(self): security_dict1 = { 'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type' } db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) self.assertRaises( exception.ShareNetworkSecurityServiceDissociationError, db_api.share_network_remove_security_service, self.fake_context, self.share_nw_dict['id'], security_dict1['id'])
def test_remove_security_service_dissociation_error(self): security_dict1 = {'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type'} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) self.assertRaises( exception.ShareNetworkSecurityServiceDissociationError, db_api.share_network_remove_security_service, self.fake_context, self.share_nw_dict['id'], security_dict1['id'])
def test_get_with_one_security_service(self): security_dict1 = { "id": "fake security service id1", "project_id": self.fake_context.project_id, "type": "fake type", } db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) db_api.share_network_add_security_service(self.fake_context, self.share_nw_dict["id"], security_dict1["id"]) result = db_api.share_network_get(self.fake_context, self.share_nw_dict["id"]) self.assertEqual(len(result["security_services"]), 1) self._check_fields(expected=security_dict1, actual=result["security_services"][0])
def test_create_one_network(self): result = db_api.share_network_create(self.fake_context, self.share_nw_dict) self._check_fields(expected=self.share_nw_dict, actual=result) self.assertEqual(len(result["shares"]), 0) self.assertEqual(len(result["security_services"]), 0) self.assertEqual(len(result["network_allocations"]), 0)
def test_get_with_one_security_service(self): security_dict1 = {'id': 'fake security service id1', 'project_id': self.fake_context.project_id, 'type': 'fake type'} db_api.share_network_create(self.fake_context, self.share_nw_dict) db_api.security_service_create(self.fake_context, security_dict1) db_api.share_network_add_security_service(self.fake_context, self.share_nw_dict['id'], security_dict1['id']) result = db_api.share_network_get(self.fake_context, self.share_nw_dict['id']) self.assertEqual(len(result['security_services']), 1) self._check_fields(expected=security_dict1, actual=result['security_services'][0])
def create(self, req, body): """Creates a new share network.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'create') if not body or RESOURCE_NAME not in body: raise exc.HTTPUnprocessableEntity() values = body[RESOURCE_NAME] values['project_id'] = context.project_id values['user_id'] = context.user_id if 'nova_net_id' in values: msg = _("nova networking is not supported starting in Ocata.") raise exc.HTTPBadRequest(explanation=msg) try: reservations = QUOTAS.reserve(context, share_networks=1) except exception.OverQuota as e: overs = e.kwargs['overs'] usages = e.kwargs['usages'] quotas = e.kwargs['quotas'] def _consumed(name): return (usages[name]['reserved'] + usages[name]['in_use']) if 'share_networks' in overs: LOG.warning( "Quota exceeded for %(s_pid)s, " "tried to create " "share-network (%(d_consumed)d of %(d_quota)d " "already consumed).", { 's_pid': context.project_id, 'd_consumed': _consumed('share_networks'), 'd_quota': quotas['share_networks'] }) raise exception.ShareNetworksLimitExceeded( allowed=quotas['share_networks']) else: try: share_network = db_api.share_network_create(context, values) except db_exception.DBError: msg = "Could not save supplied data due to database error" raise exc.HTTPBadRequest(explanation=msg) QUOTAS.commit(context, reservations) return self._view_builder.build_share_network(req, share_network)
def test_get_all_one_record(self): db_api.share_network_create(self.fake_context, self.share_nw_dict) result = db_api.share_network_get_all(self.fake_context) self.assertEqual(len(result), 1) self._check_fields(expected=self.share_nw_dict, actual=result[0])
def create(self, req, body): """Creates a new share network.""" context = req.environ['manila.context'] policy.check_policy(context, RESOURCE_NAME, 'create') if not body or RESOURCE_NAME not in body: raise exc.HTTPUnprocessableEntity() share_network_values = body[RESOURCE_NAME] share_network_subnet_values = copy.deepcopy(share_network_values) share_network_values['project_id'] = context.project_id share_network_values['user_id'] = context.user_id if 'nova_net_id' in share_network_values: msg = _("nova networking is not supported starting in Ocata.") raise exc.HTTPBadRequest(explanation=msg) share_network_values.pop('availability_zone', None) share_network_values.pop('neutron_net_id', None) share_network_values.pop('neutron_subnet_id', None) if req.api_version_request >= api_version.APIVersionRequest("2.51"): if 'availability_zone' in share_network_subnet_values: try: az = db_api.availability_zone_get( context, share_network_subnet_values['availability_zone']) share_network_subnet_values['availability_zone_id'] = ( az['id']) share_network_subnet_values.pop('availability_zone') except exception.AvailabilityZoneNotFound: msg = (_("The provided availability zone %s does not " "exist.") % share_network_subnet_values['availability_zone']) raise exc.HTTPBadRequest(explanation=msg) common.check_net_id_and_subnet_id(share_network_subnet_values) try: reservations = QUOTAS.reserve(context, share_networks=1) except exception.OverQuota as e: overs = e.kwargs['overs'] usages = e.kwargs['usages'] quotas = e.kwargs['quotas'] def _consumed(name): return (usages[name]['reserved'] + usages[name]['in_use']) if 'share_networks' in overs: LOG.warning( "Quota exceeded for %(s_pid)s, " "tried to create " "share-network (%(d_consumed)d of %(d_quota)d " "already consumed).", { 's_pid': context.project_id, 'd_consumed': _consumed('share_networks'), 'd_quota': quotas['share_networks'] }) raise exception.ShareNetworksLimitExceeded( allowed=quotas['share_networks']) else: # Tries to create the new share network try: share_network = db_api.share_network_create( context, share_network_values) except db_exception.DBError as e: LOG.exception(e) msg = "Could not create share network." raise exc.HTTPInternalServerError(explanation=msg) share_network_subnet_values['share_network_id'] = ( share_network['id']) share_network_subnet_values.pop('id', None) # Try to create the share network subnet. If it fails, the service # must rollback the share network creation. try: db_api.share_network_subnet_create( context, share_network_subnet_values) except db_exception.DBError: db_api.share_network_delete(context, share_network['id']) msg = _('Could not create share network.') raise exc.HTTPInternalServerError(explanation=msg) QUOTAS.commit(context, reservations) share_network = db_api.share_network_get(context, share_network['id']) return self._view_builder.build_share_network(req, share_network)