Skip to content

Commit

Permalink
resolving merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Jainpriyal committed Apr 1, 2016
2 parents 41e85e4 + cc2b085 commit 490bf94
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 45 deletions.
14 changes: 10 additions & 4 deletions lib/jnpr/jsnapy/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,19 @@ def get_xml_reply(self, db, snap):
"ERROR, Database for either pre or post snapshot is not present in given path !!",
extra=self.log_detail)
return
elif os.path.isfile(snap):
elif os.path.isfile(snap) and os.stat(snap).st_size > 0:
xml_value = etree.parse(snap)
##### sometimes snapshot files are empty, when cmd/rpc reply do not contain any value
elif os.path.isfile(snap) and os.stat(snap).st_size <= 0:
self.logger_check.error(
colorama.Fore.RED +
"ERROR, Snapshot file is empty !!",
extra=self.log_detail)
return
else:
self.logger_check.error(
colorama.Fore.RED +
"ERROR, Pre snapshot file: %s is not present in given path !!" %
snap,
"ERROR, Snapshot file is not present in given path !!",
extra=self.log_detail)
return
return xml_value
Expand Down Expand Up @@ -372,7 +378,7 @@ def generate_test_files(
extra=self.log_detail)
try:
if tests[val][0].keys()[0] == 'command':
command = tests[val][0].get('command')
command = tests[val][0].get('command').split('|')[0].strip()
reply_format = tests[val][0].get('format', 'xml')
self.logger_check.info(
colorama.Fore.BLUE +
Expand Down
70 changes: 46 additions & 24 deletions lib/jnpr/jsnapy/snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from jnpr.jsnapy import get_path
from jnpr.junos.exception import RpcError
from jnpr.jsnapy.sqlite_store import JsnapSqlite

import lxml

class Parser:

Expand All @@ -23,12 +23,21 @@ def _write_file(self, rpc_reply, format, output_file):
:param format: xml/text
:param output_file: name of file
"""
if isinstance(rpc_reply, bool) and format == "text":
self.logger_snap.error(
colorama.Fore.RED +
"ERROR!! requested node is not present", extra=self.log_detail)
### pyEz returns true if there is no output of given command ###
### Ex. show configuration security certificates returns nothing if its not set


if rpc_reply is True :
with open(output_file, 'w') as f:
f.write("")
self.logger_snap.info(
colorama.Fore.BLUE +
"\nOutput of requested Command/RPC is empty", extra=self.log_detail)

else:
err = rpc_reply.xpath("//rpc-error")
"""
### No need of checking reply, as writing all rpc-replies including error and warbing in snap files
err = rpc_reply.xpath("//rpc-error") if isinstance(rpc_reply, lxml.etree._Element) else list()
if len(err):
self.logger_snap.error(
colorama.Fore.RED +
Expand All @@ -38,8 +47,9 @@ def _write_file(self, rpc_reply, format, output_file):
self.logger_snap.error(colorama.Fore.RED +
err_node.findtext('.//error-message'), extra=self.log_detail)
else:
with open(output_file, 'w') as f:
f.write(etree.tostring(rpc_reply))
"""
with open(output_file, 'w') as f:
f.write(etree.tostring(rpc_reply))

def _write_warning(
self, reply, db, snap_file, hostname, cmd_name, cmd_format, output_file):
Expand All @@ -62,12 +72,15 @@ def _check_reply(self, rpc_reply, format):
:param format: xml/ text
:return: return false if reply contains error ow return rpc reply
"""
if isinstance(rpc_reply, bool) and format == "text":
self.logger_snap.error(
colorama.Fore.RED +
"ERROR!! requested node is not present", extra=self.log_detail)
if rpc_reply is True :
self.logger_snap.info(
colorama.Fore.BLUE +
"\nOutput of requested Command/RPC is empty", extra=self.log_detail)
return ""
else:
err = rpc_reply.xpath("//rpc-error")
"""
### No need of checking rpc replies as writing all replies inluding warnings and errors in snap files
err = rpc_reply.xpath("//rpc-error") if isinstance(rpc_reply, lxml.etree._Element) else list()
if len(err):
self.logger_snap.error(
colorama.Fore.RED +
Expand All @@ -78,11 +91,15 @@ def _check_reply(self, rpc_reply, format):
"Complete Error Message: %s" % rpc_reply,
extra=self.log_detail)
for err_node in err:
self.logger_snap.error(colorama.Fore.RED +
err_node.findtext('.//error-message'), extra=self.log_detail)
self.logger_snap.error(
err_node.findtext(
colorama.Fore.RED +
'.//error-message'), extra=self.log_detail)
return(False)
else:
return etree.tostring(rpc_reply)
return(False)
"""
return etree.tostring(rpc_reply)


def generate_snap_file(self, output_file, hostname, name, cmd_format):
"""
Expand All @@ -92,7 +109,8 @@ def generate_snap_file(self, output_file, hostname, name, cmd_format):
:param cmd_format: xml/text
:return: return output file
"""
cmd_rpc = re.sub('/|\*|\.|-', '_', name)
name = name.split('|')[0].strip()
cmd_rpc = re.sub('/|\*|\.|-|\|', '_', name)
if os.path.isfile(output_file):
return output_file
else:
Expand Down Expand Up @@ -138,14 +156,20 @@ def run_cmd(self, test_file, t, formats, dev, output_file, hostname, db):
cmd_format = test_file[t][0].get('format', 'xml')
cmd_format = cmd_format if cmd_format in formats else 'xml'
self.command_list.append(command)
cmd_name = '_'.join(command.split())
cmd_name = command.split('|')[0].strip()
cmd_name = '_'.join(cmd_name.split())
try:
self.logger_snap.info(
colorama.Fore.BLUE +
"Taking snapshot for %s ................" %
command,
extra=self.log_detail)
rpc_reply_command = dev.rpc.cli(command, format=cmd_format)
##### for commands containing "| display xml" only text format works in PyEz
if re.search('\|\s+display\s+xml',command):
rpc_reply_command = dev.rpc.cli(command, format='text')
else:
rpc_reply_command = dev.rpc.cli(command, format=cmd_format)

except RpcError as err:
snap_file = self.generate_snap_file(
output_file,
Expand Down Expand Up @@ -185,8 +209,7 @@ def run_cmd(self, test_file, t, formats, dev, output_file, hostname, db):
cmd_name,
cmd_format)
self._write_file(rpc_reply_command, cmd_format, snap_file)
if db['store_in_sqlite'] is True and self._check_reply(
rpc_reply_command, cmd_format):
if db['store_in_sqlite'] is True:
self.store_in_sqlite(
db,
hostname,
Expand Down Expand Up @@ -333,8 +356,7 @@ def run_rpc(self, test_file, t, formats, dev, output_file, hostname, db):
reply_format)
self._write_file(rpc_reply, reply_format, snap_file)

if db['store_in_sqlite'] is True and self._check_reply(
rpc_reply, reply_format):
if db['store_in_sqlite'] is True:
self.store_in_sqlite(
db,
hostname,
Expand Down
63 changes: 46 additions & 17 deletions lib/jnpr/jsnapy/testop.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,12 +1160,19 @@ def no_diff(self, x_path, ele_list, err_mssg,
# one xpath has only one set of id
data1 = self._get_data(id_list, pre_nodes)
data2 = self._get_data(id_list, post_nodes)
for k in data1:
# making union of id keys
data1_key = set(data1.keys())
data2_key = set(data2.keys())
keys_union = data1_key.union(data2_key)
# iterating through ids which are present either in pre
# snapshot or post snapshot or both
for k in keys_union:
for length in range(len(k)):
# making dictionary of ids for given xpath, ex id_0,
# id_1 ..etc
iddict[
'id_' + str(length)] = [k[length][i].strip() for i in range(len(k[length]))]
#iddict['id_' + str(length)] = k[length].strip()
if k in data2:
if k in data1 and k in data2:
predict, postdict = self._get_nodevalue(
predict, postdict, data1[k], data2[k], x_path, ele_list[0], err_mssg)
predict, postdict = self._get_nodevalue(
Expand Down Expand Up @@ -1205,14 +1212,17 @@ def no_diff(self, x_path, ele_list, err_mssg,
post=postdict), extra=self.log_detail)
else:
self.logger_testop.error(colorama.Fore.RED +
"ERROR, id miss match occurred!!! id list in pre snapshots is: %s" % iddict, extra=self.log_detail)
tresult['id_miss_match'].append(iddict.copy())
"""
for kval in k:
"ERROR, ID miss match occurred!!!", extra=self.log_detail)
if k in data1:
self.logger_testop.error(
"ID list '%s' is not present in post snapshot" %
iddict, extra=self.log_detail)
else:
self.logger_testop.error(
"Missing node: %s" %
kval.strip(), extra= self.log_detail)
"""
"ID list '%s' is not present in pre snapshot" %
iddict, extra=self.log_detail)
tresult['id_miss_match'].append(iddict.copy())

res = False
self.print_result('no-diff', res)
tresult['result'] = res
Expand Down Expand Up @@ -1294,8 +1304,10 @@ def list_not_less(
post=postdict), extra=self.log_detail)
else:
self.logger_testop.error(colorama.Fore.RED +
"ERROR, id miss match occurred!!! id list not in post snapshots is: %s" %
iddict, extra=self.log_detail)
"ERROR!! ID miss match occurred!! ", extra=self.log_detail)
self.logger_testop.error(
"\nID list ' %s ' is not present in post snapshots " %
iddict, extra=self.log_detail)
tresult['id_miss_match'].append(iddict.copy())
# for kval in range(len(k)-1):
# print "kval, k", kval,k
Expand Down Expand Up @@ -1382,7 +1394,10 @@ def list_not_more(
else:
tresult['id_miss_match'] = []
self.logger_testop.error(colorama.Fore.RED +
"ERROR, id miss match occurred !! id list not in pre snapshots is: %s" % iddict, extra=self.log_detail)
"ERROR, ID miss match occurred !!", extra=self.log_detail)
self.logger_testop.error(
"\nID list ' %s ' is not present in pre snapshots" %
iddict, extra=self.log_detail)
tresult['id_miss_match'].append(iddict.copy())
# for kval in range(len(k)-1):
# self.logger_testop.error(
Expand Down Expand Up @@ -1431,15 +1446,19 @@ def delta(self, x_path, ele_list, err_mssg,
predata = self._get_data(id_list, pre_nodes)
postdata = self._get_data(id_list, post_nodes)

for k in predata:
predata_keys = set(predata.keys())
postdata_keys = set(postdata.keys())
keys_union = predata_keys.union(postdata_keys)

for k in keys_union:
# checking if id in first data set is present in second data
# set or not
for length in range(len(k)):
#iddict['id_' + str(length)] = k[length]
iddict[
'id_' + str(length)] = [k[length][i].strip() for i in range(len(k[length]))]

if k in postdata:
if k in predata and k in postdata:
predict, postdict = self._get_nodevalue(
predict, postdict, predata[k], postdata[k], x_path, node_name, err_mssg)
predict, postdict = self._get_nodevalue(
Expand Down Expand Up @@ -1611,8 +1630,18 @@ def delta(self, x_path, ele_list, err_mssg,
res = False

else:
self.logger_testop.error(colorama.Fore.RED + "\n ERROR!! id miss match occurred !! mismatched id from pre snapshot"
"is: %s" % iddict, extra=self.log_detail)
self.logger_testop.error(
colorama.Fore.RED +
"\nERROR!! id miss match occurred !!",
extra=self.log_detail)
if k in predata:
self.logger_testop.error(
"ID list '%s' is not present in post snapshot" %
iddict, extra=self.log_detail)
else:
self.logger_testop.error(
"ID list '%s' is not present in pre snapshot" %
iddict, extra=self.log_detail)
tresult['id_miss_match'].append(iddict.copy())

# for kval in k:
Expand Down

0 comments on commit 490bf94

Please sign in to comment.