Junos Automation (Scripting)
Highlighted
Junos Automation (Scripting)

Error in jsnapy

‎04-04-2019 07:59 PM

Hi all,

When executing the config_snap.yml on windows 10, it gives the following "ERROR!! KeyError".  How can be fixed this error?

 

H:\>jsnapy --snap pre_snapfile.yml -f config_snap.yml
ERROR!! KeyError
Complete Message: 0

H:\>

 

The Config_snap.yml file is under the C:\Users\user1\jsnapy>

Config_snap.yml

hosts:
-device: X.X.X.X
-username: demo
-passwd: demo
tests:
-test_is_equal.yml

------------------------------------------------

The test_is_equal.yml file is under the C:\Users\user1\jsnapy\testfiles>

 

test_interfaces_terse:
- command: show interfaces terse ge*
-item:
id: ./name
xpath: //pyhsical-interface[nomalize-space(name)] = "ge-0/0/1"]
tests:
- is-equal: admin-status, up
info: "Test Succeeded !!! admin-status is equal to <{{post['admin-status']}}> with oper-status
<{{pre['oper-status']}}>"
err: "Test failed !!! admin-status is equal to down, it is <{{post['admin-status']}}> with oper-status
<{{pre['oper-status']}}>"

-------------------------------------------------------

The jsnapy.cg file has got the followings:

config_file_path = C:\user1\eeker\jsnapy
snapshot_path = C:\Users\user1\jsnapy\snapshots
test_file_path = C:\Users\user1\jsnapy\testfiles

 

 

 

H:\>
H:\>pip list
Package Version
------------ ----------
asn1crypto 0.24.0
bcrypt 3.1.6
cffi 1.12.2
colorama 0.4.1
configparser 3.7.4
cryptography 2.6.1
future 0.17.1
icdiff 1.9.1
Jinja2 2.10
jsnapy 1.3.2
junos-eznc 2.2.1.dev0
lxml 4.3.2
MarkupSafe 1.1.1
ncclient 0.6.3
netaddr 0.7.19
paramiko 2.4.2
pip 19.0.3
pyasn1 0.4.5
pycparser 2.19
PyNaCl 1.3.0
pyparsing 2.3.1
pyserial 3.4
PyYAML 5.1
scp 0.13.2
selectors2 2.0.1
setuptools 40.8.0
six 1.12.0

H:\>

 

Thx.

Arix

9 REPLIES 9
Junos Automation (Scripting)

Re: Error in jsnapy

‎04-04-2019 08:12 PM

check the square backets in xpath

 

xpath: //pyhsical-interface[nomalize-space(name)] = "ge-0/0/1"]

 

Junos Automation (Scripting)

Re: Error in jsnapy

‎04-04-2019 08:48 PM

That is right.....I corrected it.... But the error is still the same...

 

H:\>jsnapy --snap pre_snapfile.yml -f config_snap.yml
ERROR!! KeyError
Complete Message: 0

Junos Automation (Scripting)

Re: Error in jsnapy

[ Edited ]
‎04-09-2019 11:26 AM

Hi Arix.

 

It looks to me that there is an incorrectly defined data structure in "config_snap.yml" file (your input is not well formatted in post). Based on instructions on JSNAPY GitHub web page (https://github.com/Juniper/jsnapy/wiki/2.-Writing-test-and-config-file) the YAML file should look something like this:

 

---
hosts:
  - device: X.X.X.X
    username: demo
    passwd: demo
tests:
  - test_is_equal.yml

 

The difference against your input is that the "username" and "passwd" haven't "dash" sign in front of them.  This does make different data structure (one item with three key/value pair dictionary instead of list with three items). Please mind the proper indentation (space only - no tabulator) as well.

 

There are two small typos inside your xpath value yet:

pyhsical-interface  -> physical-interface

nomalize-space(name)  -> normalize-space(name)]

 

Adding whole test_is_equal.yml with formatting:

 

---
test_interfaces_terse:
- command: show interfaces terse ge*
- item:
     id: ./name
     xpath: //physical-interface[normalize-space(name) = "ge-0/0/1"]
     tests:
       - is-equal: admin-status, up
         info: "Test Succeeded !!! admin-status is equal to <{{post['admin-status']}}> with oper-status <{{pre['oper-status']}}>"
         err: "Test failed !!! admin-status is equal to down, it is <{{post['admin-status']}}> with oper-status <{{pre['oper-status']}}>"

 

