Passed
Push — master ( 5413c2...c98987 )
by Vinicius
08:23 queued 06:42
created

test_flow_builder_intra_evc   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 617
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 428
dl 0
loc 617
ccs 102
cts 102
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Functions

Rating   Name   Duplication   Size   Complexity  
B test_build_int_flows_intra_evpl() 0 303 3
B test_build_int_flows_intra_epl() 0 299 3
1
"""Test flow_builder."""
2
3 1
from unittest.mock import MagicMock
4 1
from napps.kytos.telemetry_int.managers import flow_builder as fb
5 1
from napps.kytos.telemetry_int.managers.int import INTManager
6 1
from napps.kytos.telemetry_int.utils import ProxyPort, get_cookie
7 1
from napps.kytos.telemetry_int import settings
8 1
from napps.kytos.telemetry_int.kytos_api_helper import _map_stored_flows_by_cookies
9 1
from kytos.lib.helpers import get_controller_mock, get_switch_mock, get_interface_mock
10 1
from kytos.core.common import EntityStatus
11
12
13 1
def test_build_int_flows_intra_evpl(
14
    evcs_data, intra_evc_evpl_flows_data, monkeypatch
15
) -> None:
16
    """Test build INT flows intra EVPL."""
17 1
    controller = get_controller_mock()
18 1
    int_manager = INTManager(controller)
19 1
    get_proxy_port_or_raise = MagicMock()
20 1
    monkeypatch.setattr(
21
        "napps.kytos.telemetry_int.utils.get_proxy_port_or_raise",
22
        get_proxy_port_or_raise,
23
    )
24
25 1
    evc_id = "3766c105686749"
26 1
    dpid_a = "00:00:00:00:00:00:00:01"
27 1
    mock_switch_a = get_switch_mock(dpid_a, 0x04)
28 1
    mock_interface_a1 = get_interface_mock("s1-eth1", 1, mock_switch_a)
29 1
    mock_interface_a1.id = f"{dpid_a}:{mock_interface_a1.port_number}"
30 1
    mock_interface_a10 = get_interface_mock("s1-eth10", 10, mock_switch_a)
31 1
    mock_interface_a1.metadata = {"proxy_port": mock_interface_a10.port_number}
32 1
    mock_interface_a10.status = EntityStatus.UP
33 1
    mock_interface_a11 = get_interface_mock("s1-eth11", 11, mock_switch_a)
34 1
    mock_interface_a11.status = EntityStatus.UP
35 1
    mock_interface_a10.metadata = {
36
        "looped": {
37
            "port_numbers": [
38
                mock_interface_a10.port_number,
39
                mock_interface_a11.port_number,
40
            ]
41
        }
42
    }
43
44 1
    mock_interface_z1 = get_interface_mock("s1-eth2", 1, mock_switch_a)
45 1
    mock_interface_z1.status = EntityStatus.UP
46 1
    mock_interface_z1.id = f"{dpid_a}:{mock_interface_a1.port_number}"
47 1
    mock_interface_z20 = get_interface_mock("s1-eth20", 20, mock_switch_a)
48 1
    mock_interface_z1.metadata = {"proxy_port": mock_interface_z20.port_number}
49 1
    mock_interface_z20.status = EntityStatus.UP
50 1
    mock_interface_z21 = get_interface_mock("s1-eth21", 21, mock_switch_a)
51 1
    mock_interface_z21.status = EntityStatus.UP
52 1
    mock_interface_z20.metadata = {
53
        "looped": {
54
            "port_numbers": [
55
                mock_interface_z20.port_number,
56
                mock_interface_z21.port_number,
57
            ]
58
        }
59
    }
60
61 1
    mock_switch_a.get_interface_by_port_no = lambda port_no: {
62
        mock_interface_a10.port_number: mock_interface_a10,
63
        mock_interface_a11.port_number: mock_interface_a11,
64
        mock_interface_z20.port_number: mock_interface_z20,
65
        mock_interface_z21.port_number: mock_interface_z21,
66
    }[port_no]
