-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsnapy_verification
703 lines (577 loc) · 29.5 KB
/
jsnapy_verification
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
https://github.com/Juniper/jsnapy/wiki/4.-Module
mkdir jsnapy_debug
python3.9 -m venv venv
source venv/bin/activate
git clone https://github.com/Juniper/jsnapy.git
cd jsnapy/
Install all the requirements :
pip install -r requirements.txt
or Install the modules :
pip install junos-eznc
pip install colorama
pip install icdiff==1.9.1
setup :
python setup.py sdist
python setup.py install
jsnapy --help
usage:
This tool enables you to capture and audit runtime environment of
networked devices running the Junos operating system (Junos OS)
Tool to capture snapshots and compare them
It supports four subcommands:
--snap, --check, --snapcheck, --diff
1. Take snapshot:
jsnapy --snap pre_snapfile -f main_configfile
2. Compare snapshots:
jsnapy --check post_snapfile pre_snapfile -f main_configfile
3. Compare current configuration:
jsnapy --snapcheck snapfile -f main_configfile
4. Take diff without specifying test case:
jsnapy --diff pre_snapfile post_snapfile -f main_configfile
positional arguments:
pre_snapfile pre snapshot filename
post_snapfile post snapshot filename
optional arguments:
-h, --help show this help message and exit
--snap take the snapshot for commands specified in test file
--check compare pre and post snapshots based on test operators specified in test file
--snapcheck check current snapshot based on test file
--diff display difference between two snapshots
-V, --version displays version
-T TESTFILES [TESTFILES ...], --testfiles TESTFILES [TESTFILES ...]
test file paths
-f FILE, --file FILE config file to take snapshot
--local whether to run snapcheck on local snapshot
--folder FOLDER custom directory path for lookup
-t HOSTNAME, --hostname HOSTNAME
hostname
-p PASSWD, --passwd PASSWD
password to login
-l LOGIN, --login LOGIN
username to login
-P PORT, --port PORT port no to connect to device
-v, --verbosity Set verbosity
-v: Debug level messages
-vv: Info level messages
-vvv: Warning level messages
-vvvv: Error level messages
-vvvvv: Critical level messages
================================================================================================
------------------------------------------------------------------------------------------------
jsnapy verification using config file
------------------------------------------------------------------------------------------------
================================================================================================
1) Create config file with host and test details
hosts:
- device: x.x.x.x
username : xyz
passwd: xyz
port: 22
tests:
- test_sw_version.yml
2) Create test file test_sw_version.yml
tests_include:
- test_sw_version
test_sw_version:
- command: show version
- item:
xpath: '//software-information'
tests:
- all-same: junos-version
err: "Test Failed!!! The versions are not the same. From the PRE snapshot, the version is: <{{pre['junos-version']}}>. From the POST snapshot, the version is <{{post['junos-version']}}>!! "
3) Take the pre snapshot
jsnapy --snap pre -f common_config.yml
Connecting to device x.x.x.x ................
Taking snapshot of COMMAND: show version
4) Take the post snapshot
jsnapy --snap post -f common_config.yml
Connecting to device x.x.x.x ................
Taking snapshot of COMMAND: show version
5) Compare the pre and post snapshots are same or not
jsnapy --check pre post -f common_config.yml
**************************** Device: x.x.x.x ****************************
Tests Included: test_sw_version
*************************** Command: show version ***************************
PASS | Value of all "junos-version" at xpath "//software-information" is same [ 1 value matched ]
------------------------------- Final Result!! -------------------------------
test_sw_version : Passed
Total No of tests passed: 1
Total No of tests failed: 0
Overall Tests passed!!!
6) Take the snapshot and verify the test
jsnapy --snapcheck -f common_config.yml
Connecting to device 10.220.1.222 ................
Taking snapshot of COMMAND: show version
**************************** Device: x.x.x.x ****************************
Tests Included: test_sw_version
*************************** Command: show version ***************************
PASS | Value of all "junos-version" at xpath "//software-information" is same [ 1 value matched ]
------------------------------- Final Result!! -------------------------------
test_sw_version : Passed
Total No of tests passed: 1
Total No of tests failed: 0
Overall Tests passed!!!
7) Get the diff between pre and post snapshots
jsnapy --diff pre post -f common_config.yml -v
jsnapy.cfg file location used : /root/ansible_release_v105_final/venv/etc/jsnapy
Configuration file location used : /root/ansible_release_v105_final/venv/etc/jsnapy
**************************** Device: x.x.x.x ****************************
Tests Included: test_sw_version
*************************** Command: show version ***************************
/root/ansible_release_v105_final/venv/etc/jsnapy/snapshots/x.x.x.x_pre_show_version.xml /root/ansible_release_v105_final/venv/etc/jsnapy/snapshots/x.x.x.x_post_show_version.xml
<software-information><host-name>evoeventtesta</host-name><product-model>mx960</pro <software-information><host-name>evoeventtesta</host-name><product-model>mx960</pro
duct-model><product-name>mx960</product-name><junos-version>22.3X60.28</junos-versi duct-model><product-name>mx960</product-name><junos-version>22.3</junos-version><pa
on><package-information><name>os-kernel</name><package-name>os-kernel-prd-x86-64-20 ckage-information><name>os-kernel</name><package-name>os-kernel-prd-x86-64-20240227
240227.bc22e47_builder_stable_12_223</package-name><comment>JUNOS OS Kernel 64-bit .bc22e47_builder_stable_12_223</package-name><comment>JUNOS OS Kernel 64-bit [2024
8) jsnapy tests with snap filename
jsnapy --snap /root/ansible_release_v105_final/venv/etc/jsnapy/snapshots/pre_show_version.xml -f common_config.yml -v
jsnapy --snap /root/ansible_release_v105_final/venv/etc/jsnapy/snapshots/post_show_version.xml -f common_config.yml -v
jsnapy --check /root/ansible_release_v105_final/venv/etc/jsnapy/snapshots/pre_show_version.xml /root/ansible_release_v105_final/venv/etc/jsnapy/snapshots/post_show_version.xml -f common_config.yml -v
===============================================================================================================================================
-----------------------------------------------------------------------------------------------------------------------------------------------
jsnapy verifcation with interfaces admin-status
-----------------------------------------------------------------------------------------------------------------------------------------------
================================================================================================================================================
1) create config file
cat common_config.yml
hosts:
- device: 10.220.1.222
username : regress
passwd: MaRtInI
port: 22
tests:
- test_loopback_no_diff.yml
2) Create test file
cat test_loopback_no_diff.yml
tests_include:
- test_loopback_admin_status
test_loopback_admin_status:
- command: show interfaces terse lo*
- iterate:
xpath: //logical-interface
id: './name'
tests:
- no-diff: admin-status
err: "Test Failed!! name list changed, <{{pre['admin-status']}}> with name <{{id_0}}>"
info: "Test successful!! name list is same>"
1) Modify the device config
set routing-instances LO10 interface lo0.10
set routing-instances LO10 instance-type virtual-router
set interfaces lo0 unit 10 family inet
commit
2) Take the pre snapshot
jsnapy --snap pre -f common_config.yml
Connecting to device x.x.x.x ................
Taking snapshot of COMMAND: show interfaces terse lo*
3) Deactivate the interface config
deactivate interface lo0
commit
4) Take the post snapshot
jsnapy --snap post -f common_config.yml
Connecting to device x.x.x.x ................
Taking snapshot of COMMAND: show interfaces terse lo*
5) Compare the pre and post snapshots
jsnapy --check post pre -f common_config.yml
**************************** Device: x.x.x.x ****************************
Tests Included: test_loopback_admin_status
********************* Command: show interfaces terse lo* *********************
Test Failed!! name list changed, <None> with name <['lo0.10']>
FAIL | All "admin-status" is not same in pre and post snapshot [ 3 value matched / 1 value failed ]
------------------------------- Final Result!! -------------------------------
test_loopback_admin_status : Failed
Total No of tests passed: 0
Total No of tests failed: 1
Overall Tests failed!!!
=============================================================================================================================
=============================================================================================================================
Config.yml file with default port 830
~/jsnapy_debug/jsnapy/samples# vi config_check.yml
hosts:
- device: xx.xx.xx.xx
username : xyz
passwd: xyz
tests:
- test_no_diff.yml
~/jsnapy_debug/jsnapy/samples# vi test_no_diff.yml
tests_include:
- test_command_version
Config.yml with port 22
~/jsnapy_debug/jsnapy/samples# vi config_check.yml
hosts:
- device: xx.xx.xx.xx
username : xyz
passwd: xyz
port: 22
tests:
- test_no_diff.yml
# use '/' in your test cases apart from xpath if u want to search all elements irrespective of hierarchy, ex: in id if u use /name instead of name
# then it will search in all the names in given xpath irrespective of their position
# for simple, one test using command
test_command_version:
- command: show interfaces terse lo*
- iterate:
xpath: physical-interface
id: './name'
tests:
- no-diff: oper-status # element in which test is performed
err: "Test Failed!! oper-status got changed, before it was <{{pre['oper-status']}}>, now it is <{{post['oper-status']}}> with name <{{id_0}}> and admin status <{{post['admin-status']}}>
"
info: "Test succeeded!! oper-status is same with value, before it is <{{pre['oper-status']}}> now it is <{{post['oper-status']}}> with admin status <{{post['admin-status']}}> "
jsnapy --snap pre -f config_check.yml
jsnapy --snap post -f config_check.yml
jsnapy --check pre post -f config_check.yml
/var/logs/jsnapy# ls -lrt
total 20
-rwxr-xr-x 1 root root 0 Jan 14 10:13 __init__.py
-rwxr-xr-x 1 root root 0 Jan 14 10:13 info.log
-rwxr-xr-x 1 root root 0 Jan 14 10:13 errors.log
-rwxr-xr-x 1 root root 0 Jan 14 10:13 debug.log
-rwxr-xr-x 1 root root 0 Jan 14 10:13 critical.log
-rw-r--r-- 1 root root 1020 Jan 14 11:45 jsnapy.log.4
-rw-r--r-- 1 root root 934 Jan 14 11:45 jsnapy.log.3
-rw-r--r-- 1 root root 966 Jan 14 11:46 jsnapy.log.2
-rw-r--r-- 1 root root 969 Jan 14 11:46 jsnapy.log.1
-rw-r--r-- 1 root root 368 Jan 14 11:46 jsnapy.log
etc/jsnapy# ls
jsnapy.cfg logging.yml snapshots testfiles
=====================================================================================================
Errors:
=====================================================================================================
~/jsnapy_issue_392/jsnapy# jsnapy --help
Traceback (most recent call last):
File "/root/jsnapy_issue_392/venv/bin/jsnapy", line 33, in <module>
sys.exit(load_entry_point('jsnapy==1.3.6', 'console_scripts', 'jsnapy')())
File "/root/jsnapy_issue_392/venv/bin/jsnapy", line 25, in importlib_load_entry_point
return next(matches).load()
File "/usr/local/lib/python3.9/importlib/metadata.py", line 77, in load
module = import_module(match.group('module'))
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/root/jsnapy_issue_392/venv/lib/python3.9/site-packages/jnpr/jsnapy/__init__.py", line 83, in <module>
from jnpr.jsnapy.jsnapy import SnapAdmin
File "/root/jsnapy_issue_392/venv/lib/python3.9/site-packages/jnpr/jsnapy/jsnapy.py", line 24, in <module>
from jnpr.junos import Device
ModuleNotFoundError: No module named 'jnpr.junos'
Fix :
unistall and install junos-eznc
:~/jsnapy_issue_392# python -m pip uninstall junos-eznc
Found existing installation: junos-eznc 2.6.3
Uninstalling junos-eznc-2.6.3:
Would remove:
/root/jsnapy_issue_392/venv/lib/python3.9/site-packages/junos_eznc-2.6.3-py3.7-nspkg.pth
/root/jsnapy_issue_392/venv/lib/python3.9/site-packages/junos_eznc-2.6.3.dist-info/*
Proceed (y/n)? y
Successfully uninstalled junos-eznc-2.6.3
~/jsnapy_issue_392# python -m pip install junos-eznc
Collecting junos-eznc
Using cached junos_eznc-2.6.3-py2.py3-none-any.whl (196 kB)
Requirement already satisfied: yamlordereddictloader in ./venv/lib/python3.9/site-packages (from junos-eznc) (0.4.0)
Requirement already satisfied: transitions in ./venv/lib/python3.9/site-packages (from junos-eznc) (0.8.11)
Requirement already satisfied: paramiko>=1.15.2 in ./venv/lib/python3.9/site-packages (from junos-eznc) (2.10.3)
Requirement already satisfied: PyYAML>=5.1 in ./venv/lib/python3.9/site-packages (from junos-eznc) (6.0)
Requirement already satisfied: six in ./venv/lib/python3.9/site-packages (from junos-eznc) (1.16.0)
Requirement already satisfied: lxml>=3.2.4 in ./venv/lib/python3.9/site-packages (from junos-eznc) (4.8.0)
Requirement already satisfied: netaddr in ./venv/lib/python3.9/site-packages (from junos-eznc) (0.8.0)
Requirement already satisfied: pyserial in ./venv/lib/python3.9/site-packages (from junos-eznc) (3.5)
Requirement already satisfied: pyparsing in ./venv/lib/python3.9/site-packages (from junos-eznc) (3.0.7)
Requirement already satisfied: jinja2>=2.7.1 in ./venv/lib/python3.9/site-packages (from junos-eznc) (3.1.1)
Requirement already satisfied: scp>=0.7.0 in ./venv/lib/python3.9/site-packages (from junos-eznc) (0.14.4)
Requirement already satisfied: ncclient==0.6.9 in ./venv/lib/python3.9/site-packages (from junos-eznc) (0.6.9)
Requirement already satisfied: setuptools>0.6 in ./venv/lib/python3.9/site-packages (from ncclient==0.6.9->junos-eznc) (56.0.0)
Requirement already satisfied: MarkupSafe>=2.0 in ./venv/lib/python3.9/site-packages (from jinja2>=2.7.1->junos-eznc) (2.1.1)
Requirement already satisfied: cryptography>=2.5 in ./venv/lib/python3.9/site-packages (from paramiko>=1.15.2->junos-eznc) (36.0.2)
Requirement already satisfied: pynacl>=1.0.1 in ./venv/lib/python3.9/site-packages (from paramiko>=1.15.2->junos-eznc) (1.5.0)
Requirement already satisfied: bcrypt>=3.1.3 in ./venv/lib/python3.9/site-packages (from paramiko>=1.15.2->junos-eznc) (3.2.0)
Requirement already satisfied: cffi>=1.1 in ./venv/lib/python3.9/site-packages (from bcrypt>=3.1.3->paramiko>=1.15.2->junos-eznc) (1.15.0)
Requirement already satisfied: pycparser in ./venv/lib/python3.9/site-packages (from cffi>=1.1->bcrypt>=3.1.3->paramiko>=1.15.2->junos-eznc) (2.21)
Installing collected packages: junos-eznc
Successfully installed junos-eznc-2.6.3
WARNING: You are using pip version 21.1.3; however, version 22.0.4 is available.
You should consider upgrading via the '/root/jsnapy_issue_392/venv/bin/python -m pip install --upgrade pip' command.
jsnapy --help
usage:
This tool enables you to capture and audit runtime environment of
networked devices running the Junos operating system (Junos OS)
Tool to capture snapshots and compare them
It supports four subcommands:
--snap, --check, --snapcheck, --diff
1. Take snapshot:
jsnapy --snap pre_snapfile -f main_configfile
2. Compare snapshots:
jsnapy --check post_snapfile pre_snapfile -f main_configfile
3. Compare current configuration:
jsnapy --snapcheck snapfile -f main_configfile
4. Take diff without specifying test case:
jsnapy --diff pre_snapfile post_snapfile -f main_configfile
positional arguments:
pre_snapfile pre snapshot filename
post_snapfile post snapshot filename
optional arguments:
-h, --help show this help message and exit
--snap take the snapshot for commands specified in test file
--check compare pre and post snapshots based on test operators specified in test file
--snapcheck check current snapshot based on test file
--diff display difference between two snapshots
-V, --version displays version
-T TESTFILES [TESTFILES ...], --testfiles TESTFILES [TESTFILES ...]
test file paths
-f FILE, --file FILE config file to take snapshot
--local whether to run snapcheck on local snapshot
--folder FOLDER custom directory path for lookup
-t HOSTNAME, --hostname HOSTNAME
hostname
-p PASSWD, --passwd PASSWD
password to login
-l LOGIN, --login LOGIN
username to login
-P PORT, --port PORT port no to connect to device
-v, --verbosity Set verbosity
-v: Debug level messages
-vv: Info level messages
-vvv: Warning level messages
-vvvv: Error level messages
-vvvvv: Critical level messages
===================================================================================================================
Fix for mising logging.yml file :
pip3 install -U jsnapy==1.3.7 --no-binary :all: --no-cache-dir
To check with predefined snapshot filenames instead of pre and post :
cat junos_jsnapy/common_config.yml
hosts:
- device: x.x.x.x
username : xyz
passwd: xyz
port: 22
tests:
- junos_jsnapy/test_junos_storage.yml
cat junos_jsnapy/test_junos_storage.yml
tests_include:
- check_storage
check_storage:
- command: show system storage
- iterate:
xpath: //system-storage-information/filesystem[normalize-space(mounted-on)='/.mount']
tests:
- is-lt: used-percent, 95
info: "File system {{post['mounted-on']}} use less than 95%"
err: "File system {{post['mounted-on']}} use {{post['used-percent']}} %"
~/ansible_release_test5/ansible-junos-stdlib/tests# jsnapy --snapcheck /root/ansible_release_test5/venv/etc/jsnapy/snapshots/test1_pre_show_system_storage.xml -f junos_jsnapy/common_config.yml -v
jsnapy.cfg file location used : /root/ansible_release_test5/venv/etc/jsnapy
Configuration file location used : /root/ansible_release_test5/venv/etc/jsnapy
Connecting to device x.x.x.x ................
Tests Included: check_storage
Taking snapshot of COMMAND: show system storage
**************************** Device: x.x.x.x ****************************
Tests Included: check_storage
************************ Command: show system storage ************************
-----------------------Performing is-lt Test Operation-----------------------
File system /.mount use less than 95%
PASS | All "used-percent" is less than 95 [ 1 value matched ]
------------------------------- Final Result!! -------------------------------
check_storage : Passed
Total No of tests passed: 1
Total No of tests failed: 0
Overall Tests passed!!!
:~/ansible_release_test5/ansible-junos-stdlib/tests# jsnapy --check /root/ansible_release_test5/venv/etc/jsnapy/snapshots/test1_pre_show_system_storage.xml /root/ansible_release_test5/venv/etc/jsnapy/snapshots/test1_pre_show_system_storage.xml -f junos_jsnapy/common_config.yml -v
jsnapy.cfg file location used : /root/ansible_release_test5/venv/etc/jsnapy
Configuration file location used : /root/ansible_release_test5/venv/etc/jsnapy
**************************** Device: x.x.x.x ****************************
Tests Included: check_storage
************************ Command: show system storage ************************
-----------------------Performing is-lt Test Operation-----------------------
File system /.mount use less than 95%
PASS | All "used-percent" is less than 95 [ 1 value matched ]
------------------------------- Final Result!! -------------------------------
check_storage : Passed
Total No of tests passed: 1
Total No of tests failed: 0
Overall Tests passed!!!
:~/ansible_release_test5/ansible-junos-stdlib/tests# cat test.py
from jnpr.jsnapy import SnapAdmin
from pprint import pprint
from jnpr.junos import Device
js = SnapAdmin()
config_file = "/root/ansible_release_test5/ansible-junos-stdlib/tests/junos_jsnapy/common_config.yml"
js.snap(config_file, "/root/ansible_release_test5/venv/etc/jsnapy/snapshots/pre_test1.xml")
js.snap(config_file, "/root/ansible_release_test5/venv/etc/jsnapy/snapshots/post_test1.xml")
chk = js.check(config_file, "/root/ansible_release_test5/venv/etc/jsnapy/snapshots/pre_test1.xml", "/root/ansible_release_test5/venv/etc/jsnapy/snapshots/post_test1.xml")
for check in chk:
print ("Tested on", check.device)
print ("Final result: ", check.result)
print ("Total passed: ", check.no_passed)
print ("Total failed:", check.no_failed)
pprint(check.test_results)
~/ansible_release_test5/ansible-junos-stdlib/tests# python test.py
Connecting to device x.x.x.x ................
Taking snapshot of COMMAND: show system storage
Connecting to device x.x.x.x ................
Taking snapshot of COMMAND: show system storage
**************************** Device: x.x.x.x ****************************
Tests Included: check_storage
************************ Command: show system storage ************************
PASS | All "used-percent" is less than 95 [ 1 value matched ]
------------------------------- Final Result!! -------------------------------
check_storage : Passed
Total No of tests passed: 1
Total No of tests failed: 0
Overall Tests passed!!!
Tested on x.x.x.x
Final result: Passed
Total passed: 1
Total failed: 0
{'show system storage': [{'count': {'fail': 0, 'pass': 1},
'expected_node_value': 95.0,
'failed': [],
'node_name': 'used-percent',
'passed': [{'actual_node_value': '61',
'id': {},
'message': 'File system /.mount use less '
'than 95%',
'post': {'mounted-on': '/.mount',
'used-percent': '61'},
'pre': {'used-percent': '61'}}],
'result': True,
'test_name': 'check_storage',
'testoperation': 'is-lt',
'xpath': "//system-storage-information/filesystem[normalize-space(mounted-on)='/.mount']"}]}
~/ansible_release_test5/ansible-junos-stdlib/tests# cat test1.py
from jnpr.jsnapy import SnapAdmin
from pprint import pprint
from jnpr.junos import Device
js = SnapAdmin()
config_file = "/root/ansible_release_test5/ansible-junos-stdlib/tests/junos_jsnapy/common_config.yml"
#js.snap(config_file, "/root/ansible_release_test5/venv/etc/jsnapy/snapshots/pre_test1.xml")
#js.snap(config_file, "/root/ansible_release_test5/venv/etc/jsnapy/snapshots/post_test1.xml")
chk = js.snapcheck(config_file, "/root/ansible_release_test5/venv/etc/jsnapy/snapshots/pre_test1.xml")
for check in chk:
print ("Tested on", check.device)
print ("Final result: ", check.result)
print ("Total passed: ", check.no_passed)
print ("Total failed:", check.no_failed)
pprint(check.test_results)
:~/ansible_release_test5/ansible-junos-stdlib/tests# python test1.py
Connecting to device x.x.x.x ................
Taking snapshot of COMMAND: show system storage
**************************** Device: x.x.x.x ****************************
Tests Included: check_storage
************************ Command: show system storage ************************
PASS | All "used-percent" is less than 95 [ 1 value matched ]
------------------------------- Final Result!! -------------------------------
check_storage : Passed
Total No of tests passed: 1
Total No of tests failed: 0
Overall Tests passed!!!
Tested on x.x.x.x
Final result: Passed
Total passed: 1
Total failed: 0
{'show system storage': [{'count': {'fail': 0, 'pass': 1},
'expected_node_value': 95.0,
'failed': [],
'node_name': 'used-percent',
'passed': [{'actual_node_value': '61',
'id': {},
'message': 'File system /.mount use less '
'than 95%',
'post': {'mounted-on': '/.mount',
'used-percent': '61'},
'pre': {'used-percent': '61'}}],
'result': True,
'test_name': 'check_storage',
'testoperation': 'is-lt',
'xpath': "//system-storage-information/filesystem[normalize-space(mounted-on)='/.mount']"}]}
--------------------------------------------------------------------------------------------------------------------------------------------
Example:
cat common_config.yml
tests:
- test_snmp.yml
#can send mail by specifying:
#mail: send_mail.yml
cat test_snmp.yml
tests_include:
- test_snmp_config
test_snmp_config:
- rpc: get-configuration
- iterate:
xpath: /configuration/snmp
tests:
- exists: community[name='mycommunity']
info: SNMP community ''mycommunity' is configured.
err: SNMP community ''mycommunity' is not configured!
Without device handle
from jnpr.jsnapy import SnapAdmin
from pprint import pprint
from jnpr.junos import Device
js = SnapAdmin()
config_file = "/root/jsnapy_test1/jsnapy/common_config.yml"
#js.snap(config_file, "pre")
#js.snap(config_file, "post")
chk = js.snapcheck(config_file)
#chk = js.check(config_file, "pre", "post")
for check in chk:
print ("Tested on", check.device)
print ("Final result: ", check.result)
print ("Total passed: ", check.no_passed)
print ("Total failed:", check.no_failed)
print(check.test_details['get-configuration'])
#print(type(check))
#pprint(dir(check))
with Dev handle:
from jnpr.jsnapy import SnapAdmin
from pprint import pprint
from jnpr.junos import Device
dev_obj = Device(host='x.x.x.x, user='xyz', password='xyz')
dev_obj.open()
js = SnapAdmin()
config_file = "common_config.yml"
snapvalue = js.snapcheck(config_file, "pre", dev=dev_obj)
for snapcheck in snapvalue:
print ("\n -----------snapcheck----------")
print ("Tested on", snapcheck.device)
print ("Final result: ", snapcheck.result)
print ("Total passed: ", snapcheck.no_passed)
print ("Total failed:", snapcheck.no_failed)
pprint(snapcheck.test_results)
---------------------------------------------------------------------
cat test_interface.yml
tests_include:
- test_interface
# use '/' in your test cases apart from xpath if u want to search all elements irrespective of hierarchy, ex: in id if u use /name instead of name
# then it will search in all the names in given xpath irrespective of their position
# for simple, one test using command
test_interface:
- command: show interfaces terse
- iterate:
xpath: '//physical-interface'
id: './name'
tests:
- no-diff: oper-status # element in which test is performed
err: "Test Failed!! oper-status got changed, before it was <{{pre['oper-status']}}>, now it is <{{post['oper-status']}}>"
info: "oper-status is same before it was <{{pre['oper-status']}}>, now it is <{{post['oper-status']}}>"
- list-not-less: oper-status
err: "name list changed, oper-state: <{{pre['oper-state']}}> with name <{{id_0}}> is not present in post-snap"
info: "name list is same, oper-state is present in pre-snap: <{{pre['oper-status']}}> and post-snap: <{{post['oper-status']}}>"
- list-not-more: name
err: "<{{post['name']}}> name is not present in pre snapshot"
info: "name is same, before it was <{{pre['name']}}>, now it is <{{post['name']}}>"
from jnpr.jsnapy import SnapAdmin
from pprint import pprint
from jnpr.junos import Device
dev_obj = Device(host='x.x.x.x', user='xyz', password='xyz')
dev_obj.open()
config_data = {'tests': ['TEST_FILES/test_interface.yml']}
js = SnapAdmin()
snapvalue = js.snap(config_data, "pre", dev=dev_obj)
python test_snmp_dev.py
Taking snapshot of COMMAND: show interfaces terse
~/ansible_release_v105_final/jsnapy/tests# ls ~/ansible_release_v105_final/venv/etc/jsnapy/snapshots/
10.220.24.28_pre_show_interfaces_terse.xml README