def findConfigFiles(search=None): """ Look for and return a list of configuration files. The behavior depends on whether the search argument is a file, a directory, or None. If search is None, return a list of files in the system config directory. If search is a file name (not a path), look for it in the working directory first, and the system directory second. If search is a full path to a file, and it exists, then return that file. If search is a directory, return the files in that directory. """ if search is None: search = getSystemConfigDir() files = list() if os.path.isfile(search): files.append(search) elif os.path.isdir(search): for fn in os.listdir(search): path = os.path.join(search, fn) files.append(path) else: path = os.path.join(getSystemConfigDir(), search) if os.path.isfile(path): files.append(path) return files
def findConfigFiles(search=None): """ Look for and return a list of configuration files. The behavior depends on whether the search argument is a file, a directory, or None. If search is None, return a list of files in the system config directory. If search is a file name (not a path), look for it in the working directory first, and the system directory second. If search is a full path to a file, and it exists, then return that file. If search is a directory, return the files in that directory. """ if search is None: search = getSystemConfigDir() files = list() if os.path.isfile(search): files.append(search) elif os.path.isdir(search): for fn in os.listdir(search): path = "{}/{}".format(search, fn) files.append(path) else: path = "{}/{}".format(getSystemConfigDir(), search) if os.path.isfile(path): files.append(path) return files
def test_uci(): """ Test UCI file utility module """ from paradrop.lib.utils import uci from paradrop.lib import settings # Test functions for finding path to UCI files settings.UCI_CONFIG_DIR = "/tmp/config.d" assert uci.getSystemConfigDir() == "/tmp/config.d" assert uci.getSystemPath("network") == "/tmp/config.d/network" # Test stringify function assert uci.stringify("a") == "a" blob = {"a": "b"} assert uci.stringify(blob) == blob blob = {"a": {"b": "c"}} assert uci.stringify(blob) == blob blob = {"a": ["b", "c"]} assert uci.stringify(blob) == blob blob = {"a": 5} strblob = {"a": "5"} assert uci.stringify(blob) == strblob assert uci.isMatch(blob, strblob) # Write a realistic configuration and load with uci module path = writeTempFile(NETWORK_WAN_CONFIG) config = uci.UCIConfig(path) # Test if it found the config section that we know should be there empty = {} assert config.getConfig(empty) == [] match = {"type": "interface", "name": "wan", "comment": "__PARADROP__"} assert len(config.getConfig(match)) == 1 match = {"type": "interface", "name": "wan", "comment": "chute"} assert config.getConfig(match) == [] assert config.getConfigIgnoreComments(empty) == [] assert len(config.getConfigIgnoreComments(match)) == 1 # More existence tests assert not config.existsConfig(empty, empty) match_config = { "type": "interface", "name": "wan", "comment": "__PARADROP__" } match_options = { "ifname": "eth0", "proto": "dhcp" } assert config.existsConfig(match_config, match_options) # Test adding and removing config.delConfigs([(match_config, match_options)]) assert not config.existsConfig(match_config, match_options) config.addConfigs([(match_config, match_options)]) assert config.existsConfig(match_config, match_options) config.delConfig(match_config, match_options) assert not config.existsConfig(match_config, match_options) config.addConfig(match_config, match_options) assert config.existsConfig(match_config, match_options) # Get configuration by chute name assert config.getChuteConfigs("none") == [] assert len(config.getChuteConfigs("__PARADROP__")) == 1 # Test saving and reloading config.save(backupToken="backup") config2 = uci.UCIConfig(path) # Simple test for the equality operators assert config == config2 assert not (config != config2) # Test chuteConfigsMatch function assert not uci.chuteConfigsMatch(config.getChuteConfigs("__PARADROP__"), config2.getChuteConfigs("none")) assert uci.chuteConfigsMatch(config.getChuteConfigs("__PARADROP__"), config2.getChuteConfigs("__PARADROP__")) # Further test the equality operators config2.filepath = "NOMATCH" assert not (config == config2) assert config != config2 config2.filepath = config.filepath config2.myname = "NOMATCH" assert not (config == config2) assert config != config2 config2.myname = config.myname config2.config = [] assert not (config == config2) assert config != config2
def test_uci(): """ Test UCI file utility module """ from paradrop.lib.utils import uci from paradrop.base import settings # Test functions for finding path to UCI files settings.loadSettings(mode="unittest") assert uci.getSystemConfigDir() == "/tmp/.paradrop-test/uci/config.d/" assert uci.getSystemPath( "network") == "/tmp/.paradrop-test/uci/config.d/network" # Test stringify function assert uci.stringify("a") == "a" blob = {"a": "b"} assert uci.stringify(blob) == blob blob = {"a": {"b": "c"}} assert uci.stringify(blob) == blob blob = {"a": ["b", "c"]} assert uci.stringify(blob) == blob blob = {"a": 5} strblob = {"a": "5"} assert uci.stringify(blob) == strblob assert uci.isMatch(blob, strblob) # Write a realistic configuration and load with uci module path = writeTempFile(NETWORK_WAN_CONFIG) config = uci.UCIConfig(path) # Test if it found the config section that we know should be there empty = {} assert config.getConfig(empty) == [] match = {"type": "interface", "name": "wan", "comment": "__PARADROP__"} assert len(config.getConfig(match)) == 1 match = {"type": "interface", "name": "wan", "comment": "chute"} assert config.getConfig(match) == [] assert config.getConfigIgnoreComments(empty) == [] assert len(config.getConfigIgnoreComments(match)) == 1 # More existence tests assert not config.existsConfig(empty, empty) match_config = { "type": "interface", "name": "wan", "comment": "__PARADROP__" } match_options = {"ifname": "eth0", "proto": "dhcp"} assert config.existsConfig(match_config, match_options) # Test adding and removing config.delConfigs([(match_config, match_options)]) assert not config.existsConfig(match_config, match_options) config.addConfigs([(match_config, match_options)]) assert config.existsConfig(match_config, match_options) config.delConfig(match_config, match_options) assert not config.existsConfig(match_config, match_options) config.addConfig(match_config, match_options) assert config.existsConfig(match_config, match_options) # Get configuration by chute name assert config.getChuteConfigs("none") == [] assert len(config.getChuteConfigs("__PARADROP__")) == 1 # Test saving and reloading config.save(backupToken="backup") config2 = uci.UCIConfig(path) # Simple test for the equality operators assert config == config2 assert not (config != config2) # Test chuteConfigsMatch function assert not uci.chuteConfigsMatch(config.getChuteConfigs("__PARADROP__"), config2.getChuteConfigs("none")) assert uci.chuteConfigsMatch(config.getChuteConfigs("__PARADROP__"), config2.getChuteConfigs("__PARADROP__")) # Further test the equality operators config2.filepath = "NOMATCH" assert not (config == config2) assert config != config2 config2.filepath = config.filepath config2.myname = "NOMATCH" assert not (config == config2) assert config != config2 config2.myname = config.myname config2.config = [] assert not (config == config2) assert config != config2