def test_function_return_value(): data = { "tun_num": 17, "wan_ip_1": "80.241.1.1", "wan_ip_2": "90.18.10.2", "tun_ip_1": "10.255.1.1 255.255.255.252", "tun_ip_2": "10.255.1.2 255.255.255.252", } correct_value_1 = ("interface Tunnel 17\n" "ip address 10.255.1.1 255.255.255.252\n" "tunnel source 80.241.1.1\n" "tunnel destination 90.18.10.2\n" "tunnel protection ipsec profile GRE") correct_value_2 = ("interface Tunnel 17\n" "ip address 10.255.1.2 255.255.255.252\n" "tunnel source 90.18.10.2\n" "tunnel destination 80.241.1.1\n" "tunnel protection ipsec profile GRE") template1 = "templates/gre_ipsec_vpn_1.txt" template2 = "templates/gre_ipsec_vpn_2.txt" return_cfg1, return_cfg2 = task_20_5.create_vpn_config( template1, template2, data) return_cfg1 = strip_empty_lines(return_cfg1) return_cfg2 = strip_empty_lines(return_cfg2) assert ( correct_value_1 in return_cfg1 ), "В итоговой конфигурации неправильно указаны настройки Tunnel для первой стороны" assert ( correct_value_2 in return_cfg2 ), "В итоговой конфигурации неправильно указаны настройки Tunnel для второй стороны"
def test_send_show_command_different_command(first_router_from_devices_yaml, r1_test_telnet_connection): r1 = task_22_2.CiscoTelnet(**first_router_from_devices_yaml) correct_return_value = strip_empty_lines( r1_test_telnet_connection.send_command("sh ip int | i address")) return_value = strip_empty_lines( r1.send_show_command("sh ip int | i address")) assert (correct_return_value in return_value ), "Метод send_show_command возвращает неправильное значение"
def test_send_show_command_different_command(first_router_from_devices_yaml, r1_test_telnet_connection): r1 = task_22_2.CiscoTelnet(**first_router_from_devices_yaml) correct_return_value = strip_empty_lines( r1_test_telnet_connection.send_command("sh ip int | i address")) return_value = strip_empty_lines( r1.send_show_command("sh ip int | i address")) assert (correct_return_value in return_value), "send_show_command method returns wrong value"
def test_send_show_command(first_router_from_devices_yaml, r1_test_telnet_connection): r1 = task_22_2.CiscoTelnet(**first_router_from_devices_yaml) check_attr_or_method(r1, method="_write_line") check_attr_or_method(r1, method="send_show_command") correct_return_value = strip_empty_lines( r1_test_telnet_connection.send_command("sh ip int br")) return_value = strip_empty_lines(r1.send_show_command("sh ip int br")) assert (correct_return_value in return_value ), "Метод send_show_command возвращает неправильное значение"
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_send_show_command_parse_false(first_router_from_devices_yaml, r1_test_telnet_connection): r1 = task_22_2a.CiscoTelnet(**first_router_from_devices_yaml) correct_return_value = r1_test_telnet_connection.send_command( "sh ip int br", strip_command=False, strip_prompt=False) return_value = r1.send_show_command("sh ip int br", parse=False, templates="templates", index="index") assert strip_empty_lines(correct_return_value) == strip_empty_lines( return_value ), "send_show_command method returns wrong value with parse=False"
def test_send_show_command_parse_false(first_router_from_devices_yaml, r1_test_telnet_connection): r1 = task_22_2a.CiscoTelnet(**first_router_from_devices_yaml) correct_return_value = r1_test_telnet_connection.send_command( "sh ip int br", strip_command=False, strip_prompt=False) return_value = r1.send_show_command("sh ip int br", parse=False, templates="templates", index="index") assert strip_empty_lines(correct_return_value) == strip_empty_lines( return_value ), "Метод send_show_command возвращает неправильное значение с parse=False"
def test_function_return_value_different_args(r1_test_connection, first_router_from_devices_yaml): """ Checking the function with different arguments """ correct_return_value = r1_test_connection.send_command( "sh int description") return_value = task_18_1.send_show_command(first_router_from_devices_yaml, "sh int description") 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(return_value) == strip_empty_lines( correct_return_value), "Function returns wrong value"
def test_function_return_value_different_args(r1_test_connection, first_router_from_devices_yaml): """ Проверка работы функции с другими аргументами """ correct_return_value = r1_test_connection.send_command( "sh int description") return_value = task_18_1.send_show_command(first_router_from_devices_yaml, "sh int description") assert return_value != None, "Функция ничего не возвращает" assert ( type(return_value) == str ), f"По заданию функция должна возвращать строку, а возвращает {type(return_value).__name__}" assert strip_empty_lines(return_value) == strip_empty_lines( correct_return_value), "Функция возвращает неправильное значение"
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(r1_test_connection, first_router_from_devices_yaml): """ first_router_from_devices_yaml - is the first device from the devices.yaml file r1_test_connection - is the SSH session with the first device from the devices.yaml file. Used to check the output """ correct_return_value = r1_test_connection.send_command("sh ip int br") return_value = task_18_1.send_show_command(first_router_from_devices_yaml, "sh ip int br") 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(return_value) == strip_empty_lines( correct_return_value), "Function returns wrong value"
def unified_columns_output(output): output = strip_empty_lines(output) lines = [ re.split(r" +", line.strip()) for line in output.strip().split("\n") ] formatted = [("{:25}" * len(line)).format(*line) for line in lines] return "\n".join(formatted)
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(r1_test_connection, first_router_from_devices_yaml): """ Тест проверяет работу функции send_show_command first_router_from_devices_yaml - это первое устройство из файла devices.yaml r1_test_connection - это сессия SSH с первым устройством из файла devices.yaml Используется для проверки вывода """ correct_return_value = r1_test_connection.send_command("sh ip int br") return_value = task_18_1.send_show_command(first_router_from_devices_yaml, "sh ip int br") assert return_value != None, "Функция ничего не возвращает" assert ( type(return_value) == str ), f"По заданию функция должна возвращать строку, а возвращает {type(return_value).__name__}" assert strip_empty_lines(return_value) == strip_empty_lines( correct_return_value), "Функция возвращает неправильное значение"
def test_function_return_value_different_args(r1_test_connection, first_router_from_devices_yaml): """ Checking the function with different arguments """ test_commands = [ "interface Loopback 100", "ip address 10.1.1.100 255.255.255.255", ] correct_return_value = r1_test_connection.send_config_set(test_commands) return_value = task_18_2.send_config_commands( first_router_from_devices_yaml, test_commands) 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(return_value) == strip_empty_lines( correct_return_value), "Function returns wrong value"
def test_function_return_value_different_args(r1_test_connection, first_router_from_devices_yaml): """ Проверка работы функции с другими аргументами """ test_commands = [ "interface Loopback 100", "ip address 10.1.1.100 255.255.255.255", ] correct_return_value = r1_test_connection.send_config_set(test_commands) return_value = task_18_2.send_config_commands( first_router_from_devices_yaml, test_commands) assert return_value != None, "Функция ничего не возвращает" assert ( type(return_value) == str ), f"По заданию функция должна возвращать строку, а возвращает {type(return_value).__name__}" assert strip_empty_lines(return_value) == strip_empty_lines( correct_return_value), "Функция возвращает неправильное значение"
def test_function_return_value(r1_test_connection, first_router_from_devices_yaml): """ Function check """ test_commands = [ "logging 10.255.255.1", "logging buffered 20010", "no logging console", ] correct_return_value = r1_test_connection.send_config_set(test_commands) return_value = task_18_2.send_config_commands( first_router_from_devices_yaml, test_commands) 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(return_value) == strip_empty_lines( correct_return_value), "Function returns wrong value"
def test_function_return_value(r1_test_connection, first_router_from_devices_yaml): """ Проверка работы функции """ test_commands = [ "logging 10.255.255.1", "logging buffered 20010", "no logging console", ] correct_return_value = r1_test_connection.send_config_set(test_commands) return_value = task_18_2.send_config_commands( first_router_from_devices_yaml, test_commands) assert return_value != None, "Функция ничего не возвращает" assert ( type(return_value) == str ), f"По заданию функция должна возвращать строку, а возвращает {type(return_value).__name__}" assert strip_empty_lines(return_value) == strip_empty_lines( correct_return_value), "Функция возвращает неправильное значение"
def test_function_return_value( capsys, r1_test_connection, first_router_from_devices_yaml ): """ Проверка работы функции """ test_commands = [ "interface Loopback 100", "ip address 10.1.1.100 255.255.255.255", ] correct_return_value = r1_test_connection.send_config_set(test_commands) return_value = task_18_2a.send_config_commands( first_router_from_devices_yaml, test_commands ) # проверяем возвращаемое значение assert return_value != None, "Функция ничего не возвращает" assert ( type(return_value) == str ), f"По заданию функция должна возвращать строку, а возвращает {type(return_value).__name__}" assert ( strip_empty_lines(return_value) == strip_empty_lines(correct_return_value) ), "Функция возвращает неправильное значение" # по умолчанию, log должно быть равным True # и на stdout должно выводиться сообщение correct_stdout = f"{r1_test_connection.host}" out, err = capsys.readouterr() assert out != "", "Сообщение об ошибке не выведено на stdout" assert correct_stdout in out, "Выведено неправильное сообщение об ошибке" # проверяем, что с log=False вывода в stdout нет return_value = task_18_2a.send_config_commands( first_router_from_devices_yaml, test_commands, log=False ) correct_stdout = "" out, err = capsys.readouterr() assert ( out == correct_stdout ), "Сообщение об ошибке не должно выводиться на stdout, когда log=False"
def test_function_return_value_from_single_device( three_routers_from_devices_yaml, r1_r2_r3_test_connection, tmpdir, device, command): """ Проверка работы функции """ ssh = create_ssh_connect(device) output = f"{ssh.find_prompt()}{command}\n{ssh.send_command(command)}\n" ssh.disconnect() dest_filename = tmpdir.mkdir("test_tasks").join("task_19_2.txt") return_value = task_19_2.send_show_command_to_devices( devices=[device], command=command, filename=dest_filename, limit=3, ) assert return_value == None, "Функция должна возвращать None" dest_file_content = dest_filename.read().strip() assert strip_empty_lines(output) == strip_empty_lines( dest_file_content), f"В итоговом файле нет вывода с {device['host']}"
def test_function_return_value_from_single_device( three_routers_from_devices_yaml, r1_r2_r3_test_connection, tmpdir, device, command): """ Function check """ ssh = create_ssh_connect(device) correct_output = strip_empty_lines( f"{ssh.find_prompt()}{command}\n{ssh.send_command(command)}\n") ssh.disconnect() dest_filename = tmpdir.mkdir("test_tasks").join("task_19_2.txt") return_value = task_19_2.send_show_command_to_devices( devices=[device], command=command, filename=dest_filename, limit=3, ) assert None == return_value, "The function must return None" dest_file_content = strip_empty_lines(dest_filename.read().strip()) assert (correct_output == dest_file_content ), f"Output file does not have output from {device['host']}"
def test_function_return_value(): data = { "tun_num": 17, "wan_ip_1": "80.241.1.1", "wan_ip_2": "90.18.10.2", "tun_ip_1": "10.255.1.1 255.255.255.252", "tun_ip_2": "10.255.1.2 255.255.255.252", } correct_value_1 = ( "interface Tunnel 17\n" "ip address 10.255.1.1 255.255.255.252\n" "tunnel source 80.241.1.1\n" "tunnel destination 90.18.10.2\n" "tunnel protection ipsec profile GRE" ) correct_value_2 = ( "interface Tunnel 17\n" "ip address 10.255.1.2 255.255.255.252\n" "tunnel source 90.18.10.2\n" "tunnel destination 80.241.1.1\n" "tunnel protection ipsec profile GRE" ) template1 = "templates/gre_ipsec_vpn_1.txt" template2 = "templates/gre_ipsec_vpn_2.txt" return_cfg1, return_cfg2 = task_20_5.create_vpn_config(template1, template2, data) return_cfg1 = strip_empty_lines(return_cfg1) return_cfg2 = strip_empty_lines(return_cfg2) assert ( correct_value_1 in return_cfg1 ), "In the final configuration, the Tunnel settings for the first side are incorrect" assert ( correct_value_2 in return_cfg2 ), "In the final configuration, the Tunnel settings for the second side are incorrect"
def test_function_different_input(): correct_return_value = ("router ospf 1\n" "router-id 10.0.0.1\n" "auto-cost reference-bandwidth 30000\n" "network 10.55.1.1 0.0.0.0 area 0\n" "network 10.55.2.1 0.0.0.0 area 5\n" "network 10.55.3.1 0.0.0.0 area 5\n" "passive-interface Fa0/1.100\n" "passive-interface Fa0/1.200\n" "passive-interface Fa0/1.300\n") template = "templates/ospf.txt" data = { "ospf_intf": [ { "area": 0, "ip": "10.55.1.1", "name": "Fa0/1.100", "passive": True }, { "area": 5, "ip": "10.55.2.1", "name": "Fa0/1.200", "passive": True }, { "area": 5, "ip": "10.55.3.1", "name": "Fa0/1.300", "passive": True }, ], "process": 1, "ref_bw": 30000, "router_id": "10.0.0.1", } return_value = render_jinja_template(template, data) correct_lines = set(correct_return_value.splitlines()) return_value = strip_empty_lines(return_value) return_lines = set(return_value.splitlines()) assert (correct_lines == return_lines ), "Not all lines are present in the router ospf configuration"
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 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 = render_jinja_template(template, data) correct_lines = set(correct_return_value.splitlines()) return_value = strip_empty_lines(return_value) return_lines = set(return_value.splitlines()) assert correct_lines == return_lines, "В итоговой конфигурации ospf не все строки"