-
Notifications
You must be signed in to change notification settings - Fork 128
353: Netmask MIB enhancement #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import sonic_ax_impl | ||
| import sys | ||
| from unittest import TestCase | ||
| from unittest.mock import MagicMock | ||
|
|
||
| if sys.version_info.major == 3: | ||
| from unittest import mock | ||
|
|
@@ -12,7 +13,7 @@ | |
| 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, InterfacesUpdater, DbTables | ||
| from sonic_ax_impl.mibs.ietf.rfc1213 import NextHopUpdater, InterfacesUpdater, DbTables, NetmaskUpdater | ||
|
|
||
|
|
||
| class TestNextHopUpdater(TestCase): | ||
|
|
@@ -163,3 +164,77 @@ def mock_dbs_get_all(dbs, db_name, hash, *args, **kwargs): | |
| except TypeError: | ||
| self.fail("Caught Type error") | ||
| self.assertTrue(counter == None) | ||
|
|
||
| class TestNetmaskUpdater(TestCase): | ||
| @mock.patch("sonic_ax_impl.mibs.Namespace.init_namespace_dbs", return_value="mock_db_conn") | ||
| @mock.patch('sonic_ax_impl.mibs.get_index_from_str', return_value=1) | ||
| @mock.patch('ax_interface.util.ip2byte_tuple', side_effect=lambda ip: tuple(map(int, ip.split('.')))) | ||
| def test_update_netmask_info(self, mock_ip2byte, mock_get_index, mock_namespace): | ||
| updater = NetmaskUpdater() | ||
| updater._update_netmask_info("eth0", "192.168.1.1/24") | ||
|
|
||
| expected_key = (4, 192, 168, 1, 1) | ||
| self.assertIn(expected_key, updater.netmask_map) | ||
| self.assertIn(expected_key, updater.netmask_list) | ||
|
|
||
| @mock.patch("ax_interface.util.ip2byte_tuple", side_effect=lambda ip: tuple(map(int, ip.split('.')))) | ||
| @mock.patch("sonic_ax_impl.mibs.get_index_from_str", return_value=2) | ||
| @mock.patch("sonic_ax_impl.mibs.ietf.rfc1213.Namespace.init_namespace_dbs", return_value="mock_db_conn") | ||
| @mock.patch("sonic_ax_impl.mibs.ietf.rfc1213.os.popen") | ||
| def test_update_data(self, mock_popen, mock_init_ns, mock_get_index, mock_ip2byte): | ||
| mock_dbs_keys = mock.MagicMock(return_value=[ | ||
| "INTF_TABLE:Eth0:192.168.1.1/24", | ||
| "INTF_TABLE:Docker0:10.0.0.1/8" | ||
| ]) | ||
| mock_init_ns.return_value = "mock_db_conn" | ||
|
|
||
| with mock.patch("sonic_ax_impl.mibs.ietf.rfc1213.Namespace.dbs_keys", mock_dbs_keys): | ||
| def popen_side_effect(cmd): | ||
| mock_proc = MagicMock() | ||
| if "Eth0" in cmd and "print $2" in cmd: | ||
|
||
| mock_proc.read.return_value = "192.168.1.1/24\n" | ||
| elif "docker0" in cmd and "print $2" in cmd: | ||
| mock_proc.read.return_value = "172.17.0.1/24\n" | ||
| elif "docker0" in cmd and "print $4" in cmd: | ||
| mock_proc.read.return_value = "172.17.255.255/24\n" | ||
| else: | ||
| mock_proc.read.return_value = "192.168.1.1/24\n" | ||
| return mock_proc | ||
|
|
||
| mock_popen.side_effect = popen_side_effect | ||
|
|
||
| updater = NetmaskUpdater() | ||
| updater.update_data() | ||
|
|
||
| self.assertGreater(len(updater.netmask_map), 0) | ||
| self.assertTrue(all(isinstance(k, tuple) for k in updater.netmask_list)) | ||
|
|
||
| def test_get_next(self): | ||
| updater = NetmaskUpdater() | ||
| updater.netmask_list = [(4, 10, 0, 0, 1), (4, 192, 168, 1, 1), (4, 192, 168, 1, 2)] | ||
| self.assertEqual(updater.get_next((4, 10, 0, 0, 1)), (4, 192, 168, 1, 1)) | ||
| self.assertEqual(updater.get_next((4, 192, 168, 1, 2)), None) | ||
|
|
||
| @mock.patch('sonic_ax_impl.mibs.get_index_from_str', return_value=1) | ||
| @mock.patch('ax_interface.util.ip2byte_tuple', side_effect=lambda ip: tuple(map(int, ip.split('.')))) | ||
| def test_update_netmask_info_invalid_ip(self, mock_ip2byte, mock_get_index): | ||
| updater = NetmaskUpdater() | ||
|
|
||
| # IP missing prefix length | ||
| updater._update_netmask_info("eth0", "192.168.1.1") | ||
| self.assertEqual(len(updater.netmask_map), 0) | ||
| self.assertEqual(len(updater.netmask_list), 0) | ||
|
|
||
| # Malformed IP | ||
| updater._update_netmask_info("eth0", "300.168.1.1/24") | ||
| self.assertEqual(len(updater.netmask_map), 0) | ||
| self.assertEqual(len(updater.netmask_list), 0) | ||
|
|
||
| @mock.patch("sonic_ax_impl.mibs.ietf.rfc1213.os.popen", side_effect=OSError("popen failed")) | ||
| def test_update_data_popen_fail(self, mock_popen): | ||
| updater = NetmaskUpdater() | ||
| # Should handle popen failure gracefully without exception | ||
| updater.update_data() | ||
| self.assertEqual(len(updater.netmask_map), 0) | ||
| self.assertEqual(len(updater.netmask_list), 0) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test mocks 'Eth0' and 'Docker0' in the interface names, but the actual implementation regex pattern (line 220 in rfc1213.py) expects interface names starting with uppercase followed by lowercase (like "Eth0"). However, these don't match typical SONiC interface naming conventions like "Ethernet8" or "eth0". The test should use realistic interface names that would actually appear in production to properly validate the implementation.