Debian Codegraph @

neutron_lib/tests/unit/exceptions/test_exceptions.py

raw
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
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
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
11 # under the License.
12
13 """
14 test_exceptions
15 ----------------------------------
16
17 Tests for `neutron_lib.exception` module.
18 """
19
20 import functools
21
22 from neutron_lib._i18n import _
23 import neutron_lib.exceptions as ne
24 from neutron_lib.tests import _base as base
25
26
27 def _raise(exc_class, **kwargs):
28 raise exc_class(**kwargs)
29
30
31 class TestExceptionsBase(base.BaseTestCase):
32
33 def _check_nexc(self, exc_class, expected_msg, **kwargs):
34 raise_exc_class = functools.partial(_raise, exc_class)
35 e = self.assertRaises(exc_class, raise_exc_class, **kwargs)
36 self.assertEqual(expected_msg, str(e))
37
38
39 class TestExceptions(TestExceptionsBase):
40
41 def test_base(self):
42 self._check_nexc(
43 ne.NeutronException,
44 _('An unknown exception occurred.'))
45
46 def test_not_found(self):
47 self._check_nexc(
48 ne.NotFound,
49 _('An unknown exception occurred.'))
50
51 def test_conflict(self):
52 self._check_nexc(
53 ne.Conflict,
54 _('An unknown exception occurred.'))
55
56 def test_bad_request(self):
57 self._check_nexc(
58 ne.BadRequest,
59 _('Bad A request: B.'),
60 resource='A', msg='B')
61
62 def test_bad_request_misused(self):
63 try:
64 self._check_nexc(
65 ne.BadRequest,
66 _('Bad A request: B.'),
67 resource='A', msg='B')
68 except AttributeError:
69 pass
70
71 def test_not_authorized(self):
72 self._check_nexc(
73 ne.NotAuthorized,
74 _("Not authorized."))
75
76 def test_service_unavailable(self):
77 self._check_nexc(
78 ne.ServiceUnavailable,
79 _("The service is unavailable."))
80
81 def test_admin_required(self):
82 self._check_nexc(
83 ne.AdminRequired,
84 _("User does not have admin privileges: hoser."),
85 reason="hoser")
86
87 def test_object_not_found(self):
88 self._check_nexc(
89 ne.ObjectNotFound,
90 _("Object fallout tato not found."),
91 id="fallout tato")
92
93 def test_network_not_found(self):
94 self._check_nexc(
95 ne.NetworkNotFound,
96 _("Network spam could not be found."),
97 net_id="spam")
98
99 def test_subnet_not_found(self):
100 self._check_nexc(
101 ne.SubnetNotFound,
102 _("Subnet root could not be found."),
103 subnet_id="root")
104
105 def test_port_not_found(self):
106 self._check_nexc(
107 ne.PortNotFound,
108 _("Port harbor could not be found."),
109 port_id="harbor")
110
111 def test_port_not_found_on_network(self):
112 self._check_nexc(
113 ne.PortNotFoundOnNetwork,
114 _("Port serial could not be found on network USB."),
115 port_id="serial", net_id="USB")
116
117 def test_device_not_found_error(self):
118 self._check_nexc(
119 ne.DeviceNotFoundError,
120 _("Device 'device' does not exist."),
121 device_name="device")
122
123 def test_in_use(self):
124 self._check_nexc(
125 ne.InUse,
126 _("The resource is in use."))
127
128 def test_network_in_use(self):
129 self._check_nexc(
130 ne.NetworkInUse,
131 _("Unable to complete operation on network foo. "
132 "There are one or more ports still in use on the network."),
133 net_id="foo")
134
135 def test_network_in_use_custom_reason(self):
136 self._check_nexc(
137 ne.NetworkInUse,
138 _("Unable to complete operation on network foo. not full."),
139 net_id="foo", reason="not full")
140
141 def test_subnet_in_use(self):
142 self._check_nexc(
143 ne.SubnetInUse,
144 _("Unable to complete operation on subnet garbage: not full."),
145 subnet_id="garbage", reason="not full")
146
147 def test_subnet_in_use_no_reason(self):
148 self._check_nexc(
149 ne.SubnetInUse,
150 _("Unable to complete operation on subnet garbage: "
151 "One or more ports have an IP allocation from this subnet."),
152 subnet_id="garbage")
153
154 def test_subnet_pool_in_use(self):
155 self._check_nexc(
156 ne.SubnetPoolInUse,
157 _("Unable to complete operation on subnet pool ymca. because."),
158 subnet_pool_id="ymca", reason="because")
159
160 def test_subnet_pool_in_use_no_reason(self):
161 self._check_nexc(
162 ne.SubnetPoolInUse,
163 _("Unable to complete operation on subnet pool ymca. "
164 "Two or more concurrent subnets allocated."),
165 subnet_pool_id="ymca")
166
167 def test_port_in_use(self):
168 self._check_nexc(
169 ne.PortInUse,
170 _("Unable to complete operation on port a for network c. "
171 "Port already has an attached device b."),
172 port_id='a', device_id='b', net_id='c')
173
174 def test_service_port_in_use(self):
175 self._check_nexc(
176 ne.ServicePortInUse,
177 _("Port harbor cannot be deleted directly via the "
178 "port API: docking."),
179 port_id='harbor', reason='docking')
180
181 def test_port_bound(self):
182 self._check_nexc(
183 ne.PortBound,
184 _("Unable to complete operation on port bigmac, "
185 "port is already bound, port type: ketchup, "
186 "old_mac onions, new_mac salt."),
187 port_id='bigmac', vif_type='ketchup', old_mac='onions',
188 new_mac='salt')
189
190 def test_mac_address_in_use(self):
191 self._check_nexc(
192 ne.MacAddressInUse,
193 _("Unable to complete operation for network nutters. "
194 "The mac address grill is in use."),
195 net_id='nutters', mac='grill')
196
197 def test_invalid_ip_for_network(self):
198 self._check_nexc(
199 ne.InvalidIpForNetwork,
200 _("IP address shazam! is not a valid IP "
201 "for any of the subnets on the specified network."),
202 ip_address='shazam!')
203
204 def test_invalid_ip_for_subnet(self):
205 self._check_nexc(
206 ne.InvalidIpForSubnet,
207 _("IP address 300.400.500.600 is not a valid IP "
208 "for the specified subnet."),
209 ip_address='300.400.500.600')
210
211 def test_ip_address_in_use(self):
212 self._check_nexc(
213 ne.IpAddressInUse,
214 _("Unable to complete operation for network boredom. "
215 "The IP address crazytown is in use."),
216 net_id='boredom', ip_address='crazytown')
217
218 def test_vlan_id_in_use(self):
219 self._check_nexc(
220 ne.VlanIdInUse,
221 _("Unable to create the network. The VLAN virtual on physical "
222 "network phys is in use."),
223 vlan_id='virtual', physical_network='phys')
224
225 def test_tunnel_id_in_use(self):
226 self._check_nexc(
227 ne.TunnelIdInUse,
228 _("Unable to create the network. The tunnel ID sewer is in use."),
229 tunnel_id='sewer')
230
231 def test_resource_exhausted(self):
232 self._check_nexc(
233 ne.ResourceExhausted,
234 _("The service is unavailable."))
235
236 def test_no_network_available(self):
237 self._check_nexc(
238 ne.NoNetworkAvailable,
239 _("Unable to create the network. "
240 "No project network is available for allocation."))
241
242 def test_subnet_mismatch_for_port(self):
243 self._check_nexc(
244 ne.SubnetMismatchForPort,
245 _("Subnet on port porter does not match "
246 "the requested subnet submit."),
247 port_id='porter', subnet_id='submit')
248
249 def test_invalid(self):
250 try:
251 raise ne.Invalid("hello world")
252 except ne.Invalid as e:
253 self.assertEqual(e.msg, "hello world")
254
255 def test_invalid_input(self):
256 self._check_nexc(
257 ne.InvalidInput,
258 _("Invalid input for operation: warp core breach."),
259 error_message='warp core breach')
260
261 def test_ip_address_generation_failure(self):
262 self._check_nexc(
263 ne.IpAddressGenerationFailure,
264 _("No more IP addresses available on network nuke."),
265 net_id='nuke')
266
267 def test_preexisting_device_failure(self):
268 self._check_nexc(
269 ne.PreexistingDeviceFailure,
270 _("Creation failed. hal9000 already exists."),
271 dev_name='hal9000')
272
273 def test_over_quota(self):
274 self._check_nexc(
275 ne.OverQuota,
276 _("Quota exceeded for resources: tube socks."),
277 overs='tube socks')
278
279 def test_invalid_content_type(self):
280 self._check_nexc(
281 ne.InvalidContentType,
282 _("Invalid content type porn."),
283 content_type='porn')
284
285 def test_external_ip_address_exhausted(self):
286 self._check_nexc(
287 ne.ExternalIpAddressExhausted,
288 _("Unable to find any IP address on external network darpanet."),
289 net_id='darpanet')
290
291 def test_invalid_configuration_option(self):
292 self._check_nexc(
293 ne.InvalidConfigurationOption,
294 _("An invalid value was provided for which muppet: big bird."),
295 opt_name='which muppet', opt_value='big bird')
296
297 def test_network_tunnel_range_error(self):
298 self._check_nexc(
299 ne.NetworkTunnelRangeError,
300 _("Invalid network tunnel range: 'rats' - present."),
301 tunnel_range='rats', error='present')
302
303 def test_network_tunnel_range_error_tuple(self):
304 self._check_nexc(
305 ne.NetworkTunnelRangeError,
306 _("Invalid network tunnel range: '3:4' - present."),
307 tunnel_range=(3, 4), error='present')
308
309 def test_policy_init_error(self):
310 self._check_nexc(
311 ne.PolicyInitError,
312 _("Failed to initialize policy policy because reason."),
313 policy='policy', reason='reason')
314
315 def test_policy_check_error(self):
316 self._check_nexc(
317 ne.PolicyCheckError,
318 _("Failed to check policy policy because reason."),
319 policy='policy', reason='reason')