Merge "Fix default qos policy when creating network"

This commit is contained in:
Jenkins 2017-08-24 14:11:00 +00:00 committed by Gerrit Code Review
commit 989532a7ac
5 changed files with 74 additions and 5 deletions

View File

@ -66,8 +66,10 @@ class QosCoreResourceExtension(base.CoreResourceExtension):
def _create_network_policy(self, context, network, network_changes): def _create_network_policy(self, context, network, network_changes):
qos_policy_id = network_changes.get(qos_consts.QOS_POLICY_ID) qos_policy_id = network_changes.get(qos_consts.QOS_POLICY_ID)
if not qos_policy_id: if not qos_policy_id:
qos_policy_id = policy_object.QosPolicyDefault.get_object( policy_obj = policy_object.QosPolicyDefault.get_object(
context, project_id=network['project_id']) context, project_id=network['project_id'])
if policy_obj is not None:
qos_policy_id = policy_obj.qos_policy_id
if qos_policy_id is not None: if qos_policy_id is not None:
policy = self._get_policy_obj(context, qos_policy_id) policy = self._get_policy_obj(context, qos_policy_id)

View File

@ -370,10 +370,10 @@ class BaseNetworkTest(test.BaseTestCase):
@classmethod @classmethod
def create_qos_policy(cls, name, description=None, shared=False, def create_qos_policy(cls, name, description=None, shared=False,
tenant_id=None): tenant_id=None, is_default=False):
"""Wrapper utility that returns a test QoS policy.""" """Wrapper utility that returns a test QoS policy."""
body = cls.admin_client.create_qos_policy( body = cls.admin_client.create_qos_policy(
name, description, shared, tenant_id) name, description, shared, tenant_id, is_default)
qos_policy = body['policy'] qos_policy = body['policy']
cls.qos_policies.append(qos_policy) cls.qos_policies.append(qos_policy)
return qos_policy return qos_policy

View File

@ -37,6 +37,17 @@ class QosTestJSON(base.BaseAdminNetworkTest):
if driver['name'] == driver_name: if driver['name'] == driver_name:
return driver return driver
def _create_project(self):
# Add a project to conduct the test
test_project = data_utils.rand_name('test_project_')
test_description = data_utils.rand_name('desc_')
project = self.identity_admin_client.create_project(
name=test_project,
description=test_description)['project']
self.addCleanup(
self.identity_admin_client.delete_project, project['id'])
return project
@decorators.idempotent_id('108fbdf7-3463-4e47-9871-d07f3dcf5bbb') @decorators.idempotent_id('108fbdf7-3463-4e47-9871-d07f3dcf5bbb')
def test_create_policy(self): def test_create_policy(self):
policy = self.create_qos_policy(name='test-policy', policy = self.create_qos_policy(name='test-policy',
@ -385,6 +396,37 @@ class QosTestJSON(base.BaseAdminNetworkTest):
self.client.create_qos_policy, self.client.create_qos_policy,
'test-policy', 'test policy', False) 'test-policy', 'test policy', False)
@decorators.idempotent_id('18d94f22-b9d5-4390-af12-d30a0cfc4cd3')
def test_default_policy_creating_network_without_policy(self):
project_id = self._create_project()['id']
policy = self.create_qos_policy(name='test-policy',
tenant_id=project_id,
is_default=True)
network = self.create_network('test network', client=self.admin_client,
project_id=project_id)
self.addCleanup(self._disassociate_network,
self.admin_client, network['id'])
retrieved_network = self.admin_client.show_network(network['id'])
self.assertEqual(
policy['id'], retrieved_network['network']['qos_policy_id'])
@decorators.idempotent_id('807cce45-38e5-482d-94db-36e1796aba73')
def test_default_policy_creating_network_with_policy(self):
project_id = self._create_project()['id']
self.create_qos_policy(name='test-policy',
tenant_id=project_id,
is_default=True)
policy = self.create_qos_policy(name='test-policy',
tenant_id=project_id)
network = self.create_network('test network', client=self.admin_client,
project_id=project_id,
qos_policy_id=policy['id'])
self.addCleanup(self._disassociate_network,
self.admin_client, network['id'])
retrieved_network = self.admin_client.show_network(network['id'])
self.assertEqual(
policy['id'], retrieved_network['network']['qos_policy_id'])
class QosBandwidthLimitRuleTestJSON(base.BaseAdminNetworkTest): class QosBandwidthLimitRuleTestJSON(base.BaseAdminNetworkTest):

View File

@ -559,12 +559,13 @@ class NetworkClientJSON(service_client.RestClient):
return service_client.ResponseBody(resp, body) return service_client.ResponseBody(resp, body)
def create_qos_policy(self, name, description=None, shared=False, def create_qos_policy(self, name, description=None, shared=False,
tenant_id=None): tenant_id=None, is_default=False):
uri = '%s/qos/policies' % self.uri_prefix uri = '%s/qos/policies' % self.uri_prefix
post_data = { post_data = {
'policy': { 'policy': {
'name': name, 'name': name,
'shared': shared 'shared': shared,
'is_default': is_default
} }
} }
if description is not None: if description is not None:

View File

@ -15,6 +15,7 @@
import mock import mock
from neutron_lib import context from neutron_lib import context
from oslo_utils import uuidutils
from neutron.common import exceptions as n_exc from neutron.common import exceptions as n_exc
from neutron.core_extensions import base as base_core from neutron.core_extensions import base as base_core
@ -336,3 +337,26 @@ class QosCoreResourceExtensionTestCase(base.BaseTestCase):
qos_policy = mock.Mock() qos_policy = mock.Mock()
qos_policy.id = qos_policy_id qos_policy.id = qos_policy_id
self._test_extract_fields_for_network(qos_policy_id) self._test_extract_fields_for_network(qos_policy_id)
def test__create_network_policy(self):
default_policy_id = uuidutils.generate_uuid()
network_policy_id = uuidutils.generate_uuid()
policy_mock = mock.MagicMock(qos_policy_id=default_policy_id)
network_changes = mock.Mock()
network = {'id': 'dummy_id', 'project_id': 'dummy_project',
qos_consts.QOS_POLICY_ID: None}
with mock.patch.object(policy.QosPolicyDefault, 'get_object',
return_value=policy_mock),\
mock.patch.object(policy.QosPolicy, 'get_object'):
# Creating network with policy id
network_changes.get.return_value = network_policy_id
self.core_extension._create_network_policy(
self.context, network, network_changes)
self.assertEqual(network_policy_id,
network[qos_consts.QOS_POLICY_ID])
# Creating network without policy id
network_changes.get.return_value = None
self.core_extension._create_network_policy(
self.context, network, network_changes)
self.assertEqual(default_policy_id,
network[qos_consts.QOS_POLICY_ID])