예제 #1
0
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 (return_value == 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"
예제 #2
0
def test_function_return_value(capsys, r1_test_connection,
                               first_router_from_devices_yaml):
    """
    Function check
    """
    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, "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"

    # by default, log should be True and a message should be printed to stdout
    correct_stdout = f"connecting to {r1_test_connection.host}"
    stdout, err = capsys.readouterr()
    assert stdout != "", "Error message not printed to stdout"
    assert correct_stdout in stdout.lower(), "Wrong error message printed"

    # check that with log=False there is no output to stdout
    return_value = task_18_2a.send_config_commands(
        first_router_from_devices_yaml, test_commands, log=False)
    correct_stdout = ""
    stdout, err = capsys.readouterr()
    assert (
        correct_stdout == stdout
    ), "The error message should not be printed to stdout when log=False"