67
68 1
    pp_a = ProxyPort(controller, source=mock_interface_a10)
69 1
    assert pp_a.source == mock_interface_a10
70 1
    assert pp_a.destination == mock_interface_a11
71 1
    pp_z = ProxyPort(controller, source=mock_interface_z20)
72 1
    assert pp_z.source == mock_interface_z20
73 1
    assert pp_z.destination == mock_interface_z21
74
75 1
    get_proxy_port_or_raise.side_effect = [pp_a, pp_z]
76 1
    evcs_data = {evc_id: evcs_data[evc_id]}
77 1
    evcs_data = int_manager._validate_map_enable_evcs(evcs_data)
78 1
    stored_flows = _map_stored_flows_by_cookies(intra_evc_evpl_flows_data)
79
80 1
    cookie = get_cookie(evc_id, settings.MEF_COOKIE_PREFIX)
81 1
    flows = fb.build_int_flows(evcs_data, stored_flows)[cookie]
82
83 1
    n_expected_source_flows, n_expected_sink_flows = 3, 2
84 1
    assert len(flows) == (n_expected_source_flows + n_expected_sink_flows) * 2
85
86 1
    expected_uni_a_source_flows = [
87
        {
88
            "flow": {
89
                "owner": "telemetry_int",
90
                "cookie": int(0xA83766C105686749),
91
                "match": {"in_port": 1, "dl_vlan": 200, "dl_type": 2048, "nw_proto": 6},
92
                "table_id": 0,
93
                "table_group": "evpl",
94
                "priority": 20100,
95
                "idle_timeout": 0,
96
                "hard_timeout": 0,
97
                "instructions": [
98
                    {
99
                        "instruction_type": "apply_actions",
100
                        "actions": [{"action_type": "push_int"}],
101
                    },
102
                    {"instruction_type": "goto_table", "table_id": 2},
103
                ],
104
            }
105
        },
106
        {
107
            "flow": {
108
                "owner": "telemetry_int",
109
                "cookie": int(0xA83766C105686749),
110
                "match": {
111
                    "in_port": 1,
112
                    "dl_vlan": 200,
113
                    "dl_type": 2048,
114
                    "nw_proto": 17,
115
                },
116
                "table_id": 0,
117
                "table_group": "evpl",
118
                "priority": 20100,
119
                "idle_timeout": 0,
120
                "hard_timeout": 0,
121
                "instructions": [
122
                    {
123
                        "instruction_type": "apply_actions",
124
                        "actions": [{"action_type": "push_int"}],
125
                    },
126
                    {"instruction_type": "goto_table", "table_id": 2},
127
                ],
128
            }
129
        },
130
        {
131
            "flow": {
132
                "owner": "telemetry_int",
133
                "cookie": int(0xA83766C105686749),
134
                "match": {"in_port": 1, "dl_vlan": 200},
135
                "table_id": 2,
136
                "table_group": "base",
137
                "priority": 20000,
138
                "idle_timeout": 0,
139
                "hard_timeout": 0,
140
                "instructions": [
141
                    {
142
                        "instruction_type": "apply_actions",
143
                        "actions": [
144
                            {"action_type": "add_int_metadata"},
145
                            {"action_type": "output", "port": 20},
146
                        ],
147
                    }
148
                ],
149
            }
150
        },
151
    ]
