diff --git a/neutron/core_extensions/qos.py b/neutron/core_extensions/qos.py index 0b7fb97ecaa..0e1624d2913 100644 --- a/neutron/core_extensions/qos.py +++ b/neutron/core_extensions/qos.py @@ -66,8 +66,10 @@ class QosCoreResourceExtension(base.CoreResourceExtension): def _create_network_policy(self, context, network, network_changes): qos_policy_id = network_changes.get(qos_consts.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']) + if policy_obj is not None: + qos_policy_id = policy_obj.qos_policy_id if qos_policy_id is not None: policy = self._get_policy_obj(context, qos_policy_id) diff --git a/neutron/tests/tempest/api/base.py b/neutron/tests/tempest/api/base.py index 31285acafdc..207a30ff666 100644 --- a/neutron/tests/tempest/api/base.py +++ b/neutron/tests/tempest/api/base.py @@ -370,10 +370,10 @@ class BaseNetworkTest(test.BaseTestCase): @classmethod 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.""" body = cls.admin_client.create_qos_policy( - name, description, shared, tenant_id) + name, description, shared, tenant_id, is_default) qos_policy = body['policy'] cls.qos_policies.append(qos_policy) return qos_policy diff --git a/neutron/tests/tempest/api/test_qos.py b/neutron/tests/tempest/api/test_qos.py index 7735710484b..6a44abbbf21 100644 --- a/neutron/tests/tempest/api/test_qos.py +++ b/neutron/tests/tempest/api/test_qos.py @@ -37,6 +37,17 @@ class QosTestJSON(base.BaseAdminNetworkTest): if driver['name'] == driver_name: 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') def test_create_policy(self): policy = self.create_qos_policy(name='test-policy', @@ -385,6 +396,37 @@ class QosTestJSON(base.BaseAdminNetworkTest): self.client.create_qos_policy, '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): diff --git a/neutron/tests/tempest/services/network/json/network_client.py b/neutron/tests/tempest/services/network/json/network_client.py index 12cdf971bb9..358dd64d848 100644 --- a/neutron/tests/tempest/services/network/json/network_client.py +++ b/neutron/tests/tempest/services/network/json/network_client.py @@ -559,12 +559,13 @@ class NetworkClientJSON(service_client.RestClient): return service_client.ResponseBody(resp, body) 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 post_data = { 'policy': { 'name': name, - 'shared': shared + 'shared': shared, + 'is_default': is_default } } if description is not None: diff --git a/neutron/tests/unit/core_extensions/test_qos.py b/neutron/tests/unit/core_extensions/test_qos.py index 152f88314fd..031fd4344b9 100644 --- a/neutron/tests/unit/core_extensions/test_qos.py +++ b/neutron/tests/unit/core_extensions/test_qos.py @@ -15,6 +15,7 @@ import mock from neutron_lib import context +from oslo_utils import uuidutils from neutron.common import exceptions as n_exc from neutron.core_extensions import base as base_core @@ -336,3 +337,26 @@ class QosCoreResourceExtensionTestCase(base.BaseTestCase): qos_policy = mock.Mock() qos_policy.id = 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])