示例#1
0
def test_get_config_file_when_config_file_not_exists(mocker):
    mocked_os = mocker.patch("connord.resources.os")
    mocked_os.path.exists.return_value = False

    try:
        resources.get_config_file()
        assert False
    except resources.ResourceNotFoundError as error:
        actual_message = str(error)

    assert actual_message.startswith("Resource does not exist:")
示例#2
0
def test_get_config_file_when_global_config_file_exists(mocker):
    mocked_os = mocker.patch("connord.resources.os")
    mocked_os.path.exists.return_value = True

    actual_config_file = resources.get_config_file()
    expected_config_file = "/etc/connord/config.yml"

    assert expected_config_file == actual_config_file
示例#3
0
def test_get_config_file_when_path_not_exists(mocker):
    # setup
    mockbase = MockBase("resources")
    mockbase.setup(resources)
    test_dir = "/test/dir"
    mocked_config_dir = _mock_get_config_dir(mocker, test_dir)
    mocked_exists = mockbase.mock_os_any(mocker, "path.exists", False)
    expected_result = test_dir + "/config.yml"

    # run and assert
    try:
        resources.get_config_file()
        assert False
    except resources.ResourceNotFoundError as error:
        assert error.resource_file == expected_result

    mocked_config_dir.assert_called_once()
    mocked_exists.assert_called_once_with(expected_result)
示例#4
0
def render_template(config_file, server=None, protocol=None):
    """Render a jinja2 template with data from config.yml per default. Adds some
    useful variables to the environment which can be uses in rules and fallback
    files.

    :param config_file: the template
    :param server: if None this defaults to 0.0.0.0/0
    :param protocol: if None this defaults to 'udp'
    :returns: the rendered template as string
    """
    config_data_file = resources.get_config_file()
    env = Environment(
        loader=FileSystemLoader(os.path.dirname(config_data_file)),
        trim_blocks=True,
        lstrip_blocks=True,
    )
    with open(config_data_file, "r") as config_data:
        config_data_dict = yaml.safe_load(config_data)
        if server:
            config_data_dict["vpn_remote"] = server["ip_address"]
        else:
            config_data_dict["vpn_remote"] = "0.0.0.0/0"

        if protocol:
            config_data_dict["vpn_protocol"] = protocol
        else:
            config_data_dict["vpn_protocol"] = "udp"
            protocol = "udp"

        if protocol == "udp":
            config_data_dict["vpn_port"] = "1194"
        elif protocol == "tcp":
            config_data_dict["vpn_port"] = "443"
        else:
            raise TypeError("Unknown protocol '{}'.".format(protocol))

        default_ip, default_iface = get_default_gateway(ipv6=False)
        if default_iface:
            try:
                interface_addresses = get_interface_addresses(default_iface)
                config_data_dict["gateway"] = {
                    "ip_address": default_ip,
                    "interface": default_iface,
                }
                config_data_dict["lan"] = interface_addresses
            except KeyError:
                config_data_dict["gateway"] = {}
                config_data_dict["lan"] = {}

        config_data_dict = merge_environment(config_data_dict)
        template = env.get_template(os.path.basename(config_file))
        return template.render(config_data_dict)
示例#5
0
def test_get_config_file_when_path_exists(mocker):
    # setup
    mockbase = MockBase("resources")
    mockbase.setup(resources)
    test_dir = "/test/dir"
    mocked_config_dir = _mock_get_config_dir(mocker, test_dir)
    mocked_exists = mockbase.mock_os_any(mocker, "path.exists", True)
    expected_result = test_dir + "/config.yml"

    # run
    actual_result = resources.get_config_file()

    # assert
    mocked_config_dir.assert_called_once()
    mocked_exists.assert_called_once_with(expected_result)
    assert actual_result == expected_result