152
153 1
    expected_uni_z_source_flows = [
154
        {
155
            "flow": {
156
                "owner": "telemetry_int",
157
                "cookie": int(0xA83766C105686749),
158
                "match": {"in_port": 2, "dl_vlan": 200, "dl_type": 2048, "nw_proto": 6},
159
                "table_id": 0,
160
                "table_group": "evpl",
161
                "priority": 20100,
162
                "idle_timeout": 0,
163
                "hard_timeout": 0,
164
                "instructions": [
165
                    {
166
                        "instruction_type": "apply_actions",
167
                        "actions": [{"action_type": "push_int"}],
168
                    },
169
                    {"instruction_type": "goto_table", "table_id": 2},
170
                ],
171
            }
172
        },
173
        {
174
            "flow": {
175
                "owner": "telemetry_int",
176
                "cookie": int(0xA83766C105686749),
177
                "match": {
178
                    "in_port": 2,
179
                    "dl_vlan": 200,
180
                    "dl_type": 2048,
181
                    "nw_proto": 17,
182
                },
183
                "table_id": 0,
184
                "table_group": "evpl",
185
                "priority": 20100,
186
                "idle_timeout": 0,
187
                "hard_timeout": 0,
188
                "instructions": [
189
                    {
190
                        "instruction_type": "apply_actions",
191
                        "actions": [{"action_type": "push_int"}],
192
                    },
193
                    {"instruction_type": "goto_table", "table_id": 2},
194
                ],
195
            }
196
        },
197
        {
198
            "flow": {
199
                "owner": "telemetry_int",
200
                "cookie": int(0xA83766C105686749),
201
                "match": {"in_port": 2, "dl_vlan": 200},
202
                "table_id": 2,
203
                "table_group": "base",
204
                "priority": 20000,
205
                "idle_timeout": 0,
206
                "hard_timeout": 0,
207
                "instructions": [
208
                    {
209
                        "instruction_type": "apply_actions",
210
                        "actions": [
211
                            {"action_type": "add_int_metadata"},
212
                            {"action_type": "output", "port": 10},
213
                        ],
214
                    }
215
                ],
216
            }
217
        },
218
    ]
219
220 1
    expected_uni_a_sink_flows = [
221
        {
222
            "flow": {
223
                "owner": "telemetry_int",
224
                "cookie": int(0xA83766C105686749),
225
                "match": {"in_port": 11, "dl_vlan": 200},
226
                "table_id": 0,
227
                "table_group": "evpl",
228
                "priority": 20000,
229
                "idle_timeout": 0,
230
                "hard_timeout": 0,
231
                "instructions": [
232
                    {
233
                        "instruction_type": "apply_actions",
234
                        "actions": [{"action_type": "send_report"}],
235
                    },
236
                    {"instruction_type": "goto_table", "table_id": 2},
237
                ],
238
            },
239
        },
240
        {
241
            "flow": {
242
                "owner": "telemetry_int",
243
                "cookie": int(0xA83766C105686749),
244
                "match": {"in_port": 11, "dl_vlan": 200},
245
                "table_id": 2,
246
                "table_group": "base",
247
                "priority": 20000,
248
                "idle_timeout": 0,
249
                "hard_timeout": 0,
250
                "instructions": [
251
                    {
252
                        "instruction_type": "apply_actions",
253
                        "actions": [
254
                            {"action_type": "pop_int"},
255
                            {"action_type": "set_vlan", "vlan_id": 200},
256
                            {"action_type": "output", "port": 1},
257
                        ],
258
                    }
259
                ],
260
            },
261
        },
262
    ]
263 1
    expected_uni_z_sink_flows = [
264
        {
265
            "flow": {
266
                "owner": "telemetry_int",
267
                "cookie": int(0xA83766C105686749),
268
                "match": {"in_port": 21, "dl_vlan": 200},
269
                "table_id": 0,
270
                "table_group": "evpl",
271
                "priority": 20000,
272
                "idle_timeout": 0,
273
                "hard_timeout": 0,
274
                "instructions": [
275
                    {
276
                        "instruction_type": "apply_actions",
277
                        "actions": [{"action_type": "send_report"}],
278
                    },
279
                    {"instruction_type": "goto_table", "table_id": 2},
280
                ],
281
            },
282
        },
283
        {
284
            "flow": {
285
                "owner": "telemetry_int",
286
                "cookie": int(0xA83766C105686749),
287
                "match": {"in_port": 21, "dl_vlan": 200},
288
                "table_id": 2,
289
                "table_group": "base",
290
                "priority": 20000,
291
                "idle_timeout": 0,
292
                "hard_timeout": 0,
293
                "instructions": [
294
                    {
295
                        "instruction_type": "apply_actions",
296
                        "actions": [
297
                            {"action_type": "pop_int"},
298
                            {"action_type": "set_vlan", "vlan_id": 200},
299
                            {"action_type": "output", "port": 2},
300
                        ],
301
                    }
302
                ],
303
            },
304
        },
305
    ]
