def test_fulfillDeviceRequest_additional_requests(): """ Test additional requests passed with the configuration. This test setup has two radios configured on different spectrum bands. The first AP interface has no requests and is assigned to the first device. The second AP requests a specific hardware (2.4 GHz) and is assigned to the second device. """ reservations = collections.defaultdict(DeviceReservations) hostConfig = { 'wifi': [ {'id': 'pci-wifi-0', 'hwmode': '11a'}, {'id': 'pci-wifi-1', 'hwmode': '11g'} ] } def getCache(key): if key == "deviceReservations": return reservations elif key == "hostConfig": return hostConfig update = MagicMock() update.cache_get.side_effect = getCache config1 = { 'type': 'wifi', 'mode': 'ap' } config2 = { 'type': 'wifi', 'mode': 'ap', 'requests': { 'hwmode': '11g' } } devices = { 'wifi': [ {'id': 'pci-wifi-0', 'name': 'wlan0'}, {'id': 'pci-wifi-1', 'name': 'wlan1'} ] } result = network.fulfillDeviceRequest(update, config1, devices) assert result['id'] == 'pci-wifi-0' result = network.fulfillDeviceRequest(update, config2, devices) assert result['id'] == 'pci-wifi-1'
def test_fulfillDeviceRequest(): update = MagicMock() reservations = collections.defaultdict(DeviceReservations) update.cache_get.return_value = reservations devices = { 'wifi': [ {'name': 'wlan0'}, {'name': 'wlan1'} ], 'lan': [ {'name': 'eth0'}, {'name': 'eth1'} ] } # Test exclusive access type interfaces (monitor). # # The first interface is assigned to the first device, second interface # to the second device, and the third raises an exception. config = { 'type': 'wifi', 'mode': 'monitor' } result = network.fulfillDeviceRequest(update, config, devices) assert result['name'] == 'wlan0' result = network.fulfillDeviceRequest(update, config, devices) assert result['name'] == 'wlan1' assert_raises(Exception, network.fulfillDeviceRequest, update, config, devices) reservations = collections.defaultdict(DeviceReservations) update.cache_get.return_value = reservations # Test shared access type interfaces (AP). # # This should assign several consecutive interfaces to the first device, # then to the second, then finally raise an exception. config = { 'type': 'wifi-ap' } for i in range(network.MAX_AP_INTERFACES): result = network.fulfillDeviceRequest(update, config, devices) print(result) assert result['name'] == 'wlan0' for i in range(network.MAX_AP_INTERFACES): result = network.fulfillDeviceRequest(update, config, devices) assert result['name'] == 'wlan1' assert_raises(Exception, network.fulfillDeviceRequest, update, config, devices) reservations = collections.defaultdict(DeviceReservations) update.cache_get.return_value = reservations # Test mixed assignment. # # The first interface gains exclusive access to the first device. # Following interfaces gain shared access to the second device. config = { 'type': 'wifi-monitor' } result = network.fulfillDeviceRequest(update, config, devices) assert result['name'] == 'wlan0' config = { 'type': 'wifi-ap' } for i in range(network.MAX_AP_INTERFACES): result = network.fulfillDeviceRequest(update, config, devices) print(result) assert result['name'] == 'wlan1' assert_raises(Exception, network.fulfillDeviceRequest, update, config, devices) # Test lan type interfaces. config = { 'type': 'lan' } result = network.fulfillDeviceRequest(update, config, devices) assert result['name'] == 'eth0' result = network.fulfillDeviceRequest(update, config, devices) assert result['name'] == 'eth1' assert_raises(Exception, network.fulfillDeviceRequest, update, config, devices)