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
- from genie .conf import Genie
15
- from genie .abstract import Lookup
14
+ from genie .testbed import load
16
15
17
- # import the genie libs
18
- from genie .libs import ops # noqa
19
16
20
17
# Get your logger for your script
21
18
log = logging .getLogger (__name__ )
22
19
23
-
24
20
###################################################################
25
21
# COMMON SETUP SECTION #
26
22
###################################################################
27
23
28
24
class common_setup (aetest .CommonSetup ):
29
25
""" Common Setup section """
30
26
31
- # CommonSetup have subsection.
32
- # You can have 1 to as many subsection as wanted
33
-
34
27
# Connect to each device in the testbed
35
28
@aetest .subsection
36
29
def connect (self , testbed ):
37
- genie_testbed = Genie . init (testbed )
30
+ genie_testbed = load (testbed )
38
31
self .parent .parameters ['testbed' ] = genie_testbed
39
32
device_list = []
40
33
for device in genie_testbed .devices .values ():
41
34
log .info (banner (
42
- "Connect to device '{d}'" . format ( d = device .name ) ))
35
+ f "Connect to device '{ device .name } '" ))
43
36
try :
44
37
device .connect ()
38
+ device_list .append (device )
45
39
except Exception as e :
46
- self .failed ("Failed to establish connection to '{}'" .format (
47
- device .name ))
48
-
49
- device_list .append (device )
40
+ log .info (f"Failed to establish connection to '{ device .name } '" )
50
41
51
- # Pass list of devices the to testcases
52
42
self .parent .parameters .update (dev = device_list )
43
+ log .debug (self .parent .parameters )
44
+
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' ])
49
+
53
50
54
51
55
52
###################################################################
56
53
# TESTCASES SECTION #
57
54
###################################################################
58
55
59
- # Testcase name : vxlan_consistency_check
60
- class CRC_count_check (aetest .Testcase ):
61
- """ This is user Testcases section """
56
+ #Capture Interfaces statistics on the device and tabulate
57
+ #Also look for CRC Errors. If CRC Errors fail then change
58
+ #variable 'passing' from 0 to 1
59
+ class CRC_count_Check (aetest .Testcase ):
60
+ @aetest .setup
61
+ def setup (self , dev ):
62
+ #Setup has been marked for looping in Common Setup(create_testcases) with the argument dev
63
+ #dev is the list of devices in the current testbed
64
+ self .dev = dev
65
+ log .info (banner (f"Gathering Interface Information from { dev .name } " ))
66
+ self .interface_info = dev .learn ('interface' )
67
+
68
+ list_of_interfaces = self .interface_info .info .keys ()
69
+ self .mega_dict = {}
70
+ self .mega_dict [dev .name ] = {}
71
+ self .mega_tabular = []
72
+ self .passing = 0
73
+
74
+
75
+ for ints , props in self .interface_info .info .items ():
76
+ counters = props .get ('counters' )
77
+ if counters :
78
+ smaller_tabular = []
79
+ if 'in_crc_errors' in counters :
80
+ self .mega_dict [dev .name ][ints ] = counters ['in_crc_errors' ]
81
+ smaller_tabular .append (dev .name )
82
+ smaller_tabular .append (ints )
83
+ smaller_tabular .append (str (counters ['in_crc_errors' ]))
84
+ if counters ['in_crc_errors' ]:
85
+ smaller_tabular .append ('Failed' )
86
+ self .passing = 1
87
+ else :
88
+ smaller_tabular .append ('Passed' )
62
89
63
- # First test section
64
- @ aetest .test
65
- def learn_interfaces (self ):
66
- """ Sample test section. Only print """
90
+ else :
91
+ self .mega_dict [dev .name ][ints ] = None
92
+ smaller_tabular .append (dev .name )
93
+ smaller_tabular .append (ints )
94
+ smaller_tabular .append ('N/A' )
95
+ smaller_tabular .append ('N/A' )
96
+ self .mega_tabular .append (smaller_tabular )
97
+ self .mega_tabular .append (['-' * sum (len (i ) for i in smaller_tabular )])
67
98
68
- self .all_interfaces = {}
69
- for dev in self .parent .parameters ['dev' ]:
70
- log .info (banner ("Gathering Interface Information from {}" .format (
71
- dev .name )))
72
- abstract = Lookup .from_device (dev )
73
- intf = abstract .ops .interface .interface .Interface (dev )
74
- intf .learn ()
75
- self .all_interfaces [dev .name ] = intf .info
76
-
77
- # Second test section
78
- @ aetest .test
79
- def check_CRC (self ):
80
-
81
- mega_dict = {}
82
- mega_tabular = []
83
- for device , ints in self .all_interfaces .items ():
84
- mega_dict [device ] = {}
85
- for name , props in ints .items ():
86
- counters = props .get ('counters' )
87
- if counters :
88
- smaller_tabular = []
89
- if 'in_crc_errors' in counters :
90
- mega_dict [device ][name ] = counters ['in_crc_errors' ]
91
- smaller_tabular .append (device )
92
- smaller_tabular .append (name )
93
- smaller_tabular .append (str (counters ['in_crc_errors' ]))
94
- if counters ['in_crc_errors' ]:
95
- smaller_tabular .append ('Failed' )
96
- else :
97
- smaller_tabular .append ('Passed' )
98
- else :
99
- mega_dict [device ][name ] = None
100
- smaller_tabular .append (device )
101
- smaller_tabular .append (name )
102
- smaller_tabular .append ('N/A' )
103
- smaller_tabular .append ('N/A' )
104
- mega_tabular .append (smaller_tabular )
105
99
106
- mega_tabular . append ([ '-' * sum ( len ( i ) for i in smaller_tabular )] )
100
+ aetest . loop . mark ( self . interface_check , intf = list_of_interfaces )
107
101
108
- log .info (tabulate (mega_tabular ,
102
+ # create table and display. Test fails if variable 'passing' = 1.
103
+ # which means there are some CRC errors
104
+ @aetest .test
105
+ def table_display (self ):
106
+ log .info (tabulate (self .mega_tabular ,
109
107
headers = ['Device' , 'Interface' ,
110
108
'CRC Errors Counter' ,
111
109
'Passed/Failed' ],
112
110
tablefmt = 'orgtbl' ))
113
111
114
- for dev in mega_dict :
115
- for intf in mega_dict [dev ]:
116
- if mega_dict [dev ][intf ]:
117
- self .failed ("{d}: {name} CRC ERRORS: {e}" .format (
118
- d = dev , name = intf , e = mega_dict [dev ][intf ]))
112
+ if self .passing == 1 :
113
+ self .failed ('Some interfaces have CRC errors' )
114
+ else :
115
+ self .passed
116
+
117
+ # Test created for each interface. If CRC errors then Interface test will fail
118
+ @aetest .test
119
+ def interface_check (self , intf ):
120
+ # This test has been marked for loop. intf is the looping argument (list of interfaces)
121
+ # Thus this test is run for each interface in the intf list.
122
+ #for int, errors in self.parent.parameters['mega'].items():
123
+ for int , errors in self .mega_dict [self .dev .name ].items ():
124
+ if errors :
125
+ self .failed (f'Interface { int } has crc errors { errors } ' )
126
+ else :
127
+ self .passed (f'No errors on { int } ' )
119
128
120
- self .passed ("All devices' interfaces CRC ERRORS Count is: 'Zero'" )
121
129
122
130
# #####################################################################
123
131
# #### COMMON CLEANUP SECTION ###
@@ -136,10 +144,12 @@ class common_cleanup(aetest.CommonCleanup):
136
144
# here is an example of 1 subsection
137
145
138
146
@aetest .subsection
139
- def clean_everything (self ):
140
- """ Common Cleanup Subsection """
141
- log .info ("Aetest Common Cleanup " )
147
+
148
+ def disconnect (self ):
149
+ log .info ("Aetest Common Cleanup disconnecting devices" )
150
+ for dev in self .parent .parameters ['dev' ]:
151
+ dev .disconnect ()
142
152
143
153
144
154
if __name__ == '__main__' : # pragma: no cover
145
- aetest .main ()
155
+ aetest .main ()
0 commit comments