Repository navigation
Expand file tree
/
Copy pathtest_undersync_mech.py
More file actions
604 lines (458 loc) · 20.9 KB
/
Copy pathtest_undersync_mech.py
File metadata and controls
604 lines (458 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import logging
from unittest.mock import MagicMock
import pytest
from neutron.conf.plugins.ml2 import config as ml2_config
from neutron_lib import constants as p_const
from neutron_lib import exceptions as exc
from neutron_lib.api.definitions import portbindings
from neutron_lib.callbacks import events
from neutron_lib.callbacks import priority_group
from neutron_lib.callbacks import resources
from neutron_lib.plugins.ml2 import api
from neutron_understack import undersync_mech
from neutron_understack.undersync_mech import UndersyncDriver
def _make_context(vnic_type=portbindings.VNIC_BAREMETAL, segments=None):
context = MagicMock()
context.current = {
"id": "port-1",
portbindings.VNIC_TYPE: vnic_type,
portbindings.PROFILE: {"physical_network": "physnet1"},
}
context.segments_to_bind = segments or []
return context
@pytest.fixture
def vlan_segment():
def _make(segment_id="seg-vlan-1"):
return {
api.ID: segment_id,
api.NETWORK_TYPE: p_const.TYPE_VLAN,
api.SEGMENTATION_ID: 100,
api.PHYSICAL_NETWORK: "physnet1",
api.MTU: 1500,
}
return _make
@pytest.fixture
def vxlan_segment():
def _make(segment_id="seg-vxlan-1"):
return {
api.ID: segment_id,
api.NETWORK_TYPE: p_const.TYPE_VXLAN,
api.SEGMENTATION_ID: 1000,
api.PHYSICAL_NETWORK: None,
api.MTU: 1450,
}
return _make
class TestUndersyncDriverBindPort:
def test_skips_port_without_a_binding_profile(self, undersync_driver, vlan_segment):
ctx = _make_context(segments=[vlan_segment()])
del ctx.current[portbindings.PROFILE]
undersync_driver.bind_port(ctx)
ctx.set_binding.assert_not_called()
@pytest.mark.parametrize(
"binding_profile",
[{}, {"local_link_information": []}, {"physical_network": None}],
)
def test_skips_port_without_a_physical_network(
self, undersync_driver, vlan_segment, binding_profile
):
ctx = _make_context(segments=[vlan_segment()])
ctx.current[portbindings.PROFILE] = binding_profile
undersync_driver.bind_port(ctx)
ctx.set_binding.assert_not_called()
def test_binds_vlan_segment(self, undersync_driver, vlan_segment):
seg = vlan_segment()
ctx = _make_context(segments=[seg])
undersync_driver.bind_port(ctx)
ctx.set_binding.assert_called_once_with(
segment_id=seg[api.ID],
vif_type=portbindings.VIF_TYPE_OTHER,
vif_details={},
status=p_const.PORT_STATUS_ACTIVE,
)
def test_binds_first_vlan_segment_only(self, undersync_driver, vlan_segment):
seg1 = vlan_segment("seg-vlan-1")
seg2 = vlan_segment("seg-vlan-2")
ctx = _make_context(segments=[seg1, seg2])
undersync_driver.bind_port(ctx)
ctx.set_binding.assert_called_once_with(
segment_id=seg1[api.ID],
vif_type=portbindings.VIF_TYPE_OTHER,
vif_details={},
status=p_const.PORT_STATUS_ACTIVE,
)
def test_skips_vxlan_segment(self, undersync_driver, vxlan_segment):
ctx = _make_context(segments=[vxlan_segment()])
undersync_driver.bind_port(ctx)
ctx.set_binding.assert_not_called()
def test_skips_unsupported_vnic_type(self, undersync_driver, vlan_segment):
ctx = _make_context(vnic_type="direct", segments=[vlan_segment()])
undersync_driver.bind_port(ctx)
ctx.set_binding.assert_not_called()
def test_normal_vnic_type_is_not_supported(self, undersync_driver, vlan_segment):
ctx = _make_context(
vnic_type=portbindings.VNIC_NORMAL, segments=[vlan_segment()]
)
undersync_driver.bind_port(ctx)
ctx.set_binding.assert_not_called()
def test_binds_vlan_when_preceded_by_vxlan(
self, undersync_driver, vxlan_segment, vlan_segment
):
vlan = vlan_segment()
ctx = _make_context(segments=[vxlan_segment(), vlan])
undersync_driver.bind_port(ctx)
ctx.set_binding.assert_called_once_with(
segment_id=vlan[api.ID],
vif_type=portbindings.VIF_TYPE_OTHER,
vif_details={},
status=p_const.PORT_STATUS_ACTIVE,
)
def test_empty_segments_to_bind(self, undersync_driver):
ctx = _make_context(segments=[])
undersync_driver.bind_port(ctx)
ctx.set_binding.assert_not_called()
def test_skips_direct_vnic_type(self, undersync_driver, vlan_segment):
ctx = _make_context(
vnic_type=portbindings.VNIC_DIRECT, segments=[vlan_segment()]
)
undersync_driver.bind_port(ctx)
ctx.set_binding.assert_not_called()
def test_logs_warning_when_no_vlan_segment_found(
self, undersync_driver, vxlan_segment, caplog
):
ctx = _make_context(segments=[vxlan_segment()])
undersync_driver.bind_port(ctx)
assert "no VLAN segment found" in caplog.text
ctx.set_binding.assert_not_called()
class TestInitialize:
@pytest.fixture(autouse=True)
def subscribe(self, mocker):
"""Keep throwaway drivers out of the process-global callback registry."""
return mocker.patch("neutron_understack.undersync_mech.registry.subscribe")
@pytest.fixture(autouse=True)
def _ml2_driver_order(self, oslo_config):
ml2_config.register_ml2_plugin_opts(oslo_config.conf)
oslo_config.config(
mechanism_drivers=["ovn", "understack", "baremetal", "undersync"],
group="ml2",
)
def test_builds_the_client_from_the_configured_url(self, mocker, oslo_config):
oslo_config.config(
undersync_url="http://undersync.example:8080", group="ml2_understack"
)
undersync_cls = mocker.patch("neutron_understack.undersync_mech.Undersync")
UndersyncDriver().initialize()
undersync_cls.assert_called_once_with("http://undersync.example:8080")
def test_undersync_client_uses_the_ironic_keystone_session(self, mocker):
"""The client borrows the [ironic] credentials rather than its own."""
session = mocker.MagicMock()
get_session = mocker.patch(
"neutron_understack.config.get_session", return_value=session
)
driver = UndersyncDriver()
driver.initialize()
get_session.assert_called_once_with("ironic")
assert driver.undersync._session == session
def test_refuses_to_start_when_listed_before_understack(self, mocker, oslo_config):
"""Misordered, it would sync before understack released the segment."""
undersync_cls = mocker.patch("neutron_understack.undersync_mech.Undersync")
oslo_config.config(
mechanism_drivers=["ovn", "undersync", "baremetal", "understack"],
group="ml2",
)
with pytest.raises(
undersync_mech.MechanismDriverOrderError, match="ovn,undersync,baremetal"
):
UndersyncDriver().initialize()
undersync_cls.assert_not_called()
def test_starts_without_understack_loaded(self, mocker, oslo_config):
"""There is nothing to be ordered against, so nothing to enforce."""
mocker.patch("neutron_understack.undersync_mech.Undersync")
oslo_config.config(mechanism_drivers=["ovn", "undersync"], group="ml2")
UndersyncDriver().initialize()
def test_subscribes_to_the_trunk_events(self, mocker, subscribe):
mocker.patch("neutron_understack.undersync_mech.Undersync")
UndersyncDriver().initialize()
subscribed = {(call.args[1], call.args[2]) for call in subscribe.call_args_list}
assert subscribed == {
(resources.SUBPORTS, events.PRECOMMIT_DELETE),
(resources.TRUNK, events.PRECOMMIT_DELETE),
(resources.SUBPORTS, events.AFTER_CREATE),
(resources.SUBPORTS, events.AFTER_DELETE),
(resources.TRUNK, events.AFTER_DELETE),
}
def test_every_subscription_is_cancellable(self, mocker, subscribe):
"""An UndersyncError must abort the request, not be swallowed.
neutron-lib only re-raises from a postcommit event when the callback
was subscribed cancellable, so dropping this silently loses failures.
"""
mocker.patch("neutron_understack.undersync_mech.Undersync")
UndersyncDriver().initialize()
assert all(call.kwargs["cancellable"] for call in subscribe.call_args_list)
def test_after_hooks_run_later_than_the_understack_trunk_driver(
self, mocker, subscribe
):
"""The trunk driver subscribes at PRIORITY_DEFAULT and must go first."""
mocker.patch("neutron_understack.undersync_mech.Undersync")
UndersyncDriver().initialize()
after_hooks = [
call
for call in subscribe.call_args_list
if call.args[2] != events.PRECOMMIT_DELETE
]
assert after_hooks
assert all(
call.kwargs["priority"] > priority_group.PRIORITY_DEFAULT
for call in after_hooks
)
class TestUpdatePortPostcommit:
def test_syncs_when_the_port_is_bound(
self, undersync_driver, undersync_client, port_context
):
undersync_driver.update_port_postcommit(port_context)
undersync_client.sync.assert_called_once_with("physnet")
def test_syncs_when_the_port_is_unbound(
self, undersync_driver, undersync_client, port_context
):
port_context._binding.vif_type = portbindings.VIF_TYPE_UNBOUND
undersync_driver.update_port_postcommit(port_context)
undersync_client.sync.assert_called_once_with("physnet")
def test_reads_the_physnet_from_the_original_port_when_unbinding(
self, undersync_driver, undersync_client, port_context
):
"""Only ``original`` still carries a binding profile once unbound."""
port_context._binding.vif_type = portbindings.VIF_TYPE_UNBOUND
port_context.current[portbindings.PROFILE] = {
"physical_network": "physnet-current"
}
port_context.original[portbindings.PROFILE] = {
"physical_network": "physnet-original"
}
undersync_driver.update_port_postcommit(port_context)
undersync_client.sync.assert_called_once_with("physnet-original")
@pytest.mark.parametrize(
"binding_profile", [{"physical_network": None}], indirect=True
)
def test_does_not_sync_without_a_physnet(
self, undersync_driver, undersync_client, port_context
):
undersync_driver.update_port_postcommit(port_context)
undersync_client.sync.assert_not_called()
def test_skips_non_baremetal_ports(
self, undersync_driver, undersync_client, port_context
):
port_context.current[portbindings.VNIC_TYPE] = portbindings.VNIC_NORMAL
undersync_driver.update_port_postcommit(port_context)
undersync_client.sync.assert_not_called()
def test_does_not_sync_a_port_that_was_never_bound(
self, undersync_driver, undersync_client, port_context
):
port_context._binding.vif_type = portbindings.VIF_TYPE_UNBOUND
port_context._original_vif_type = portbindings.VIF_TYPE_UNBOUND
undersync_driver.update_port_postcommit(port_context)
undersync_client.sync.assert_not_called()
class TestDeletePortPostcommit:
def test_syncs_the_ports_vlan_group(
self, undersync_driver, undersync_client, port_context
):
undersync_driver.delete_port_postcommit(port_context)
undersync_client.sync.assert_called_once_with("physnet")
@pytest.mark.parametrize(
"binding_profile", [{"physical_network": None}], indirect=True
)
def test_does_not_sync_without_a_physnet(
self, undersync_driver, undersync_client, port_context
):
undersync_driver.delete_port_postcommit(port_context)
undersync_client.sync.assert_not_called()
def test_skips_non_baremetal_ports(
self, undersync_driver, undersync_client, port_context
):
port_context.current[portbindings.VNIC_TYPE] = portbindings.VNIC_NORMAL
undersync_driver.delete_port_postcommit(port_context)
undersync_client.sync.assert_not_called()
@pytest.mark.usefixtures("_bound_parent_port")
class TestTrunkEventHandlers:
@pytest.fixture
def _bound_parent_port(self, mocker, port_object) -> None:
mocker.patch(
"neutron_understack.utils.fetch_port_object", return_value=port_object
)
@pytest.mark.parametrize(
"handler_name",
["_subports_after_create", "_subports_after_delete", "_trunk_after_delete"],
)
def test_syncs_the_parent_ports_vlan_group(
self, mocker, undersync_driver, undersync_client, trunk, handler_name
):
handler = getattr(undersync_driver, handler_name)
handler(None, None, None, mocker.Mock(states=[trunk]))
undersync_client.sync.assert_called_once_with("physnet")
@pytest.mark.parametrize(
"handler_name",
["_subports_after_create", "_subports_after_delete", "_trunk_after_delete"],
)
def test_does_not_sync_when_the_parent_port_is_unbound(
self,
mocker,
undersync_driver,
undersync_client,
trunk,
port_object,
handler_name,
):
port_object.bindings[0].vif_type = portbindings.VIF_TYPE_UNBOUND
handler = getattr(undersync_driver, handler_name)
handler(None, None, None, mocker.Mock(states=[trunk]))
undersync_client.sync.assert_not_called()
def test_trunk_delete_without_subports_does_nothing(
self, mocker, undersync_driver, undersync_client, trunk
):
trunk.sub_ports = []
fetch = mocker.patch(
"neutron_understack.utils.fetch_port_object", return_value=None
)
undersync_driver._trunk_after_delete(
None, None, None, mocker.Mock(states=[trunk])
)
undersync_client.sync.assert_not_called()
fetch.assert_not_called()
@pytest.mark.parametrize(
"binding_profile", [{"physical_network": None}], indirect=True
)
def test_logs_instead_of_raising_when_the_physnet_is_missing(
self, mocker, caplog, undersync_driver, undersync_client, trunk
):
"""This runs postcommit, so raising would only surface as a 500."""
caplog.set_level(logging.ERROR, logger=undersync_mech.LOG.name)
undersync_driver._subports_after_delete(
None, None, None, mocker.Mock(states=[trunk])
)
assert "physical_network is required" in caplog.text
undersync_client.sync.assert_not_called()
class TestValidateParentPhysnet:
@pytest.fixture
def _bound_parent_port(self, mocker, port_object) -> None:
mocker.patch(
"neutron_understack.utils.fetch_port_object", return_value=port_object
)
@pytest.mark.usefixtures("_bound_parent_port")
@pytest.mark.parametrize("resource", [resources.SUBPORTS, resources.TRUNK])
def test_accepts_a_parent_port_with_a_physnet(
self, mocker, undersync_driver, trunk, resource
):
undersync_driver._validate_parent_physnet(
resource, events.PRECOMMIT_DELETE, None, mocker.Mock(states=[trunk])
)
@pytest.mark.usefixtures("_bound_parent_port")
@pytest.mark.parametrize("resource", [resources.SUBPORTS, resources.TRUNK])
@pytest.mark.parametrize(
"binding_profile", [{"physical_network": None}], indirect=True
)
def test_rejects_a_parent_port_without_a_physnet(
self, mocker, undersync_driver, trunk, resource
):
with pytest.raises(exc.BadRequest, match="physical_network is required"):
undersync_driver._validate_parent_physnet(
resource, events.PRECOMMIT_DELETE, None, mocker.Mock(states=[trunk])
)
@pytest.mark.parametrize(
"binding_profile", [{"physical_network": None}], indirect=True
)
def test_ignores_an_unbound_parent_port(
self, mocker, undersync_driver, trunk, port_object
):
"""An unbound parent has no switchport config, so nothing can leak."""
port_object.bindings[0].vif_type = portbindings.VIF_TYPE_UNBOUND
mocker.patch(
"neutron_understack.utils.fetch_port_object", return_value=port_object
)
undersync_driver._validate_parent_physnet(
resources.SUBPORTS,
events.PRECOMMIT_DELETE,
None,
mocker.Mock(states=[trunk]),
)
def test_ignores_a_trunk_with_no_subports(self, mocker, undersync_driver, trunk):
"""Never fetches the port: there is no switchport config to protect."""
trunk.sub_ports = []
fetch = mocker.patch("neutron_understack.utils.fetch_port_object")
undersync_driver._validate_parent_physnet(
resources.TRUNK,
events.PRECOMMIT_DELETE,
None,
mocker.Mock(states=[trunk]),
)
fetch.assert_not_called()
class TestUndersyncDriverLagName:
def _bonded_ctx(self, vlan_segment, port_id="Eth1/17"):
ctx = _make_context(segments=[vlan_segment()])
ctx.current[portbindings.PROFILE].update(
{
"local_link_information": [{"port_id": port_id, "switch_id": "aa"}],
"local_group_information": {"id": "g1", "name": "bond0"},
}
)
return ctx
def test_precommit_stores_lag(self, undersync_driver, vlan_segment, mocker):
ctx = self._bonded_ctx(vlan_segment)
store = mocker.patch.object(undersync_mech.utils, "set_active_binding_profile")
undersync_driver.update_port_precommit(ctx)
profile = ctx.current[portbindings.PROFILE]
assert profile["local_group_information"]["lag"] == "Po1170"
store.assert_called_once_with(ctx.plugin_context, "port-1", profile)
def test_precommit_keeps_existing_lag(self, undersync_driver, vlan_segment, mocker):
ctx = self._bonded_ctx(vlan_segment)
ctx.current[portbindings.PROFILE]["local_group_information"]["lag"] = "Po9"
store = mocker.patch.object(undersync_mech.utils, "set_active_binding_profile")
undersync_driver.update_port_precommit(ctx)
assert (
ctx.current[portbindings.PROFILE]["local_group_information"]["lag"] == "Po9"
)
store.assert_not_called()
def test_precommit_ignores_unbonded_port(
self, undersync_driver, vlan_segment, mocker
):
ctx = _make_context(segments=[vlan_segment()])
store = mocker.patch.object(undersync_mech.utils, "set_active_binding_profile")
undersync_driver.update_port_precommit(ctx)
store.assert_not_called()
def test_precommit_rejects_unnameable_anchor(
self, undersync_driver, vlan_segment, mocker
):
ctx = self._bonded_ctx(vlan_segment, port_id="Eth2/1")
store = mocker.patch.object(undersync_mech.utils, "set_active_binding_profile")
with pytest.raises(exc.InvalidInput):
undersync_driver.update_port_precommit(ctx)
store.assert_not_called()
def test_lag_derived_for_bonded_port(self, undersync_driver, vlan_segment):
ctx = self._bonded_ctx(vlan_segment)
assert undersync_driver._lag_for(ctx.current) == "Po1170"
def test_no_lag_for_unbonded_port(self, undersync_driver, vlan_segment):
ctx = _make_context(segments=[vlan_segment()])
assert undersync_driver._lag_for(ctx.current) is None
def test_stored_lag_wins(self, undersync_driver, vlan_segment):
ctx = self._bonded_ctx(vlan_segment)
ctx.current[portbindings.PROFILE]["local_group_information"]["lag"] = "Po9"
assert undersync_driver._lag_for(ctx.current) == "Po9"
def test_update_postcommit_reads_lag(self, undersync_driver, vlan_segment):
ctx = self._bonded_ctx(vlan_segment)
ctx.vif_type = portbindings.VIF_TYPE_OTHER
undersync_driver._postcommit_lag = MagicMock(return_value="Po1170")
undersync_driver.update_port_postcommit(ctx)
undersync_driver._postcommit_lag.assert_called_once_with(ctx.current)
def test_delete_postcommit_reads_lag(self, undersync_driver, vlan_segment):
ctx = self._bonded_ctx(vlan_segment)
undersync_driver._postcommit_lag = MagicMock(return_value="Po1170")
undersync_driver.delete_port_postcommit(ctx)
undersync_driver._postcommit_lag.assert_called_once_with(ctx.current)
def test_postcommit_lag_swallows_bad_anchor(self, undersync_driver, vlan_segment):
ctx = self._bonded_ctx(vlan_segment, port_id="Eth2/1")
assert undersync_driver._postcommit_lag(ctx.current) is None
def test_precommit_rejects_bond_without_links(
self, undersync_driver, vlan_segment, mocker
):
ctx = self._bonded_ctx(vlan_segment)
ctx.current[portbindings.PROFILE]["local_link_information"] = []
mocker.patch.object(undersync_mech.utils, "set_active_binding_profile")
with pytest.raises(exc.InvalidInput):
undersync_driver.update_port_precommit(ctx)