def get_config_path(table, fallback=False, ipv6=False): """Return rules path of the table""" if fallback: if ipv6: table_regex = re.compile(r"[0-9]*[-]?{}6.fallback".format(table)) else: table_regex = re.compile(r"[0-9]*[-]?{}.fallback".format(table)) file_list = resources.list_config_dir(filetype="fallback") else: if ipv6: table_regex = re.compile(r"[0-9]*[-]?{}6.rules".format(table)) else: table_regex = re.compile(r"[0-9]*[-]?{}.rules".format(table)) file_list = resources.list_config_dir(filetype="rules") configs_found = [] for file_ in file_list: result = table_regex.search(file_) if result: configs_found.append(file_) if not configs_found: if fallback: error_message = ( "Could not find a fallback configuration for '{}' " "table.").format(table) else: error_message = "Could not find a configuration for '{}' table.".format( table) raise resources.ResourceNotFoundError(table, error_message) elif len(configs_found) > 1: raise IptablesError("Ambiguous file paths: {}.".format(configs_found)) return configs_found[0]
def test_list_config_dir_when_filetype_is_set_and_list_has_no_files(mocker): # setup mockbase = MockBase("resources") mockbase.setup(resources) test_dir = "/test/dir" mocked_config_dir = _mock_get_config_dir(mocker, test_dir) dir_list = list() mocked_listdir = mockbase.mock_os_any(mocker, "listdir", dir_list) # run actual_list = resources.list_config_dir(filetype=".txt") # assert mocked_config_dir.assert_called_once() mocked_listdir.assert_called_once_with("/test/dir") assert not actual_list
def apply_config_dir(server=None, protocol=None, filetype="rules"): """High-level command to apply the whole configuration directory with rules or fallback files in it. :param server: If None this applies 0.0.0.0/0 instead :param protocol: If None the default 'udp' is taken :param filetype: default is rules but may be fallback too. :returns: True on success """ config_files = resources.list_config_dir(filetype=filetype) retval = True for config_file in config_files: if not apply_config(config_file, server, protocol): retval = False return retval
def test_list_config_dir_when_filetype_is_none_and_list_has_files(mocker): # setup mockbase = MockBase("resources") mockbase.setup(resources) test_dir = "/test/dir" mocked_config_dir = _mock_get_config_dir(mocker, test_dir) # pylint: disable=protected-access dir_list = ["some", "other"] mocked_listdir = mockbase.mock_os_any(mocker, "listdir", dir_list) expected_list = [test_dir + "/" + _file for _file in dir_list] # run actual_list = resources.list_config_dir() # assert mocked_config_dir.assert_called_once() mocked_listdir.assert_called_once_with("/test/dir") assert actual_list == expected_list