Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![PyPi Version](https://img.shields.io/pypi/v/jsnapy.svg)](https://pypi.python.org/pypi/jsnapy/)
[![PyPi Version](https://img.shields.io/pypi/v/jsnapy.svg)](https://pypi.python.org/pypi/jsnapy/)
[![Coverage Status](https://travis-ci.org/Juniper/jsnapy.svg?branch=master)](https://travis-ci.org/Juniper/jsnapy)
[![Coverage Status](https://coveralls.io/repos/github/Juniper/jsnapy/badge.svg?branch=master)](https://coveralls.io/github/Juniper/jsnapy?branch=master)

Expand Down
74 changes: 45 additions & 29 deletions lib/jnpr/jsnapy/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@
# All rights reserved.
#

import base64
import hashlib
import json
import logging
import os
import re
import sys
from os.path import expanduser

import colorama
import logging
import yaml
from icdiff import diff, codec_print, get_options, ConsoleDiff
from lxml import etree

from jnpr.jsnapy import get_path
from jnpr.jsnapy.operator import Operator
from jnpr.jsnapy.sqlite_get import SqliteExtractXml
from icdiff import diff, codec_print, get_options, ConsoleDiff
from jnpr.jsnapy.xml_comparator import XmlComparator
from jnpr.jsnapy import get_path
import hashlib
import json
import base64
from os.path import expanduser


class Comparator:

def __init__(self):
self.logger_check = logging.getLogger(__name__)
self.log_detail = {'hostname': None}


def is_op(self, op):
"""
Expand Down Expand Up @@ -73,7 +75,7 @@ def generate_snap_file(self, device, prefix, name, reply_format):
'snapshot_path')),
sfile)
return snapfile

def splitter(self,value):
f = lambda x: x.split(']')[1].count(',') if '[' in x and ']' in x else x.count(',')
value_list = [x[::-1].strip() for x in value[::-1].split(",",f(value))][::-1]
Expand Down Expand Up @@ -174,9 +176,9 @@ def expression_evaluator(self, elem_test, op, x_path, id_list, iter, teston,
Analyze the given elementary test case and call the appopriate operator
like is_equal() or no_diff()
call operator.Operator methods to compare snapshots based on given test cases
:param elem_test: elementary test operation dictionary
:param elem_test: elementary test operation dictionary
:param op: operator.Operator object
:param x_path: xpath for the command/rpc
:param x_path: xpath for the command/rpc
:param id_list: id list of elements to use while matching up in different snapshots
:param iter: True if iterate is specified in the test file
:param teston: command/rpc to perform test
Expand Down Expand Up @@ -286,7 +288,7 @@ def expression_builder(self, sub_expr, parent_op=None, **kwargs):
Recursively builds the boolean expression of the provided sub_expr for evaluation
:param sub_expr: dictionary object of the sub_expr that needs to be converted
:param parent_op: parent operator of the sub_expr
:param kwargs: dictionary of arguments required by function Comparator.expression_evaluator
:param kwargs: dictionary of arguments required by function Comparator.expression_evaluator
:return: str object of the boolean expression formed of the provided sub_expr
"""
ret_expr = []
Expand All @@ -295,8 +297,8 @@ def expression_builder(self, sub_expr, parent_op=None, **kwargs):
or ( len(sub_expr) < 2 and self.is_binary_op(parent_op))):
self.logger_check.info(
colorama.Fore.RED +
"ERROR!!! Malformed sub-expression", extra=self.log_detail)
return
"ERROR!!! Malformed sub-expression", extra=self.log_detail)
return
for elem in sub_expr:
keys = list(elem.keys())
#this list helps us differentiate b/w conditional and elementary operation
Expand Down Expand Up @@ -325,7 +327,7 @@ def expression_builder(self, sub_expr, parent_op=None, **kwargs):
'list-not-more'
]):
continue

ret_expr.append(str(res))
if res and parent_op and parent_op.lower() == 'or':
break
Expand All @@ -334,27 +336,27 @@ def expression_builder(self, sub_expr, parent_op=None, **kwargs):
else:
self.logger_check.info(
colorama.Fore.RED +
"ERROR!!! Malformed sub-expression", extra=self.log_detail)
continue
"ERROR!!! Malformed sub-expression", extra=self.log_detail)
continue

expr = ''
if parent_op is None:
if len(ret_expr) > 1:
expr = ' and '.join(ret_expr)
elif len(ret_expr) == 1 :
expr = ret_expr[0]

else:
parent_op = str(parent_op).lower()

if len(ret_expr) == 1 and self.is_unary_op(parent_op):
expr = '{0} {1}'.format(parent_op,ret_expr[0])
elif len(ret_expr) >= 1 :
expr = ' {0} '.format(parent_op).join(ret_expr)
if expr is not '':
if expr is not '':
expr = '(' +expr+ ')'
return expr


def compare_reply(
self, op, tests, test_name, teston, check, db, snap1, snap2=None, action=None):
Expand Down Expand Up @@ -388,7 +390,7 @@ def compare_reply(
else:
op.no_passed = op.no_passed + 1
else:
#this result is going to be associated with the whole test case
#this result is going to be associated with the whole test case
final_result = None

for test in tests:
Expand Down Expand Up @@ -418,10 +420,10 @@ def compare_reply(
id_list = []
testcases = test['item']['tests']
iter = False

kwargs = {'op': op,
'x_path': x_path,
'id_list': id_list,
'x_path': x_path,
'id_list': id_list,
'iter': iter,
'teston': teston,
'check': check,
Expand All @@ -433,8 +435,8 @@ def compare_reply(
'top_ignore_null': top_ignore_null
}
final_boolean_expr = self.expression_builder(testcases, None, **kwargs)
#for cases where skip was encountered due to ignore-null
if final_boolean_expr is '' or final_boolean_expr is None or final_boolean_expr == str(None):
#for cases where skip was encountered due to ignore-null
if final_boolean_expr is '' or final_boolean_expr is None or final_boolean_expr == str(None):
continue

result = eval(final_boolean_expr)
Expand All @@ -443,7 +445,7 @@ def compare_reply(
if final_result is None:
final_result = True # making things normal
final_result = final_result and result

op.result_dict[test_name] = final_result

def compare_diff(self, pre_snap_file, post_snap_file, check_from_sqlite):
Expand Down Expand Up @@ -625,18 +627,32 @@ def generate_test_files(
"Tests Included: %s " %
(val),
extra=self.log_detail)

# This is Where we are going to print the description mentioned by the user for the testcase
# Enumerate generate a tuple of index and corresponding element in the list
# index[0] is index and index[1] is the dictionary in which it will search.

description_index = [index[0] for index in enumerate(tests[val]) if 'description' in index[1]]
if len(description_index) > 0:
description_index = description_index[0]
description = tests[val][description_index]['description']
if description is not None:
self.logger_check.info(
"Description: %s " %
(description),
extra=self.log_detail)
try:
if any('command' in d for d in tests[val]):
index = next((i for i, x in enumerate(tests[val]) if 'command' in x), 0)
command = tests[val][index].get('command').split('|')[0].strip()
reply_format = tests[val][0].get('format', 'xml')
message = self._print_testmssg("Command: "+command, "*")

self.logger_check.info(
colorama.Fore.BLUE +
message,
extra=self.log_detail)

name = '_'.join(command.split())
teston = command

Expand Down
1 change: 1 addition & 0 deletions tests/unit/configs/conditional_op_pass.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ test_command_version:
- contains: //bgp-options, LogUpDown
- is-equal: local-address, unspecified
- command: show bgp neighbor
- description: "This Test is for bgp flap count"