diff --git a/netjsonconfig/backends/openwrt/renderers.py b/netjsonconfig/backends/openwrt/renderers.py index ded7a19b4..75fbf6a64 100644 --- a/netjsonconfig/backends/openwrt/renderers.py +++ b/netjsonconfig/backends/openwrt/renderers.py @@ -19,7 +19,7 @@ def _get_interfaces(self): interfaces = self.config.get('interfaces', []) # this line ensures interfaces are not entirely # ignored if they do not contain any address - default_addresses = [{'proto': 'none'}] + default_address = [{'proto': 'none'}] # results container uci_interfaces = [] for interface in interfaces: @@ -34,8 +34,12 @@ def _get_interfaces(self): if interface.get('type') == 'bridge': is_bridge = True bridge_members = ' '.join(interface['bridge_members']) + # ensure address list is not never empty, even when 'addresses' is [] + address_list = interface.get('addresses') + if not address_list: + address_list = default_address # address list defaults to empty list - for address in interface.get('addresses', default_addresses): + for address in address_list: # prepare new UCI interface directive uci_interface = deepcopy(interface) if network: @@ -46,11 +50,11 @@ def _get_interfaces(self): if uci_interface.get('disabled'): uci_interface['enabled'] = not interface['disabled'] del uci_interface['disabled'] - if uci_interface.get('addresses'): + if 'addresses' in uci_interface: del uci_interface['addresses'] - if uci_interface.get('type'): + if 'type' in uci_interface: del uci_interface['type'] - if uci_interface.get('wireless'): + if 'wireless' in uci_interface: del uci_interface['wireless'] # default values address_key = None diff --git a/tests/openwrt/test_network.py b/tests/openwrt/test_network.py index 0351b3fe8..0e5dd0c7c 100644 --- a/tests/openwrt/test_network.py +++ b/tests/openwrt/test_network.py @@ -874,3 +874,30 @@ def test_mac_address_format(self): # empty is valid (will be ignored) o.config['interfaces'][0]['mac'] = '' o.validate() + + def test_default_addresses(self): + """ + the following configuration dictionary caused empty output up to 0.4.0 + """ + o = OpenWrt({ + "interfaces": [ + { + "type": "bridge", + "network": "lan", + "addresses": [], + "name": "br-lan", + "bridge_members": [ + "eth0", + "eth1" + ] + } + ] + }) + expected = self._tabs("""package network + +config interface 'lan' + option ifname 'eth0 eth1' + option proto 'none' + option type 'bridge' +""") + self.assertEqual(o.render(), expected)