Ejemplo n.º 1
0
def test_getNetworkConfig():
    update = UpdateObject({'name': 'test'})
    update.old = None

    update.new.net = {'wifi': {'dhcp': {}}}

    update.new.setCache('deviceReservations', {})
    update.new.setCache('interfaceReservations', set())
    update.new.setCache('subnetReservations', set())

    update.new.state = update.new.STATE_RUNNING

    # Exception: Interafce definition missing field(s)
    assert_raises(Exception, network.getNetworkConfig, update)

    update.new.net = {
        'vlan': {
            'intfName': 'eth1',
            'type': 'vlan',
            'dhcp': {},
            'vlan_id': 5
        }
    }

    network.getNetworkConfig(update)

    # Should have called setCache with a list containing one interface.
    value = update.new.getCache('networkInterfaces')
    assert isinstance(value, list) and len(value) == 1

    # Should have passed through the dhcp section.
    iface = value[0]
    assert 'dhcp' in iface
Ejemplo n.º 2
0
def test_getNetworkConfig():
    update = UpdateObject({'name': 'test'})
    update.old = None

    service = Service(name="main")
    service.interfaces = {
        "wlan0": {}
    }

    update.new = Chute(name="test")
    update.new.add_service(service)

    update.cache_set('deviceReservations', {})
    update.cache_set('interfaceReservations', set())
    update.cache_set('subnetReservations', set())

    update.state = "running"

    # Exception: Interafce definition missing field(s)
    assert_raises(Exception, network.getNetworkConfig, update)

    service.interfaces = {
        "eth1": {
            'intfName': 'eth1',
            'type': 'vlan',
            'dhcp': {},
            'vlan_id': 5
        }
    }

    network.getNetworkConfig(update)

    # Should have called setCache with a list containing one interface.
    value = update.cache_get('networkInterfaces')
    assert isinstance(value, list) and len(value) == 1

    # Should have passed through the dhcp section.
    iface = value[0]
    assert 'dhcp' in iface
Ejemplo n.º 3
0
def test_get_network_config():
    """
    Test generating network configuration for a chute update.
    """
    from paradrop.core.config import network
    from paradrop.core.config.reservations import DeviceReservations

    # Test normal case where key is defined and encryption is implied.
    iface = dict()
    cfg = {'key': 'password'}
    network.getWifiKeySettings(cfg, iface)
    assert iface['key'] == "password"

    # Test normal case where encryption is set to "none".
    iface = dict()
    cfg = {'encryption': 'none'}
    network.getWifiKeySettings(cfg, iface)
    assert iface['encryption'] == "none"

    # Test error case where encryption is defined but no key is present.
    iface = dict()
    cfg = {'encryption': 'psk2'}
    assert_raises(Exception, network.getWifiKeySettings, cfg, iface)

    update = UpdateObject({'name': 'test'})
    update.old = None
    update.new = MagicMock()

    # Should do nothing on a chute with no "networkInterfaces" cache key.
    network.reclaimNetworkResources(update.new)

    # Chute has no net information, we should pass silently.
    assert network.getNetworkConfig(update) is None

    wireless = {
        'encryption': 'psk2',
        'key': 'password'
    }

    service = Service(name="main")
    service.interfaces = {
        "wlan0": {
            'type': 'wifi-ap',
            'intfName': 'wlan0',
            'wireless': wireless
        }
    }

    chute = Chute(name="test")
    chute.add_service(service)
    update.new = chute

    devices = {
        'wifi': [{'name': 'wlan0', 'mac': '00:11:22:33:44:55', 'phy': 'phy0'}]
    }
    update.cache_set("networkDevices", devices)
    update.cache_set("deviceReservations", {
        "wlan0": DeviceReservations()
    })
    update.cache_set("subnetReservations", set())
    update.cache_set("interfaceReservations", set())

    # Missing 'ssid' field should raise exception.
    assert_raises(Exception, network.getNetworkConfig, update)

    wireless['ssid'] = "Paradrop"

    # Need to make a writable location for our config files.
    settings.UCI_CONFIG_DIR = tempfile.mkdtemp()
    settings.UCI_BACKUP_DIR = tempfile.mkdtemp()

    # Try the normal sequence of steps for installing a new chute.
    network.getNetworkConfig(update)
    network.getOSNetworkConfig(update)
    network.setOSNetworkConfig(update)
    network.abortNetworkConfig(update)

    # Set up state so that old chute matches new chute.
    update.old = Chute(name="test")
    update.old.add_service(service)
    ifaces = list(update.cache_get("networkInterfaces"))
    update.old.setCache("networkInterfaces", ifaces)

    # Now try sequence of steps that would occur for a restart.
    network.reclaimNetworkResources(update.old)
    network.getNetworkConfig(update)
    network.getOSNetworkConfig(update)
    network.setOSNetworkConfig(update)

    # Try asking for a new chute without any interfaces.
    service.interfaces = {}

    # This would be a restart where we remove an interface that was in old but
    # not in new.
    network.getNetworkConfig(update)
    network.getOSNetworkConfig(update)
    network.setOSNetworkConfig(update)
    network.abortNetworkConfig(update)

    # Try a network interface with missing 'type' field.
    service.interfaces = {
        "wlan0": {
            'intfName': 'wlan0',
        }
    }
    assert_raises(Exception, network.getNetworkConfig, update)

    # Try asking for something funny.
    service.interfaces = {
        "wlan0": {
            'type': 'something funny',
            'intfName': 'wlan0',
        }
    }
    assert_raises(Exception, network.getNetworkConfig, update)

    # Clean up our config dir
    pdos.remove(settings.UCI_CONFIG_DIR)
    pdos.remove(settings.UCI_BACKUP_DIR)