Skip to content

Commit

Permalink
[OpenWrt] Fixed empty output bug if addresses is empty list
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Mar 24, 2016
1 parent 11da509 commit b061ee4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
14 changes: 9 additions & 5 deletions netjsonconfig/backends/openwrt/renderers.py
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/openwrt/test_network.py
Expand Up @@ -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)

0 comments on commit b061ee4

Please sign in to comment.