forked from sonic-net/sonic-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_snmp_test.py
884 lines (787 loc) · 50.3 KB
/
config_snmp_test.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
import sys
import os
import click
from click.testing import CliRunner
import show.main as show
import clear.main as clear
import config.main as config
import config.validated_config_db_connector as validated_config_db_connector
import pytest
from unittest import mock
from unittest.mock import patch
from utilities_common.db import Db
tabular_data_show_run_snmp_contact_expected = """\
Contact Contact Email\n--------- --------------------\ntestuser [email protected]
"""
json_data_show_run_snmp_contact_expected = """\
{'testuser': '[email protected]'}
"""
config_snmp_contact_add_del_new_contact ="""\
Contact name testuser and contact email [email protected] have been added to configuration
Restarting SNMP service...
"""
config_snmp_location_add_new_location ="""\
SNMP Location public has been added to configuration
Restarting SNMP service...
"""
expected_snmp_community_add_new_community_ro_output = {"TYPE": "RO"}
expected_snmp_community_add_new_community_rw_output = {"TYPE": "RW"}
expected_snmp_community_replace_existing_community_with_new_community_output = {'TYPE': 'RW'}
expected_snmp_user_priv_ro_md5_des_config_db_output = {'SNMP_USER_AUTH_PASSWORD': 'user_auth_pass',
'SNMP_USER_AUTH_TYPE': 'MD5',
'SNMP_USER_ENCRYPTION_PASSWORD': 'user_encrypt_pass',
'SNMP_USER_ENCRYPTION_TYPE': 'DES',
'SNMP_USER_PERMISSION': 'RO',
'SNMP_USER_TYPE': 'Priv'}
expected_snmp_user_priv_ro_md5_aes_config_db_output = {'SNMP_USER_AUTH_PASSWORD': 'user_auth_pass',
'SNMP_USER_AUTH_TYPE': 'MD5',
'SNMP_USER_ENCRYPTION_PASSWORD': 'user_encrypt_pass',
'SNMP_USER_ENCRYPTION_TYPE': 'AES',
'SNMP_USER_PERMISSION': 'RO',
'SNMP_USER_TYPE': 'Priv'}
expected_snmp_user_priv_ro_sha_des_config_db_output = {'SNMP_USER_AUTH_PASSWORD': 'user_auth_pass',
'SNMP_USER_AUTH_TYPE': 'SHA',
'SNMP_USER_ENCRYPTION_PASSWORD': 'user_encrypt_pass',
'SNMP_USER_ENCRYPTION_TYPE': 'DES',
'SNMP_USER_PERMISSION': 'RO',
'SNMP_USER_TYPE': 'Priv'}
expected_snmp_user_priv_ro_sha_aes_config_db_output = {'SNMP_USER_AUTH_PASSWORD': 'user_auth_pass',
'SNMP_USER_AUTH_TYPE': 'SHA',
'SNMP_USER_ENCRYPTION_PASSWORD': 'user_encrypt_pass',
'SNMP_USER_ENCRYPTION_TYPE': 'AES',
'SNMP_USER_PERMISSION': 'RO',
'SNMP_USER_TYPE': 'Priv'}
expected_snmp_user_priv_ro_hmac_sha_2_des_config_db_output = {'SNMP_USER_AUTH_PASSWORD': 'user_auth_pass',
'SNMP_USER_AUTH_TYPE': 'HMAC-SHA-2',
'SNMP_USER_ENCRYPTION_PASSWORD': 'user_encrypt_pass',
'SNMP_USER_ENCRYPTION_TYPE': 'DES',
'SNMP_USER_PERMISSION': 'RO',
'SNMP_USER_TYPE': 'Priv'}
expected_snmp_user_priv_ro_hmac_sha_2_aes_config_db_output = {'SNMP_USER_AUTH_PASSWORD': 'user_auth_pass',
'SNMP_USER_AUTH_TYPE': 'HMAC-SHA-2',
'SNMP_USER_ENCRYPTION_PASSWORD': 'user_encrypt_pass',
'SNMP_USER_ENCRYPTION_TYPE': 'AES',
'SNMP_USER_PERMISSION': 'RO',
'SNMP_USER_TYPE': 'Priv'}
expected_snmp_user_priv_rw_md5_des_config_db_output = {'SNMP_USER_AUTH_PASSWORD': 'user_auth_pass',
'SNMP_USER_AUTH_TYPE': 'MD5',
'SNMP_USER_ENCRYPTION_PASSWORD': 'user_encrypt_pass',
'SNMP_USER_ENCRYPTION_TYPE': 'DES',
'SNMP_USER_PERMISSION': 'RW',
'SNMP_USER_TYPE': 'Priv'}
expected_snmp_user_priv_rw_md5_aes_config_db_output = {'SNMP_USER_AUTH_PASSWORD': 'user_auth_pass',
'SNMP_USER_AUTH_TYPE': 'MD5',
'SNMP_USER_ENCRYPTION_PASSWORD': 'user_encrypt_pass',
'SNMP_USER_ENCRYPTION_TYPE': 'AES',
'SNMP_USER_PERMISSION': 'RW',
'SNMP_USER_TYPE': 'Priv'}
expected_snmp_user_priv_rw_sha_des_config_db_output = {'SNMP_USER_AUTH_PASSWORD': 'user_auth_pass',
'SNMP_USER_AUTH_TYPE': 'SHA',
'SNMP_USER_ENCRYPTION_PASSWORD': 'user_encrypt_pass',
'SNMP_USER_ENCRYPTION_TYPE': 'DES',
'SNMP_USER_PERMISSION': 'RW',
'SNMP_USER_TYPE': 'Priv'}
expected_snmp_user_priv_rw_sha_aes_config_db_output = {'SNMP_USER_AUTH_PASSWORD': 'user_auth_pass',
'SNMP_USER_AUTH_TYPE': 'SHA',
'SNMP_USER_ENCRYPTION_PASSWORD': 'user_encrypt_pass',
'SNMP_USER_ENCRYPTION_TYPE': 'AES',
'SNMP_USER_PERMISSION': 'RW',
'SNMP_USER_TYPE': 'Priv'}
expected_snmp_user_priv_rw_hmac_sha_2_des_config_db_output = {'SNMP_USER_AUTH_PASSWORD': 'user_auth_pass',
'SNMP_USER_AUTH_TYPE': 'HMAC-SHA-2',
'SNMP_USER_ENCRYPTION_PASSWORD': 'user_encrypt_pass',
'SNMP_USER_ENCRYPTION_TYPE': 'DES',
'SNMP_USER_PERMISSION': 'RW',
'SNMP_USER_TYPE': 'Priv'}
expected_snmp_user_priv_rw_hmac_sha_2_aes_config_db_output = {'SNMP_USER_AUTH_PASSWORD': 'user_auth_pass',
'SNMP_USER_AUTH_TYPE': 'HMAC-SHA-2',
'SNMP_USER_ENCRYPTION_PASSWORD': 'user_encrypt_pass',
'SNMP_USER_ENCRYPTION_TYPE': 'AES',
'SNMP_USER_PERMISSION': 'RW',
'SNMP_USER_TYPE': 'Priv'}
class TestSNMPConfigCommands(object):
@classmethod
def setup_class(cls):
print("SETUP")
os.environ["UTILITIES_UNIT_TESTING"] = "1"
# Add snmp community tests
def test_config_snmp_community_add_new_community_ro(self):
config.ADHOC_VALIDATION = True
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["add"],
["Everest", "ro"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP community Everest added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_COMMUNITY", "Everest") == expected_snmp_community_add_new_community_ro_output
def test_config_snmp_community_add_new_community_rw(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["add"],
["Shasta", "rw"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP community Shasta added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_COMMUNITY", "Shasta") == expected_snmp_community_add_new_community_rw_output
def test_config_snmp_community_add_new_community_with_invalid_type(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["add"], ["Everest", "RT"])
print(result.exit_code)
assert result.exit_code == 1
assert 'Invalid community type. Must be either RO or RW' in result.output
def test_config_snmp_community_add_invalid_community_over_32_characters(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["add"],
["over_32_character_community_string", "ro"])
print(result.exit_code)
assert result.exit_code == 2
assert 'FAILED: SNMP community string length should be not be greater than 32' in result.output
def test_config_snmp_community_add_invalid_community_with_excluded_special_characters(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["add"],
["Test@snmp", "ro"])
print(result.exit_code)
assert result.exit_code == 2
assert 'FAILED: SNMP community string should not have any of these special symbols' in result.output
def test_config_snmp_community_add_existing_community(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["add"], ["Rainer", "rw"])
print(result.exit_code)
assert result.exit_code == 3
assert 'SNMP community Rainer is already configured' in result.output
# Del snmp community tests
def test_config_snmp_community_del_existing_community(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["del"],
["Rainer"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP community Rainer removed from configuration' in result.output
assert db.cfgdb.get_entry("SNMP_COMMUNITY", "Everest") == {}
def test_config_snmp_community_del_non_existing_community(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["del"], ["Everest"])
print(result.exit_code)
assert result.exit_code == 1
assert 'SNMP community Everest is not configured' in result.output
# Replace snmp community tests
def test_config_snmp_community_replace_existing_community_with_new_community(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["replace"],
["Rainer", "Everest"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP community Everest added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_COMMUNITY", "Everest") == \
expected_snmp_community_replace_existing_community_with_new_community_output
def test_config_snmp_community_replace_existing_community_non_existing_community(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["replace"],
["Denali", "Everest"])
print(result.exit_code)
assert result.exit_code == 1
assert 'Current SNMP community Denali is not configured' in result.output
def test_config_snmp_community_replace_new_community_already_exists(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["replace"],
["Rainer", "msft"])
print(result.exit_code)
assert result.exit_code == 3
assert 'New SNMP community msft to replace current SNMP community Rainer already configured' in result.output
def test_config_snmp_community_replace_with_invalid_new_community_bad_symbol(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["replace"],
["Rainer", "msft@"])
print(result.exit_code)
assert result.exit_code == 2
assert 'FAILED: SNMP community string should not have any of these special symbols' in result.output
def test_config_snmp_community_replace_with_invalid_new_community_over_32_chars(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["replace"],
["Rainer", "over_32_characters_community_string"])
print(result.exit_code)
assert result.exit_code == 2
assert 'FAILED: SNMP community string length should be not be greater than 32' in result.output
# Del snmp contact when CONTACT not setup in REDIS
def test_config_snmp_contact_del_without_contact_redis(self):
db = Db()
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["del"], ["blah"], obj=db)
print(result.exit_code)
assert result.exit_code == 2
assert 'Contact name blah is not configured' in result.output
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {}
def test_config_snmp_contact_modify_without_contact_redis(self):
db = Db()
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["modify"],
["blah", "[email protected]"], obj=db)
print(result.exit_code)
assert result.exit_code == 3
assert 'Contact name blah is not configured' in result.output
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {}
def test_config_snmp_contact_add_del_new_contact(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"],
["testuser", "[email protected]"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_contact_add_del_new_contact
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "[email protected]"}
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["del"],
["testuser"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert 'SNMP contact testuser removed from configuration' in result.output
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {}
# Add snmp contact tests
def test_config_snmp_contact_add_with_existing_contact(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"],
["testuser", "[email protected]"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_contact_add_del_new_contact
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "[email protected]"}
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"],
["blah", "[email protected]"], obj=db)
print(result.exit_code)
assert result.exit_code == 1
assert 'Contact already exists. Use sudo config snmp contact modify instead' in result.output
def test_config_snmp_contact_add_invalid_email(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"],
["testuser", "testusercontoso.com"], obj=db)
print(result.exit_code)
assert result.exit_code == 2
assert "Contact email testusercontoso.com is not valid" in result.output
# Delete snmp contact tests
def test_config_snmp_contact_del_new_contact_when_contact_exists(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"],
["testuser", "[email protected]"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_contact_add_del_new_contact
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "[email protected]"}
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["del"], ["blah"], obj=db)
print(result.exit_code)
assert result.exit_code == 1
assert 'SNMP contact blah is not configured' in result.output
def test_config_snmp_contact_del_with_existing_contact(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"],
["testuser", "[email protected]"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_contact_add_del_new_contact
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "[email protected]"}
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["del"],
["testuser"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP contact testuser removed from configuration' in result.output
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {}
# Modify snmp contact tests
def test_config_snmp_contact_modify_email_with_existing_contact(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"],
["testuser", "[email protected]"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_contact_add_del_new_contact
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "[email protected]"}
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["modify"],
["testuser", "[email protected]"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP contact testuser email updated to [email protected]' in result.output
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "[email protected]"}
def test_config_snmp_contact_modify_contact_and_email_with_existing_entry(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"],
["testuser", "[email protected]"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_contact_add_del_new_contact
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "[email protected]"}
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["modify"],
["testuser", "[email protected]"], obj=db)
print(result.exit_code)
assert result.exit_code == 1
assert 'SNMP contact testuser [email protected] already exists' in result.output
def test_config_snmp_contact_modify_existing_contact_with_invalid_email(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"],
["testuser", "[email protected]"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_contact_add_del_new_contact
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "[email protected]"}
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["modify"],
["testuser", "testuser@contosocom"], obj=db)
print(result.exit_code)
assert result.exit_code == 2
assert 'Contact email testuser@contosocom is not valid' in result.output
def test_config_snmp_contact_modify_new_contact_with_invalid_email(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["add"],
["testuser", "[email protected]"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_contact_add_del_new_contact
assert db.cfgdb.get_entry("SNMP", "CONTACT") == {"testuser": "[email protected]"}
result = runner.invoke(config.config.commands["snmp"].commands["contact"].commands["modify"],
["blah", "blah@contoso@com"], obj=db)
print(result.exit_code)
assert result.exit_code == 2
assert 'Contact email blah@contoso@com is not valid' in result.output
# Add snmp location tests
def test_config_snmp_location_add_exiting_location_with_same_location_already_existing(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["add"],
["public"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_location_add_new_location
assert db.cfgdb.get_entry("SNMP", "LOCATION") == {"Location": "public"}
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["add"],
["public"], obj=db)
print(result.exit_code)
assert result.exit_code == 1
assert 'Location already exists' in result.output
def test_config_snmp_location_add_new_location_with_location_already_existing(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["add"],
["public"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_location_add_new_location
assert db.cfgdb.get_entry("SNMP", "LOCATION") == {"Location": "public"}
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["add"],
["Mile High"], obj=db)
print(result.exit_code)
assert result.exit_code == 1
assert 'Location already exists' in result.output
# Del snmp location tests
def test_config_snmp_location_del_with_existing_location(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["add"],
["public"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_location_add_new_location
assert db.cfgdb.get_entry("SNMP", "LOCATION") == {"Location": "public"}
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["del"],
["public"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP Location public removed from configuration' in result.output
assert db.cfgdb.get_entry("SNMP", "LOCATION") == {}
def test_config_snmp_location_del_new_location_with_location_already_existing(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["add"],
["public"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_location_add_new_location
assert db.cfgdb.get_entry("SNMP", "LOCATION") == {"Location": "public"}
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["del"],
["Mile High"], obj=db)
print(result.exit_code)
assert result.exit_code == 1
assert 'SNMP Location Mile High does not exist. The location is public' in result.output
# Modify snmp location tests
def test_config_snmp_location_modify_with_same_location(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["add"],
["public"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_location_add_new_location
assert db.cfgdb.get_entry("SNMP", "LOCATION") == {"Location": "public"}
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["modify"],
["public"], obj=db)
print(result.exit_code)
assert result.exit_code == 1
assert 'SNMP location public already exists' in result.output
def test_config_snmp_location_modify_without_redis(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["modify"],
["Rainer"],obj=db)
print(result.exit_code)
assert result.exit_code == 2
assert "Cannot modify SNMP Location. You must use 'config snmp location add " \
"command <snmp_location>'" in result.output
assert db.cfgdb.get_entry("SNMP", "LOCATION") == {}
def test_config_snmp_location_modify_without_existing_location(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["add"],
["public"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == config_snmp_location_add_new_location
assert db.cfgdb.get_entry("SNMP", "LOCATION") == {"Location": "public"}
result = runner.invoke(config.config.commands["snmp"].commands["location"].commands["modify"],
["Rainer"],obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert "SNMP location Rainer modified in configuration" in result.output
assert db.cfgdb.get_entry("SNMP", "LOCATION") == {"Location": "Rainer"}
# Add snmp user tests
def test_config_snmp_user_add_invalid_user_name_over_32_characters(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["over_32_characters_community_user", "noAUthNoPRiv", "ro"])
print(result.exit_code)
assert result.exit_code == 1
assert 'FAILED: SNMP user over_32_characters_community_user length should not be greater than 32 characters' \
in result.output
def test_config_snmp_user_add_excluded_special_characters_in_username(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["Test@user", "noAUthNoPRiv", "ro"])
print(result.exit_code)
assert result.exit_code == 1
assert 'FAILED: SNMP user Test@user should not have any of these special symbols' in result.output
def test_config_snmp_user_add_existing_user(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_1", "noAUthNoPRiv", "ro"])
print(result.exit_code)
assert result.exit_code == 14
assert 'SNMP user test_nopriv_RO_1 is already configured' in result.output
def test_config_snmp_user_add_invalid_user_type(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "nopriv", "ro"])
print(result.exit_code)
print(result)
print(result.output)
assert result.exit_code == 2
assert "Invalid user type. Must be one of these one of these three 'noauthnopriv' or 'authnopriv' or 'priv'" in result.output
def test_config_snmp_user_add_invalid_permission_type(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "noauthnopriv", "ab"])
print(result.exit_code)
assert result.exit_code == 3
assert "Invalid community type. Must be either RO or RW" in result.output
def test_config_snmp_user_add_user_type_noauthnopriv_with_unnecessary_auth_type(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "noauthnopriv", "ro", "sha"])
print(result.exit_code)
assert result.exit_code == 4
assert "User auth type not used with 'noAuthNoPriv'. Please use 'AuthNoPriv' or 'Priv' instead" in result.output
def test_config_snmp_user_add_user_type_authnopriv_missing_auth_type(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "authnopriv", "ro"])
print(result.exit_code)
assert result.exit_code == 5
assert "User auth type is missing. Must be MD5, SHA, or HMAC-SHA-2" in result.output
def test_config_snmp_user_add_user_type_authnopriv_missing_auth_password(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "authnopriv", "ro", "sha"])
print(result.exit_code)
assert result.exit_code == 7
assert "User auth password is missing" in result.output
def test_config_snmp_user_add_user_type_authnopriv_with_unnecessary_encrypt_type(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "authnopriv", "ro", "sha", "testauthpass", "DES"])
print(result.exit_code)
assert result.exit_code == 9
assert "User encrypt type not used with 'AuthNoPriv'. Please use 'Priv' instead" in result.output
def test_config_snmp_user_add_user_type_priv_missing_auth_type(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "priv", "ro"])
print(result.exit_code)
assert result.exit_code == 5
assert "User auth type is missing. Must be MD5, SHA, or HMAC-SHA-2" in result.output
def test_config_snmp_user_add_user_type_priv_missing_auth_password(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "priv", "ro", "md5"])
print(result.exit_code)
assert result.exit_code == 7
assert "User auth password is missing" in result.output
def test_config_snmp_user_add_user_type_priv_missing_encrypt_type(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "priv", "ro", "md5", "testauthpass"])
print(result.exit_code)
assert result.exit_code == 10
assert "User encrypt type is missing. Must be DES or AES" in result.output
def test_config_snmp_user_add_user_type_priv_invalid_encrypt_password_over_64_characters(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "priv", "ro", "md5", "testauthpass", "DES",
"superlongencryptionpasswordtotestbeingoverthesixtyfourcharacterlimit"])
print(result.exit_code)
assert result.exit_code == 13
assert "FAILED: SNMP user password length should be not be greater than 64" in result.output
def test_config_snmp_user_add_user_type_priv_invalid_encrypt_password_excluded_special_characters(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "priv", "ro", "md5", "testauthpass", "DES", "testencrypt@pass"])
print(result.exit_code)
assert result.exit_code == 13
assert "FAILED: SNMP user password should not have any of these special symbols" in result.output
def test_config_snmp_user_add_user_type_priv_invalid_encrypt_password_not_long_enough(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "priv", "ro", "md5", "testauthpass", "DES", "test1"])
print(result.exit_code)
assert result.exit_code == 13
assert "FAILED: SNMP user password length should be at least 8 characters" in result.output
def test_config_snmp_user_add_invalid_auth_type(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "authnopriv", "ro", "DM5", "user_auth_pass"])
print(result.exit_code)
assert result.exit_code == 6
assert "Invalid user authentication type. Must be one of these 'MD5', 'SHA', or 'HMAC-SHA-2'" in result.output
def test_config_snmp_user_add_missing_auth_password(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "authnopriv", "ro", "SHA", ""])
print(result.exit_code)
assert result.exit_code == 7
assert 'User auth password is missing' in result.output
def test_config_snmp_user_add_invalid_encrypt_type(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "priv", "ro", "SHA", "user_auth_pass", "EAS", "user_encrypt_pass"])
print(result.exit_code)
assert result.exit_code == 11
assert "Invalid user encryption type. Must be one of these two 'DES' or 'AES'" in result.output
def test_config_snmp_user_add_missing_encrypt_password(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_3", "priv", "ro", "SHA", "user_auth_pass", "AES"])
print(result.exit_code)
assert result.exit_code == 12
assert 'User encrypt password is missing' in result.output
def test_config_snmp_user_add_user_already_existing(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_nopriv_RO_1", "noauthnopriv", "ro"])
print(result.exit_code)
assert result.exit_code == 14
assert 'SNMP user test_nopriv_RO_1 is already configured' in result.output
def test_config_snmp_user_add_valid_user_priv_ro_md5_des(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_priv_RO_7", "priv", "ro", "MD5", "user_auth_pass", "DES", "user_encrypt_pass"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_priv_RO_7 added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_USER", "test_priv_RO_7") == expected_snmp_user_priv_ro_md5_des_config_db_output
def test_config_snmp_user_add_valid_user_priv_ro_md5_aes(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_priv_RO_8", "priv", "ro", "MD5", "user_auth_pass", "AES", "user_encrypt_pass"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_priv_RO_8 added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_USER", "test_priv_RO_8") == expected_snmp_user_priv_ro_md5_aes_config_db_output
def test_config_snmp_user_add_valid_user_priv_ro_sha_des(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_priv_RO_9", "priv", "ro", "SHA", "user_auth_pass", "DES", "user_encrypt_pass"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_priv_RO_9 added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_USER", "test_priv_RO_9") == expected_snmp_user_priv_ro_sha_des_config_db_output
def test_config_snmp_user_add_valid_user_priv_ro_sha_aes(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_priv_RO_10", "priv", "ro", "SHA", "user_auth_pass", "AES", "user_encrypt_pass"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_priv_RO_10 added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_USER", "test_priv_RO_10") == expected_snmp_user_priv_ro_sha_aes_config_db_output
def test_config_snmp_user_add_valid_user_priv_ro_hmac_sha_2_des(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_priv_RO_11", "priv", "ro", "HMAC-SHA-2", "user_auth_pass", "DES", "user_encrypt_pass"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_priv_RO_11 added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_USER", "test_priv_RO_11") == \
expected_snmp_user_priv_ro_hmac_sha_2_des_config_db_output
def test_config_snmp_user_add_valid_user_priv_ro_hmac_sha_2_aes(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_priv_RO_12", "priv", "ro", "HMAC-SHA-2", "user_auth_pass", "AES", "user_encrypt_pass"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_priv_RO_12 added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_USER", "test_priv_RO_12") == \
expected_snmp_user_priv_ro_hmac_sha_2_aes_config_db_output
def test_config_snmp_user_add_valid_user_priv_rw_md5_des(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_priv_RW_7", "priv", "rw", "MD5", "user_auth_pass", "DES", "user_encrypt_pass"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_priv_RW_7 added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_USER", "test_priv_RW_7") == expected_snmp_user_priv_rw_md5_des_config_db_output
def test_config_snmp_user_add_valid_user_priv_rw_md5_aes(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_priv_RW_8", "priv", "rw", "MD5", "user_auth_pass", "AES", "user_encrypt_pass"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_priv_RW_8 added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_USER", "test_priv_RW_8") == expected_snmp_user_priv_rw_md5_aes_config_db_output
def test_config_snmp_user_add_valid_user_priv_rw_sha_des(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_priv_RW_9", "priv", "rw", "SHA", "user_auth_pass", "DES", "user_encrypt_pass"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_priv_RW_9 added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_USER", "test_priv_RW_9") == expected_snmp_user_priv_rw_sha_des_config_db_output
def test_config_snmp_user_add_valid_user_priv_rw_sha_aes(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_priv_RW_10", "priv", "rw", "SHA", "user_auth_pass", "AES", "user_encrypt_pass"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_priv_RW_10 added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_USER", "test_priv_RW_10") == expected_snmp_user_priv_rw_sha_aes_config_db_output
def test_config_snmp_user_add_valid_user_priv_rw_hmac_sha_2_des(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_priv_RW_11", "priv", "rw", "HMAC-SHA-2", "user_auth_pass", "DES", "user_encrypt_pass"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_priv_RW_11 added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_USER", "test_priv_RW_11") == \
expected_snmp_user_priv_rw_hmac_sha_2_des_config_db_output
def test_config_snmp_user_add_valid_user_priv_rw_hmac_sha_2_aes(self):
db = Db()
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["add"],
["test_priv_RW_12", "priv", "rw", "HMAC-SHA-2", "user_auth_pass", "AES", "user_encrypt_pass"], obj=db)
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_priv_RW_12 added to configuration' in result.output
assert db.cfgdb.get_entry("SNMP_USER", "test_priv_RW_12") == \
expected_snmp_user_priv_rw_hmac_sha_2_aes_config_db_output
# Del snmp user tests
def test_config_snmp_user_del_valid_user(self):
runner = CliRunner()
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["del"],
["test_nopriv_RO_1"])
print(result.exit_code)
assert result.exit_code == 0
assert 'SNMP user test_nopriv_RO_1 removed from configuration' in result.output
def test_config_snmp_user_del_invalid_user(self):
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["user"].commands["del"],
["test_nopriv_RO_2"])
print(result.exit_code)
assert result.exit_code == 1
assert 'SNMP user test_nopriv_RO_2 is not configured' in result.output
@pytest.mark.parametrize("invalid_email", ['test@contoso', 'test.contoso.com', 'testcontoso@com',
'123_%contoso.com', '[email protected]'])
def test_is_valid_email(self, invalid_email):
output = config.is_valid_email(invalid_email)
assert output == False
@patch("validated_config_db_connector.device_info.is_yang_config_validation_enabled", mock.Mock(return_value=True))
@patch("config.validated_config_db_connector.ValidatedConfigDBConnector.validated_set_entry", mock.Mock(side_effect=ValueError))
def test_config_snmp_community_add_new_community_with_invalid_type_yang_validation(self):
config.ADHOC_VALIDATION = False
runner = CliRunner()
result = runner.invoke(config.config.commands["snmp"].commands["community"].commands["add"], ["Everest", "RT"])
print(result.exit_code)
assert result.exit_code != 0
assert 'SNMP community configuration failed' in result.output
@classmethod
def teardown_class(cls):
print("TEARDOWN")
os.environ["UTILITIES_UNIT_TESTING"] = "0"