306
307 1
    expected_flows = (
308
        expected_uni_a_source_flows
309
        + expected_uni_z_source_flows
310
        + expected_uni_z_sink_flows
311
        + expected_uni_a_sink_flows
312
    )
313
314 1
    for i, flow in enumerate(flows):
315 1
        assert (i, flow["flow"]) == (i, expected_flows[i]["flow"])
316
317
318 1
def test_build_int_flows_intra_epl(
319
    evcs_data, intra_evc_epl_flows_data, monkeypatch
320
) -> None:
321
    """Test build INT flows intra EPL."""
322 1
    controller = get_controller_mock()
323 1
    int_manager = INTManager(controller)
324 1
    get_proxy_port_or_raise = MagicMock()
325 1
    monkeypatch.setattr(
326
        "napps.kytos.telemetry_int.utils.get_proxy_port_or_raise",
327
        get_proxy_port_or_raise,
328
    )
329
330 1
    evc_id = "3766c105686749"
331 1
    dpid_a = "00:00:00:00:00:00:00:01"
332 1
    mock_switch_a = get_switch_mock(dpid_a, 0x04)
333 1
    mock_interface_a1 = get_interface_mock("s1-eth1", 1, mock_switch_a)
334 1
    mock_interface_a1.id = f"{dpid_a}:{mock_interface_a1.port_number}"
335 1
    mock_interface_a10 = get_interface_mock("s1-eth10", 10, mock_switch_a)
336 1
    mock_interface_a1.metadata = {"proxy_port": mock_interface_a10.port_number}
337 1
    mock_interface_a10.status = EntityStatus.UP
338 1
    mock_interface_a11 = get_interface_mock("s1-eth11", 11, mock_switch_a)
339 1
    mock_interface_a11.status = EntityStatus.UP
340 1
    mock_interface_a10.metadata = {
341
        "looped": {
342
            "port_numbers": [
343
                mock_interface_a10.port_number,
344
                mock_interface_a11.port_number,
345
            ]
346
        }
347
    }
348
349 1
    mock_interface_z1 = get_interface_mock("s1-eth2", 1, mock_switch_a)
350 1
    mock_interface_z1.status = EntityStatus.UP
351 1
    mock_interface_z1.id = f"{dpid_a}:{mock_interface_a1.port_number}"
352 1
    mock_interface_z20 = get_interface_mock("s1-eth20", 20, mock_switch_a)
353 1
    mock_interface_z1.metadata = {"proxy_port": mock_interface_z20.port_number}
354 1
    mock_interface_z20.status = EntityStatus.UP
355 1
    mock_interface_z21 = get_interface_mock("s1-eth21", 21, mock_switch_a)
356 1
    mock_interface_z21.status = EntityStatus.UP
357 1
    mock_interface_z20.metadata = {
358
        "looped": {
359
            "port_numbers": [
360
                mock_interface_z20.port_number,
361
                mock_interface_z21.port_number,
362
            ]
363
        }
364
    }
365
366 1
    mock_switch_a.get_interface_by_port_no = lambda port_no: {
367
        mock_interface_a10.port_number: mock_interface_a10,
368
        mock_interface_a11.port_number: mock_interface_a11,
369
        mock_interface_z20.port_number: mock_interface_z20,
370
        mock_interface_z21.port_number: mock_interface_z21,
371
    }[port_no]
372
373 1
    pp_a = ProxyPort(controller, source=mock_interface_a10)
374 1
    assert pp_a.source == mock_interface_a10
375 1
    assert pp_a.destination == mock_interface_a11
376 1
    pp_z = ProxyPort(controller, source=mock_interface_z20)
