-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtest_plugin_nxos.py
1030 lines (887 loc) · 37.9 KB
/
test_plugin_nxos.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
"""
Unittests for NXOS plugin
Uses the unicon.plugins.tests.mock.mock_device_ios script to test NXOS plugin.
"""
__author__ = "Dave Wapstra <[email protected]>"
import os
import yaml
import logging
import unittest
from unittest.mock import patch
import unicon
from unicon import Connection
from unicon.eal.dialogs import Statement, Dialog
from unicon.core.errors import SubCommandFailure, StateMachineError, \
ConnectionError
from unicon.plugins.tests.mock.mock_device_nxos import MockDeviceTcpWrapperNXOS
from unicon.eal.dialogs import Dialog
from unicon.mock.mock_device import mockdata_path
with open(os.path.join(mockdata_path, 'nxos/nxos_mock_data.yaml'), 'rb') as datafile:
mock_data = yaml.safe_load(datafile.read())
unicon.settings.Settings.POST_DISCONNECT_WAIT_SEC = 0
unicon.settings.Settings.GRACEFUL_DISCONNECT_WAIT_SEC = 0.2
class TestNxosPluginConnect(unittest.TestCase):
def test_login_connect(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco')
c.connect()
assert c.spawn.match.match_output == 'end\r\nswitch# '
c.disconnect()
def test_login_kerberos(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state username_kerberos'],
os='nxos',
username='cisco',
tacacs_password='cisco')
c.connect()
assert c.spawn.match.match_output == 'end\r\nswitch# '
c.disconnect()
def test_login_connect_connectReply(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco',
connect_reply=Dialog([[r'^(.*?)Password:']]))
c.connect()
self.assertIn("^(.*?)Password:", str(c.connection_provider.get_connection_dialog()))
c.disconnect()
class TestNxosPluginShellexec(unittest.TestCase):
def test_shellexec(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco')
output = c.shellexec(['sudo yum list installed | grep n9000'])
assert output == "\r\n".join("""\
sudo yum list installed | grep n9000
base-files.n9000 3.0.14-r74.2 installed
bfd.lib32_n9000 1.0.0-r0 installed
bash-4.2$""".splitlines())
assert c.spawn.match.match_output == 'exit\r\nswitch# '
c.disconnect()
@patch.object(unicon.settings.Settings, 'POST_DISCONNECT_WAIT_SEC', 0)
@patch.object(unicon.settings.Settings, 'GRACEFUL_DISCONNECT_WAIT_SEC', 0.2)
class TestNxosN3KPluginShellexec(unittest.TestCase):
def test_shellexec_n3k(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec_n3k'],
os='nxos',
platform='n3k',
username='cisco',
tacacs_password='cisco')
c.shellexec(['ls'])
assert c.spawn.match.match_output == 'exit\r\nswitch# '
c.disconnect()
class TestNxosPluginBashService(unittest.TestCase):
def test_bash(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco')
with c.bash_console() as console:
console.execute('ls')
self.assertIn('exit', c.spawn.match.match_output)
self.assertIn('switch#', c.spawn.match.match_output)
c.disconnect()
def test_bash_ha(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec',
'mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco')
c.connect()
with c.bash_console() as console:
console.execute('ls')
self.assertIn('exit', c.active.spawn.match.match_output)
self.assertIn('switch#', c.active.spawn.match.match_output)
c.disconnect()
def test_bash_ha_standby(self):
ha = MockDeviceTcpWrapperNXOS(port=0, state='exec,nxos_exec_standby')
ha.start()
c = Connection(hostname='switch',
start=['telnet 127.0.0.1 ' + str(ha.ports[0]), 'telnet 127.0.0.1 ' + str(ha.ports[1])],
os='nxos', username='cisco', tacacs_password='cisco')
try:
c.connect()
with c.bash_console(target='standby') as console:
console.execute('ls', target='standby')
self.assertIn('exit', c.standby.spawn.match.match_output)
self.assertIn('switch(standby)#', c.standby.spawn.match.match_output)
c.disconnect()
finally:
ha.stop()
class TestNxosPluginGuestshellService(unittest.TestCase):
def test_guestshell_basic(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco')
with c.guestshell() as gs:
output = gs.execute('pwd')
self.assertEqual('/home/admin', output)
self.assertIn('exit', c.spawn.match.match_output)
self.assertIn('switch#', c.spawn.match.match_output)
c.disconnect()
def test_guestshell_enable(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco')
with c.guestshell(enable_guestshell=True, retries=5) as gs:
gs.execute('pwd')
self.assertIn('exit', c.spawn.match.match_output)
self.assertIn('switch#', c.spawn.match.match_output)
# Attempt to activate again - guestshell is already active
with c.guestshell(enable_guestshell=True, retries=5) as gs:
gs.execute('pwd')
c.disconnect()
def test_guestshell_retries_exceeded_enable(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco')
with self.assertRaises(SubCommandFailure) as err:
with c.guestshell(enable_guestshell=True, retries=2) as gs:
gs.execute("pwd")
self.assertEqual("Failed to enable guestshell after 2 tries",
str(err.exception))
c.disconnect()
def test_guestshell_retries_exceeded_activate(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco')
with self.assertRaises(SubCommandFailure) as err:
with c.guestshell(enable_guestshell=True, retries=3) as gs:
gs.execute("pwd")
self.assertEqual("Guestshell failed to become activated after 3 tries",
str(err.exception))
c.disconnect()
def test_ha_guestshell_basic(self):
ha = MockDeviceTcpWrapperNXOS(port=0, state='exec,nxos_exec_standby', hostname='switch')
ha.start()
d = Connection(hostname='switch',
start=['telnet 127.0.0.1 ' + str(ha.ports[0]),
'telnet 127.0.0.1 ' + str(ha.ports[1])],
os='nxos',
username='cisco',
tacacs_password='cisco')
try:
d.connect()
with d.guestshell() as gs:
output = gs.execute('pwd')
self.assertEqual('/home/admin', output)
self.assertIn('exit', d.active.spawn.match.match_output)
self.assertIn('switch#', d.active.spawn.match.match_output)
d.disconnect()
finally:
ha.stop()
class TestNxosPluginAttachConsoleService(unittest.TestCase):
def test_shell(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco')
with c.attach_console(1) as console:
console.execute('ls')
self.assertEqual(c.state_machine.current_state, 'enable')
c.disconnect()
class TestNxosPluginAttachModule(unittest.TestCase):
def test_attach_module(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
credentials=dict(
default=dict(
username='cisco',
password='cisco')
),
init_exec_commands=[],
init_config_commands=[]
)
with c.attach(1) as m:
m.execute('debug platform internal tah elam asic 0', allow_state_change=True)
m.execute('trigger init asic 0 slice 2 lu-a2d 1 in-select 9 out-select 1 use-src-id 25', allow_state_change=True)
m.execute('set outer ipv4 dst_ip 225.1.1.1 src_ip 11.2.1.100')
self.assertEqual(c.state_machine.current_state, 'enable')
c.disconnect()
def test_attach_module(self):
c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
credentials=dict(
default=dict(
username='cisco',
password='cisco')
),
init_exec_commands=[],
init_config_commands=[]
)
with c.attach(1) as m:
m.execute('debug platform internal tah elam asic 0', allow_state_change=True)
m.execute('trigger init asic 0 slice 2 lu-a2d 1 in-select 9 out-select 1 use-src-id 25', allow_state_change=True)
m.execute('set outer ipv4 dst_ip 225.1.1.1 src_ip 11.2.1.100')
self.assertEqual(c.state_machine.current_state, 'enable')
c.disconnect()
class TestNxosPluginPing6Service(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.d = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco')
cls.d.connect()
cls.ha = MockDeviceTcpWrapperNXOS(port=0, state='exec,nxos_exec_standby')
cls.ha.start()
cls.ha_device = Connection(hostname='switch',
start=['telnet 127.0.0.1 ' + str(cls.ha.ports[0]), 'telnet 127.0.0.1 ' + str(cls.ha.ports[1])],
os='nxos', username='cisco', tacacs_password='cisco')
cls.ha_device.connect()
@classmethod
@patch.object(unicon.settings.Settings, 'POST_DISCONNECT_WAIT_SEC', 0)
@patch.object(unicon.settings.Settings, 'GRACEFUL_DISCONNECT_WAIT_SEC', 0.2)
def tearDownClass(cls):
cls.d.disconnect()
cls.ha_device.disconnect()
cls.ha.stop()
def test_ha_ping6(self):
try:
self.ha_device.ping6(addr="2003::7010", vrf="management")
result = True
except Exception as e:
print('Error in ping6 service for dual rp: {}'.format(e))
result = False
self.assertTrue(result)
def test_single_rp_ping6(self):
try:
self.d.ping6(addr="2003::7010", vrf="management")
result = True
except Exception as e:
print('Error in ping6 service for single rp: {}'.format(e))
result = False
self.assertTrue(result)
class TestNxosPluginExecute(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[])
cls.c.connect()
@classmethod
def tearDownClass(cls):
cls.c.disconnect()
def test_execute_show_feature(self):
cmd = 'show feature'
expected_response = mock_data['exec']['commands'][cmd].strip()
r = self.c.execute(cmd).replace('\r', '')
self.assertEqual(r, expected_response)
def test_execute_error_pattern(self):
for cmd in ['not a real command', 'system mode maintenance | command failed']:
with self.assertRaises(SubCommandFailure):
self.c.execute(cmd)
def test_execute_error_pattern_negative(self):
self.c.execute('not a real command partial')
def test_execute_copy_not_allowed(self):
with self.assertRaises(SubCommandFailure):
self.c.execute('copy sftp://server/root/nxos.7.0.3.I7.8.bin bootflash:///nxos.7.0.3.I7.8.bin vrf management')
with self.assertRaises(SubCommandFailure):
self.c.execute('copy scp://localhost/nxos.7.0.3.I7.8.bin bootflash:///nxos.7.0.3.I7.8.bin vrf management')
def test_module_reload(self):
self.c.execute('reload module 1')
def test_show_logging(self):
self.maxDiff = None
cmd = 'show logging logfile'
output = self.c.execute(cmd).replace('\r', '')
expected_response = mock_data['exec']['commands'][cmd].strip()
self.assertEqual(output, expected_response)
class TestNxosCrash(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.c = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[])
cls.c.connect()
@classmethod
@patch.object(unicon.settings.Settings, 'POST_DISCONNECT_WAIT_SEC', 0)
@patch.object(unicon.settings.Settings, 'GRACEFUL_DISCONNECT_WAIT_SEC', 0.2)
def tearDownClass(cls):
cls.c.execute('boot', allow_state_change=True)
cls.c.disconnect()
def test_execute_crash(self):
self.c.enable()
with self.assertRaises(StateMachineError):
self.c.execute('crash command')
self.assertEqual(self.c.state_machine.current_state, 'rommon')
class TestNxosPluginReloadService(unittest.TestCase):
def test_reload_config_lock_retries_succeed_with_default(self):
dev = Connection(
hostname='N93_1',
start=['mock_device_cli --os nxos --state login2 --hostname N93_1'],
os='nxos',
username='cisco',
tacacs_password='cisco',
enable_password='cisco',
)
dev.connect()
dev.start = ['mock_device_cli --os nxos --state reconnect_login --hostname N93_1']
dev.settings.RELOAD_RECONNECT_WAIT = 1
dev.settings.CONFIG_LOCK_RETRY_SLEEP = 1
dev.reload()
dev.configure('no logging console')
dev.disconnect()
def test_reload_config_lock_retries_succeed(self):
dev = Connection(
hostname='N93_1',
start=['mock_device_cli --os nxos --state login2 --hostname N93_1'],
os='nxos',
username='cisco',
tacacs_password='cisco',
enable_password='cisco',
)
dev.connect()
dev.settings.RELOAD_RECONNECT_WAIT = 1
dev.settings.CONFIG_LOCK_RETRY_SLEEP = 1
dev.start = ['mock_device_cli --os nxos --state reconnect_login --hostname N93_1']
dev.reload(config_lock_retries=2, config_lock_retry_sleep=1)
dev.configure('no logging console')
dev.disconnect()
def test_reload_config_lock_retries_fail(self):
dev = Connection(
hostname='N93_1',
start=['mock_device_cli --os nxos --state login2 --hostname N93_1'],
os='nxos',
username='cisco',
tacacs_password='cisco',
enable_password='cisco',
)
dev.connect()
dev.settings.RELOAD_RECONNECT_WAIT = 1
dev.settings.CONFIG_LOCK_RETRY_SLEEP = 1
dev.settings.CONFIG_LOCK_RETRIES = 1
dev.start = ['mock_device_cli --os nxos --state reconnect_login --hostname N93_1']
with self.assertRaises(SubCommandFailure):
dev.reload(config_lock_retries=1, config_lock_retry_sleep=1)
def test_reload_skip_poap(self):
dev = Connection(
hostname='N93_1',
start=['mock_device_cli --os nxos --state login2 --hostname N93_1'],
os='nxos',
username='cisco',
tacacs_password='cisco',
enable_password='cisco',
)
dev.connect()
dev.settings.RELOAD_RECONNECT_WAIT = 1
dev.reload(reload_command='reload skip_poap')
dev.configure('no logging console')
dev.disconnect()
def test_reload_skip_poap2(self):
dev = Connection(
hostname='N93_1',
start=['mock_device_cli --os nxos --state exec2 --hostname N93_1'],
os='nxos',
username='cisco',
tacacs_password='cisco',
enable_password='cisco',
)
dev.connect()
dev.settings.RELOAD_RECONNECT_WAIT = 1
dev.reload(reload_command='reload skip_poap2')
dev.reload(reload_command='reload skip_poap2')
dev.configure('no logging console')
dev.disconnect()
def test_reload_sleep_succeed(self):
dev = Connection(
hostname='N93_1',
start=['mock_device_cli --os nxos --state login2 --hostname N93_1'],
os='nxos',
username='cisco',
tacacs_password='cisco',
enable_password='cisco',
)
dev.connect()
dev.settings.POST_RELOAD_WAIT = 1
reconnect_sleep_value = 0.05
with self.assertLogs(dev.log, logging.DEBUG) as cm:
dev.reload(reload_command='reload buffer settle',
reconnect_sleep=reconnect_sleep_value)
self.assertIn(
f'INFO:{dev.log.name}:Waiting for boot messages to settle for '
f'{reconnect_sleep_value} seconds',
cm.output)
self.assertNotIn(
f'INFO:{dev.log.name}:Waiting for boot messages to settle for '
f'{dev.settings.POST_RELOAD_WAIT} seconds',
cm.output)
def test_reload_sleep_timeout(self):
dev = Connection(
hostname='N93_1',
start=['mock_device_cli --os nxos --state login2 --hostname N93_1'],
os='nxos',
username='cisco',
tacacs_password='cisco',
enable_password='cisco',
)
dev.connect()
with self.assertLogs(dev.log, logging.DEBUG) as cm:
dev.reload(reload_command='reload buffer settle',
reconnect_sleep=1.5,
timeout=1)
self.assertIn(
f'INFO:{dev.log.name}:Time out, trying to acces device..',
cm.output)
def test_reload_with_error_pattern(self):
c = Connection(
hostname='N93_1',
start=['mock_device_cli --os nxos --state login2 --hostname N93_1'],
os='nxos',
username='cisco',
tacacs_password='cisco',
enable_password='cisco',
)
install_add_one_shot_dialog = Dialog([
Statement(pattern=r".*reload of the system\. "
r"Do you want to proceed\? \[y\/n\]",
action='sendline(y)',
loop_continue=True,
continue_timer=False),
Statement(pattern=r"FAILED:.* ",
action=None,
loop_continue=False,
continue_timer=False),
])
error_pattern=[r"FAILED:.* ",]
try:
c.connect()
c.settings.POST_RELOAD_WAIT = 1
with self.assertRaises(SubCommandFailure):
c.reload('active_install_add',
reply=install_add_one_shot_dialog,
error_pattern = error_pattern)
self.assertEqual(c.state_machine.current_state, 'enable')
finally:
c.disconnect()
def test_reload_ha_with_error_pattern(self):
md = MockDeviceTcpWrapperNXOS(port=0, state='exec,nxos_exec_standby')
md.start()
c = Connection(hostname='switch',
start=['telnet 127.0.0.1 ' + str(md.ports[0]),
'telnet 127.0.0.1 ' + str(md.ports[1])],
os='nxos', username='cisco', tacacs_password='cisco')
install_add_one_shot_dialog = Dialog([
Statement(pattern=r"FAILED:.* ",
action=None,
loop_continue=False,
continue_timer=False),
])
error_pattern=[r"FAILED:.* ",]
try:
c.connect()
c.settings.POST_RELOAD_WAIT = 1
with self.assertRaises(SubCommandFailure):
c.reload('active_install_add',
reply=install_add_one_shot_dialog,
error_pattern = error_pattern)
finally:
c.disconnect()
md.stop()
class TestNxosPluginMaintenanceMode(unittest.TestCase):
def test_maint_mode(self):
dev = Connection(
hostname='N93_1',
start=['mock_device_cli --os nxos --state exec_maint --hostname N93_1'],
os='nxos',
credentials={
'defaut': {
'username': 'cisco',
'password': 'cisco'
}
}
)
dev.connect()
dev.disconnect()
class TestNxosPluginDebugMode(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.dev = Connection(
hostname='N93_1',
start=['mock_device_cli --os nxos --state exec --hostname N93_1'],
os='nxos',
credentials={
'defaut': {
'username': 'cisco',
'password': 'cisco'
}
}
)
cls.dev.connect()
@classmethod
def tearDownClass(cls):
cls.dev.disconnect()
def test_debug_prompt(self):
self.dev.execute('load dplug', allow_state_change=True)
self.dev.enable()
def test_debug_sqlite(self):
self.dev.execute('load dplug', allow_state_change=True)
self.dev.execute('sqlite3 test.db', allow_state_change=True)
self.dev.enable()
class TestNxosIncorrectLogin(unittest.TestCase):
def test_incorrect_login(self):
dev = Connection(
hostname='switch',
start=['mock_device_cli --os nxos --state password4'],
os='nxos',
credentials={
'default': {
'username': 'admin',
'password': 'cisco'
}
}
)
dev.connect()
dev.disconnect()
class TestNxosPluginConfigure(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[])
cls.dev.connect()
def test_execute_configure_commit(self):
acl_cfg = "configure session acl6\nip access-list acl6\n"\
"10 permit ip 63.1.1.1/24 64.1.1.1/24\nip access-list acl5\n10 permit ip 130.1.1.1/24 140.1.1.1/24"
out = self.dev.configure(acl_cfg, commit=True)
self.assertIn('Commit Successful', out)
def test_configure_error_pattern(self):
for cmd in ['b', 'boot']:
with self.assertRaises(SubCommandFailure):
self.dev.configure(cmd)
self.dev.disconnect()
def test_config_locked(self):
c = Connection(hostname='RouterRP',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
mit=True,
init_exec_commands=[],
init_config_commands=[],
settings=dict(POST_DISCONNECT_WAIT_SEC=0,GRACEFUL_DISCONNECT_WAIT_SEC=0.2),
log_buffer=True
)
c.connect()
c.execute('set config lock count 2')
c.settings.CONFIG_LOCK_RETRIES = 0
c.settings.CONFIG_LOCK_RETRY_SLEEP = 0
with self.assertRaises(StateMachineError):
c.configure('')
c.execute('set config lock count 2')
with self.assertRaises(StateMachineError):
c.configure('', lock_retries=1, lock_retry_sleep=1)
c.execute('set config lock count 3')
c.settings.CONFIG_LOCK_RETRIES = 1
c.settings.CONFIG_LOCK_RETRY_SLEEP = 1
with self.assertRaises(StateMachineError):
c.configure('')
c.execute('set config lock count 3')
c.settings.CONFIG_LOCK_RETRIES = 5
c.settings.CONFIG_LOCK_RETRY_SLEEP = 1
c.configure('')
c.disconnect()
def test_configure_update_hostname(self):
self.dev.configure('switchname Router')
class TestNxosConfigureDual(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[])
cls.dev.connect()
def test_configure_dual(self):
out = self.dev.configure_dual(['feature isis'])
self.assertIn('Verification Succeeded.', out)
# test on normal configure
config = self.dev.configure('no logging console')
self.assertIn('no logging console', config)
def test_configure_dual_mode(self):
out = self.dev.configure(['feature isis'], mode='dual')
self.assertIn('Verification Succeeded.', out)
# test on normal configure
config = self.dev.configure('no logging console')
self.assertIn('no logging console', config)
def test_configure_dual_mode_attribute(self):
self.dev.configure.mode = 'dual'
out = self.dev.configure(['feature isis'])
self.assertIn('Verification Succeeded.', out)
self.dev.configure.mode = 'default'
# test on normal configure
config = self.dev.configure('no logging console')
self.assertIn('no logging console', config)
def test_connect_config_dual(self):
dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state config_dual_commit'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[]
)
dev.connect()
self.assertEqual(dev.state_machine.current_state, 'enable')
@classmethod
def tearDownClass(cls):
cls.dev.disconnect()
class TestNxosPluginSwitchtoVdc(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.c = Connection(hostname='N77',
start=['mock_device_cli --os nxos --state exec --hostname N77'],
os='nxos',
credentials=dict(default=dict(username='cisco', password='cisco')),
log_buffer=True)
cls.c.connect()
@classmethod
def tearDownClass(cls):
cls.c.disconnect()
def test_switchto_vdc_switchback(self):
self.c.switchto('N77_3')
self.c.switchback()
def test_switchto_new_vdc_switchback(self):
self.c.switchto('N77_4')
self.c.switchback()
class TestNxosL2ribClient(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state exec'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[],
log_buffer=True)
cls.dev.connect()
def test_l2rib_client(self):
self.dev.execute('run bash sudo su', allow_state_change=True)
self.dev.execute('/isan/bin/l2rib_dt -r', allow_state_change=True)
self.dev.execute('help')
self.dev.enable()
def test_l2rib_client_context_manager(self):
with self.dev.l2rib_dt() as rib:
rib.execute('help')
def test_l2rib_client_execute(self):
self.dev.l2rib_dt().execute('help')
self.dev.enable()
def test_l2rib_client_id(self):
with self.dev.l2rib_dt(client_id=1000) as rib:
rib.execute('help')
@classmethod
def tearDownClass(cls):
cls.dev.disconnect()
class TestNxosRommonBoot(unittest.TestCase):
def test_loader_init(self):
dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state loader'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[],
log_buffer=True,
settings={'BOOT_TIMEOUT': 10},
mit=True)
try:
dev.connect()
self.assertEqual(dev.state_machine.current_state, 'rommon')
finally:
dev.disconnect()
def test_loader_state(self):
dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state loader'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[],
settings={'BOOT_TIMEOUT': 10},
log_buffer=True)
try:
dev.connect()
self.assertEqual(dev.state_machine.current_state, 'enable')
finally:
dev.disconnect()
def test_loader_init_commands(self):
dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state loader'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[],
log_buffer=True,
settings={
'ROMMON_INIT_COMMANDS': ['recoverymode=1'],
'BOOT_TIMEOUT': 10}
)
try:
output = dev.connect()
assert 'recoverymode' in output
self.assertEqual(dev.state_machine.current_state, 'enable')
finally:
dev.disconnect()
def test_loader_boot_image_from_loader(self):
dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state loader'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[],
log_buffer=True,
settings={'BOOT_TIMEOUT': 10},
image_to_boot='test.bin'
)
try:
dev.connect()
self.assertEqual(dev.state_machine.current_state, 'enable')
finally:
dev.disconnect()
def test_loader_boot_image_from_boot(self):
dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state boot'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[],
log_buffer=True,
settings={'BOOT_TIMEOUT': 10},
image_to_boot='test.bin'
)
try:
dev.connect()
self.assertEqual(dev.state_machine.current_state, 'enable')
finally:
dev.disconnect()
def test_loader_boot_image_via_boot(self):
dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state loader'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[],
log_buffer=True,
settings={'BOOT_TIMEOUT': 10},
image_to_boot='new.bin'
)
try:
dev.connect()
self.assertEqual(dev.state_machine.current_state, 'enable')
finally:
dev.disconnect()
def test_loader_boot_image_via_boot_with_config_init_cmds(self):
dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state loader'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[],
log_buffer=True,
image_to_boot='new.bin',
settings={
'BOOT_INIT_CONFIG_COMMANDS': ['admin-password secret'],
'BOOT_TIMEOUT': 10}
)
try:
dev.connect()
self.assertEqual(dev.state_machine.current_state, 'enable')
finally:
dev.disconnect()
def test_connect_in_boot(self):
dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state boot'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[],
log_buffer=True,
image_to_boot='new.bin',
settings={
'BOOT_INIT_CONFIG_COMMANDS': ['admin-password secret'],
'BOOT_TIMEOUT': 10}
)
try:
output = dev.connect()
assert 'admin-password secret' in output
self.assertEqual(dev.state_machine.current_state, 'enable')
finally:
dev.disconnect()
def test_connect_in_boot_config(self):
dev = Connection(hostname='switch',
start=['mock_device_cli --os nxos --state boot_config'],
os='nxos',
username='cisco',
tacacs_password='cisco',
init_exec_commands=[],
init_config_commands=[],
log_buffer=True,
image_to_boot='new.bin',
settings={