/
test_CiscoConfParse.py
1648 lines (1487 loc) · 54.3 KB
/
test_CiscoConfParse.py
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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from operator import attrgetter
from itertools import repeat
from copy import deepcopy
from mock import patch
import platform
import sys
import re
import os
sys.path.insert(0, "..")
from ciscoconfparse.ciscoconfparse import CiscoConfParse, IOSCfgLine, IOSIntfLine
from ciscoconfparse.ciscoconfparse import CiscoPassword
from ciscoconfparse.ccp_util import IPv4Obj
from passlib.hash import cisco_type7
import pytest
def testParse_asa_as_ios(config_a02):
"""Test for Github issue #42 parse asa banner with ios syntax"""
parse = CiscoConfParse(config_a02, syntax='ios', factory=False)
assert not (parse is None)
def testParse_asa_as_asa(config_a02):
parse = CiscoConfParse(config_a02, syntax='asa', factory=False)
assert not (parse is None)
def testValues_find_objects_dna(parse_c01_factory):
"""Test to find an object by its dna"""
obj = parse_c01_factory.find_objects_dna('IOSIntf')[0]
assert isinstance(obj, IOSIntfLine)
def testValues_macro_01():
CONFIG = """!
interface FastEthernet 0/1
ip address 172.16.1.1 255.255.255.0
no ip proxy-arp
!
macro name THIS_MACRO
do sh ip int brief
@
macro name THAT_MACRO
do sh int status
@
line vty 0 4
transport preferred none
end""".splitlines()
parse = CiscoConfParse(CONFIG)
mobjs = parse.find_objects('^macro')
assert mobjs[0].linenum == 5
assert parse.objs[6].parent == mobjs[0]
assert parse.objs[6].linenum == 6
assert parse.objs[7].parent == mobjs[0]
assert parse.objs[7].linenum == 7
assert mobjs[1].linenum == 8
assert parse.objs[9].parent == mobjs[1]
assert parse.objs[9].linenum == 9
assert parse.objs[10].parent == mobjs[1]
assert parse.objs[10].linenum == 10
def testValues_banner_delimiter_01():
# Test banner delimiter on the same lines
CONFIG = ['!', 'banner motd ^ trivial banner here ^', 'end']
parse = CiscoConfParse(CONFIG)
bannerobj = parse.find_objects('^banner')[0]
BANNER_LINE_NUMBER = 1
assert bannerobj.linenum == BANNER_LINE_NUMBER
for obj in bannerobj.children:
assert obj.parent.linenum == BANNER_LINE_NUMBER
def testValues_banner_delimiter_02():
# Test multiple banner delimiters on the same lines
CONFIG = ['!', 'banner motd ^ trivial banner here ^^', 'end']
parse = CiscoConfParse(CONFIG)
bannerobj = parse.find_objects('^banner')[0]
BANNER_LINE_NUMBER = 1
assert bannerobj.linenum == BANNER_LINE_NUMBER
for obj in bannerobj.children:
assert obj.parent.linenum == BANNER_LINE_NUMBER
def testValues_banner_delimiter_03():
# Test banner delimiter on different lines
CONFIG = ['!', 'banner motd ^', ' trivial banner here ^', 'end']
parse = CiscoConfParse(CONFIG)
bannerobj = parse.find_objects('^banner')[0]
BANNER_LINE_NUMBER = 1
assert bannerobj.linenum == BANNER_LINE_NUMBER
for obj in bannerobj.children:
assert obj.parent.linenum == BANNER_LINE_NUMBER
def testValues_banner_delimiter_04():
# Test multiple banner delimiters on different lines
CONFIG = ['!', 'banner motd ^', ' trivial banner here ^^', 'end']
parse = CiscoConfParse(CONFIG)
bannerobj = parse.find_objects('^banner')[0]
BANNER_LINE_NUMBER = 1
assert bannerobj.linenum == BANNER_LINE_NUMBER
for obj in bannerobj.children:
assert obj.parent.linenum == BANNER_LINE_NUMBER
def testValues_banner_delimiter_05():
# Test multiple banners
CONFIG = ['!', 'banner motd ^', ' trivial banner1 here ^',
'banner exec ^', ' trivial banner2 here ^',
'end']
parse = CiscoConfParse(CONFIG)
bannerobj = parse.find_objects(r'^banner\smotd')[0]
BANNER_LINE_NUMBER = 1
assert bannerobj.linenum == BANNER_LINE_NUMBER
for obj in bannerobj.children:
assert obj.parent.linenum == BANNER_LINE_NUMBER
bannerobj = parse.find_objects(r'^banner\sexec')[0]
BANNER_LINE_NUMBER = 3
assert bannerobj.linenum == BANNER_LINE_NUMBER
for obj in bannerobj.children:
assert obj.parent.linenum == BANNER_LINE_NUMBER
def testValues_banner_delimiter_06():
"""Test banners with a literal cntl-c delimiter"""
### WARNING: Be very careful editing this because vim eats cntl-c easily
CONFIG = """!
banner motd
================================================================================
# #
# This banner uses a literal control-c character to test cntl-c handling #
# #
================================================================================
!
line con 0
authorization exec AAA_METHOD
login authentication AAA_METHOD
!
end""".splitlines()
parse = CiscoConfParse(CONFIG, ignore_blank_lines=True)
assert parse.find_objects(r'banner')[0].text=='banner motd '
assert parse.find_objects(r'banner')[0].children[-1].text==''
def testValues_banner_delimiter_07():
"""Test banners with blank lines in them and ignore_blank_lines=False"""
CONFIG = """!
banner motd z
================================================================================
# This banner has blank lines to ensure that parsing this preserves the blanks
================================================================================
z
!
line con 0
authorization exec AAA_METHOD
login authentication AAA_METHOD
!
end""".splitlines()
parse = CiscoConfParse(CONFIG, ignore_blank_lines=False)
assert len(parse.find_objects(r'banner')[0].children)==8
def testValues_aaa_authfailmsg_delimiter_01():
# Test auth fail-message delimiter on the same line...
CONFIG = ['!', 'aaa authentication fail-message ^ trivial banner here ^', 'end']
parse = CiscoConfParse(CONFIG)
bannerobj = parse.find_objects(r'^aaa\sauthentication\sfail-message')[0]
BANNER_LINE_NUMBER = 1
assert bannerobj.linenum == BANNER_LINE_NUMBER
for obj in bannerobj.children:
assert obj.parent.linenum == BANNER_LINE_NUMBER
assert len(bannerobj.children)==0
def testValues_aaa_authfailmsg_delimiter_02():
# Test multiple banner delimiters on different lines
CONFIG = ['!', 'aaa authentication fail-message ^',
' trivial banner here ^',
'end']
parse = CiscoConfParse(CONFIG)
bannerobj = parse.find_objects(r'^aaa\sauthentication\sfail-message')[0]
BANNER_LINE_NUMBER = 1
assert bannerobj.linenum == BANNER_LINE_NUMBER
for obj in bannerobj.children:
assert obj.parent.linenum == BANNER_LINE_NUMBER
assert len(bannerobj.children)==1
def testValues_aaa_authfailmsg_delete_01():
# Ensure aa authentication fail-message banners are correctly deleted
CONFIG = ['!',
'aaa authentication fail-message ^',
' trivial banner1 here ^',
'interface GigabitEthernet0/0',
' ip address 192.0.2.1 255.255.255.0',
'banner exec ^', ' trivial banner2 here ^',
'end']
parse = CiscoConfParse(CONFIG)
for obj in parse.find_objects(r'^aaa\sauthentication\sfail-message'):
obj.delete()
parse.commit()
assert parse.find_objects(r'^aaa\sauthentication\sfail-message')==[]
def testValues_banner_delete_01():
# Ensure multiline banners are correctly deleted
CONFIG = ['!',
'banner motd ^', ' trivial banner1 here ^',
'interface GigabitEthernet0/0',
' ip address 192.0.2.1 255.255.255.0',
'banner exec ^', ' trivial banner2 here ^',
'end']
parse = CiscoConfParse(CONFIG)
for obj in parse.find_objects('^banner'):
obj.delete()
parse.commit()
assert parse.find_objects('^banner')==[]
def testValues_banner_delete_02():
# Ensure multiline banners are correctly deleted
#
# Check for Github issue #37
CONFIG = ['!',
'interface GigabitEthernet0/0',
' ip address 192.0.2.1 255.255.255.0',
'banner motd ^', ' trivial banner1 here ^',
'interface GigabitEthernet0/1',
' ip address 192.0.2.1 255.255.255.0',
'banner exec ^', ' trivial banner2 here ^',
'end']
parse = CiscoConfParse(CONFIG)
for obj in parse.find_objects('^banner'):
obj.delete()
parse.commit()
assert parse.find_objects('^banner')==[]
# Github issue #37 assigned Gi0/1's child to Gi0/0 after deleting
# the banner motd line...
for obj in parse.find_objects('^interface'):
assert len(obj.children)==1
def testValues_banner_child_parsing_01(parse_c01):
"""Associate banner lines as parent / child"""
result_correct = {
59: 58,
60: 58,
61: 58,
62: 58,
63: 63, # Ensure the line *after* the banner isn't a child
}
banner_obj = parse_c01.find_objects('^banner')[0]
# Check banner's parent line (should be its line number)
assert banner_obj.parent.linenum == 58
for child in banner_obj.all_children:
test_result = child.parent.linenum
assert result_correct[child.linenum]==test_result
def testValues_banner_child_parsing_02():
## Test for Github issue #115
config = """!
banner exec ^
!!! You should leave now !!!
In case you haven't heard me, I'll give you another reason...
!!! I have been known to be violent when I'm hungry !!!
This router is protected by a hungry admin
^
!
"""
parse = CiscoConfParse(config.splitlines())
parent_intf = {
# Line 2's parent should be 1, etc...
2: 1,
3: 1,
4: 1,
5: 1,
6: 1,
}
for obj in parse.find_objects(''):
result_correct = parent_intf.get(obj.linenum, False)
if result_correct:
test_result = obj.parent.linenum
## Does this object parent's line number match?
assert result_correct==test_result
def testValues_parent_child_parsing_01(parse_c01):
parent_intf = {
# Line 13's parent should be 11, etc...
13: 11,
16: 15,
17: 15,
18: 15,
19: 15,
22: 21,
23: 21,
24: 21,
25: 21,
}
for obj in parse_c01.find_objects(''):
result_correct = parent_intf.get(obj.linenum, False)
if result_correct:
test_result = obj.parent.linenum
## Does this object parent's line number match?
assert result_correct==test_result
def testValues_parent_child_parsing_02(parse_c01):
cfg = parse_c01
# Expected child / parent line numbers before the insert
parent_intf_before = {
# Line 11 is Serial1/0, child is line 13
13: 11,
# Line 15 is GigabitEthernet4/1
16: 15,
17: 15,
18: 15,
19: 15,
# Line 21 is GigabitEthernet4/2
22: 21,
23: 21,
24: 21,
25: 21,
}
# Expected child / parent line numbers after the insert
# Validate line numbers *before* inserting
for obj in cfg.find_objects(''):
result_correct = parent_intf_before.get(obj.linenum, False)
if result_correct:
test_result = obj.parent.linenum
## Does this object parent's line number match?
assert result_correct==test_result
# Insert lines here...
for intf_obj in cfg.find_objects(r'^interface\sGigabitEthernet'):
# Configured with an access vlan...
if ' switchport access vlan 100' in set(map(attrgetter('text'), intf_obj.children)):
intf_obj.insert_after(' spanning-tree portfast')
cfg.atomic()
parent_intf_after = {
# Line 11 is Serial1/0, child is line 13
13: 11,
# Line 15 is GigabitEthernet4/1
16: 15,
17: 15,
18: 15,
19: 15,
# Line 22 is GigabitEthernet4/2
23: 22,
24: 22,
25: 22,
26: 22,
}
# Validate line numbers *after* inserting
for obj in cfg.find_objects(''):
result_correct = parent_intf_after.get(obj.linenum, False)
if result_correct:
test_result = obj.parent.linenum
## Does this object parent's line number match?
assert result_correct==test_result
def testValues_find_lines(parse_c01):
c01_intf = ['interface Serial 1/0']
c01_find_gige_no_exactmatch = [
'interface GigabitEthernet4/1',
'interface GigabitEthernet4/2',
'interface GigabitEthernet4/3',
'interface GigabitEthernet4/4',
'interface GigabitEthernet4/5',
'interface GigabitEthernet4/6',
'interface GigabitEthernet4/7',
'interface GigabitEthernet4/8']
find_lines_Values = (
# Ensure exact matches work regardless of the exactmatch boolean
({'linespec': "interface Serial 1/0",
'exactmatch': False}, c01_intf),
({'linespec': "interface Serial 1/0",
'exactmatch': True}, c01_intf),
# Ensure we can find string matches inside an interface config block
({'linespec': "encapsulation",
'exactmatch': False}, [' encapsulation ppp']),
# Ensure exactmatch=False catches beginning phrases in the config
({'linespec': "interface GigabitEthernet4/",
'exactmatch': False}, c01_find_gige_no_exactmatch),
# Ensure exactmatch=False catches single words in the config
({'linespec': "igabitEthernet",
'exactmatch': False}, c01_find_gige_no_exactmatch),
# Negative test: matches are Case-Sensitive
({'linespec': "GigaBitEtherNeT",
'exactmatch': False}, []),
# Negative test for exactmatch=True
({'linespec': "interface GigabitEthernet4/",
'exactmatch': True}, []),
# Negative test for exactmatch=True and ignore_ws=False
({'linespec': "interface Serial1/0",
'exactmatch': True, 'ignore_ws': False}, []),
)
for args, result_correct in find_lines_Values:
test_result = parse_c01.find_lines(**args)
assert result_correct==test_result
def testValues_find_children(parse_c01):
c01_pmap_children = [
'policy-map QOS_1',
' class GOLD',
' class SILVER',
' class BRONZE',
]
find_children_Values = (
({'linespec': "policy-map", 'exactmatch': False},
c01_pmap_children),
({'linespec': "policy-map", 'exactmatch': True}, []),
)
for args, result_correct in find_children_Values:
test_result = parse_c01.find_children(**args)
assert result_correct==test_result
def testValues_find_all_children01(parse_c01):
## test find_all_chidren
c01_pmap_all_children = [
'policy-map QOS_1',
' class GOLD',
' priority percent 10',
' class SILVER',
' bandwidth 30',
' random-detect',
' class BRONZE',
' random-detect',
]
find_all_children_Values = (
({'linespec': "policy-map", 'exactmatch': False},
c01_pmap_all_children),
({'linespec': "policy-map", 'exactmatch': True}, []),
)
for args, result_correct in find_all_children_Values:
test_result = parse_c01.find_all_children(**args)
assert result_correct==test_result
def testValues_find_all_chidren02():
"""Ensure we don't need a comment at the end of a """
""" parent / child block to identify the end of the family"""
CONFIG = ['thing1',
' foo',
' bar',
' 100',
' 200',
' 300',
' 400',
'thing2',]
RESULT_CORRECT = ['thing1',
' foo',
' bar',
' 100',
' 200',
' 300',
' 400',]
cfg = CiscoConfParse(CONFIG)
test_result = cfg.find_all_children('^thing1')
assert RESULT_CORRECT==test_result
def testValues_find_blocks(parse_c01):
result_correct = [
'banner login ^C',
'This is a router, and you cannot have it.',
'Log off now while you still can type. I break the fingers',
'of all tresspassers.',
'^C',
]
test_result = parse_c01.find_blocks('tresspasser')
assert result_correct==test_result
def testValues_find_parents_w_child(parse_c01):
c01_parents_w_child_power = [
'interface GigabitEthernet4/1',
'interface GigabitEthernet4/2',
]
c01_parents_w_child_voice = [
'interface GigabitEthernet4/1',
'interface GigabitEthernet4/2',
'interface GigabitEthernet4/3',
]
find_parents_w_child_Values = (
({'parentspec': "interf", 'childspec': "power inline"},
c01_parents_w_child_power),
({'parentspec': "interf", 'childspec': " switchport voice"},
c01_parents_w_child_voice),
({'parentspec': "^interface$", 'childspec': "switchport voice"},
[]),
)
## test find_parents_w_child
for args, result_correct in find_parents_w_child_Values:
test_result = parse_c01.find_parents_w_child(**args)
assert result_correct==test_result
def testValues_find_parents_wo_child(parse_c01):
c01_parents_wo_child_power = [
'interface GigabitEthernet4/3',
'interface GigabitEthernet4/4',
'interface GigabitEthernet4/5',
'interface GigabitEthernet4/6',
'interface GigabitEthernet4/7',
'interface GigabitEthernet4/8',
]
find_parents_wo_child_Values = (
({'parentspec': "interface Gig", 'childspec': "power inline"},
c01_parents_wo_child_power),
)
## test find_parents_wo_child
for args, result_correct in find_parents_wo_child_Values:
test_result = parse_c01.find_parents_wo_child(**args)
assert result_correct==test_result
def testValues_find_children_w_parents(parse_c01):
c01_children_w_parents_switchport = [
' switchport',
' switchport access vlan 100',
' switchport voice vlan 150',
]
find_children_w_parents_Values = (
({'parentspec': "^interface GigabitEthernet4/1",
'childspec': "switchport"},
c01_children_w_parents_switchport),
)
## test find_children_w_parents
for args, result_correct in find_children_w_parents_Values:
test_result = parse_c01.find_children_w_parents(**args)
assert result_correct==test_result
def testValues_find_objects_w_parents(parse_c01):
c01_children_w_parents_switchport = [
' switchport',
' switchport access vlan 100',
' switchport voice vlan 150',
]
find_objects_w_parents_Values = (
({'parentspec': "^interface GigabitEthernet4/1",
'childspec': "switchport"},
c01_children_w_parents_switchport),
)
## test find_children_w_parents
for args, result_correct in find_objects_w_parents_Values:
test_result = list(map(attrgetter('text'),
parse_c01.find_objects_w_parents(**args)))
assert result_correct==test_result
def testValues_find_objects_w_all_children(parse_c01):
result_correct = parse_c01.find_objects(r'^interface GigabitEthernet4/[12]')
test_result = parse_c01.find_objects_w_all_children('^interface',
['switchport voice', 'power inline'])
assert test_result==result_correct
def testValues_delete_lines_01():
"""Catch bugs similar to those fixed by https://github.com/mpenning/ciscoconfparse/pull/140"""
config = [
'interface FastEthernet0/2',
' switchport port-security maximum 2',
' switchport port-security violation restrict',
' switchport port-security',
' switchport mode trunk',
' switchport trunk allowed 300,532',
' switchport nonegotiate',
'!',
'end'
]
result_correct = [
'interface FastEthernet0/2',
' switchport mode trunk',
' switchport trunk allowed 300,532',
' switchport nonegotiate',
'!',
'end'
]
parse = CiscoConfParse(config, syntax='ios')
parse.delete_lines('port-security') # Delete lines from config
assert parse.ioscfg==result_correct
def testValues_delete_lines_02():
"""Catch bugs similar to those fixed by https://github.com/mpenning/ciscoconfparse/pull/140"""
config = [
'interface FastEthernet0/2',
' switchport mode trunk',
' switchport trunk allowed 300,532',
' switchport nonegotiate',
' switchport port-security maximum 2',
' switchport port-security violation restrict',
' switchport port-security',
'!',
'end'
]
result_correct = [
'interface FastEthernet0/2',
' switchport mode trunk',
' switchport trunk allowed 300,532',
' switchport nonegotiate',
'!',
'end'
]
parse = CiscoConfParse(config, syntax='ios')
parse.delete_lines('port-security') # Delete lines from config
assert parse.ioscfg==result_correct
def testValues_delete_lines_03():
"""Catch bugs similar to those fixed by https://github.com/mpenning/ciscoconfparse/pull/140"""
config = [
'! port-security',
'interface FastEthernet0/2',
' switchport port-security maximum 2',
' switchport port-security violation restrict',
' switchport port-security',
' switchport mode trunk',
' switchport trunk allowed 300,532',
' switchport nonegotiate',
'!',
'interface FastEthernet0/3',
' switchport port-security maximum 2',
' switchport port-security violation restrict',
' switchport port-security',
' switchport mode trunk',
' switchport trunk allowed 300,532',
' switchport nonegotiate',
'!',
'end'
]
result_correct = [
'interface FastEthernet0/2',
' switchport mode trunk',
' switchport trunk allowed 300,532',
' switchport nonegotiate',
'!',
'interface FastEthernet0/3',
' switchport mode trunk',
' switchport trunk allowed 300,532',
' switchport nonegotiate',
'!',
'end'
]
parse = CiscoConfParse(config, syntax='ios')
parse.delete_lines('port-security') # Delete lines from config
assert parse.ioscfg==result_correct
def testValues_replace_lines_01(parse_c01):
c01_replace_gige_no_exactmatch = [
'interface GigabitEthernet8/1',
'interface GigabitEthernet8/2',
'interface GigabitEthernet8/3',
'interface GigabitEthernet8/4',
'interface GigabitEthernet8/5',
'interface GigabitEthernet8/6',
'interface GigabitEthernet8/7',
'interface GigabitEthernet8/8'
]
replace_lines_Values01 = (
# Ensure basic replacements work
({'linespec': r"interface Serial 1/0",
'replacestr': "interface Serial 2/0"},
['interface Serial 2/0']),
# Ensure we make multiple replacements as required
({'linespec': "GigabitEthernet4/",
'replacestr': "GigabitEthernet8/", 'exactmatch': False},
c01_replace_gige_no_exactmatch),
)
# We have to parse multiple times because of replacements
for args, result_correct in replace_lines_Values01:
test_result = parse_c01.replace_lines(**args)
assert result_correct==test_result
def testValues_replace_lines_02(parse_c01):
c01_replace_gige_exclude = [
'interface GigabitEthernet8/1',
'interface GigabitEthernet8/2',
'interface GigabitEthernet8/3',
'interface GigabitEthernet8/7',
'interface GigabitEthernet8/8'
]
replace_lines_Values02 = (
# Ensure exactmatch rejects substrings
({'linespec': "interface Serial 1/",
'replacestr': "interface Serial 2/", 'exactmatch': True},
[]),
# Ensure we exclude lines which match excludespec
({'linespec': "GigabitEthernet4/",
'excludespec': r'/4|/5|/6',
'replacestr': "GigabitEthernet8/", 'exactmatch': False},
c01_replace_gige_exclude),
)
for args, result_correct in replace_lines_Values02:
test_result = parse_c01.replace_lines(**args)
assert result_correct==test_result
def testValues_replace_lines_03(parse_c01):
"""Ensure we can use a compiled regexp in excludespec"""
c01_replace_gige_exclude = [
'interface GigabitEthernet8/1',
'interface GigabitEthernet8/2',
'interface GigabitEthernet8/3',
'interface GigabitEthernet8/7',
'interface GigabitEthernet8/8'
]
replace_lines_Values03 = (
# Ensure we can use a compiled regexp in excludespec...
({'linespec': "GigabitEthernet4/",
'excludespec': re.compile(r'/4|/5|/6'),
'replacestr': "GigabitEthernet8/", 'exactmatch': False},
c01_replace_gige_exclude),
)
for args, result_correct in replace_lines_Values03:
test_result = parse_c01.replace_lines(**args)
assert result_correct==test_result
def testValues_replace_children_01(parse_c01):
"""Test child line replacement"""
c01_replace_children = [
' power inline static max 30000',
' power inline static max 30000'
]
replace_children_Values = (
({'parentspec': "GigabitEthernet4/",
'childspec': 'max 7000',
'excludespec': re.compile(r'/4|/5|/6'),
'replacestr': "max 30000", 'exactmatch': False},
c01_replace_children),
)
# We have to parse multiple times because of replacements
for args, result_correct in replace_children_Values:
test_result = parse_c01.replace_children(**args)
assert result_correct==test_result
@pytest.mark.xfail(sys.version_info[0]==3,
reason="Difflib.SequenceMatcher is broken in Python3")
def testValues_sync_diff_01(parse_c01):
## test sync_diff as a drop-in replacement for req_cfgspec_excl_diff()
## This test mirrors testValues_req_cfgspec_excl_diff()
result_correct = ['no logging 1.1.3.17',
'logging 1.1.3.4',
'logging 1.1.3.6',
]
test_result = parse_c01.sync_diff(
[
'logging 1.1.3.4',
'logging 1.1.3.5',
'logging 1.1.3.6',
],
r"^logging\s+",
r"logging\s+\d+\.\d+\.\d+\.\d+",
)
assert result_correct==test_result
@pytest.mark.xfail(sys.version_info[0]==3,
reason="Difflib.SequenceMatcher is broken in Python3")
def testValues_sync_diff_03():
## config_01 is the starting point
config_01 = ['!',
'interface GigabitEthernet 1/5',
' ip address 1.1.1.2 255.255.255.0',
' standby 5 ip 1.1.1.1',
' standby 5 preempt',
'!']
required_config = ['!',
'interface GigabitEthernet 1/5',
' switchport',
' switchport mode access',
' switchport access vlan 5',
' switchport nonegotiate',
'!',
'interface Vlan5',
' no shutdown',
' ip address 1.1.1.2 255.255.255.0',
' standby 5 ip 1.1.1.1',
' standby 5 preempt',
'!',
]
result_correct = ['interface GigabitEthernet 1/5',
' no ip address 1.1.1.2 255.255.255.0',
' no standby 5 ip 1.1.1.1',
' no standby 5 preempt',
'interface GigabitEthernet 1/5',
' switchport',
' switchport mode access',
' switchport access vlan 5',
' switchport nonegotiate',
'interface Vlan5',
' no shutdown',
' ip address 1.1.1.2 255.255.255.0',
' standby 5 ip 1.1.1.1',
' standby 5 preempt',
]
linespec = r''
parse = CiscoConfParse(config_01)
test_result = parse.sync_diff(required_config, linespec, linespec)
assert result_correct==test_result
@pytest.mark.xfail(sys.version_info[0]==3,
reason="Difflib.SequenceMatcher is broken in Python3")
def testValues_sync_diff_04():
"""Test diffs against double-spacing for children (such as NXOS)"""
## config_01 is the starting point
config_01 = ['!',
'interface GigabitEthernet 1/5',
' ip address 1.1.1.2 255.255.255.0',
' standby 5 ip 1.1.1.1',
' standby 5 preempt',
'!']
required_config = ['!',
'interface GigabitEthernet 1/5',
' switchport',
' switchport mode access',
' switchport access vlan 5',
' switchport nonegotiate',
'!',
'interface Vlan5',
' no shutdown',
' ip address 1.1.1.2 255.255.255.0',
' standby 5 ip 1.1.1.1',
' standby 5 preempt',
'!',
]
result_correct = ['interface GigabitEthernet 1/5',
' no ip address 1.1.1.2 255.255.255.0',
' no standby 5 ip 1.1.1.1',
' no standby 5 preempt',
'interface GigabitEthernet 1/5',
' switchport',
' switchport mode access',
' switchport access vlan 5',
' switchport nonegotiate',
'interface Vlan5',
' no shutdown',
' ip address 1.1.1.2 255.255.255.0',
' standby 5 ip 1.1.1.1',
' standby 5 preempt',
]
linespec = r''
parse = CiscoConfParse(config_01)
test_result = parse.sync_diff(required_config, linespec, linespec)
assert result_correct==test_result
@pytest.mark.xfail(sys.version_info[0]==3,
reason="Difflib.SequenceMatcher is broken in Python3")
def testValues_sync_diff_05():
## config_01 is the starting point
config_01 = ['!',
'vlan 51',
' state active',
'vlan 53',
'!']
required_config = ['!',
'vlan 51',
' name SOME-VLAN',
' state active',
'vlan 52',
' name BLAH',
' state active',
'!',]
result_correct = ['no vlan 53', 'vlan 51', ' name SOME-VLAN', 'vlan 52',
' name BLAH', ' state active']
linespec = r'vlan\s+\S+|name\s+\S+|state.+'
parse = CiscoConfParse(config_01)
test_result = parse.sync_diff(required_config, linespec, linespec)
assert result_correct==test_result
@pytest.mark.xfail(sys.version_info[0]==3,
reason="Difflib.SequenceMatcher is broken in Python3")
def testValues_sync_diff_06():
"""Test diffs against double-spacing for children (such as NXOS)"""
## config_01 is the starting point
config_01 = ['!',
'vlan 51',
' state active',
'vlan 53',
'!',
]
required_config = ['!',
'vlan 51',
' name SOME-VLAN',
' state active',
'vlan 52',
' name BLAH',
' state active',
'!',]
result_correct = ['no vlan 53', 'vlan 51', ' name SOME-VLAN', 'vlan 52',
' name BLAH', ' state active']
linespec = r'vlan\s+\S+|name\s+\S+|state.+'
parse = CiscoConfParse(config_01)
test_result = parse.sync_diff(required_config, linespec, linespec)
assert result_correct==test_result
@pytest.mark.xfail(sys.version_info[0]==3,
reason="Difflib.SequenceMatcher is broken in Python3")
def testValues_sync_diff_07():
"""Test diffs with remove_lines=False"""
## config_01 is the starting point
config_01 = ['!',
'vlan 51',
' state active',
'vlan 53',
'!',
'vtp mode transparent',
]
required_config = ['!',
'vlan 51',
' name SOME-VLAN',
' state active',
'vlan 52',
' name BLAH',
' state active',
'!',]
result_correct = ['vlan 51', ' name SOME-VLAN', 'vlan 52',
' name BLAH', ' state active']
linespec = r'vlan\s+\S+|name\s+\S+|state.+'
parse = CiscoConfParse(config_01)
test_result = parse.sync_diff(required_config, linespec, linespec, remove_lines=False)
assert result_correct==test_result
@pytest.mark.xfail(sys.version_info[0]==3,
reason="Difflib.SequenceMatcher is broken in Python3")
def testValues_sync_diff_08():
"""Test diffs with explicit ignore_order=False"""
## config_01 is the starting point
config_01 = ['!',
'vlan 51',
' state active',
'vlan 53',
'!',
'vtp mode transparent',
]
required_config = ['!',
'vtp mode transparent',
'vlan 52',
' name BLAH',
' state active',
'vlan 51',
' name SOME-VLAN',
' state active',
'!',]
result_correct = ['no vlan 53',
'vlan 52',
' name BLAH',
' state active',
'vlan 51',
' name SOME-VLAN',
]
linespec = r'vlan\s+\S+|name\s+\S+|state.+|vtp'
parse = CiscoConfParse(config_01)
test_result = parse.sync_diff(required_config, linespec, linespec, ignore_order=False)
assert result_correct==test_result
@pytest.mark.xfail(sys.version_info[0]==3,
reason="Difflib.SequenceMatcher is broken in Python3")
def testValues_sync_diff_09():
"""Test diffs with explicit ignore_order=True"""
## config_01 is the starting point
config_01 = ['!',
'vlan 51',
' state active',
'vlan 53',
'!',
'vtp mode transparent',
]
required_config = ['!',
'vtp mode transparent',