377 1
    assert pp_z.source == mock_interface_z20
378 1
    assert pp_z.destination == mock_interface_z21
379
380 1
    get_proxy_port_or_raise.side_effect = [pp_a, pp_z]
381 1
    evcs_data = {evc_id: evcs_data[evc_id]}
382 1
    evcs_data = int_manager._validate_map_enable_evcs(evcs_data)
383 1
    stored_flows = _map_stored_flows_by_cookies(intra_evc_epl_flows_data)
384
385 1
    cookie = get_cookie(evc_id, settings.MEF_COOKIE_PREFIX)
386 1
    flows = fb.build_int_flows(evcs_data, stored_flows)[cookie]
387
388 1
    n_expected_source_flows, n_expected_sink_flows = 3, 2
389 1
    assert len(flows) == (n_expected_source_flows + n_expected_sink_flows) * 2
390
391 1
    expected_uni_a_source_flows = [
392
        {
393
            "flow": {
394
                "owner": "telemetry_int",
395
                "cookie": int(0xA83766C105686749),
396
                "match": {"in_port": 1, "dl_type": 2048, "nw_proto": 6},
397
                "table_id": 0,
398
                "table_group": "epl",
399
                "priority": 10100,
400
                "idle_timeout": 0,
401
                "hard_timeout": 0,
402
                "instructions": [
403
                    {
404
                        "instruction_type": "apply_actions",
405
                        "actions": [{"action_type": "push_int"}],
406
                    },
407
                    {"instruction_type": "goto_table", "table_id": 2},
408
                ],
409
            }
410
        },
411
        {
412
            "flow": {
413
                "owner": "telemetry_int",
414
                "cookie": int(0xA83766C105686749),
415
                "match": {
416
                    "in_port": 1,
417
                    "dl_type": 2048,
418
                    "nw_proto": 17,
419
                },
420
                "table_id": 0,
421
                "table_group": "epl",
422
                "priority": 10100,
423
                "idle_timeout": 0,
424
                "hard_timeout": 0,
425
                "instructions": [
426
                    {
427
                        "instruction_type": "apply_actions",
428
                        "actions": [{"action_type": "push_int"}],
429
                    },
430
                    {"instruction_type": "goto_table", "table_id": 2},
431
                ],
432
            }
433
        },
434
        {
435
            "flow": {
436
                "owner": "telemetry_int",
437
                "cookie": int(0xA83766C105686749),
438
                "match": {"in_port": 1},
439
                "table_id": 2,
440
                "table_group": "base",
441
                "priority": 10000,
442
                "idle_timeout": 0,
443
                "hard_timeout": 0,
444
                "instructions": [
445
                    {
446
                        "instruction_type": "apply_actions",
447
                        "actions": [
448
                            {"action_type": "add_int_metadata"},
449
                            {"action_type": "output", "port": 20},
450
                        ],
451
                    }
452
                ],
453
            }
454
        },
455
    ]
456
457 1
    expected_uni_z_source_flows = [
458
        {
459
            "flow": {
460
                "owner": "telemetry_int",
461
                "cookie": int(0xA83766C105686749),
462
                "match": {"in_port": 2, "dl_type": 2048, "nw_proto": 6},
463
                "table_id": 0,
464
                "table_group": "epl",
465
                "priority": 10100,
466
                "idle_timeout": 0,
467
                "hard_timeout": 0,
468
                "instructions": [
469
                    {
470
                        "instruction_type": "apply_actions",
471
                        "actions": [{"action_type": "push_int"}],
472
                    },
473
                    {"instruction_type": "goto_table", "table_id": 2},
474
                ],
475
            }
476
        },
477
        {
478
            "flow": {
479
                "owner": "telemetry_int",
480
                "cookie": int(0xA83766C105686749),
481
                "match": {
482
                    "in_port": 2,
483
                    "dl_type": 2048,
484
                    "nw_proto": 17,
485
                },
486
                "table_id": 0,
487
                "table_group": "epl",
488
                "priority": 10100,
489
                "idle_timeout": 0,
490
                "hard_timeout": 0,
491
                "instructions": [
492
                    {
493
                        "instruction_type": "apply_actions",
494
                        "actions": [{"action_type": "push_int"}],
495
                    },
496
                    {"instruction_type": "goto_table", "table_id": 2},
497
                ],
498
            }
499
        },
500
        {
501
            "flow": {
502
                "owner": "telemetry_int",
503
                "cookie": int(0xA83766C105686749),
504
                "match": {"in_port": 2},
505
                "table_id": 2,
506
                "table_group": "base",
507
                "priority": 10000,
508
                "idle_timeout": 0,
509
                "hard_timeout": 0,
510
                "instructions": [
511
                    {
512
                        "instruction_type": "apply_actions",
513
                        "actions": [
514
                            {"action_type": "add_int_metadata"},
515
                            {"action_type": "output", "port": 10},
516
                        ],
517
                    }
518
                ],
519
            }
520
        },
521
    ]
