Skip to content

Commit

Permalink
[openwrt] Add test and improve dialup interface type
Browse files Browse the repository at this point in the history
Signed-off-by: Konrad Kreitmair <kkreitmair@tdt.de>
  • Loading branch information
kkreitmair committed Aug 11, 2020
1 parent acbd54d commit 9afd432
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
@@ -1,6 +1,10 @@
Change log
==========

- Added dialup interface handling for openwrt backend.
This change is backward incompatible for custom defined interfaces of the
proto type ``other``.

Version 0.8.1 [2020-05-28]
--------------------------

Expand Down
12 changes: 11 additions & 1 deletion netjsonconfig/backends/openwrt/converters/interfaces.py
Expand Up @@ -248,7 +248,8 @@ def __netjson_addresses(self, interface):
return interface
if proto not in ['static', 'dhcp', 'dhcpv6', 'none']:
interface['proto'] = proto
interface['type'] = 'other'
interface['type'] = self.__get_special_interface_type(interface)

addresses = []
ipv4 = interface.pop('ipaddr', [])
ipv6 = interface.pop('ip6addr', [])
Expand All @@ -272,6 +273,15 @@ def __netjson_addresses(self, interface):
interface['addresses'] = addresses
return interface

def __get_special_interface_type(self, interface):
username = interface.get('username', False)
password = interface.get('password', False)

if username and password:
return 'dialup'

return 'other'

def __netjson_address(self, address, interface):
ip = ip_interface(address)
family = 'ipv{0}'.format(ip.version)
Expand Down
30 changes: 11 additions & 19 deletions netjsonconfig/backends/openwrt/schema.py
Expand Up @@ -114,14 +114,16 @@
},
"dialup_interface": {
"title": "Dialup interface",
"required": [
"proto",
"username",
"password"
],
"required": ["proto", "username", "password"],
"allOf": [
{
"properties": {
"type": {
"type": "string",
"enum": ["dialup"],
"default": "dialup",
"propertyOrder": 1,
},
"proto": {
"type": "string",
"enum": [
Expand All @@ -135,7 +137,7 @@
"pppoe",
"pptp",
"qmi",
"wwan"
"wwan",
],
"default": "pppoe",
"propertyOrder": 8,
Expand All @@ -149,11 +151,11 @@
"type": "string",
"description": "password for authentication in protocols like PPPoE",
"propertyOrder": 10,
}
},
}
},
{"$ref": "#/definitions/interface_settings"},
]
],
},
"base_radio_settings": {
"properties": {
Expand Down Expand Up @@ -209,17 +211,7 @@
}
},
"interfaces": {
"type": "array",
"title": "Interfaces",
"uniqueItems": True,
"additionalItems": True,
"propertyOrder": 2,
"items": {
"title": "Interface",
"oneOf": [
{"$ref": "#/definitions/dialup_interface"}
]
}
"items": {"oneOf": [{"$ref": "#/definitions/dialup_interface"}]}
},
"routes": {
"items": {
Expand Down
42 changes: 42 additions & 0 deletions tests/openwrt/test_dialup.py
@@ -0,0 +1,42 @@
import unittest

from netjsonconfig import OpenWrt
from netjsonconfig.utils import _TabsMixin


class TestDialup(unittest.TestCase, _TabsMixin):
maxDiff = None

_dialup_interface_netjson = {
"interfaces": [
{
"mtu": 1448,
"network": "xdsl",
"type": "dialup",
"name": "dsl0",
"password": "jf93nf82o023$",
"username": "dsluser",
"proto": "pppoe",
},
]
}

_dialup_interface_uci = """package network
config interface 'xdsl'
option ifname 'dsl0'
option mtu '1448'
option password 'jf93nf82o023$'
option proto 'pppoe'
option username 'dsluser'
"""

def test_render_dialup_interface(self):
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).config
expected = self._dialup_interface_netjson
self.assertDictEqual(result, expected)
14 changes: 5 additions & 9 deletions tests/openwrt/test_interfaces.py
Expand Up @@ -601,25 +601,21 @@ def test_parse_custom_proto(self):
native = self._tabs(
"""package network
config interface 'ppp0'
config interface 'custom_if0'
option device '/dev/usb/modem1'
option ifname 'ppp0'
option ifname 'custom_if0'
option ipv6 '1'
option keepalive '3'
option password 'pwd0123'
option proto 'ppp'
option username 'user1'
option proto 'custom'
"""
)
expected = {
"interfaces": [
{
"name": "ppp0",
"name": "custom_if0",
"type": "other",
"proto": "ppp",
"proto": "custom",
"device": "/dev/usb/modem1",
"username": "user1",
"password": "pwd0123",
"keepalive": '3',
"ipv6": '1',
}
Expand Down

0 comments on commit 9afd432

Please sign in to comment.