7
7
from tabulate import tabulate
8
8
9
9
# 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
12
12
13
13
# Genie Imports
14
14
from genie .testbed import load
17
17
# Get your logger for your script
18
18
log = logging .getLogger (__name__ )
19
19
20
-
21
20
###################################################################
22
21
# COMMON SETUP SECTION #
23
22
###################################################################
@@ -36,105 +35,88 @@ def connect(self, testbed):
36
35
f"Connect to device '{ device .name } '" ))
37
36
try :
38
37
device .connect ()
38
+ device_list .append (device )
39
39
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 } '" )
43
41
44
- # Pass list of devices the to testcases
45
42
self .parent .parameters .update (dev = device_list )
46
- print (self .parent .parameters )
47
-
43
+ log .debug (self .parent .parameters )
48
44
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' ])
60
49
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' )
66
57
67
- # Second test section
68
- @ aetest .test
69
- def check_CRC (self ):
70
58
59
+ list_of_interfaces = self .interface_info .info .keys ()
71
60
mega_dict = {}
61
+ mega_dict [dev .name ] = {}
72
62
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
88
78
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' ],
99
102
headers = ['Device' , 'Interface' ,
100
103
'CRC Errors Counter' ,
101
104
'Passed/Failed' ],
102
105
tablefmt = 'orgtbl' ))
103
106
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
132
111
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 } ' )
137
121
138
122
139
- if __name__ == '__main__' : # pragma: no cover
140
- aetest .main ()
0 commit comments