|
| 1 | +#!/bin/env python |
| 2 | + |
| 3 | +# To get a logger for the script |
| 4 | +import logging |
| 5 | + |
| 6 | +# To build the table at the end |
| 7 | +from tabulate import tabulate |
| 8 | + |
| 9 | +# Needed for aetest script |
| 10 | +from ats import aetest |
| 11 | +from ats.log.utils import banner |
| 12 | + |
| 13 | +# Genie Imports |
| 14 | +from genie.conf import Genie |
| 15 | +from genie.abstract import Lookup |
| 16 | + |
| 17 | +# import the genie libs |
| 18 | +from genie.libs import ops # noqa |
| 19 | + |
| 20 | +# Get your logger for your script |
| 21 | +log = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +################################################################### |
| 25 | +# COMMON SETUP SECTION # |
| 26 | +################################################################### |
| 27 | + |
| 28 | +class common_setup(aetest.CommonSetup): |
| 29 | + """ Common Setup section """ |
| 30 | + |
| 31 | + # CommonSetup have subsection. |
| 32 | + # You can have 1 to as many subsection as wanted |
| 33 | + |
| 34 | + # Connect to each device in the testbed |
| 35 | + @aetest.subsection |
| 36 | + def connect(self, testbed): |
| 37 | + genie_testbed = Genie.init(testbed) |
| 38 | + self.parent.parameters['testbed'] = genie_testbed |
| 39 | + device_list = [] |
| 40 | + for device in genie_testbed.devices.values(): |
| 41 | + log.info(banner( |
| 42 | + "Connect to device '{d}'".format(d=device.name))) |
| 43 | + try: |
| 44 | + device.connect() |
| 45 | + except Exception as e: |
| 46 | + self.failed("Failed to establish connection to '{}'".format( |
| 47 | + device.name)) |
| 48 | + |
| 49 | + device_list.append(device) |
| 50 | + |
| 51 | + # Pass list of devices the to testcases |
| 52 | + self.parent.parameters.update(dev=device_list) |
| 53 | + |
| 54 | + |
| 55 | +################################################################### |
| 56 | +# TESTCASES SECTION # |
| 57 | +################################################################### |
| 58 | + |
| 59 | +# Testcase name : vxlan_consistency_check |
| 60 | +class CRC_count_check(aetest.Testcase): |
| 61 | + """ This is user Testcases section """ |
| 62 | + |
| 63 | + # First test section |
| 64 | + @ aetest.test |
| 65 | + def learn_interfaces(self): |
| 66 | + """ Sample test section. Only print """ |
| 67 | + |
| 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 | + |
| 106 | + mega_tabular.append(['-'*sum(len(i) for i in smaller_tabular)]) |
| 107 | + |
| 108 | + log.info(tabulate(mega_tabular, |
| 109 | + headers=['Device', 'Interface', |
| 110 | + 'CRC Errors Counter', |
| 111 | + 'Passed/Failed'], |
| 112 | + tablefmt='orgtbl')) |
| 113 | + |
| 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])) |
| 119 | + |
| 120 | + self.passed("All devices' interfaces CRC ERRORS Count is: 'Zero'") |
| 121 | + |
| 122 | +# ##################################################################### |
| 123 | +# #### COMMON CLEANUP SECTION ### |
| 124 | +# ##################################################################### |
| 125 | + |
| 126 | + |
| 127 | +# This is how to create a CommonCleanup |
| 128 | +# You can have 0 , or 1 CommonCleanup. |
| 129 | +# CommonCleanup can be named whatever you want :) |
| 130 | +class common_cleanup(aetest.CommonCleanup): |
| 131 | + """ Common Cleanup for Sample Test """ |
| 132 | + |
| 133 | + # CommonCleanup follow exactly the same rule as CommonSetup regarding |
| 134 | + # subsection |
| 135 | + # You can have 1 to as many subsections as wanted |
| 136 | + # here is an example of 1 subsection |
| 137 | + |
| 138 | + @aetest.subsection |
| 139 | + def clean_everything(self): |
| 140 | + """ Common Cleanup Subsection """ |
| 141 | + log.info("Aetest Common Cleanup ") |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == '__main__': # pragma: no cover |
| 145 | + aetest.main() |
0 commit comments