Skip to content

Commit

Permalink
[bug] Fix switch vlan ordering openwisp#104
Browse files Browse the repository at this point in the history
  • Loading branch information
atb00ker committed Apr 4, 2020
1 parent 90ab88b commit 772cec1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
17 changes: 15 additions & 2 deletions netjsonconfig/backends/base/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def to_netjson(self, remove_block=True):
to a NetJSON configuration dictionary (``self.config``)
"""
result = OrderedDict()
# copy list
intermediate_data = list(self.intermediate_data[self.intermediate_key])
# Clean intermediate data
intermediate_data = self.clean_intermediate_data(list(self.intermediate_data[self.intermediate_key]))
# iterate over copied intermediate data structure
for index, block in enumerate(intermediate_data):
if self.should_skip_block(block):
Expand All @@ -109,6 +109,19 @@ def to_netjson(self, remove_block=True):
# return result, expects dict
return result

def clean_intermediate_data(self, intermediate_data):
"""
Utility method called to clean data for backend in ``to_netjson``
"""
clean_intermediate_data, appendto_clean_intermediate_data = [], []
for block in intermediate_data:
if '.type' in block and block['.type'] == 'switch_vlan':
appendto_clean_intermediate_data.append(block)
else:
clean_intermediate_data.append(block)
clean_intermediate_data.extend(appendto_clean_intermediate_data)
return clean_intermediate_data

def to_netjson_loop(self, block, result, index=None): # pragma: nocover
"""
Utility method called in the loop of ``to_netjson``
Expand Down
35 changes: 35 additions & 0 deletions tests/openwrt/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,37 @@ def test_rules_no_src_dest(self):
option vlan '3'
"""

_switch_uci_reorder = """package network
config switch_vlan 'switch0_vlan1'
option device 'switch0'
option ports '0t 2 3 4 5'
option vid '1'
option vlan '1'
config switch 'switch0'
option enable_vlan '1'
option name 'switch0'
option reset '1'
"""

_switch_netjson_reorder = {
"switch": [
{
"name": "switch0",
"reset": True,
"enable_vlan": True,
"vlan": [
{
"device": "switch0",
"vlan": 1,
"ports": "0t 2 3 4 5"
}
]
}
]
}

def test_render_switch(self):
o = OpenWrt(self._switch_netjson)
expected = self._tabs(self._switch_uci)
Expand All @@ -292,3 +323,7 @@ def test_render_switch(self):
def test_parse_switch(self):
o = OpenWrt(native=self._switch_uci)
self.assertEqual(o.config, self._switch_netjson)

def test_parse_switch_reorder(self):
o = OpenWrt(native=self._switch_uci_reorder)
self.assertEqual(o.config, self._switch_netjson_reorder)

0 comments on commit 772cec1

Please sign in to comment.