Skip to content

Commit 468b786

Browse files
author
simon
committed
Each Interface individual test, looping over testcases
1 parent 4d0d295 commit 468b786

File tree

1 file changed

+69
-87
lines changed

1 file changed

+69
-87
lines changed

crc_errors/CRC_Count_check.py

+69-87
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from tabulate import tabulate
88

99
# Needed for aetest script
10-
from ats import aetest
11-
from ats.log.utils import banner
10+
from pyats import aetest
11+
from pyats.log.utils import banner
1212

1313
# Genie Imports
1414
from genie.testbed import load
@@ -17,7 +17,6 @@
1717
# Get your logger for your script
1818
log = logging.getLogger(__name__)
1919

20-
2120
###################################################################
2221
# COMMON SETUP SECTION #
2322
###################################################################
@@ -36,105 +35,88 @@ def connect(self, testbed):
3635
f"Connect to device '{device.name}'"))
3736
try:
3837
device.connect()
38+
device_list.append(device)
3939
except Exception as e:
40-
self.failed(f"Failed to establish connection to '{device.name}'")
41-
42-
device_list.append(device)
40+
log.info(f"Failed to establish connection to '{device.name}'")
4341

44-
# Pass list of devices the to testcases
4542
self.parent.parameters.update(dev=device_list)
46-
print(self.parent.parameters)
47-
43+
log.debug(self.parent.parameters)
4844

49-
###################################################################
50-
# TESTCASES SECTION #
51-
###################################################################
52-
53-
# Testcase name : Check for CRC errors
54-
class CRC_count_check(aetest.Testcase):
55-
""" This is user Testcases section """
56-
57-
# First test section
58-
@ aetest.test
59-
def learn_interfaces(self):
45+
@aetest.subsection
46+
def create_testcases(self):
47+
#below creates a loop over the CRC_count_Check Class using dev as the iterator
48+
aetest.loop.mark(CRC_count_Check, dev=self.parent.parameters['dev'])
6049

61-
self.all_interfaces = {}
62-
for dev in self.parent.parameters['dev']:
63-
log.info(banner(f"Gathering Interface Information from {dev.name}"))
64-
intf = dev.learn('interface')
65-
self.all_interfaces[dev.name] = intf.info
50+
class CRC_count_Check(aetest.Testcase):
51+
@aetest.setup
52+
def setup(self, dev):
53+
#Setup has been marked for looping in Common Setup(create_testcases) with the argument dev
54+
#dev is the list of devices in the current testbed
55+
log.info(banner(f"Gathering Interface Information from {dev.name}"))
56+
self.interface_info = dev.learn('interface')
6657

67-
# Second test section
68-
@ aetest.test
69-
def check_CRC(self):
7058