The output of snapshot creation and test result on my machine (Ubuntu 18.04) just for completness:

 

mat@ubuntu-autom:~/python/jsnapy$ jsnapy  --snap pre_snapfile.yml -f config_snap.yml
Connecting to device SRX1 ................
Taking snapshot of COMMAND: show interfaces terse ge*

mat@ubuntu-autom:~/python/jsnapy$ jsnapy  --snap post_snapfile.yml -f config_snap.yml
Connecting to device SRX1 ................
Taking snapshot of COMMAND: show interfaces terse ge*

mat@ubuntu-autom:~/python/jsnapy$ jsnapy  --check pre_snapfile.yml post_snapfile.yml -f config_snap.yml
******************************** Device: SRX1 ********************************
Tests Included: test_interfaces_terse
********************* Command: show interfaces terse ge* *********************
PASS | All "admin-status" is equal to "up" [ 1 matched ]
------------------------------- Final Result!! -------------------------------
test_interfaces_terse : Passed
Total No of tests passed: 1
Total No of tests failed: 0
Overall Tests passed!!!
mat@ubuntu-autom:~/python/jsnapy$

 

Please try to adjust formatting of your  YAML files "config_snap.yml" and "test_is_equal.yml". Run the tests again and let us know the results.

 

In case of troubles please paste the content of both YAML files using "insert code" section to keep formatting. We will try to assist you further.

 

Please mark thread as solved once issues are gone so community users can benefit from solution.

 

 

Regards
Luděk Matoušek
JNCIS-ENT, JNCIS-SP, JNCIP-SEC, JNCIA-DevOps
Junos Automation (Scripting)

Re: Error in jsnapy

‎04-09-2019 06:55 PM

Hi Luděk,

Many thanks for reply and help.

Unfortunately it didn't work..I am using Windows 10. The following error gives again.  I am not sure what is the wrong in  line 7, column 12 in Config_snap.yml file:

 

c:\Users\erix\jsnapy\snapshots>jsnapy --snap pre_snapfile.yml -f config_snap.yml
ERROR!! YAML file not defined properly,
Complete Message: mapping values are not allowed here
in "C:\Users\erix\jsnapy\config_snap.yml", line 7, column 12

c:\Users\erix\jsnapy\snapshots>

---------------------------------------

Config_snap.yml file:

 

hosts:
-device: x.x.x.x.x

   username : demo
   passwd: demo
tests:
-test_is_equal.yml

-------------------------------------------

Test file (-test_is_equal.yml):

 

test_interfaces_terse:
- command: show interfaces terse ge*
- item:
    id: ./name
    xpath: //physical-interface[normalize-space(name) = "ge-0/0/1"]
tests:
  - is-equal: admin-status, up
      info: "Test Succeeded !! admin-status is equal to <{{post['admin- status']}}> with oper-status<{{pre['oper-status']}}>"
      err: "Test Failed !! admin-status is not equal to down, it is <{{post['admin- status']}}> with oper-status <{{pre['oper-status']}}>"

 

Thanks,

Arix

Junos Automation (Scripting)

Re: Error in jsnapy

[ Edited ]
‎04-10-2019 05:16 AM

Hi Arix.

 

I receive same error like you do when I copy & paste content of your "config_snap.yml"file.

 

The issue is caused by extra space between username keyword and colon.


@Erdix wrote:

   username : demo


 

It should be without space:

 username: demo

 

I found other formatting issues with spacing (YAML is quite sensitive about it).

 

I was able to run jsnapy test successfully after fixing this and the other formatting issues (my Windows 10 machine this time). 

I am attaching both YAML file in ZIP archive for you.  You can compare your files with mine and adjust your files accordingly.

 

Please test and let us know if it helped you.

 

My other machine (physical):

Windows 10 version 1803 build 17134.648

Python 2.7.16  (x64)

 

