jsnapy icon indicating copy to clipboard operation
jsnapy copied to clipboard

XPath behaviour seems inconsistent

Open gavmckee80 opened this issue 8 years ago • 2 comments

Let me provide a simple example

I want to check if the AS path of a route entry has changed from pre to post snap

tests_include:

  • check_bgp_routes

check_bgp_routes:

  • command: show route extensive protocol bgp
  • iterate: xpath: //route-table/rt/rt-destination/rt-entry tests: - no-diff: /as-path # element in which test is performed err: "WARNING: AS path has changed for route" info: "Checking for changes in AS path"

result = js.check(config_file,"pre","post") *************************** Device: 192.168.56.11 *************************** Tests Included: check_bgp_routes ***************** Command: show route extensive protocol bgp ***************** ERROR!! Nodes are not present in given Xpath: <//route-table/rt/rt-destination/rt-entry> FAIL | All "/as-path" is not same in pre and post snapshot [ 0 matched / 1 failed ] ------------------------------- Final Result!! ------------------------------- Total No of tests passed: 0 Total No of tests failed: 1 Overall Tests failed!!!

However if I load the pre check XML file into a Table widget rt = RouteTable(path='/etc/jsnapy/snapshots/192.168.56.11_pre_show_route_extensive_protocol_bgp.xml').get()

rt.xml.xpath('//route-table/rt/rt-entry/as-path') [<Element as-path at 0x7ffada26f3b0>, <Element as-path at 0x7ffada26fcb0>, <Element as-path at 0x7ffada26f050>, <Element as-path at 0x7ffada26f098>, <Element as-path at 0x7ffada26f170>, <Element as-path at 0x7ffada26f1b8>, <Element as-path at 0x7ffada26fbd8>, <Element as-path at 0x7ffada26ff80>, <Element as-path at 0x7ffada26f2d8>, <Element as-path at 0x7ffada26f0e0>, <Element as-path at 0x7ffada26f488>, <Element as-path at 0x7ffada26f440>, <Element as-path at 0x7ffada26f4d0>, <Element as-path at 0x7ffada26f518>, <Element as-path at 0x7ffada26f560>, <Element as-path at 0x7ffada26f5a8>, <Element as-path at 0x7ffada26f5f0>, <Element as-path at 0x7ffada26ff38>, <Element as-path at 0x7ffada26fe60>, <Element as-path at 0x7ffada26f6c8>, <Element as-path at 0x7ffada26fa28>, <Element as-path at 0x7ffada26fb48>, <Element as-path at 0x7ffada26fd88>, <Element as-path at 0x7ffada26fe18>, <Element as-path at 0x7ffada26f878>, <Element as-path at 0x7ffada26f8c0>, <Element as-path at 0x7ffada26f950>, <Element as-path at 0x7ffada26f908>, <Element as-path at 0x7ffada26f710>, <Element as-path at 0x7ffada26fab8>, <Element as-path at 0x7ffada26fc68>, <Element as-path at 0x7ffada26fb00>, <Element as-path at 0x7ffada26f248>, <Element as-path at 0x7ffada26f368>, <Element as-path at 0x7ffada26f128>, <Element as-path at 0x7ffada26f680>, <Element as-path at 0x7ffada26ffc8>, <Element as-path at 0x7ffada26f830>, <Element as-path at 0x7ffada26f200>, <Element as-path at 0x7ffada26f3f8>, <Element as-path at 0x7ffada272050>, <Element as-path at 0x7ffada272098>]

You can see that the XAPTH I provide returns multiple nodes. Can anyone provide any guidance ?

gavmckee80 avatar Jul 29 '16 15:07 gavmckee80

@gavmckee80 Your xpath expression doesn't look right to me. I believe rt-entry and rt-destination are both direct children of rt.

When I have problems crafting xpath expressions, I use the xpath validator at http://www.xpathtester.com/xpath. Note that you'll probably have to remove XML namespace info. The page claims to support them but I haven't been able to get it working properly.

Removing XML namespace info by hand is difficult for large XML text. Here's an example of how you can use the remove_namespaces function from PyEZ.

jeffbrl avatar Jul 30 '16 13:07 jeffbrl

@jeffbrl , thanks for having a look at this

I usually test my xpath by loading the XML snapshot into a PyEZ table widget and using the

.xml.xpath() command

I'll double check my xpath and test again.

thanks

Gav

gavmckee80 avatar Jul 30 '16 18:07 gavmckee80