59+
list_of_interfaces=self.interface_info.info.keys()
7160
mega_dict = {}
61+
mega_dict[dev.name] = {}
7262
mega_tabular = []
73-
for device, ints in self.all_interfaces.items():
74-
mega_dict[device] = {}
75-
for name, props in ints.items():
76-
counters = props.get('counters')
77-
if counters:
78-
smaller_tabular = []
79-
if 'in_crc_errors' in counters:
80-
mega_dict[device][name] = counters['in_crc_errors']
81-
smaller_tabular.append(device)
82-
smaller_tabular.append(name)
83-
smaller_tabular.append(str(counters['in_crc_errors']))
84-
if counters['in_crc_errors']:
85-
smaller_tabular.append('Failed')
86-
else:
87-
smaller_tabular.append('Passed')
63+
passing=0
64+
65+
66+
for ints, props in self.interface_info.info.items():
67+
counters = props.get('counters')
68+
if counters:
69+
smaller_tabular = []
70+
if 'in_crc_errors' in counters:
71+
mega_dict[dev.name][ints] = counters['in_crc_errors']
72+
smaller_tabular.append(dev.name)
73+
smaller_tabular.append(ints)
74+
smaller_tabular.append(str(counters['in_crc_errors']))
75+
if counters['in_crc_errors']:
76+
smaller_tabular.append('Failed')
77+
passing=1
8878
else:
89-
mega_dict[device][name] = None
90-
smaller_tabular.append(device)
91-
smaller_tabular.append(name)
92-
smaller_tabular.append('N/A')
93-
smaller_tabular.append('N/A')
94-
mega_tabular.append(smaller_tabular)
95-
96-
mega_tabular.append(['-'*sum(len(i) for i in smaller_tabular)])
97-
98-
log.info(tabulate(mega_tabular,
79+
smaller_tabular.append('Passed')
80+
81+
else:
82+
mega_dict[dev.name][ints] = None
83+
smaller_tabular.append(dev.name)
84+
smaller_tabular.append(ints)
85+
smaller_tabular.append('N/A')
86+
smaller_tabular.append('N/A')
87+
mega_tabular.append(smaller_tabular)
88+
mega_tabular.append(['-' * sum(len(i) for i in smaller_tabular)])
89+
90+
#pass megadict to interface test function
91+
self.parent.parameters.update(mega=mega_dict[dev.name])
92+
#pass megatable list to table_display test function
93+
self.parent.parameters.update(megatable=mega_tabular)
94+
#pass passing variable to table_display function in order indicate pass or fail - 0=pass 1=fail
95+
self.parent.parameters.update(passing=passing)
96+
97+
aetest.loop.mark(self.interface_check, intf=list_of_interfaces)
98+
99+
@aetest.test
100+
def table_display(self):
101+
log.info(tabulate(self.parent.parameters['megatable'],
99102
headers=['Device', 'Interface',
100103
'CRC Errors Counter',
101104
'Passed/Failed'],
102105
tablefmt='orgtbl'))
103106

104-
for dev in mega_dict:
105-
for intf in mega_dict[dev]:
106-
if mega_dict[dev][intf]:
107-
self.failed(f"{dev}: {intf} CRC ERRORS: {mega_dict[dev][intf]}")
108-
109-
self.passed("All devices' interfaces CRC ERRORS Count is: 'Zero'")
110-
111-
# #####################################################################
112-
# #### COMMON CLEANUP SECTION ###
113-
# #####################################################################
114-
115-
116-
# This is how to create a CommonCleanup
117-
# You can have 0 , or 1 CommonCleanup.
118-
# CommonCleanup can be named whatever you want :)
119-
class common_cleanup(aetest.CommonCleanup):
120-
""" Common Cleanup for Sample Test """
121-
122-
# CommonCleanup follow exactly the same rule as CommonSetup regarding
123-
# subsection
124-
# You can have 1 to as many subsections as wanted
125-
# here is an example of 1 subsection
126-
127-
# @aetest.subsection
128-
# def clean_everything(self):
129-
# """ Common Cleanup Subsection """
130-
# log.info("Aetest Common Cleanup ")
131-
@aetest.subsection
107+
if self.parent.parameters['passing']==1:
108+
self.failed('Some interfaces have CRC errors')
109+
else:
110+
self.passed
132111

133-
def disconnect(self):
134-
log.info("Aetest Common Cleanup disconnecting devices")
135-
for dev in self.parent.parameters['dev']:
136-
dev.disconnect()
112+
@aetest.test
113+
def interface_check(self, intf):
114+
# This test has been marked for loop. intf is the looping argument (list of interfaces)
115+
# Thus this test is run for each interface in the intf list.
116+
for int, errors in self.parent.parameters['mega'].items():
117+
if errors:
118+
self.failed(f'Interface {int} has crc errors {errors}')
119+
else:
120+
self.passed(f'No errors on {int}')
137121

138122

139-
if __name__ == '__main__': # pragma: no cover
140-
aetest.main()

0 commit comments

Comments
 (0)