From 052d14c91065578f671ef371aac300a47ae44233 Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Fri, 17 May 2024 13:59:57 -0400 Subject: [PATCH] [fix] Fixed parsing route without gateway #290 Fixes #290 --- .../backends/openwrt/converters/routes.py | 2 +- tests/openwrt/test_network.py | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/netjsonconfig/backends/openwrt/converters/routes.py b/netjsonconfig/backends/openwrt/converters/routes.py index f428bbfaa..d2a8c858f 100644 --- a/netjsonconfig/backends/openwrt/converters/routes.py +++ b/netjsonconfig/backends/openwrt/converters/routes.py @@ -54,7 +54,7 @@ def __netjson_route(self, route, i): { "device": route.pop('interface'), "destination": str(ip_interface(network)), - "next": route.pop('gateway'), + "next": route.pop('gateway', ''), "cost": route.pop( 'metric', self._schema['properties']['cost']['default'] ), diff --git a/tests/openwrt/test_network.py b/tests/openwrt/test_network.py index a2c37c774..f41257cf9 100644 --- a/tests/openwrt/test_network.py +++ b/tests/openwrt/test_network.py @@ -119,6 +119,40 @@ def test_parse_routes(self): o = OpenWrt(native=self._routes_uci) self.assertEqual(o.config, self._routes_netjson) + _routes_nogw_netjson = { + "routes": [ + {"device": "wan", "destination": "192.168.100.1/32", "next": "", "cost": 0} + ] + } + _routes_nogw_uci = """package network + +config route 'route1' + option interface 'wan' + option metric '0' + option netmask '255.255.255.255' + option target '192.168.100.1' +""" + + def test_render_route_nogw(self): + o = OpenWrt(self._routes_nogw_netjson) + expected = self._tabs(self._routes_nogw_uci) + self.assertEqual(o.render(), expected) + + def test_parse_routes_nogw(self): + o = OpenWrt(native=self._routes_nogw_uci) + self.assertEqual(o.config, self._routes_nogw_netjson) + + with self.subTest('minimalistic route'): + minimal_uci = """package network + +config route 'route1' + option interface 'wan' + option target '192.168.100.1' + option netmask '255.255.255.255' +""" + o = OpenWrt(native=minimal_uci) + self.assertEqual(o.config, self._routes_nogw_netjson) + _rules_netjson = { "ip_rules": [ {