522
523 1
    expected_uni_a_sink_flows = [
524
        {
525
            "flow": {
526
                "owner": "telemetry_int",
527
                "cookie": int(0xA83766C105686749),
528
                "match": {"in_port": 11},
529
                "table_id": 0,
530
                "table_group": "epl",
531
                "priority": 10000,
532
                "idle_timeout": 0,
533
                "hard_timeout": 0,
534
                "instructions": [
535
                    {
536
                        "instruction_type": "apply_actions",
537
                        "actions": [{"action_type": "send_report"}],
538
                    },
539
                    {"instruction_type": "goto_table", "table_id": 2},
540
                ],
541
            },
542
        },
543
        {
544
            "flow": {
545
                "owner": "telemetry_int",
546
                "cookie": int(0xA83766C105686749),
547
                "match": {"in_port": 11},
548
                "table_id": 2,
549
                "table_group": "base",
550
                "priority": 10000,
551
                "idle_timeout": 0,
552
                "hard_timeout": 0,
553
                "instructions": [
554
                    {
555
                        "instruction_type": "apply_actions",
556
                        "actions": [
557
                            {"action_type": "pop_int"},
558
                            {"action_type": "output", "port": 1},
559
                        ],
560
                    }
561
                ],
562
            },
563
        },
564
    ]
565 1
    expected_uni_z_sink_flows = [
566
        {
567
            "flow": {
568
                "owner": "telemetry_int",
569
                "cookie": int(0xA83766C105686749),
570
                "match": {"in_port": 21},
571
                "table_id": 0,
572
                "table_group": "epl",
573
                "priority": 10000,
574
                "idle_timeout": 0,
575
                "hard_timeout": 0,
576
                "instructions": [
577
                    {
578
                        "instruction_type": "apply_actions",
579
                        "actions": [{"action_type": "send_report"}],
580
                    },
581
                    {"instruction_type": "goto_table", "table_id": 2},
582
                ],
583
            },
584
        },
585
        {
586
            "flow": {
587
                "owner": "telemetry_int",
588
                "cookie": int(0xA83766C105686749),
589
                "match": {"in_port": 21},
590
                "table_id": 2,
591
                "table_group": "base",
592
                "priority": 10000,
593
                "idle_timeout": 0,
594
                "hard_timeout": 0,
595
                "instructions": [
596
                    {
597
                        "instruction_type": "apply_actions",
598
                        "actions": [
599
                            {"action_type": "pop_int"},
600
                            {"action_type": "output", "port": 2},
601
                        ],
602
                    }
603
                ],
604
            },
605
        },
606
    ]
607
608 1
    expected_flows = (
609
        expected_uni_a_source_flows
610
        + expected_uni_z_source_flows
611
        + expected_uni_z_sink_flows
612
        + expected_uni_a_sink_flows
613
    )
614
615 1
    for i, flow in enumerate(flows):
616
        assert (i, flow["flow"]) == (i, expected_flows[i]["flow"])
617