#!/usr/bin/env python3

# To get a logger for the script
import logging

from pyats import aetest
from pyats.log.utils import banner

# Genie Imports
from genie.conf import Genie

# To handle errors with connections to devices
from unicon.core import errors

import argparse
from pyats.topology import loader

# Get your logger for your script
global log
log = logging.getLogger(__name__)
log.level = logging.INFO


class ConnectToAllDevices(aetest.CommonSetup):
    """
    CommonSetup class to prepare for testcases
    Establishes connections to all devices in testbed
    """

    @aetest.subsection
    def establish_connections(self, testbed):
        """
        Establishes connections to all devices in testbed
        :param testbed:
        :return:
        """

        genie_testbed = Genie.init(testbed)
        self.parent.parameters['testbed'] = genie_testbed
        device_list = []
        for device in genie_testbed.devices.values():
            log.info(banner(
                f"Connect to device '{device.name}'"))
            try:
                device.connect(log_stdout=False)
            except errors.ConnectionError:
                self.failed(f"Failed to establish "
                            f"connection to '{device.name}'")
            device_list.append(device)
        # Pass list of devices to testcases
        self.parent.parameters.update(dev=device_list)


class VerifyLogging(aetest.Testcase):
    """
    VerifyLogging Testcase - collect show logging information from devices
    Verify that all devices do not have 'ERROR|WARN' messages in logs
    """

    @aetest.setup
    def setup(self):
        devices = self.parent.parameters['dev']
        tests_names = [f'Checking errors in logs on "{device.name}"' for device in devices]
        aetest.loop.mark(self.error_logs, device=devices, uids=tests_names)

    @aetest.test
    def error_logs(self, device):
        if device.os == 'asa':
            output = device.execute('show logging | i ASA-[1-3]')
        else:
            output = device.execute('show logging | i ERROR|WARN')

        if len(output) > 0:
            if len(output.splitlines()) <= 10:
                self.failed(banner(f'Found ERROR in log:\n{output}'))
            else:
                for log_line in output.splitlines()[:9]:
                    log.error(banner(f'Found ERROR in log: {log_line}'))
                self.failed(banner(f'Output is too huge. Please check the rest of log on device'))
        else:
            pass


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--testbed', dest='testbed',
                        type=loader.load)

    args, unknown = parser.parse_known_args()

    aetest.main(**vars(args))

#!/usr/bin/env python3

# To get a logger for the script
import logging

from pyats import aetest
from pyats.log.utils import banner

# Genie Imports
from genie.conf import Genie

# To handle errors with connections to devices
from unicon.core import errors

import argparse
from pyats.topology import loader

# Get your logger for your script
global log
log = logging.getLogger(__name__)
log.level = logging.INFO


class ConnectToAllDevices(aetest.CommonSetup):
    """
    CommonSetup class to prepare for testcases
    Establishes connections to all devices in testbed
    """

    @aetest.subsection
    def establish_connections(self, testbed):
        """
        Establishes connections to all devices in testbed
        :param testbed:
        :return:
        """

        genie_testbed = Genie.init(testbed)
        self.parent.parameters['testbed'] = genie_testbed
        device_list = []
        for device in genie_testbed.devices.values():
            log.info(banner(
                f"Connect to device '{device.name}'"))
            try:
                device.connect(log_stdout=False)
            except errors.ConnectionError:
                self.failed(f"Failed to establish "
                            f"connection to '{device.name}'")
            device_list.append(device)
        # Pass list of devices to testcases
        self.parent.parameters.update(dev=device_list)


class VerifyLogging(aetest.Testcase):
    """
    VerifyLogging Testcase - collect show logging information from devices
    Verify that all devices do not have 'ERROR|WARN' messages in logs
    """

    @aetest.setup
    def setup(self):
        devices = self.parent.parameters['dev']
        tests_names = [f'Checking errors in logs on "{device.name}"' for device in devices]
        aetest.loop.mark(self.error_logs, device=devices, uids=tests_names)

    @aetest.test
    def error_logs(self, device):
        if device.os == 'asa':
            output = device.execute('show logging | i ASA-[1-3]')
        else:
            output = device.execute('show logging | i ERROR|WARN')

        if len(output) > 0:
            if len(output.splitlines()) <= 10:
                self.failed(banner(f'Found ERROR in log:\n{output}'))
            else:
                for log_line in output.splitlines()[:9]:
                    log.error(banner(f'Found ERROR in log: {log_line}'))
                self.failed(banner(f'Output is too huge. Please check the rest of log on device'))
        else:
            pass


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--testbed', dest='testbed',
                        type=loader.load)

    args, unknown = parser.parse_known_args()

    aetest.main(**vars(args))

#!/usr/bin/env python3 # To get a logger for the script # Genie Imports # To handle errors with connections to devices # Get your logger for your script CommonSetup class to prepare for testcases     Establishes connections to all devices in testbed Establishes connections to all devices in testbed         :param testbed:         :return: # Pass list of devices to testcases VerifyLogging Testcase - collect show logging information from devices     Verify that all devices do not have 'ERROR|WARN' messages in logs