1 # Copyright (c) 2015 Red Hat, Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
19 from oslo_utils import uuidutils
21 from neutron.api.rpc.callbacks.consumer import registry as consumer_reg
22 from neutron.api.rpc.callbacks import events
23 from neutron.api.rpc.callbacks import resources
24 from neutron.objects.qos import policy
25 from neutron.objects.qos import rule
26 from neutron.tests.common.agents import l2_extensions
27 from neutron.tests.functional.agent.l2 import base
30 TEST_POLICY_ID1 = "a2d72369-4246-4f19-bd3c-af51ec8d70cd"
31 TEST_POLICY_ID2 = "46ebaec0-0570-43ac-82f6-60d2b03168c5"
32 TEST_BW_LIMIT_RULE_1 = rule.QosBandwidthLimitRule(
34 qos_policy_id=TEST_POLICY_ID1,
35 id="5f126d84-551a-4dcf-bb01-0e9c0df0c793",
38 TEST_BW_LIMIT_RULE_2 = rule.QosBandwidthLimitRule(
40 qos_policy_id=TEST_POLICY_ID2,
41 id="fa9128d9-44af-49b2-99bb-96548378ad42",
46 class OVSAgentQoSExtensionTestFramework(base.OVSAgentTestFramework):
48 super(OVSAgentQoSExtensionTestFramework, self).setUp()
49 self.config.set_override('extensions', ['qos'], 'agent')
51 self.set_test_qos_rules(TEST_POLICY_ID1, [TEST_BW_LIMIT_RULE_1])
52 self.set_test_qos_rules(TEST_POLICY_ID2, [TEST_BW_LIMIT_RULE_2])
54 def _set_pull_mock(self):
56 self.qos_policies = {}
58 def _pull_mock(context, resource_type, resource_id):
59 return self.qos_policies[resource_id]
61 self.pull = mock.patch(
62 'neutron.api.rpc.handlers.resources_rpc.'
63 'ResourcesPullRpcApi.pull').start()
64 self.pull.side_effect = _pull_mock
66 def set_test_qos_rules(self, policy_id, policy_rules):
67 """This function sets the policy test rules to be exposed."""
69 qos_policy = policy.QosPolicy(
71 tenant_id=uuidutils.generate_uuid(),
73 name="Test Policy Name",
74 description="This is a policy for testing purposes",
78 qos_policy.obj_reset_changes()
79 self.qos_policies[policy_id] = qos_policy
81 def _create_test_port_dict(self, policy_id=None):
82 port_dict = super(OVSAgentQoSExtensionTestFramework,
83 self)._create_test_port_dict()
84 port_dict['qos_policy_id'] = policy_id
85 port_dict['network_qos_policy_id'] = None
88 def _get_device_details(self, port, network):
89 dev = super(OVSAgentQoSExtensionTestFramework,
90 self)._get_device_details(port, network)
91 dev['qos_policy_id'] = port['qos_policy_id']
94 def _assert_bandwidth_limit_rule_is_set(self, port, rule):
96 self.agent.int_br.get_egress_bw_limit_for_port(port['vif_name']))
97 self.assertEqual(max_rate, rule.max_kbps)
98 self.assertEqual(burst, rule.max_burst_kbps)
100 def _assert_bandwidth_limit_rule_not_set(self, port):
102 self.agent.int_br.get_egress_bw_limit_for_port(port['vif_name']))
103 self.assertIsNone(max_rate)
104 self.assertIsNone(burst)
106 def wait_until_bandwidth_limit_rule_applied(self, port, rule):
107 l2_extensions.wait_until_bandwidth_limit_rule_applied(
108 self.agent.int_br, port['vif_name'], rule)
110 def _create_port_with_qos(self):
111 port_dict = self._create_test_port_dict()
112 port_dict['qos_policy_id'] = TEST_POLICY_ID1
113 self.setup_agent_and_ports([port_dict])
114 self.wait_until_ports_state(self.ports, up=True)
115 self.wait_until_bandwidth_limit_rule_applied(port_dict,
116 TEST_BW_LIMIT_RULE_1)
120 class TestOVSAgentQosExtension(OVSAgentQoSExtensionTestFramework):
122 def test_port_creation_with_bandwidth_limit(self):
123 """Make sure bandwidth limit rules are set in low level to ports."""
125 self.setup_agent_and_ports(
126 port_dicts=self.create_test_ports(amount=1,
127 policy_id=TEST_POLICY_ID1))
128 self.wait_until_ports_state(self.ports, up=True)
130 for port in self.ports:
131 self._assert_bandwidth_limit_rule_is_set(
132 port, TEST_BW_LIMIT_RULE_1)
134 def test_port_creation_with_different_bandwidth_limits(self):
135 """Make sure different types of policies end on the right ports."""
137 port_dicts = self.create_test_ports(amount=3)
139 port_dicts[0]['qos_policy_id'] = TEST_POLICY_ID1
140 port_dicts[1]['qos_policy_id'] = TEST_POLICY_ID2
142 self.setup_agent_and_ports(port_dicts)
143 self.wait_until_ports_state(self.ports, up=True)
145 self._assert_bandwidth_limit_rule_is_set(self.ports[0],
146 TEST_BW_LIMIT_RULE_1)
148 self._assert_bandwidth_limit_rule_is_set(self.ports[1],
149 TEST_BW_LIMIT_RULE_2)
151 self._assert_bandwidth_limit_rule_not_set(self.ports[2])
153 def test_simple_port_policy_update(self):
154 self.setup_agent_and_ports(
155 port_dicts=self.create_test_ports(amount=1,
156 policy_id=TEST_POLICY_ID1))
157 self.wait_until_ports_state(self.ports, up=True)
158 policy_copy = copy.deepcopy(self.qos_policies[TEST_POLICY_ID1])
159 policy_copy.rules[0].max_kbps = 500
160 policy_copy.rules[0].max_burst_kbps = 5
161 consumer_reg.push(resources.QOS_POLICY, policy_copy, events.UPDATED)
162 self.wait_until_bandwidth_limit_rule_applied(self.ports[0],
163 policy_copy.rules[0])
164 self._assert_bandwidth_limit_rule_is_set(self.ports[0],
165 policy_copy.rules[0])
167 def test_port_qos_disassociation(self):
168 """Test that qos_policy_id set to None will remove all qos rules from
171 port_dict = self._create_port_with_qos()
173 port_dict['qos_policy_id'] = None
174 self.agent.port_update(None, port=port_dict)
176 self.wait_until_bandwidth_limit_rule_applied(port_dict, None)
178 def test_port_qos_update_policy_id(self):
179 """Test that change of qos policy id on given port refreshes all its
182 port_dict = self._create_port_with_qos()
184 port_dict['qos_policy_id'] = TEST_POLICY_ID2
185 self.agent.port_update(None, port=port_dict)
187 self.wait_until_bandwidth_limit_rule_applied(port_dict,
188 TEST_BW_LIMIT_RULE_2)
190 def test_policy_rule_delete(self):
191 port_dict = self._create_port_with_qos()
193 policy_copy = copy.deepcopy(self.qos_policies[TEST_POLICY_ID1])
194 policy_copy.rules = list()
195 consumer_reg.push(resources.QOS_POLICY, policy_copy, events.UPDATED)
197 self.wait_until_bandwidth_limit_rule_applied(port_dict, None)