From 443fa3c56d1fd5d86ccd5bbfec3d6fa294be2129 Mon Sep 17 00:00:00 2001 From: dineshbaburam91 Date: Mon, 21 Oct 2024 15:45:23 +0530 Subject: [PATCH 1/2] Added bind_addr parameter in Device() API --- lib/jnpr/junos/device.py | 2 ++ tests/unit/test_device.py | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/jnpr/junos/device.py b/lib/jnpr/junos/device.py index 37523ad15..6765d2af9 100644 --- a/lib/jnpr/junos/device.py +++ b/lib/jnpr/junos/device.py @@ -1239,6 +1239,7 @@ def __init__(self, *vargs, **kvargs): self._huge_tree = kvargs.get("huge_tree", False) self._conn_open_timeout = kvargs.get("conn_open_timeout", 30) self._look_for_keys = kvargs.get("look_for_keys", None) + self._bind_addr = kvargs.get("bind_addr", None) self._hostkey_verify = kvargs.get("hostkey_verify", False) if self._fact_style != "new": warnings.warn( @@ -1393,6 +1394,7 @@ def open(self, *vargs, **kvargs): allow_agent=allow_agent, look_for_keys=look_for_keys, ssh_config=self._sshconf_lkup(), + bind_addr=self._bind_addr, timeout=self._conn_open_timeout, device_params={ "name": "junos", diff --git a/tests/unit/test_device.py b/tests/unit/test_device.py index 42d16b559..1773ee706 100644 --- a/tests/unit/test_device.py +++ b/tests/unit/test_device.py @@ -463,6 +463,23 @@ def test_device_open(self, mock_connect, mock_execute): self.dev2.open() self.assertEqual(self.dev2.connected, True) + @patch("ncclient.manager.connect") + @patch("jnpr.junos.Device.execute") + def test_device_open_with_bind_addr(self, mock_connect, mock_execute): + with patch("jnpr.junos.utils.fs.FS.cat") as mock_cat: + mock_cat.return_value = """ + + domain jls.net + + """ + mock_connect.side_effect = self._mock_manager + mock_execute.side_effect = self._mock_manager + self.dev2 = Device( + host="2.2.2.2", user="test", password="password123", bind_addr="1.1.1.1" + ) + self.dev2.open() + self.assertEqual(self.dev2.connected, True) + @patch("ncclient.manager.connect") @patch("jnpr.junos.Device.execute") def test_device_open_with_look_for_keys_False(self, mock_connect, mock_execute): @@ -496,6 +513,7 @@ def test_device_open_with_look_for_keys_True(self, mock_connect, mock_execute): ) self.dev2.open() self.assertEqual(self.dev2.connected, True) + @patch("ncclient.manager.connect") @patch("jnpr.junos.Device.execute") def test_device_open_with_hostkey_verify_True(self, mock_connect, mock_execute): @@ -525,7 +543,10 @@ def test_device_open_with_hostkey_verify_False(self, mock_connect, mock_execute) mock_connect.side_effect = self._mock_manager mock_execute.side_effect = self._mock_manager self.dev2 = Device( - host="2.2.2.2", user="test", password="password123", hostkey_verify=False + host="2.2.2.2", + user="test", + password="password123", + hostkey_verify=False, ) self.dev2.open() self.assertEqual(self.dev2.connected, True) From cf87f0359eebec91b841b176065d2b2e61d9abfc Mon Sep 17 00:00:00 2001 From: dineshbaburam91 Date: Mon, 21 Oct 2024 15:49:41 +0530 Subject: [PATCH 2/2] Updated the comments --- lib/jnpr/junos/device.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/jnpr/junos/device.py b/lib/jnpr/junos/device.py index 6765d2af9..27133661c 100644 --- a/lib/jnpr/junos/device.py +++ b/lib/jnpr/junos/device.py @@ -1217,6 +1217,10 @@ def __init__(self, *vargs, **kvargs): *OPTIONAL* To disable public key authentication. default is ``None``. + :param str bind_addr: + *OPTIONAL* To use (local) source IP address. + default is ``None``. + :param bool hostkey_verify: *OPTIONAL* To enable ssh_known hostkey verify default is ``False``.