Example #1
0
def test_config_manager():
    """
    Test the pdconf configuration manager
    """
    from paradrop.backend.pdconfd.config.base import ConfigObject
    from paradrop.backend.pdconfd.config.manager import findConfigFiles

    files = findConfigFiles()
    assert isinstance(files, list)

    files = findConfigFiles(search="this_should_not_exist")
    assert isinstance(files, list)

    temp = tempfile.mkdtemp()
    manager = ConfigManager(writeDir=temp)

    # Test previousCommands function
    manager.previousCommands = [Mock(priority=5), Mock(priority=10)]
    result = manager.getPreviousCommands()
    assert list(result) == manager.previousCommands

    obj = ConfigObject()

    manager.currentConfig = {
        ("interface", "wan"): obj
    }

    # Make a config that matches in name and content.
    config = Mock()
    config.getTypeAndName = Mock(return_value=("interface", "wan"))
    config.optionsMatch = Mock(return_value=True)
    
    assert manager.findMatchingConfig(config, byName=False) is not None
    assert manager.findMatchingConfig(config, byName=True) is not None

    # Make a config that matches by name but not content.
    config = Mock()
    config.getTypeAndName = Mock(return_value=("interface", "wan"))
    config.optionsMatch = Mock(return_value=False)

    assert manager.findMatchingConfig(config, byName=False) is None
    assert manager.findMatchingConfig(config, byName=True) is not None

    # Now make one that differs in name but matches in content.
    config = Mock()
    config.getTypeAndName = Mock(return_value=("interface", "wan2"))
    config.optionsMatch = Mock(return_value=True)
    
    assert manager.findMatchingConfig(config, byName=False) is not None
    assert manager.findMatchingConfig(config, byName=True) is not None

    # Test waitSystemUp method
    manager.systemUp.set()
    result = manager.waitSystemUp()
    assert isinstance(result, basestring)

    pdos.remove(temp)
Example #2
0
def test_findConfigFiles(os, getSystemConfigDir):
    """
    Test the findConfigFiles function
    """
    from paradrop.backend.pdconfd.config.manager import findConfigFiles

    os.path.isfile.return_value = True
    result = findConfigFiles(search="foo")
    for fn in result:
        print(fn)
    assert result == ["foo"]

    print("---")
    os.path.isfile.return_value = False
    os.path.isdir.return_value = True
    os.listdir.return_value = ["foo", "bar"]
    result = findConfigFiles(search="dir")
    for fn in result:
        print(fn)
    assert result == ["dir/foo", "dir/bar"]

    print("---")
    getSystemConfigDir.return_value = "/etc/config"
    os.path.isfile.side_effect = [False, True]
    os.path.isdir.return_value = False
    result = findConfigFiles(search="foo")
    for fn in result:
        print(fn)
    assert result == ["/etc/config/foo"]

    print("---")
    getSystemConfigDir.return_value = "/etc/config"
    os.path.isfile.side_effect = None
    os.path.isfile.return_value = False
    os.path.isdir.return_value = True
    os.listdir.return_value = ["foo"]
    result = findConfigFiles()
    for fn in result:
        print(fn)
    assert result == ["/etc/config/foo"]