Skip to content

Commit

Permalink
[change] New syntax should apply to loopback, ethernet and bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
codesankalp committed Mar 31, 2022
1 parent 93bf9f0 commit 53371b9
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 117 deletions.
2 changes: 1 addition & 1 deletion docs/source/backends/openwrt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ usually the LAN bridge:
"radio": "radio0",
"mode": "access_point",
"ssid": "wifi service",
# "network": ["lan"] this proeprty can be omitted
# "network": ["lan"] this property can be omitted
# but may be overridden if needed
}
},
Expand Down
19 changes: 16 additions & 3 deletions netjsonconfig/backends/openwrt/converters/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,26 @@ class Interfaces(OpenWrtConverter):
}
_device_config = {}
_custom_protocols = ['ppp']
_interface_dsa_types = ['loopback', 'ethernet', 'bridge']

def __set_dsa(self, interface):
"""
changes dsa property to manange new syntax introduced in
OpenWrt 21.02 by checking supported types and protocols
"""
self.dsa = (
self.dsa
and interface.get('proto', None) not in self._custom_protocols
and interface.get('type', None) in self._interface_dsa_types
)

def to_intermediate_loop(self, block, result, index=None):
result.setdefault('network', [])
uci_name = self._get_uci_name(block.get('network') or block['name'])
address_list = self.__intermediate_addresses(block)
interface = self.__intermediate_interface(block, uci_name)
if self.dsa and interface.get('proto', None) not in self._custom_protocols:
self.__set_dsa(interface)
if self.dsa:
uci_device = self.__intermediate_device(interface)
if uci_device:
result['network'].append(self.sorted_dict(uci_device))
Expand Down Expand Up @@ -270,7 +283,7 @@ def __clean_intermediate_bridge(self, interface):
"""
Removes options that are not required in the configuration.
"""
if self.dsa and interface.get('proto', None) not in self._custom_protocols:
if self.dsa:
repeated_options = (
['ifname', 'type', 'bridge_members']
+ self._bridge_interface_options['stp']
Expand Down Expand Up @@ -424,7 +437,7 @@ def __netjson_dsa_interface(self, interface):
for option in device_config:
if 'name' in option:
continue
# ifname has been renamed to ports in OpenWrt 21.02
# ifname has been renamed to ports in OpenWrt 21.02 bridge
if option == 'ports':
interface['ifname'] = ' '.join(device_config[option])
else:
Expand Down
9 changes: 7 additions & 2 deletions netjsonconfig/backends/openwrt/converters/wireless.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def __fix_netjson_network(self, result):
del result[index]['network']
return result

def _track_bridged_wifi(self, intermediate_data=None):
def _track_bridged_wifi(self, intermediate_data=None): # noqa: C901

This comment has been minimized.

Copy link
@nemesifier

nemesifier Apr 8, 2022

Member

@codesankalp this is not the right approach at managing the complexity of a method, by skipping the complexity check, a method could become too complex in subsequent iterations, don't this.

"""
Keeps track of wireless interfaces which are members of
bridges in order to automatically determine the "network"
Expand All @@ -349,12 +349,17 @@ def _track_bridged_wifi(self, intermediate_data=None):
continue
# Get list of bridge members
try:
bridge_members = interface.get('ifname', None).split(' ')
if self.dsa and interface.get('ports', []):
bridge_members = interface.get('ports', [])
else:
bridge_members = interface.get('ifname', None).split(' ')
except AttributeError:
# Bridge interface does not contain bridge members.
# Bridge is empty.
continue
bridge_name = interface['.name']
if self.dsa:
bridge_name = bridge_name.lstrip('device_')
for physical_interface in bridge_members:
# A physical interface can be a member of multiple
# bridges. Hence, we create a list of bridge interfaces
Expand Down
2 changes: 1 addition & 1 deletion tests/openwrt/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_schema_radio_wrong_protocol(self):
}

def test_generate(self):
o = OpenWrt(self._config1, dsa=False)
o = OpenWrt(self._config1)
tar = tarfile.open(fileobj=o.generate(), mode='r')
self.assertEqual(len(tar.getmembers()), 2)
# network
Expand Down
4 changes: 2 additions & 2 deletions tests/openwrt/test_dialup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class TestDialup(unittest.TestCase, _TabsMixin):
"""

def test_render_dialup_interface(self):
result = OpenWrt(self._dialup_interface_netjson, dsa=False).render()
result = OpenWrt(self._dialup_interface_netjson).render()
expected = self._tabs(self._dialup_interface_uci)
self.assertEqual(result, expected)

def test_parse_dialup_interface(self):
result = OpenWrt(native=self._dialup_interface_uci, dsa=False).config
result = OpenWrt(native=self._dialup_interface_uci).config
expected = self._dialup_interface_netjson
self.assertDictEqual(result, expected)
76 changes: 37 additions & 39 deletions tests/openwrt/test_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class TestEncryption(unittest.TestCase, _TabsMixin):
"""

def test_render_wpa3_personal(self):
o = OpenWrt(self._wpa3_personal_netjson, dsa=False)
o = OpenWrt(self._wpa3_personal_netjson)
expected = self._tabs(self._wpa3_personal_uci)
self.assertEqual(o.render(), expected)

def test_parse_wpa3_personal(self):
o = OpenWrt(native=self._wpa3_personal_uci, dsa=False)
o = OpenWrt(native=self._wpa3_personal_uci)
self.assertEqual(o.config, self._wpa3_personal_netjson)

_wpa2_personal_mixed_netjson = {
Expand Down Expand Up @@ -94,7 +94,7 @@ def test_parse_wpa3_personal(self):
"""

def test_render_wpa2_personal_mixed(self):
o = OpenWrt(self._wpa2_personal_mixed_netjson, dsa=False)
o = OpenWrt(self._wpa2_personal_mixed_netjson)
expected = self._tabs(self._wpa2_personal_mixed_uci)
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -141,12 +141,12 @@ def test_parse_wpa2_personal_mixed(self):
"""

def test_render_wpa2_personal(self):
o = OpenWrt(self._wpa2_personal_netjson, dsa=False)
o = OpenWrt(self._wpa2_personal_netjson)
expected = self._tabs(self._wpa2_personal_uci)
self.assertEqual(o.render(), expected)

def test_parse_wpa2_personal(self):
o = OpenWrt(native=self._wpa2_personal_uci, dsa=False)
o = OpenWrt(native=self._wpa2_personal_uci)
self.assertEqual(o.config, self._wpa2_personal_netjson)

_wpa_personal_mixed_netjson = {
Expand Down Expand Up @@ -188,12 +188,12 @@ def test_parse_wpa2_personal(self):
"""

def test_render_wpa_personal_mixed(self):
o = OpenWrt(self._wpa_personal_mixed_netjson, dsa=False)
o = OpenWrt(self._wpa_personal_mixed_netjson)
expected = self._tabs(self._wpa_personal_mixed_uci)
self.assertEqual(o.render(), expected)

def test_parse_wpa_personal_mixed(self):
o = OpenWrt(native=self._wpa_personal_mixed_uci, dsa=False)
o = OpenWrt(native=self._wpa_personal_mixed_uci)
self.assertEqual(o.config, self._wpa_personal_mixed_netjson)

_wpa_personal_netjson = {
Expand Down Expand Up @@ -233,12 +233,12 @@ def test_parse_wpa_personal_mixed(self):
"""

def test_render_wpa_personal(self):
o = OpenWrt(self._wpa_personal_netjson, dsa=False)
o = OpenWrt(self._wpa_personal_netjson)
expected = self._tabs(self._wpa_personal_uci)
self.assertEqual(o.render(), expected)

def test_parse_wpa_personal(self):
o = OpenWrt(native=self._wpa_personal_uci, dsa=False)
o = OpenWrt(native=self._wpa_personal_uci)
self.assertEqual(o.config, self._wpa_personal_netjson)

_wpa2_enterprise_mixed_ap_netjson = {
Expand Down Expand Up @@ -282,7 +282,7 @@ def test_parse_wpa_personal(self):
"""

def test_render_wpa2_enterprise_mixed_ap(self):
o = OpenWrt(self._wpa2_enterprise_mixed_ap_netjson, dsa=False)
o = OpenWrt(self._wpa2_enterprise_mixed_ap_netjson)
expected = self._tabs(self._wpa2_enterprise_mixed_ap_uci)
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -341,7 +341,7 @@ def test_parse_wpa2_enterprise_mixed_ap(self):
"""

def test_render_wpa3_enterprise(self):
o = OpenWrt(self._wpa3_enterprise_ap_netjson, dsa=False)
o = OpenWrt(self._wpa3_enterprise_ap_netjson)
expected = self._tabs(self._wpa3_enterprise_ap_uci)
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -398,12 +398,12 @@ def test_parse_wpa3_enterprise(self):
"""

def test_render_wpa2_enterprise(self):
o = OpenWrt(self._wpa2_enterprise_ap_netjson, dsa=False)
o = OpenWrt(self._wpa2_enterprise_ap_netjson)
expected = self._tabs(self._wpa2_enterprise_ap_uci)
self.assertEqual(o.render(), expected)

def test_parse_wpa2_enterprise(self):
o = OpenWrt(native=self._wpa2_enterprise_ap_uci, dsa=False)
o = OpenWrt(native=self._wpa2_enterprise_ap_uci)
self.assertEqual(o.config, self._wpa2_enterprise_ap_netjson)

_wpa_enterprise_mixed_ap_netjson = {
Expand Down Expand Up @@ -445,12 +445,12 @@ def test_parse_wpa2_enterprise(self):
"""

def test_render_wpa_enterprise_mixed_ap(self):
o = OpenWrt(self._wpa_enterprise_mixed_ap_netjson, dsa=False)
o = OpenWrt(self._wpa_enterprise_mixed_ap_netjson)
expected = self._tabs(self._wpa_enterprise_mixed_ap_uci)
self.assertEqual(o.render(), expected)

def test_parse_wpa_enterprise_mixed_ap(self):
o = OpenWrt(native=self._wpa_enterprise_mixed_ap_uci, dsa=False)
o = OpenWrt(native=self._wpa_enterprise_mixed_ap_uci)
self.assertEqual(o.config, self._wpa_enterprise_mixed_ap_netjson)

_wpa_enterprise_ap_netjson = {
Expand Down Expand Up @@ -492,12 +492,12 @@ def test_parse_wpa_enterprise_mixed_ap(self):
"""

def test_render_wpa_enterprise_ap(self):
o = OpenWrt(self._wpa_enterprise_ap_netjson, dsa=False)
o = OpenWrt(self._wpa_enterprise_ap_netjson)
expected = self._tabs(self._wpa_enterprise_ap_uci)
self.assertEqual(o.render(), expected)

def test_parse_wpa_enterprise_ap(self):
o = OpenWrt(native=self._wpa_enterprise_ap_uci, dsa=False)
o = OpenWrt(native=self._wpa_enterprise_ap_uci)
self.assertEqual(o.config, self._wpa_enterprise_ap_netjson)

_wpa3_enterprise_client_netjson = {
Expand Down Expand Up @@ -546,7 +546,7 @@ def test_parse_wpa_enterprise_ap(self):
"""

def test_render_wpa3_enterprise_client(self):
o = OpenWrt(self._wpa3_enterprise_client_netjson, dsa=False)
o = OpenWrt(self._wpa3_enterprise_client_netjson)
expected = self._tabs(self._wpa3_enterprise_client_uci)
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -599,12 +599,12 @@ def test_parse_wpa3_enterprise_client(self):
"""

def test_render_wpa2_enterprise_client(self):
o = OpenWrt(self._wpa2_enterprise_tls_client_netjson, dsa=False)
o = OpenWrt(self._wpa2_enterprise_tls_client_netjson)
expected = self._tabs(self._wpa2_enterprise_client_uci)
self.assertEqual(o.render(), expected)

def test_parse_wpa2_enterprise_client(self):
o = OpenWrt(native=self._wpa2_enterprise_client_uci, dsa=False)
o = OpenWrt(native=self._wpa2_enterprise_client_uci)
self.assertEqual(o.config, self._wpa2_enterprise_tls_client_netjson)

_wpa2_enterprise_ttls_client_netjson = {
Expand Down Expand Up @@ -652,12 +652,12 @@ def test_parse_wpa2_enterprise_client(self):
"""

def test_render_wpa2_enterprise_ttls_client(self):
o = OpenWrt(self._wpa2_enterprise_ttls_client_netjson, dsa=False)
o = OpenWrt(self._wpa2_enterprise_ttls_client_netjson)
expected = self._tabs(self._wpa2_enterprise_ttls_client_uci)
self.assertEqual(o.render(), expected)

def test_parse_wpa2_enterprise_ttls_client(self):
o = OpenWrt(native=self._wpa2_enterprise_ttls_client_uci, dsa=False)
o = OpenWrt(native=self._wpa2_enterprise_ttls_client_uci)
self.assertEqual(o.config, self._wpa2_enterprise_ttls_client_netjson)

_wpa2_enterprise_peap_client_netjson = {
Expand Down Expand Up @@ -705,12 +705,12 @@ def test_parse_wpa2_enterprise_ttls_client(self):
"""

def test_render_wpa2_enterprise_peap_client(self):
o = OpenWrt(self._wpa2_enterprise_peap_client_netjson, dsa=False)
o = OpenWrt(self._wpa2_enterprise_peap_client_netjson)
expected = self._tabs(self._wpa2_enterprise_peap_client_uci)
self.assertEqual(o.render(), expected)

def test_parse_wpa2_enterprise_peap_client(self):
o = OpenWrt(native=self._wpa2_enterprise_peap_client_uci, dsa=False)
o = OpenWrt(native=self._wpa2_enterprise_peap_client_uci)
self.assertEqual(o.config, self._wpa2_enterprise_peap_client_netjson)

_wpa2_enterprise_tls_client_auth_netjson = {
Expand Down Expand Up @@ -758,7 +758,7 @@ def test_parse_wpa2_enterprise_peap_client(self):
"""

def test_render_wpa2_enterprise_tls_client_auth(self):
o = OpenWrt(self._wpa2_enterprise_tls_client_auth_netjson, dsa=False)
o = OpenWrt(self._wpa2_enterprise_tls_client_auth_netjson)
expected = self._tabs(self._wpa2_enterprise_tls_client_auth_uci)
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -800,12 +800,12 @@ def test_render_wpa2_enterprise_tls_client_auth(self):
"""

def test_render_wep_open(self):
o = OpenWrt(self._wep_open_netjson, dsa=False)
o = OpenWrt(self._wep_open_netjson)
expected = self._tabs(self._wep_open_uci)
self.assertEqual(o.render(), expected)

def test_parse_wep_open(self):
o = OpenWrt(native=self._wep_open_uci, dsa=False)
o = OpenWrt(native=self._wep_open_uci)
self.assertEqual(o.config, self._wep_open_netjson)

_wep_shared_netjson = {
Expand Down Expand Up @@ -846,12 +846,12 @@ def test_parse_wep_open(self):
"""

def test_render_wep_shared(self):
o = OpenWrt(self._wep_shared_netjson, dsa=False)
o = OpenWrt(self._wep_shared_netjson)
expected = self._tabs(self._wep_shared_uci)
self.assertEqual(o.render(), expected)

def test_parse_wep_shared(self):
o = OpenWrt(native=self._wep_shared_uci, dsa=False)
o = OpenWrt(native=self._wep_shared_uci)
self.assertEqual(o.config, self._wep_shared_netjson)

def test_encryption_disabled(self):
Expand All @@ -874,8 +874,7 @@ def test_encryption_disabled(self):
},
}
]
},
dsa=False,
}
)
expected = self._tabs(
"""package network
Expand Down Expand Up @@ -912,8 +911,7 @@ def test_no_encryption(self):
},
}
]
},
dsa=False,
}
)
expected = self._tabs(
"""package network
Expand Down Expand Up @@ -972,12 +970,12 @@ def test_no_encryption(self):
"""

def test_render_wpa2_80211s(self):
o = OpenWrt(self._wpa2_80211s_netjson, dsa=False)
o = OpenWrt(self._wpa2_80211s_netjson)
expected = self._tabs(self._wpa2_80211s_uci)
self.assertEqual(o.render(), expected)

def test_parse_wpa2_80211s(self):
o = OpenWrt(native=self._wpa2_80211s_uci, dsa=False)
o = OpenWrt(native=self._wpa2_80211s_uci)
self.assertEqual(o.config, self._wpa2_80211s_netjson)

_wpa2_adhoc_netjson = {
Expand Down Expand Up @@ -1019,12 +1017,12 @@ def test_parse_wpa2_80211s(self):
"""

def test_render_wpa2_adhoc(self):
o = OpenWrt(self._wpa2_adhoc_netjson, dsa=False)
o = OpenWrt(self._wpa2_adhoc_netjson)
expected = self._tabs(self._wpa2_adhoc_uci)
self.assertEqual(o.render(), expected)

def test_parse_wpa2_adhoc(self):
o = OpenWrt(native=self._wpa2_adhoc_uci, dsa=False)
o = OpenWrt(native=self._wpa2_adhoc_uci)
self.assertEqual(o.config, self._wpa2_adhoc_netjson)

_wps_ap_netjson = {
Expand Down Expand Up @@ -1067,12 +1065,12 @@ def test_parse_wpa2_adhoc(self):
"""

def test_render_wps_ap(self):
o = OpenWrt(self._wps_ap_netjson, dsa=False)
o = OpenWrt(self._wps_ap_netjson)
expected = self._tabs(self._wps_ap_uci)
self.assertEqual(o.render(), expected)

def test_parse_wps_ap(self):
o = OpenWrt(native=self._wps_ap_uci, dsa=False)
o = OpenWrt(native=self._wps_ap_uci)
self.assertEqual(o.config, self._wps_ap_netjson)

def test_render_ieee80211w(self):
Expand Down

0 comments on commit 53371b9

Please sign in to comment.