c:\>pip list
Package Version
------------ -------
asn1crypto 0.24.0
bcrypt 3.1.6
cffi 1.12.2
colorama 0.4.1
configparser 3.7.4
cryptography 2.6.1
enum34 1.1.6
future 0.17.1
icdiff 1.9.1
ipaddress 1.0.22
Jinja2 2.10.1
jsnapy 1.3.2
junos-eznc 2.2.0
lxml 4.3.3
MarkupSafe 1.1.1
ncclient 0.6.4
netaddr 0.7.19
paramiko 2.4.2
pip 18.1
pyasn1 0.4.5
pycparser 2.19
PyNaCl 1.3.0
pyparsing 2.4.0
pyserial 3.4
PyYAML 5.1
scp 0.13.2
selectors2 2.0.1
setuptools 40.6.2
six 1.12.0

 

 

Regards
Luděk Matoušek
JNCIS-ENT, JNCIS-SP, JNCIP-SEC, JNCIA-DevOps

Attachments

Junos Automation (Scripting)

Re: Error in jsnapy

‎04-10-2019 06:04 PM

Hi Luděk,

It is really frusrating mate... Now another error below is happening. ....

Your Python version is different than mine! Mine is 3.7.3rc1 .....

After correction on config file, now another error occurs:

I couldn't figure out, any idea or?

 

c:\Users\erix\jsnapy\snapshots>jsnapy --snap pre_snapfile.yml -f config_snap.yml
ERROR!! TypeError
Complete Message: 'NoneType' object is not subscriptable

c:\Users\erix\jsnapy\snapshots>

 

My system is Windows 10 Version 1803(OS build 17134.590).

H:\>py
Python 3.7.3rc1 (tags/v3.7.3rc1:69785b2127, Mar 12 2019, 22:37:55) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Thanks for help.

Junos Automation (Scripting)

Re: Error in jsnapy

‎04-11-2019 01:52 AM

Hi Arix.

 

I have uninstalled Python 2.7 and installed the Python 3 of same version you have (3.7.3rc1) on my Windows 10 machine. I was unable to reproduce the error.  Jsnapy CLI command works like a charm on my Win 10 machine. This is really odd.

 

My educated guess is some formatting error in YAML or the device does return empty value (type None) for some key.

Do you get some more information from Jsnapy when running it with debug flag please?

jsnapy -v --snap pre_snapfile.yml -f config_snap.yml

 

 

Regards
Luděk Matoušek
JNCIS-ENT, JNCIS-SP, JNCIP-SEC, JNCIA-DevOps
Junos Automation (Scripting)

Re: Error in jsnapy

[ Edited ]
‎04-14-2019 06:55 PM

hi Ludek,

Sorry for the late reply..

C:\Users\erix\jsnapy\snapshots>jsnapy -v --snap pre_snapfile.yml -f config_snap.yml
jsnapy.cfg file location used : C:\Users\erix\jsnapy
Configuration file location used : C:\Users\erix\jsnapy
ERROR!! TypeError
Complete Message: 'NoneType' object is not subscriptable

C:\Users\erix\jsnapy\snapshots>

 

I got a chance to read the "Day One: Enabling Automated Network Verifications with JSNAPY". And the page of 12, under the "JSNAP and JSNAPy" it says that jsnapy is based on Python 2.7.... I think this is the reason jsnapy is not operational in Python 3.7 environment yet...

I also opened a case with github.com/juniper/jsnapy to see what master people are going to comments...

 

Thanks,

 

Junos Automation (Scripting)

Re: Error in jsnapy

‎05-10-2019 04:17 AM

Hi Arix.


Reviving a silent thread.


I have checked your issue report on Github (https://github.com/Juniper/jsnapy/issues/336) and you weren't able to resolve issue yet.

 

I was using the Jsnapy code with Python 3.7 (no Python 2.x installed) on my Windows 10 machine without issues. Hence there should be no problem using Jsnapy with Python 3. I believe the used libraries (e.g. PyEZ) were already ported to support Python 3 because of planned sunset of Python 2 for 2020 (see more on PEP-0373 page).


Let's try some steps in order to pin point the cause.

1) What does show the CLI output of command "show interfaces terse ge*" on  your test device? Anything odd observed, especially related to interface ge-0/0/1?

2) Did you tried by any chance to run the Jsnapy code against another Junos running device? Was the Jsnapy returning same error?

3) Did you try to run the Jsnapy code from Ubuntu host against your test device like suggested on GitHub? What was the result?

 

 

 

Regards
Luděk Matoušek
JNCIS-ENT, JNCIS-SP, JNCIP-SEC, JNCIA-DevOps