1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
14 from oslo_config import cfg
16 from neutron.common import exceptions as n_exc
17 from neutron import context
18 from neutron import manager
19 from neutron.objects import base as base_object
20 from neutron.objects.qos import policy as policy_object
21 from neutron.objects.qos import rule as rule_object
22 from neutron.plugins.common import constants
23 from neutron.tests.unit.services.qos import base
26 DB_PLUGIN_KLASS = 'neutron.db.db_base_plugin_v2.NeutronDbPluginV2'
29 class TestQosPlugin(base.BaseQosTestCase):
32 super(TestQosPlugin, self).setUp()
33 self.setup_coreplugin()
35 mock.patch('neutron.db.api.create_object').start()
36 mock.patch('neutron.db.api.update_object').start()
37 mock.patch('neutron.db.api.delete_object').start()
38 mock.patch('neutron.db.api.get_object').start()
40 'neutron.objects.qos.policy.QosPolicy.obj_load_attr').start()
42 cfg.CONF.set_override("core_plugin", DB_PLUGIN_KLASS)
43 cfg.CONF.set_override("service_plugins", ["qos"])
45 mgr = manager.NeutronManager.get_instance()
46 self.qos_plugin = mgr.get_service_plugins().get(
49 self.qos_plugin.notification_driver_manager = mock.Mock()
51 self.ctxt = context.Context('fake_user', 'fake_tenant')
53 'policy': {'id': 7777777,
55 'name': 'test-policy',
56 'description': 'Test policy description',
60 'bandwidth_limit_rule': {'id': 7777777,
62 'max_burst_kbps': 150}}
64 self.policy = policy_object.QosPolicy(
65 self.ctxt, **self.policy_data['policy'])
67 self.rule = rule_object.QosBandwidthLimitRule(
68 self.ctxt, **self.rule_data['bandwidth_limit_rule'])
70 def _validate_notif_driver_params(self, method_name):
71 method = getattr(self.qos_plugin.notification_driver_manager,
73 self.assertTrue(method.called)
74 self.assertIsInstance(
75 method.call_args[0][1], policy_object.QosPolicy)
77 def test_add_policy(self):
78 self.qos_plugin.create_policy(self.ctxt, self.policy_data)
79 self._validate_notif_driver_params('create_policy')
81 def test_update_policy(self):
82 fields = base_object.get_updatable_fields(
83 policy_object.QosPolicy, self.policy_data['policy'])
84 self.qos_plugin.update_policy(
85 self.ctxt, self.policy.id, {'policy': fields})
86 self._validate_notif_driver_params('update_policy')
88 @mock.patch('neutron.db.api.get_object', return_value=None)
89 def test_delete_policy(self, *mocks):
90 self.qos_plugin.delete_policy(self.ctxt, self.policy.id)
91 self._validate_notif_driver_params('delete_policy')
93 def test_create_policy_rule(self):
94 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
95 return_value=self.policy):
96 self.qos_plugin.create_policy_bandwidth_limit_rule(
97 self.ctxt, self.policy.id, self.rule_data)
98 self._validate_notif_driver_params('update_policy')
100 def test_update_policy_rule(self):
101 _policy = policy_object.QosPolicy(
102 self.ctxt, **self.policy_data['policy'])
103 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
104 return_value=_policy):
105 setattr(_policy, "rules", [self.rule])
106 self.qos_plugin.update_policy_bandwidth_limit_rule(
107 self.ctxt, self.rule.id, self.policy.id, self.rule_data)
108 self._validate_notif_driver_params('update_policy')
110 def test_update_policy_rule_bad_policy(self):
111 _policy = policy_object.QosPolicy(
112 self.ctxt, **self.policy_data['policy'])
113 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
114 return_value=_policy):
115 setattr(_policy, "rules", [])
117 n_exc.QosRuleNotFound,
118 self.qos_plugin.update_policy_bandwidth_limit_rule,
119 self.ctxt, self.rule.id, self.policy.id,
122 def test_delete_policy_rule(self):
123 _policy = policy_object.QosPolicy(
124 self.ctxt, **self.policy_data['policy'])
125 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
126 return_value=_policy):
127 setattr(_policy, "rules", [self.rule])
128 self.qos_plugin.delete_policy_bandwidth_limit_rule(
129 self.ctxt, self.rule.id, _policy.id)
130 self._validate_notif_driver_params('update_policy')
132 def test_delete_policy_rule_bad_policy(self):
133 _policy = policy_object.QosPolicy(
134 self.ctxt, **self.policy_data['policy'])
135 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
136 return_value=_policy):
137 setattr(_policy, "rules", [])
139 n_exc.QosRuleNotFound,
140 self.qos_plugin.delete_policy_bandwidth_limit_rule,
141 self.ctxt, self.rule.id, _policy.id)
143 def test_get_policy_bandwidth_limit_rules_for_policy(self):
144 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
145 return_value=self.policy):
146 with mock.patch('neutron.objects.qos.rule.'
147 'QosBandwidthLimitRule.'
148 'get_objects') as get_object_mock:
149 self.qos_plugin.get_policy_bandwidth_limit_rules(
150 self.ctxt, self.policy.id)
151 get_object_mock.assert_called_once_with(
152 self.ctxt, qos_policy_id=self.policy.id)
154 def test_get_policy_bandwidth_limit_rules_for_policy_with_filters(self):
155 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
156 return_value=self.policy):
157 with mock.patch('neutron.objects.qos.rule.'
158 'QosBandwidthLimitRule.'
159 'get_objects') as get_object_mock:
161 filters = {'filter': 'filter_id'}
162 self.qos_plugin.get_policy_bandwidth_limit_rules(
163 self.ctxt, self.policy.id, filters=filters)
164 get_object_mock.assert_called_once_with(
165 self.ctxt, qos_policy_id=self.policy.id,
168 def test_get_policy_for_nonexistent_policy(self):
169 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
172 n_exc.QosPolicyNotFound,
173 self.qos_plugin.get_policy,
174 self.ctxt, self.policy.id)
176 def test_get_policy_bandwidth_limit_rule_for_nonexistent_policy(self):
177 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
180 n_exc.QosPolicyNotFound,
181 self.qos_plugin.get_policy_bandwidth_limit_rule,
182 self.ctxt, self.rule.id, self.policy.id)
184 def test_get_policy_bandwidth_limit_rules_for_nonexistent_policy(self):
185 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
188 n_exc.QosPolicyNotFound,
189 self.qos_plugin.get_policy_bandwidth_limit_rules,
190 self.ctxt, self.policy.id)
192 def test_create_policy_rule_for_nonexistent_policy(self):
193 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
196 n_exc.QosPolicyNotFound,
197 self.qos_plugin.create_policy_bandwidth_limit_rule,
198 self.ctxt, self.policy.id, self.rule_data)
200 def test_update_policy_rule_for_nonexistent_policy(self):
201 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
204 n_exc.QosPolicyNotFound,
205 self.qos_plugin.update_policy_bandwidth_limit_rule,
206 self.ctxt, self.rule.id, self.policy.id, self.rule_data)
208 def test_delete_policy_rule_for_nonexistent_policy(self):
209 with mock.patch('neutron.objects.qos.policy.QosPolicy.get_by_id',
212 n_exc.QosPolicyNotFound,
213 self.qos_plugin.delete_policy_bandwidth_limit_rule,
214 self.ctxt, self.rule.id, self.policy.id)