forked from sonic-net/sonic-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvlan_test.py
More file actions
824 lines (708 loc) · 42.2 KB
/
Copy pathvlan_test.py
File metadata and controls
824 lines (708 loc) · 42.2 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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
import os
import traceback
from unittest import mock
from click.testing import CliRunner
import config.main as config
import show.main as show
from utilities_common.db import Db
show_vlan_brief_output="""\
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| VLAN ID | IP Address | Ports | Port Tagging | DHCP Helper Address | Proxy ARP |
+===========+=================+=================+================+=======================+=============+
| 1000 | 192.168.0.1/21 | Ethernet4 | untagged | 192.0.0.1 | disabled |
| | fc02:1000::1/64 | Ethernet8 | untagged | 192.0.0.2 | |
| | | Ethernet12 | untagged | 192.0.0.3 | |
| | | Ethernet16 | untagged | 192.0.0.4 | |
| | | | | fc02:2000::1 | |
| | | | | fc02:2000::2 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 2000 | 192.168.0.10/21 | Ethernet24 | untagged | 192.0.0.1 | enabled |
| | fc02:1011::1/64 | Ethernet28 | untagged | 192.0.0.2 | |
| | | | | 192.0.0.3 | |
| | | | | 192.0.0.4 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 3000 | | | | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 4000 | | PortChannel1001 | tagged | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
"""
show_vlan_brief_in_alias_mode_output="""\
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| VLAN ID | IP Address | Ports | Port Tagging | DHCP Helper Address | Proxy ARP |
+===========+=================+=================+================+=======================+=============+
| 1000 | 192.168.0.1/21 | etp2 | untagged | 192.0.0.1 | disabled |
| | fc02:1000::1/64 | etp3 | untagged | 192.0.0.2 | |
| | | etp4 | untagged | 192.0.0.3 | |
| | | etp5 | untagged | 192.0.0.4 | |
| | | | | fc02:2000::1 | |
| | | | | fc02:2000::2 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 2000 | 192.168.0.10/21 | etp7 | untagged | 192.0.0.1 | enabled |
| | fc02:1011::1/64 | etp8 | untagged | 192.0.0.2 | |
| | | | | 192.0.0.3 | |
| | | | | 192.0.0.4 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 3000 | | | | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 4000 | | PortChannel1001 | tagged | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
"""
show_vlan_brief_empty_output="""\
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| VLAN ID | IP Address | Ports | Port Tagging | DHCP Helper Address | Proxy ARP |
+===========+=================+=================+================+=======================+=============+
| 2000 | 192.168.0.10/21 | Ethernet24 | untagged | 192.0.0.1 | enabled |
| | fc02:1011::1/64 | Ethernet28 | untagged | 192.0.0.2 | |
| | | | | 192.0.0.3 | |
| | | | | 192.0.0.4 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 3000 | | | | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 4000 | | PortChannel1001 | tagged | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
"""
show_vlan_brief_with_portchannel_output="""\
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| VLAN ID | IP Address | Ports | Port Tagging | DHCP Helper Address | Proxy ARP |
+===========+=================+=================+================+=======================+=============+
| 1000 | 192.168.0.1/21 | Ethernet4 | untagged | 192.0.0.1 | disabled |
| | fc02:1000::1/64 | Ethernet8 | untagged | 192.0.0.2 | |
| | | Ethernet12 | untagged | 192.0.0.3 | |
| | | Ethernet16 | untagged | 192.0.0.4 | |
| | | PortChannel1001 | untagged | fc02:2000::1 | |
| | | | | fc02:2000::2 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 2000 | 192.168.0.10/21 | Ethernet24 | untagged | 192.0.0.1 | enabled |
| | fc02:1011::1/64 | Ethernet28 | untagged | 192.0.0.2 | |
| | | | | 192.0.0.3 | |
| | | | | 192.0.0.4 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 3000 | | | | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 4000 | | PortChannel1001 | tagged | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
"""
show_vlan_config_output="""\
Name VID Member Mode
-------- ----- --------------- --------
Vlan1000 1000 Ethernet4 untagged
Vlan1000 1000 Ethernet8 untagged
Vlan1000 1000 Ethernet12 untagged
Vlan1000 1000 Ethernet16 untagged
Vlan2000 2000 Ethernet24 untagged
Vlan2000 2000 Ethernet28 untagged
Vlan3000 3000
Vlan4000 4000 PortChannel1001 tagged
"""
show_vlan_config_in_alias_mode_output="""\
Name VID Member Mode
-------- ----- --------------- --------
Vlan1000 1000 etp2 untagged
Vlan1000 1000 etp3 untagged
Vlan1000 1000 etp4 untagged
Vlan1000 1000 etp5 untagged
Vlan2000 2000 etp7 untagged
Vlan2000 2000 etp8 untagged
Vlan3000 3000
Vlan4000 4000 PortChannel1001 tagged
"""
config_vlan_add_dhcp_relay_output="""\
Added DHCP relay destination address 192.0.0.100 to Vlan1000
Restarting DHCP relay service...
"""
config_vlan_del_dhcp_relay_output="""\
Removed DHCP relay destination address 192.0.0.100 from Vlan1000
Restarting DHCP relay service...
"""
config_vlan_add_dhcp_relayv6_output="""\
Added DHCP relay destination address fc02:2000::3 to Vlan1000
Restarting DHCP relay service...
"""
config_vlan_del_dhcp_relayv6_output="""\
Removed DHCP relay destination address fc02:2000::3 from Vlan1000
Restarting DHCP relay service...
"""
show_vlan_brief_output_with_new_dhcp_relay_address="""\
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| VLAN ID | IP Address | Ports | Port Tagging | DHCP Helper Address | Proxy ARP |
+===========+=================+=================+================+=======================+=============+
| 1000 | 192.168.0.1/21 | Ethernet4 | untagged | 192.0.0.1 | disabled |
| | fc02:1000::1/64 | Ethernet8 | untagged | 192.0.0.2 | |
| | | Ethernet12 | untagged | 192.0.0.3 | |
| | | Ethernet16 | untagged | 192.0.0.4 | |
| | | | | 192.0.0.100 | |
| | | | | fc02:2000::1 | |
| | | | | fc02:2000::2 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 2000 | 192.168.0.10/21 | Ethernet24 | untagged | 192.0.0.1 | enabled |
| | fc02:1011::1/64 | Ethernet28 | untagged | 192.0.0.2 | |
| | | | | 192.0.0.3 | |
| | | | | 192.0.0.4 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 3000 | | | | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 4000 | | PortChannel1001 | tagged | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
"""
show_vlan_brief_output_with_new_dhcp_relayv6_address="""\
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| VLAN ID | IP Address | Ports | Port Tagging | DHCP Helper Address | Proxy ARP |
+===========+=================+=================+================+=======================+=============+
| 1000 | 192.168.0.1/21 | Ethernet4 | untagged | 192.0.0.1 | disabled |
| | fc02:1000::1/64 | Ethernet8 | untagged | 192.0.0.2 | |
| | | Ethernet12 | untagged | 192.0.0.3 | |
| | | Ethernet16 | untagged | 192.0.0.4 | |
| | | | | fc02:2000::1 | |
| | | | | fc02:2000::2 | |
| | | | | fc02:2000::3 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 2000 | 192.168.0.10/21 | Ethernet24 | untagged | 192.0.0.1 | enabled |
| | fc02:1011::1/64 | Ethernet28 | untagged | 192.0.0.2 | |
| | | | | 192.0.0.3 | |
| | | | | 192.0.0.4 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 3000 | | | | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 4000 | | PortChannel1001 | tagged | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
"""
config_add_del_vlan_and_vlan_member_output="""\
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| VLAN ID | IP Address | Ports | Port Tagging | DHCP Helper Address | Proxy ARP |
+===========+=================+=================+================+=======================+=============+
| 1000 | 192.168.0.1/21 | Ethernet4 | untagged | 192.0.0.1 | disabled |
| | fc02:1000::1/64 | Ethernet8 | untagged | 192.0.0.2 | |
| | | Ethernet12 | untagged | 192.0.0.3 | |
| | | Ethernet16 | untagged | 192.0.0.4 | |
| | | | | fc02:2000::1 | |
| | | | | fc02:2000::2 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 1001 | | Ethernet20 | untagged | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 2000 | 192.168.0.10/21 | Ethernet24 | untagged | 192.0.0.1 | enabled |
| | fc02:1011::1/64 | Ethernet28 | untagged | 192.0.0.2 | |
| | | | | 192.0.0.3 | |
| | | | | 192.0.0.4 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 3000 | | | | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 4000 | | PortChannel1001 | tagged | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
"""
config_add_del_vlan_and_vlan_member_in_alias_mode_output="""\
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| VLAN ID | IP Address | Ports | Port Tagging | DHCP Helper Address | Proxy ARP |
+===========+=================+=================+================+=======================+=============+
| 1000 | 192.168.0.1/21 | etp2 | untagged | 192.0.0.1 | disabled |
| | fc02:1000::1/64 | etp3 | untagged | 192.0.0.2 | |
| | | etp4 | untagged | 192.0.0.3 | |
| | | etp5 | untagged | 192.0.0.4 | |
| | | | | fc02:2000::1 | |
| | | | | fc02:2000::2 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 1001 | | etp6 | untagged | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 2000 | 192.168.0.10/21 | etp7 | untagged | 192.0.0.1 | enabled |
| | fc02:1011::1/64 | etp8 | untagged | 192.0.0.2 | |
| | | | | 192.0.0.3 | |
| | | | | 192.0.0.4 | |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 3000 | | | | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
| 4000 | | PortChannel1001 | tagged | | disabled |
+-----------+-----------------+-----------------+----------------+-----------------------+-------------+
"""
class TestVlan(object):
@classmethod
def setup_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "1"
print("SETUP")
def test_show_vlan(self):
runner = CliRunner()
result = runner.invoke(show.cli.commands["vlan"], [])
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
def test_show_vlan_brief(self):
runner = CliRunner()
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [])
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_vlan_brief_output
def test_show_vlan_brief_verbose(self):
runner = CliRunner()
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], ["--verbose"])
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_vlan_brief_output
def test_show_vlan_brief_in_alias_mode(self):
runner = CliRunner()
os.environ['SONIC_CLI_IFACE_MODE'] = "alias"
result = runner.invoke(show.cli.commands["vlan"].commands["brief"])
os.environ['SONIC_CLI_IFACE_MODE'] = "default"
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_vlan_brief_in_alias_mode_output
def test_show_vlan_brief_explicit_proxy_arp_disable(self):
runner = CliRunner()
db = Db()
db.cfgdb.set_entry("VLAN_INTERFACE", "Vlan1000", {"proxy_arp": "disabled"})
def test_show_vlan_config(self):
runner = CliRunner()
result = runner.invoke(show.cli.commands["vlan"].commands["config"], [])
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_vlan_config_output
def test_show_vlan_config_in_alias_mode(self):
runner = CliRunner()
os.environ['SONIC_CLI_IFACE_MODE'] = "alias"
result = runner.invoke(show.cli.commands["vlan"].commands["config"], [])
os.environ['SONIC_CLI_IFACE_MODE'] = "default"
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_vlan_config_in_alias_mode_output
def test_config_vlan_add_vlan_with_invalid_vlanid(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["vlan"].commands["add"], ["4096"])
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Invalid VLAN ID 4096 (1-4094)" in result.output
def test_config_vlan_add_vlan_with_exist_vlanid(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["vlan"].commands["add"], ["1000"])
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Vlan1000 already exists" in result.output
def test_config_vlan_del_vlan_with_invalid_vlanid(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["4096"])
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Invalid VLAN ID 4096 (1-4094)" in result.output
def test_config_vlan_del_vlan_with_nonexist_vlanid(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1001"])
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Vlan1001 does not exist" in result.output
def test_config_vlan_add_member_with_invalid_vlanid(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"], ["4096", "Ethernet4"])
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Invalid VLAN ID 4096 (1-4094)" in result.output
def test_config_vlan_add_member_with_nonexist_vlanid(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"], ["1001", "Ethernet4"])
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Vlan1001 does not exist" in result.output
def test_config_vlan_add_exist_port_member(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"], ["1000", "Ethernet4"])
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Ethernet4 is already a member of Vlan1000" in result.output
def test_config_vlan_add_nonexist_port_member(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"], ["1000", "Ethernet3"])
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Ethernet3 does not exist" in result.output
def test_config_vlan_add_nonexist_portchannel_member(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"], \
["1000", "PortChannel1011"])
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: PortChannel1011 does not exist" in result.output
def test_config_vlan_add_portchannel_member(self):
runner = CliRunner()
db = Db()
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"], \
["1000", "PortChannel1001", "--untagged"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
# show output
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_vlan_brief_with_portchannel_output
def test_config_vlan_add_rif_portchannel_member(self):
runner = CliRunner()
db = Db()
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"], \
["1000", "PortChannel0001", "--untagged"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: PortChannel0001 is a router interface!" in result.output
def test_config_vlan_del_vlan(self):
runner = CliRunner()
db = Db()
obj = {'config_db':db.cfgdb}
# del vlan with IP
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1000"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Vlan1000 can not be removed. First remove IP addresses assigned to this VLAN" in result.output
# remove vlan IP`s
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], ["Vlan1000", "192.168.0.1/21"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code != 0
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], ["Vlan1000", "fc02:1000::1/64"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code != 0
# del vlan with IP
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1000"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: VLAN ID 1000 can not be removed. First remove all members assigned to this VLAN." in result.output
vlan_member = db.cfgdb.get_table('VLAN_MEMBER')
keys = [ (k, v) for k, v in vlan_member if k == 'Vlan{}'.format(1000) ]
for k,v in keys:
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["del"], ["1000", v], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1000"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
# show output
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_vlan_brief_empty_output
def test_config_vlan_del_nonexist_vlan_member(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["del"], \
["1000", "Ethernet0"])
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Ethernet0 is not a member of Vlan1000" in result.output
def test_config_add_del_vlan_and_vlan_member(self):
runner = CliRunner()
db = Db()
# add vlan 1001
result = runner.invoke(config.config.commands["vlan"].commands["add"], ["1001"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
# add Ethernet20 to vlan 1001
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"],
["1001", "Ethernet20", "--untagged"], obj=db)
print(result.exit_code)
print(result.output)
traceback.print_tb(result.exc_info[2])
assert result.exit_code == 0
# show output
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [], obj=db)
print(result.output)
assert result.output == config_add_del_vlan_and_vlan_member_output
# remove vlan member
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["del"],
["1001", "Ethernet20"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
# add del 1001
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1001"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
# show output
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_vlan_brief_output
def test_config_add_del_vlan_and_vlan_member_in_alias_mode(self):
runner = CliRunner()
db = Db()
os.environ['SONIC_CLI_IFACE_MODE'] = "alias"
# add vlan 1001
result = runner.invoke(config.config.commands["vlan"].commands["add"], ["1001"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
# add etp6 to vlan 1001
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"],
["1001", "etp6", "--untagged"], obj=db)
print(result.exit_code)
print(result.output)
traceback.print_tb(result.exc_info[2])
assert result.exit_code == 0
# show output
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [], obj=db)
print(result.exit_code)
print(result.output)
assert result.output == config_add_del_vlan_and_vlan_member_in_alias_mode_output
# remove vlan member
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["del"],
["1001", "etp6"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
# add del 1001
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1001"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
# show output
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_vlan_brief_in_alias_mode_output
os.environ['SONIC_CLI_IFACE_MODE'] = "default"
def test_config_vlan_add_dhcp_relay_with_nonexist_vlanid(self):
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["add"],
["1001", "192.0.0.100"])
print(result.exit_code)
print(result.output)
# traceback.print_tb(result.exc_info[2])
assert result.exit_code != 0
assert "Error: Vlan1001 doesn't exist" in result.output
assert mock_run_command.call_count == 0
def test_config_vlan_add_dhcp_relay_with_invalid_vlanid(self):
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["add"],
["4096", "192.0.0.100"])
print(result.exit_code)
print(result.output)
# traceback.print_tb(result.exc_info[2])
assert result.exit_code != 0
assert "Error: Vlan4096 doesn't exist" in result.output
assert mock_run_command.call_count == 0
def test_config_vlan_add_dhcp_relay_with_invalid_ip(self):
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["add"],
["1000", "192.0.0.1000"])
print(result.exit_code)
print(result.output)
# traceback.print_tb(result.exc_info[2])
assert result.exit_code != 0
assert "Error: 192.0.0.1000 is invalid IP address" in result.output
assert mock_run_command.call_count == 0
def test_config_vlan_add_dhcp_relay_with_invalid_ipv6(self):
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["add"],
["1000", "fe80:2030:31:24"])
print(result.exit_code)
print(result.output)
# traceback.print_tb(result.exc_info[2])
assert result.exit_code != 0
assert "Error: fe80:2030:31:24 is invalid IP address" in result.output
assert mock_run_command.call_count == 0
def test_config_vlan_add_dhcp_relay_with_exist_ip(self):
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["add"],
["1000", "192.0.0.1"])
print(result.exit_code)
print(result.output)
# traceback.print_tb(result.exc_info[2])
assert result.exit_code == 0
assert "192.0.0.1 is already a DHCP relay destination for Vlan1000" in result.output
assert mock_run_command.call_count == 0
def test_config_vlan_add_dhcp_relay_with_exist_ipv6(self):
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["add"],
["1000", "fc02:2000::1"])
print(result.exit_code)
print(result.output)
# traceback.print_tb(result.exc_info[2])
assert result.exit_code == 0
assert "fc02:2000::1 is already a DHCP relay destination for Vlan1000" in result.output
assert mock_run_command.call_count == 0
def test_config_vlan_add_del_dhcp_relay_dest(self):
runner = CliRunner()
db = Db()
# add new relay dest
with mock.patch("utilities_common.cli.run_command") as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["add"],
["1000", "192.0.0.100"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_vlan_add_dhcp_relay_output
assert mock_run_command.call_count == 3
# show output
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [], obj=db)
print(result.output)
assert result.output == show_vlan_brief_output_with_new_dhcp_relay_address
# del relay dest
with mock.patch("utilities_common.cli.run_command") as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["del"],
["1000", "192.0.0.100"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_vlan_del_dhcp_relay_output
assert mock_run_command.call_count == 3
# show output
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [], obj=db)
print(result.output)
assert result.output == show_vlan_brief_output
def test_config_vlan_add_del_dhcp_relayv6_dest(self):
runner = CliRunner()
db = Db()
# add new relay dest
with mock.patch("utilities_common.cli.run_command") as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["add"],
["1000", "fc02:2000::3"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_vlan_add_dhcp_relayv6_output
assert mock_run_command.call_count == 3
# show output
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [], obj=db)
print(result.output)
assert result.output == show_vlan_brief_output_with_new_dhcp_relayv6_address
# del relay dest
with mock.patch("utilities_common.cli.run_command") as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["del"],
["1000", "fc02:2000::3"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_vlan_del_dhcp_relayv6_output
assert mock_run_command.call_count == 3
# show output
result = runner.invoke(show.cli.commands["vlan"].commands["brief"], [], obj=db)
print(result.output)
assert result.output == show_vlan_brief_output
def test_config_vlan_remove_nonexist_dhcp_relay_dest(self):
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["del"],
["1000", "192.0.0.100"])
print(result.exit_code)
print(result.output)
# traceback.print_tb(result.exc_info[2])
assert result.exit_code != 0
assert "Error: 192.0.0.100 is not a DHCP relay destination for Vlan1000" in result.output
assert mock_run_command.call_count == 0
def test_config_vlan_remove_nonexist_dhcp_relayv6_dest(self):
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["del"],
["1000", "fc02:2000::3"])
print(result.exit_code)
print(result.output)
# traceback.print_tb(result.exc_info[2])
assert result.exit_code != 0
assert "Error: fc02:2000::3 is not a DHCP relay destination for Vlan1000" in result.output
assert mock_run_command.call_count == 0
def test_config_vlan_remove_dhcp_relay_dest_with_nonexist_vlanid(self):
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["del"],
["1001", "192.0.0.1"])
print(result.exit_code)
print(result.output)
# traceback.print_tb(result.exc_info[2])
assert result.exit_code != 0
assert "Error: Vlan1001 doesn't exist" in result.output
assert mock_run_command.call_count == 0
def test_config_vlan_proxy_arp_with_nonexist_vlan_intf_table(self):
modes = ["enabled", "disabled"]
runner = CliRunner()
db = Db()
db.cfgdb.delete_table("VLAN_INTERFACE")
for mode in modes:
result = runner.invoke(config.config.commands["vlan"].commands["proxy_arp"], ["1000", mode], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Interface Vlan1000 does not exist" in result.output
def test_config_vlan_proxy_arp_with_nonexist_vlan_intf(self):
modes = ["enabled", "disabled"]
runner = CliRunner()
db = Db()
for mode in modes:
result = runner.invoke(config.config.commands["vlan"].commands["proxy_arp"], ["1001", mode], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Interface Vlan1001 does not exist" in result.output
def test_config_vlan_proxy_arp_enable(self):
runner = CliRunner()
db = Db()
result = runner.invoke(config.config.commands["vlan"].commands["proxy_arp"], ["1000", "enabled"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert db.cfgdb.get_entry("VLAN_INTERFACE", "Vlan1000") == {"proxy_arp": "enabled"}
def test_config_vlan_proxy_arp_disable(self):
runner = CliRunner()
db = Db()
result = runner.invoke(config.config.commands["vlan"].commands["proxy_arp"], ["2000", "disabled"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert db.cfgdb.get_entry("VLAN_INTERFACE", "Vlan2000") == {"proxy_arp": "disabled"}
def test_config_2_untagged_vlan_on_same_interface(self):
runner = CliRunner()
db = Db()
# add Ethernet4 to vlan 2000 as untagged - should fail as ethrnet4 is already untagged member in 1000
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"],
["2000", "Ethernet4", "--untagged"], obj=db)
print(result.exit_code)
assert result.exit_code != 0
# add Ethernet4 to vlan 2000 as tagged - should succeed
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"],
["2000", "Ethernet4" ], obj=db)
print(result.exit_code)
assert result.exit_code == 0
def test_config_set_router_port_on_member_interface(self):
db = Db()
runner = CliRunner()
obj = {'config_db':db.cfgdb}
# intf enable
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"],
["Ethernet4", "10.10.10.1/24"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code == 0
assert 'Interface Ethernet4 is a member of vlan' in result.output
def test_config_vlan_add_member_of_portchannel(self):
runner = CliRunner()
db = Db()
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"], \
["1000", "Ethernet32", "--untagged"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Ethernet32 is part of portchannel!" in result.output
@classmethod
def teardown_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"
print("TEARDOWN")