def test_function_return_value_different_args(): """ Проверка работы функции с другими аргументами """ correct_return_value = [ ["network", "mask", "distance", "metric", "nexthop"], ["10.0.24.0", "24", "110", "20", ["10.0.12.2"]], ["10.0.34.0", "24", "110", "20", ["10.0.13.3"]], ["10.2.2.2", "32", "110", "11", ["10.0.12.2"]], ["10.3.3.3", "32", "110", "11", ["10.0.13.3"]], ["10.4.4.4", "32", "110", "21", ["10.0.13.3", "10.0.12.2", "10.0.14.4"]], ["10.5.35.0", "24", "110", "20", ["10.0.13.3"]], ] with open("output/sh_ip_route_ospf.txt") as f: sh_ip_int_br = f.read() template = "templates/sh_ip_route_ospf.template" return_value = task_21_1.parse_command_output(template, sh_ip_int_br) assert return_value != None, "Функция ничего не возвращает" assert ( type(return_value) == list ), f"По заданию функция должна возвращать список, а возвращает {type(return_value).__name__}" assert ( correct_return_value == return_value ), "Функция возвращает неправильное значение"
def test_function_return_value_different_args(): """ Checking the function with different arguments """ correct_return_value = [ ["network", "mask", "distance", "metric", "nexthop"], ["10.0.24.0", "24", "110", "20", ["10.0.12.2"]], ["10.0.34.0", "24", "110", "20", ["10.0.13.3"]], ["10.2.2.2", "32", "110", "11", ["10.0.12.2"]], ["10.3.3.3", "32", "110", "11", ["10.0.13.3"]], [ "10.4.4.4", "32", "110", "21", ["10.0.13.3", "10.0.12.2", "10.0.14.4"] ], ["10.5.35.0", "24", "110", "20", ["10.0.13.3"]], ] with open("output/sh_ip_route_ospf.txt") as f: sh_ip_int_br = f.read() template = "templates/sh_ip_route_ospf.template" return_value = task_21_1.parse_command_output(template, sh_ip_int_br) assert return_value != None, "The function returns None" assert ( type(return_value) == list ), f"The function should return a list, instead it returns a {type(return_value).__name__}" assert ( return_value == correct_return_value), "Function returns wrong value"
def test_function_return_value(): """ Проверка работы функции """ correct_return_value = [ ["intf", "address", "status", "protocol"], ["FastEthernet0/0", "15.0.15.1", "up", "up"], ["FastEthernet0/1", "10.0.12.1", "up", "up"], ["FastEthernet0/2", "10.0.13.1", "up", "up"], ["FastEthernet0/3", "unassigned", "up", "up"], ["Loopback0", "10.1.1.1", "up", "up"], ["Loopback100", "100.0.0.1", "up", "up"], ] with open("output/sh_ip_int_br.txt") as f: sh_ip_int_br = f.read() template = "templates/sh_ip_int_br.template" return_value = task_21_1.parse_command_output(template, sh_ip_int_br) assert return_value != None, "Функция ничего не возвращает" assert ( type(return_value) == list ), f"По заданию функция должна возвращать список, а возвращает {type(return_value).__name__}" assert ( correct_return_value == return_value ), "Функция возвращает неправильное значение"
def parse_output_to_dict(template, command_output): dict_list = [] b = parse_command_output(template, command_output) keys, values = b[0], b[1:] for each in values: dict1 = dict(zip(keys, each)) dict_list.append(dict1) return dict_list
def test_template(): correct_return_value = [ ["mac", "ip", "vlan", "intf"], ["00:09:BB:3D:D6:58", "10.1.10.2", "10", "FastEthernet0/1"], ["00:04:A3:3E:5B:69", "10.1.5.2", "5", "FastEthernet0/10"], ["00:05:B3:7E:9B:60", "10.1.5.4", "5", "FastEthernet0/9"], ["00:09:BC:3F:A6:50", "10.1.10.6", "10", "FastEthernet0/3"], ] with open("output/sh_ip_dhcp_snooping.txt") as show: sh_ip_dhcp_snoop = show.read() template = "templates/sh_ip_dhcp_snooping.template" return_value = task_21_1.parse_command_output(template, sh_ip_dhcp_snoop) assert ( return_value == correct_return_value ), "Template templates/sh_ip_dhcp_snooping.template does not parse data correctly"
def test_template(): """ Проверка работы шаблона """ correct_return_value = [ ["mac", "ip", "vlan", "intf"], ["00:09:BB:3D:D6:58", "10.1.10.2", "10", "FastEthernet0/1"], ["00:04:A3:3E:5B:69", "10.1.5.2", "5", "FastEthernet0/10"], ["00:05:B3:7E:9B:60", "10.1.5.4", "5", "FastEthernet0/9"], ["00:09:BC:3F:A6:50", "10.1.10.6", "10", "FastEthernet0/3"], ] with open("output/sh_ip_dhcp_snooping.txt") as show: sh_ip_dhcp_snoop = show.read() template = "templates/sh_ip_dhcp_snooping.template" return_value = task_21_1.parse_command_output(template, sh_ip_dhcp_snoop) assert ( return_value == correct_return_value ), "Шаблон templates/sh_ip_dhcp_snooping.template неправильно парсит данные"
def test_function_return_value(): """ Function check """ correct_return_value = [ ["intf", "address", "status", "protocol"], ["FastEthernet0/0", "15.0.15.1", "up", "up"], ["FastEthernet0/1", "10.0.12.1", "up", "up"], ["FastEthernet0/2", "10.0.13.1", "up", "up"], ["FastEthernet0/3", "unassigned", "up", "up"], ["Loopback0", "10.1.1.1", "up", "up"], ["Loopback100", "100.0.0.1", "up", "up"], ] with open("output/sh_ip_int_br.txt") as f: sh_ip_int_br = f.read() template = "templates/sh_ip_int_br.template" return_value = task_21_1.parse_command_output(template, sh_ip_int_br) assert return_value != None, "The function returns None" assert ( type(return_value) == list ), f"The function should return a list, instead it returns a {type(return_value).__name__}" assert correct_return_value == return_value, "Function returns wrong value"
# -*- coding: utf-8 -*- """ Задание 21.2 Сделать шаблон TextFSM для обработки вывода sh ip dhcp snooping binding и записать его в файл templates/sh_ip_dhcp_snooping.template Вывод команды находится в файле output/sh_ip_dhcp_snooping.txt. Шаблон должен обрабатывать и возвращать значения таких столбцов: * mac - такого вида 00:04:A3:3E:5B:69 * ip - такого вида 10.1.10.6 * vlan - 10 * intf - FastEthernet0/10 Проверить работу шаблона с помощью функции parse_command_output из задания 21.1. """ from task_21_1 import parse_command_output with open('output/sh_ip_dhcp_snooping.txt') as f: print(parse_command_output('templates/sh_ip_dhcp_snooping.template', f.read()) )
# -*- coding: utf-8 -*- """ Задание 21.2 Сделать шаблон TextFSM для обработки вывода sh ip dhcp snooping binding и записать его в файл templates/sh_ip_dhcp_snooping.template Вывод команды находится в файле output/sh_ip_dhcp_snooping.txt. Шаблон должен обрабатывать и возвращать значения таких столбцов: * mac - такого вида 00:04:A3:3E:5B:69 * ip - такого вида 10.1.10.6 * vlan - 10 * intf - FastEthernet0/10 Проверить работу шаблона с помощью функции parse_command_output из задания 21.1. """ from task_21_1 import parse_command_output from pprint import pprint if __name__ == "__main__": output = open('output/sh_ip_dhcp_snooping.txt').read() template = 'templates/sh_ip_dhcp_snooping.template' pprint(parse_command_output(template, output))
Вывод команды находится в файле output/sh_ip_dhcp_snooping.txt. Шаблон должен обрабатывать и возвращать значения таких столбцов: * mac - такого вида 00:04:A3:3E:5B:69 * ip - такого вида 10.1.10.6 * vlan - 10 * intf - FastEthernet0/10 Проверить работу шаблона с помощью функции parse_command_output из задания 21.1. """ from tabulate import tabulate from task_21_1 import parse_command_output import re if __name__ == "__main__": regex = r'(\S+) +(\S+) +\S+ +\S+ +(\d+) +(\S+)' regex1 = r'(\S+) +(\S+) +\S+ +\S+ +(\d+) +(\S+)' output = "output/sh_ip_dhcp_snooping.txt" template = "templates/sh_ip_dhcp_snooping.template" with open(output) as f, open(template) as t: #match = re.finditer(regex1, f.read()) #list1 = [m.groups() for m in match] #print(list1) #print(f.read()) result = parse_command_output("templates/sh_ip_dhcp_snooping.template", f.read()) print(result) print(tabulate(result))