forked from sonic-net/sonic-snmpagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: zero route may have empty nexthop (sonic-net#276)
**- What I did** Fix: zero route may have empty nexthop. Improve code robustness. **- How I did it** **- How to verify it** Manual test: 1. manipulate ApplDB default route: remove nexthop field and value 2. recover 3. manipulate ApplDB default route: remove ifname field and value 4. recover Also add unit test.
- Loading branch information
1 parent
e60a64c
commit 7147354
Showing
4 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
import sys | ||
from unittest import TestCase | ||
|
||
if sys.version_info.major == 3: | ||
from unittest import mock | ||
else: | ||
import mock | ||
|
||
modules_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
sys.path.insert(0, os.path.join(modules_path, 'src')) | ||
|
||
from sonic_ax_impl.mibs.ietf.rfc1213 import NextHopUpdater | ||
|
||
class TestNextHopUpdater(TestCase): | ||
|
||
@mock.patch('sonic_ax_impl.mibs.Namespace.dbs_keys', mock.MagicMock(return_value=(["ROUTE_TABLE:0.0.0.0/0"]))) | ||
@mock.patch('sonic_ax_impl.mibs.Namespace.dbs_get_all', mock.MagicMock(return_value=({"nexthop": "10.0.0.1,10.0.0.3", "ifname": "Ethernet0,Ethernet4"}))) | ||
def test_NextHopUpdater_route_has_next_hop(self): | ||
updater = NextHopUpdater() | ||
|
||
with mock.patch('sonic_ax_impl.mibs.logger.warning') as mocked_warning: | ||
updater.update_data() | ||
|
||
# check warning | ||
mocked_warning.assert_not_called() | ||
|
||
self.assertTrue(len(updater.route_list) == 1) | ||
self.assertTrue(updater.route_list[0] == (0,0,0,0)) | ||
|
||
@mock.patch('sonic_ax_impl.mibs.Namespace.dbs_keys', mock.MagicMock(return_value=(["ROUTE_TABLE:0.0.0.0/0"]))) | ||
@mock.patch('sonic_ax_impl.mibs.Namespace.dbs_get_all', mock.MagicMock(return_value=({"ifname": "Ethernet0,Ethernet4"}))) | ||
def test_NextHopUpdater_route_no_next_hop(self): | ||
updater = NextHopUpdater() | ||
|
||
with mock.patch('sonic_ax_impl.mibs.logger.warning') as mocked_warning: | ||
updater.update_data() | ||
|
||
# check warning | ||
expected = [ | ||
mock.call("Route has no nexthop: ROUTE_TABLE:0.0.0.0/0 {'ifname': 'Ethernet0,Ethernet4'}") | ||
] | ||
mocked_warning.assert_has_calls(expected) | ||
|
||
self.assertTrue(len(updater.route_list) == 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
import sys | ||
from unittest import TestCase | ||
|
||
if sys.version_info.major == 3: | ||
from unittest import mock | ||
else: | ||
import mock | ||
|
||
modules_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
sys.path.insert(0, os.path.join(modules_path, 'src')) | ||
|
||
from sonic_ax_impl.mibs.ietf.rfc4292 import RouteUpdater | ||
|
||
class TestRouteUpdater(TestCase): | ||
|
||
@mock.patch('sonic_py_common.multi_asic.get_all_namespaces', mock.MagicMock(return_value=({"front_ns": ['']}))) | ||
@mock.patch('swsscommon.swsscommon.SonicV2Connector.get_all', mock.MagicMock(return_value=({"nexthop": "10.0.0.1", "ifname": "Ethernet0"}))) | ||
def test_RouteUpdater_route_has_next_hop_and_iframe(self): | ||
updater = RouteUpdater() | ||
|
||
with mock.patch('sonic_ax_impl.mibs.logger.warning') as mocked_warning: | ||
updater.update_data() | ||
|
||
# check warning | ||
mocked_warning.assert_not_called() | ||
|
||
self.assertTrue(len(updater.route_dest_list) == 1) | ||
self.assertTrue(updater.route_dest_list[0] == (0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 1)) | ||
|
||
@mock.patch('sonic_py_common.multi_asic.get_all_namespaces', mock.MagicMock(return_value=({"front_ns": ['']}))) | ||
@mock.patch('swsscommon.swsscommon.SonicV2Connector.get_all', mock.MagicMock(return_value=({"ifname": "Ethernet0"}))) | ||
def test_RouteUpdater_route_no_next_hop(self): | ||
updater = RouteUpdater() | ||
|
||
with mock.patch('sonic_ax_impl.mibs.logger.warning') as mocked_warning: | ||
updater.update_data() | ||
|
||
# check warning | ||
expected = [ | ||
mock.call("Route has no nexthop: ROUTE_TABLE:0.0.0.0/0 {'ifname': 'Ethernet0'}") | ||
] | ||
mocked_warning.assert_has_calls(expected) | ||
|
||
self.assertTrue(len(updater.route_dest_list) == 0) | ||
|
||
@mock.patch('sonic_py_common.multi_asic.get_all_namespaces', mock.MagicMock(return_value=({"front_ns": ['']}))) | ||
@mock.patch('swsscommon.swsscommon.SonicV2Connector.get_all', mock.MagicMock(return_value=({"nexthop": "10.0.0.1"}))) | ||
def test_RouteUpdater_route_no_iframe(self): | ||
updater = RouteUpdater() | ||
|
||
with mock.patch('sonic_ax_impl.mibs.logger.warning') as mocked_warning: | ||
updater.update_data() | ||
|
||
# check warning | ||
expected = [ | ||
mock.call("Route has no ifname: ROUTE_TABLE:0.0.0.0/0 {'nexthop': '10.0.0.1'}") | ||
] | ||
mocked_warning.assert_has_calls(expected) | ||
|
||
self.assertTrue(len(updater.route_dest_list) == 0) |