From 0b5c37a99a2312652024857f50ffa01344aa2daa Mon Sep 17 00:00:00 2001 From: Aeluin Chen Date: Sun, 15 Jan 2017 02:43:53 +0800 Subject: [PATCH 1/3] feat: Support `status` and `wan` --- route.py | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/route.py b/route.py index 0d105a0..d8a2076 100755 --- a/route.py +++ b/route.py @@ -46,7 +46,7 @@ def init(self, *args, **kwargs): if self.bundle_env == "debug": # pragma: no cover self._path_root = "%s/tests" % self._path_root - self.interfaces = [] + self.interfaces = {} try: self.load(self._path_root) except: @@ -123,7 +123,10 @@ def list_interfaces(self): inet_ip = [inet["ip"] for inet in iface_info["inet"] if "" != inet["ip"]] - if len(inet_ip): + if len(inet_ip) and \ + (iface in self.interfaces and + self.interfaces[iface]["status"] is True and + self.interfaces[iface]["wan"] is True): data.append(iface) return data @@ -141,6 +144,8 @@ def get_default(self): else: return default + default["wan"] = True + default["status"] = True default["gateway"] = gw[0] default["interface"] = gw[1] return default @@ -198,6 +203,8 @@ def _try_update_default(self, routes): """ ifaces = self.list_interfaces() if not ifaces: + # FIXME: keep or clean? + # self.update_default({}) raise IPRouteError("Interfaces should be UP.") default = {} @@ -210,13 +217,11 @@ def _try_update_default(self, routes): return # find gateway by interface - for iface in self.interfaces: - if iface["interface"] == default["interface"]: - default = iface - break + default.update(self.interfaces[default["interface"]]) current = self.get_default() - if current != default: + if current.get("interface", "") != default.get("interface", "") or \ + current.get("gateway", "") != default.get("gateway", ""): self.update_default(default) def try_update_default(self, routes): @@ -226,7 +231,7 @@ def try_update_default(self, routes): except IPRouteError as e: _logger.debug(e) - def update_router(self, interface): + def update_router(self, iface): """ Save the interface name with its gateway and update the default gateway if needed. @@ -237,18 +242,18 @@ def update_router(self, interface): Args: interface: dict format with interface "name" and/or "gateway". """ + if "status" not in iface: + iface["status"] = True + if "wan" not in iface: + iface["wan"] = True + # update the router information - for iface in self.interfaces: - if iface["interface"] == interface["name"]: - if "gateway" in interface: - iface["gateway"] = interface["gateway"] - break - else: - iface = {} - iface["interface"] = interface["name"] - if "gateway" in interface: - iface["gateway"] = interface["gateway"] - self.interfaces.append(iface) + if iface["name"] not in self.interfaces: + self.interfaces[iface["name"]] = {} + self.interfaces[iface["name"]]["status"] = iface["status"] + self.interfaces[iface["name"]]["wan"] = iface["wan"] + if "gateway" in iface: + self.interfaces[iface["name"]]["gateway"] = iface["gateway"] # check if the default gateway need to be modified self.try_update_default(self._routes) From 7ee884dafbe3c6653af8602c7f6c349457653397 Mon Sep 17 00:00:00 2001 From: Aeluin Chen Date: Fri, 3 Feb 2017 14:10:29 +0800 Subject: [PATCH 2/3] test: fix unittest --- tests/test_route.py | 196 +++++++++++++++++++++++++++++++++----------- 1 file changed, 148 insertions(+), 48 deletions(-) diff --git a/tests/test_route.py b/tests/test_route.py index de02b39..50ceda1 100644 --- a/tests/test_route.py +++ b/tests/test_route.py @@ -123,6 +123,15 @@ def test__list_interfaces(self, mock_interfaces, mock_ifaddresses): """ mock_interfaces.return_value = ["eth0", "eth1", "ppp0"] mock_ifaddresses.side_effect = mock_ip_addr_ifaddresses + self.bundle.interfaces = {} + self.bundle.interfaces["eth0"] = { + "status": True, + "wan": True + } + self.bundle.interfaces["ppp0"] = { + "status": True, + "wan": True + } ifaces = self.bundle.list_interfaces() self.assertEqual(2, len(ifaces)) @@ -165,6 +174,10 @@ def mock_ip_addr_ifaddresses_ppp0_failed(iface): "subnet": "192.168.41.0"}]} else: raise ValueError + self.bundle.interfaces["eth0"] = { + "status": True, + "wan": True + } mock_interfaces.return_value = ["eth0", "eth1", "ppp0"] mock_ifaddresses.side_effect = mock_ip_addr_ifaddresses_ppp0_failed @@ -309,21 +322,26 @@ def test__try_update_default__by_default( "gateway": "192.168.4.254" } - self.bundle.interfaces = [ - { - "interface": "eth0", + self.bundle.interfaces = { + "eth0": { + "status": True, + "wan": True, "gateway": "192.168.3.254" }, - { - "interface": "eth1", + "eth1": { + "status": True, + "wan": False, "gateway": "192.168.4.254" } - ] + } routes = ["eth0", "eth1"] self.bundle._try_update_default(routes) - mock_update_default.assert_called_once_with(self.bundle.interfaces[0]) + + default = self.bundle.interfaces["eth0"] + default["interface"] = "eth0" + mock_update_default.assert_called_once_with(default) @patch.object(IPRoute, "update_default") @patch.object(IPRoute, "get_default") @@ -342,16 +360,18 @@ def test__try_update_default__by_default_with_current_value( "gateway": "192.168.3.254" } - self.bundle.interfaces = [ - { - "interface": "eth0", + self.bundle.interfaces = { + "eth0": { + "status": True, + "wan": True, "gateway": "192.168.3.254" }, - { - "interface": "eth1", + "eth1": { + "status": True, + "wan": True, "gateway": "192.168.4.254" } - ] + } routes = ["eth0", "eth1"] @@ -376,20 +396,23 @@ def test__try_update_default__by_secondary( "gateway": "192.168.4.254" } - self.bundle.interfaces = [ - { - "interface": "eth0", + self.bundle.interfaces = { + "eth0": { + "status": False, + "wan": True, "gateway": "192.168.3.254" }, - { - "interface": "eth1", + "eth1": { + "status": True, + "wan": True, "gateway": "192.168.4.254" }, - { - "interface": "wwan0", + "wwan0": { + "status": True, + "wan": True, "gateway": "192.168.5.254" } - ] + } routes = ["eth0", "wwan0"] @@ -397,7 +420,9 @@ def test__try_update_default__by_secondary( self.bundle._try_update_default(routes) # assert - mock_update_default.assert_called_once_with(self.bundle.interfaces[2]) + default = self.bundle.interfaces["wwan0"] + default["interface"] = "wwan0" + mock_update_default.assert_called_once_with(default) @patch.object(IPRoute, "update_default") @patch.object(IPRoute, "list_interfaces") @@ -422,20 +447,41 @@ def test__update_router__update_interface( update_router: update router info by interface """ # arrange - self.bundle.interfaces = [ - {"interface": "eth0", "gateway": "192.168.31.254"}, - {"interface": "eth1", "gateway": "192.168.4.254"}] + self.bundle.interfaces = { + "eth0": { + "status": True, + "wan": True, + "gateway": "192.168.31.254" + }, + "eth1": { + "status": True, + "wan": True, + "gateway": "192.168.4.254" + } + } iface = {"name": "eth1", "gateway": "192.168.41.254"} # act self.bundle.update_router(iface) # assert + eth0 = { + "eth0": { + "status": True, + "wan": True, + "gateway": "192.168.31.254" + } + } + eth1 = { + "eth1": { + "status": True, + "wan": True, + "gateway": "192.168.41.254" + } + } self.assertEqual(2, len(self.bundle.interfaces)) - self.assertIn({"interface": "eth0", "gateway": "192.168.31.254"}, - self.bundle.interfaces) - self.assertIn({"interface": "eth1", "gateway": "192.168.41.254"}, - self.bundle.interfaces) + self.assertEqual(eth0["eth0"], self.bundle.interfaces["eth0"]) + self.assertEqual(eth1["eth1"], self.bundle.interfaces["eth1"]) @patch.object(IPRoute, 'update_default') def test__update_router__add_interface_with_gateway( @@ -444,19 +490,36 @@ def test__update_router__add_interface_with_gateway( update_router: add a new interface with gateway """ # arrange - self.bundle.interfaces = [ - {"interface": "eth0", "gateway": "192.168.31.254"}] + self.bundle.interfaces = { + "eth0": { + "status": True, + "wan": True, + "gateway": "192.168.31.254" + } + } iface = {"name": "eth1", "gateway": "192.168.41.254"} # act self.bundle.update_router(iface) # assert + eth0 = { + "eth0": { + "status": True, + "wan": True, + "gateway": "192.168.31.254" + } + } + eth1 = { + "eth1": { + "status": True, + "wan": True, + "gateway": "192.168.41.254" + } + } self.assertEqual(2, len(self.bundle.interfaces)) - self.assertIn({"interface": "eth0", "gateway": "192.168.31.254"}, - self.bundle.interfaces) - self.assertIn({"interface": "eth1", "gateway": "192.168.41.254"}, - self.bundle.interfaces) + self.assertEqual(eth0["eth0"], self.bundle.interfaces["eth0"]) + self.assertEqual(eth1["eth1"], self.bundle.interfaces["eth1"]) @patch.object(IPRoute, 'update_default') def test__update_router__add_interface_without_gateway( @@ -465,19 +528,35 @@ def test__update_router__add_interface_without_gateway( update_router: add a new interface without gateway """ # arrange - self.bundle.interfaces = [ - {"interface": "eth0", "gateway": "192.168.31.254"}] + self.bundle.interfaces = { + "eth0": { + "status": True, + "wan": True, + "gateway": "192.168.31.254" + } + } iface = {"name": "eth1"} # act self.bundle.update_router(iface) # assert + eth0 = { + "eth0": { + "status": True, + "wan": True, + "gateway": "192.168.31.254" + } + } + eth1 = { + "eth1": { + "status": True, + "wan": True + } + } self.assertEqual(2, len(self.bundle.interfaces)) - self.assertIn({"interface": "eth0", "gateway": "192.168.31.254"}, - self.bundle.interfaces) - self.assertIn({"interface": "eth1"}, - self.bundle.interfaces) + self.assertEqual(eth0["eth0"], self.bundle.interfaces["eth0"]) + self.assertEqual(eth1["eth1"], self.bundle.interfaces["eth1"]) @patch.object(IPRoute, "get_default") @patch.object(IPRoute, 'try_update_default') @@ -491,20 +570,41 @@ def test__update_router__update_default( "interface": "eth0", "gateway": "192.168.3.254" } - self.bundle.interfaces = [ - {"interface": "eth0", "gateway": "192.168.3.254"}, - {"interface": "eth1", "gateway": "192.168.4.254"}] + self.bundle.interfaces = { + "eth0": { + "status": True, + "wan": True, + "gateway": "192.168.3.254" + }, + "eth1": { + "status": True, + "wan": True, + "gateway": "192.168.4.254" + } + } iface = {"name": "eth0", "gateway": "192.168.31.254"} # act self.bundle.update_router(iface) # assert + eth0 = { + "eth0": { + "status": True, + "wan": True, + "gateway": "192.168.31.254" + } + } + eth1 = { + "eth1": { + "status": True, + "wan": True, + "gateway": "192.168.4.254" + } + } self.assertEqual(2, len(self.bundle.interfaces)) - self.assertIn({"interface": "eth0", "gateway": "192.168.31.254"}, - self.bundle.interfaces) - self.assertIn({"interface": "eth1", "gateway": "192.168.4.254"}, - self.bundle.interfaces) + self.assertEqual(eth0["eth0"], self.bundle.interfaces["eth0"]) + self.assertEqual(eth1["eth1"], self.bundle.interfaces["eth1"]) mock_try_update_default.assert_called_once_with(self.bundle.model.db) @patch.object(IPRoute, "update_default") From e494c292de6e0f1cb53693017fb4d4fba35190d4 Mon Sep 17 00:00:00 2001 From: Aeluin Chen Date: Wed, 5 Apr 2017 17:29:39 +0800 Subject: [PATCH 3/3] 1.2.0 --- build-deb/debian/changelog | 6 ++++++ bundle.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/build-deb/debian/changelog b/build-deb/debian/changelog index b993869..53a6836 100644 --- a/build-deb/debian/changelog +++ b/build-deb/debian/changelog @@ -1,3 +1,9 @@ +sanji-bundle-route (1.2.0) unstable; urgency=low + + * feat: Support `status` and `wan`. + + -- Aeluin Chen Wed, 05 Apr 2017 16:00:35 +0800 + sanji-bundle-route (1.1.0) unstable; urgency=low * feat: Get interface via shell script command. diff --git a/bundle.json b/bundle.json index 0a3d6cb..bae0a9c 100644 --- a/bundle.json +++ b/bundle.json @@ -1,6 +1,6 @@ { "name": "route", - "version": "1.1.0", + "version": "1.2.0", "author": "Aeluin Chen", "email": "aeluin.chen@moxa.com", "description": "Handles the routing table",