def test_function_return_value(): service_section = ( "service timestamps debug datetime msec localtime show-timezone\n" "service timestamps log datetime msec localtime show-timezone\n" "service tcp-keepalives-in\n" "service tcp-keepalives-out\n" "service password-encryption\n") alias_section = ( "alias exec top sh proc cpu sorted | excl 0.00%__0.00%__0.00%\n" "alias exec diff sh archive config differences nvram:startup-config system:running-config\n" "alias exec bri show ip int bri | exc unass\n" "alias exec id show int desc\n") eem_section = ( "event manager applet update-int-desc\n" " event neighbor-discovery interface regexp .*Ethernet.* cdp add\n" ' action 1.0 cli command "enable"\n' ' action 2.0 cli command "config t"\n' ' action 3.0 cli command "interface $_nd_local_intf_name"\n' ' action 4.0 cli command "description To $_nd_cdp_entry_name $_nd_port_id"\n' ) template = "templates/cisco_router_base.txt" data = {"hostname": "R1"} return_value = task_20_1.generate_config(template, data) assert service_section in return_value, "В итоговой конфигурации нет команд service" assert alias_section in return_value, "В итоговой конфигурации нет команд alias" assert ( eem_section in return_value), "В итоговой конфигурации нет настройки event manager" assert data[ "hostname"] in return_value, "В итоговой конфигурации нет hostname"
def test_function_return_value(): correct_value_vlan = "vlan 10\n" "name Marketing" correct_value_access = ("interface Fa0/1\n" "switchport mode access\n" "switchport access vlan 10") correct_value_trunk1 = "interface Fa0/23\n" "switchport trunk allowed vlan add 10\n" correct_value_trunk2 = "interface Fa0/24\n" "switchport trunk allowed vlan add 10" template = "templates/add_vlan_to_switch.txt" data = { "vlan_id": 10, "name": "Marketing", "trunk": ["Fa0/23", "Fa0/24"], "access": ["Fa0/1"], } return_value = task_20_1.generate_config(template, data) return_value = strip_empty_lines(return_value) assert ( correct_value_vlan in return_value ), "В итоговой конфигурации не создан VLAN и/или не назначено имя VLAN" assert (correct_value_access in return_value ), "В итоговой конфигурации неправильная настройка access" assert (correct_value_trunk1 in return_value and correct_value_trunk2 in return_value ), "В итоговой конфигурации неправильная настройка trunk"
def test_function_return_value(): correct_value_vlan = "vlan 10\n" "name Marketing" correct_value_access = ("interface Fa0/1\n" "switchport mode access\n" "switchport access vlan 10") correct_value_trunk1 = "interface Fa0/23\n" "switchport trunk allowed vlan add 10\n" correct_value_trunk2 = "interface Fa0/24\n" "switchport trunk allowed vlan add 10" template = "templates/add_vlan_to_switch.txt" data = { "vlan_id": 10, "name": "Marketing", "trunk": ["Fa0/23", "Fa0/24"], "access": ["Fa0/1"], } return_value = task_20_1.generate_config(template, data) return_value = strip_empty_lines(return_value) assert ( correct_value_vlan in return_value ), "In the configuration, no VLAN was created and/or no VLAN name was assigned" assert (correct_value_access in return_value ), "In the final configuration, incorrect access configuration" assert (correct_value_trunk1 in return_value and correct_value_trunk2 in return_value ), "In the final configuration, incorrect trunk configuration"
def test_function_return_value(): correct_return_value = ("\n" "hostname R3\n" "\n" "interface Loopback0\n" " ip address 10.0.0.3 255.255.255.255\n" "\n" "vlan 10\n" " name Marketing\n" "vlan 20\n" " name Voice\n" "vlan 30\n" " name Management\n" "\n" "router ospf 1\n" " router-id 10.0.0.3\n" " auto-cost reference-bandwidth 10000\n" " network 10.0.1.0 0.0.0.255 area 0\n" " network 10.0.2.0 0.0.0.255 area 2\n" " network 10.1.1.0 0.0.0.255 area 0\n") template = "templates/for.txt" data = { "id": 3, "name": "R3", "vlans": { 10: "Marketing", 20: "Voice", 30: "Management" }, "ospf": [ { "network": "10.0.1.0 0.0.0.255", "area": 0 }, { "network": "10.0.2.0 0.0.0.255", "area": 2 }, { "network": "10.1.1.0 0.0.0.255", "area": 0 }, ], } return_value = task_20_1.generate_config(template, data) assert return_value != None, "The function returns None" assert ( type(return_value) == str ), f"The function must return string, and it returns a {type(return_value).__name__}" assert strip_empty_lines(correct_return_value) == strip_empty_lines( return_value), "Function returns wrong value"
def test_function_return_value(): correct_return_value = ("\n" "hostname R3\n" "\n" "interface Loopback0\n" " ip address 10.0.0.3 255.255.255.255\n" "\n" "vlan 10\n" " name Marketing\n" "vlan 20\n" " name Voice\n" "vlan 30\n" " name Management\n" "\n" "router ospf 1\n" " router-id 10.0.0.3\n" " auto-cost reference-bandwidth 10000\n" " network 10.0.1.0 0.0.0.255 area 0\n" " network 10.0.2.0 0.0.0.255 area 2\n" " network 10.1.1.0 0.0.0.255 area 0\n") template = "templates/for.txt" data = { "id": 3, "name": "R3", "vlans": { 10: "Marketing", 20: "Voice", 30: "Management" }, "ospf": [ { "network": "10.0.1.0 0.0.0.255", "area": 0 }, { "network": "10.0.2.0 0.0.0.255", "area": 2 }, { "network": "10.1.1.0 0.0.0.255", "area": 0 }, ], } return_value = task_20_1.generate_config(template, data) assert return_value != None, "Функция ничего не возвращает" assert ( type(return_value) == str ), f"По заданию функция должна возвращать строку, а возвращает {type(return_value).__name__}" assert strip_empty_lines(correct_return_value) == strip_empty_lines( return_value), "Функция возвращает неправильное значение"
def test_function_return_value(): correct_return_value_router = ( "router ospf 10\n" "router-id 10.0.0.1\n" "auto-cost reference-bandwidth 20000\n" "network 10.255.0.1 0.0.0.0 area 0\n" "network 10.255.1.1 0.0.0.0 area 0\n" "network 10.255.2.1 0.0.0.0 area 0\n" "network 10.0.10.1 0.0.0.0 area 2\n" "network 10.0.20.1 0.0.0.0 area 2\n" "passive-interface Fa0/0.10\n" "passive-interface Fa0/0.20\n" ) correct_return_value_intf = ( "interface Fa0/1\n" "ip ospf hello-interval 1\n" "interface Fa0/1.100\n" "ip ospf hello-interval 1\n" "interface Fa0/1.200\n" "ip ospf hello-interval 1\n" ) template = "templates/ospf.txt" data = { "ospf_intf": [ {"area": 0, "ip": "10.255.0.1", "name": "Fa0/1", "passive": False}, {"area": 0, "ip": "10.255.1.1", "name": "Fa0/1.100", "passive": False}, {"area": 0, "ip": "10.255.2.1", "name": "Fa0/1.200", "passive": False}, {"area": 2, "ip": "10.0.10.1", "name": "Fa0/0.10", "passive": True}, {"area": 2, "ip": "10.0.20.1", "name": "Fa0/0.20", "passive": True}, ], "process": 10, "ref_bw": 20000, "router_id": "10.0.0.1", } return_value = task_20_1.generate_config(template, data) correct_lines_router = set(correct_return_value_router.splitlines()) correct_lines_interface = set(correct_return_value_intf.splitlines()) return_value = strip_empty_lines(return_value) return_lines = set(return_value.splitlines()) # проверяем что строки из correct_return_value_router содержатся в return_value assert correct_lines_router.issubset( return_lines ), "В итоговой конфигурации режима router ospf не все строки" # проверяем что строки из correct_return_value_intf содержатся в return_value assert correct_lines_interface.issubset( return_lines ), "В итоговой конфигурации интерфейсов не все строки"
Если VLAN необходимо добавить как access, надо настроить и режим интерфейса и добавить его в VLAN: interface Gi0/1 switchport mode access switchport access vlan 5 Для транков, необходимо только добавить VLAN в список разрешенных: interface Gi0/10 switchport trunk allowed vlan add 5 Имена переменных надо выбрать на основании примера данных, в файле data_files/add_vlan_to_switch.yaml. Проверьте шаблон templates/add_vlan_to_switch.txt на данных в файле data_files/add_vlan_to_switch.yaml, с помощью функции generate_config из задания 20.1. Не копируйте код функции generate_config. """ import yaml from task_20_1 import generate_config if __name__ == '__main__': with open('data_files/add_vlan_to_switch.yaml') as f: data = yaml.safe_load(f) print(generate_config('templates/add_vlan_to_switch.txt', data))
passive-interface Fa0/0.20 interface Fa0/1 ip ospf hello-interval 1 interface Fa0/1.100 ip ospf hello-interval 1 interface Fa0/1.200 ip ospf hello-interval 1 """ import yaml from task_20_1 import generate_config if __name__ == "__main__": with open("data_files/ospf.yml") as f: data = yaml.load(f, Loader=yaml.FullLoader) print(generate_config("templates/ospf.txt", data)) # templates/ospf.txt """ router ospf {{ process }} router-id {{ router_id }} auto-cost reference-bandwidth {{ ref_bw }} {% for intf in ospf_intf %} network {{ intf.ip}} 0.0.0.0 area {{ intf.area }} {% if intf.passive %} passive-interface {{ intf.name }} {% endif %} {% endfor %} {% for intf in ospf_intf if not intf.passive %} interface {{ intf.name }}
def create_mirror_config(dev, tmpl, data): return (generate_config(tmpl, data))
Создать шаблон templates/cisco_router_base.txt. В шаблон templates/cisco_router_base.txt должно быть включено содержимое шаблонов: * templates/cisco_base.txt * templates/alias.txt * templates/eem_int_desc.txt При этом, нельзя копировать текст шаблонов. Проверьте шаблон templates/cisco_router_base.txt, с помощью функции generate_config из задания 20.1. Не копируйте код функции generate_config. В качестве данных, используйте информацию из файла data_files/router_info.yml {% include 'cisco_base.txt' %} {% include 'alias.txt' %} {% include 'eem_int_desc.txt' %} """ import task_20_1 as g import yaml if __name__ == "__main__": data_file = "data_files/router_info.yml" template_file = "templates/cisco_router_base.txt" with open(data_file) as f: data = yaml.safe_load(f) print(g.generate_config(template_file, data))
def test_function_return_value(): correct_return_value = ("router ospf 10\n" "router-id 10.0.0.1\n" "auto-cost reference-bandwidth 20000\n" "network 10.255.0.1 0.0.0.0 area 0\n" "network 10.255.1.1 0.0.0.0 area 0\n" "network 10.255.2.1 0.0.0.0 area 0\n" "network 10.0.10.1 0.0.0.0 area 2\n" "network 10.0.20.1 0.0.0.0 area 2\n" "passive-interface Fa0/0.10\n" "passive-interface Fa0/0.20\n" "interface Fa0/1\n" "ip ospf hello-interval 1\n" "interface Fa0/1.100\n" "ip ospf hello-interval 1\n" "interface Fa0/1.200\n" "ip ospf hello-interval 1\n") template = "templates/ospf.txt" data = { "ospf_intf": [ { "area": 0, "ip": "10.255.0.1", "name": "Fa0/1", "passive": False }, { "area": 0, "ip": "10.255.1.1", "name": "Fa0/1.100", "passive": False }, { "area": 0, "ip": "10.255.2.1", "name": "Fa0/1.200", "passive": False }, { "area": 2, "ip": "10.0.10.1", "name": "Fa0/0.10", "passive": True }, { "area": 2, "ip": "10.0.20.1", "name": "Fa0/0.20", "passive": True }, ], "process": 10, "ref_bw": 20000, "router_id": "10.0.0.1", } return_value = task_20_1.generate_config(template, data) correct_lines = set(correct_return_value.splitlines()) return_value = strip_empty_lines(return_value) return_lines = set(return_value.splitlines()) assert return_lines == correct_lines, "В итоговой конфигурации ospf не все строки"
def test_function_return_value(): correct_return_value_router = ("router ospf 10\n" "router-id 10.0.0.1\n" "auto-cost reference-bandwidth 20000\n" "network 10.255.0.1 0.0.0.0 area 0\n" "network 10.255.1.1 0.0.0.0 area 0\n" "network 10.255.2.1 0.0.0.0 area 0\n" "network 10.0.10.1 0.0.0.0 area 2\n" "network 10.0.20.1 0.0.0.0 area 2\n" "passive-interface Fa0/0.10\n" "passive-interface Fa0/0.20\n") correct_return_value_intf = ("interface Fa0/1\n" "ip ospf hello-interval 1\n" "interface Fa0/1.100\n" "ip ospf hello-interval 1\n" "interface Fa0/1.200\n" "ip ospf hello-interval 1\n") template = "templates/ospf.txt" data = { "ospf_intf": [ { "area": 0, "ip": "10.255.0.1", "name": "Fa0/1", "passive": False }, { "area": 0, "ip": "10.255.1.1", "name": "Fa0/1.100", "passive": False }, { "area": 0, "ip": "10.255.2.1", "name": "Fa0/1.200", "passive": False }, { "area": 2, "ip": "10.0.10.1", "name": "Fa0/0.10", "passive": True }, { "area": 2, "ip": "10.0.20.1", "name": "Fa0/0.20", "passive": True }, ], "process": 10, "ref_bw": 20000, "router_id": "10.0.0.1", } return_value = task_20_1.generate_config(template, data) correct_lines_router = set(correct_return_value_router.splitlines()) correct_lines_interface = set(correct_return_value_intf.splitlines()) return_value = strip_empty_lines(return_value) return_lines = set(return_value.splitlines()) assert correct_lines_router.issubset( return_lines ), "Not all lines are present in the router ospf configuration" assert correct_lines_interface.issubset( return_lines ), "Not all lines are present in the final interface configuration"
def create_vpn_config(template1, template2, data_dict): return (generate_config(template1, data_dict), generate_config(template2, data_dict))
passive-interface Fa0/0.10 passive-interface Fa0/0.20 interface Fa0/1 ip ospf hello-interval 1 interface Fa0/1.100 ip ospf hello-interval 1 interface Fa0/1.200 ip ospf hello-interval 1 """ import yaml from task_20_1 import generate_config if __name__ == '__main__': with open('data_files/ospf.yml') as f: data = yaml.load(f, Loader=yaml.FullLoader) print(generate_config('templates/ospf.txt', data)) # templates/ospf.txt ''' router ospf {{ process }} router-id {{ router_id }} auto-cost reference-bandwidth {{ ref_bw }} {% for intf in ospf_intf%} network {{ intf.ip}} 0.0.0.0 area {{ intf.area}} {% if intf.passive %} passive-interface {{ intf.name}} {% endif %} {% endfor %} {% for intf in ospf_intf if not intf.passive %} interface {{ intf.name }}
def create_vpn_config(template1, template2, data_dict): a = generate_config(template1, data_dict) b = generate_config(template2, data_dict) return a, b
def create_vpn_config(template1, template2, data_dict): cfg1 = generate_config(template1, data_dict) cfg2 = generate_config(template2, data_dict) return cfg1, cfg2
# -*- coding: utf-8 -*- """ Задание 20.2 Создать шаблон templates/cisco_router_base.txt. В шаблон templates/cisco_router_base.txt должно быть включено содержимое шаблонов: * templates/cisco_base.txt * templates/alias.txt * templates/eem_int_desc.txt При этом, нельзя копировать текст шаблонов. Проверьте шаблон templates/cisco_router_base.txt, с помощью функции generate_config из задания 20.1. Не копируйте код функции generate_config. В качестве данных, используйте информацию из файла data_files/router_info.yml """ import yaml from task_20_1 import generate_config if __name__ == '__main__': with open('data_files/router_info.yml') as f: data = yaml.safe_load(f) print(generate_config('templates/cisco_router_base.txt', data))