From a1ac883eb8f07327184683d0dbc6fe62164d7c76 Mon Sep 17 00:00:00 2001 From: malinoski Date: Tue, 27 Oct 2020 11:28:16 -0300 Subject: [PATCH] Junos plugin was updated with: ignore warning commands, in a programaticaly way; removed unsused function's parameters (inherited ones); refactored class attributes in proper way. The test cases and sample codes was updated accordly --- networkapi/plugins/Juniper/JUNOS/plugin.py | 20 ++++++++++--------- .../JUNOS/samples/sample_command_line.py | 2 +- networkapi/plugins/Juniper/JUNOS/tests.py | 12 +++++------ 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/networkapi/plugins/Juniper/JUNOS/plugin.py b/networkapi/plugins/Juniper/JUNOS/plugin.py index b38d891a0..bbffabc99 100644 --- a/networkapi/plugins/Juniper/JUNOS/plugin.py +++ b/networkapi/plugins/Juniper/JUNOS/plugin.py @@ -40,11 +40,6 @@ class JUNOS(BasePlugin): - configuration = None - quantity_of_times_to_try_lock = 3 - seconds_to_wait_to_try_lock = 10 - alternative_variable_base_path_list = ['path_to_tftpboot'] - alternative_static_base_path_list = ['/mnt/scripts/tftpboot/'] def __init__(self, **kwargs): super(JUNOS, self).__init__(connect_port=830, **kwargs) @@ -54,6 +49,13 @@ def __init__(self, **kwargs): if 'seconds_to_wait_to_try_lock' in kwargs: self.seconds_to_wait_to_try_lock = kwargs.get('seconds_to_wait_to_try_lock') + self.configuration = None + self.quantity_of_times_to_try_lock = 3 + self.seconds_to_wait_to_try_lock = 10 + self.alternative_variable_base_path_list = ['path_to_tftpboot'] + self.alternative_static_base_path_list = ['/mnt/scripts/tftpboot/'] + self.ignore_warning_list = ['statement not found'] + def connect(self): """ @@ -119,7 +121,7 @@ def close(self): log.error("Unknown error while closing connection on host {}: {}".format(self.equipment_access.fqdn, e)) raise Exception - def copyScriptFileToConfig(self, filename, use_vrf='', destination=''): + def copyScriptFileToConfig(self, filename): """ Receives the file path (usually in /mnt/scripts/tftpboot/networkapi/generated_config/interface/) @@ -156,7 +158,7 @@ def copyScriptFileToConfig(self, filename, use_vrf='', destination=''): self.close() raise Exception - def exec_command(self, command, success_regex='', invalid_regex=None, error_regex=None): + def exec_command(self, command): """ Execute a junos command 'set' in the equipment. @@ -175,7 +177,7 @@ def exec_command(self, command, success_regex='', invalid_regex=None, error_rege try: self.__try_lock() self.configuration.rollback() - self.configuration.load(command, format='set') + self.configuration.load(command, format='set', ignore_warning=self.ignore_warning_list) self.configuration.commit_check() self.configuration.commit() self.configuration.unlock() @@ -227,7 +229,7 @@ def exec_command(self, command, success_regex='', invalid_regex=None, error_rege self.close() raise Exception - def ensure_privilege_level(self, privilege_level=None): + def ensure_privilege_level(self): """ Ensure privilege level verifying if the current user is super-user. diff --git a/networkapi/plugins/Juniper/JUNOS/samples/sample_command_line.py b/networkapi/plugins/Juniper/JUNOS/samples/sample_command_line.py index f411a7bd6..79add3b3a 100644 --- a/networkapi/plugins/Juniper/JUNOS/samples/sample_command_line.py +++ b/networkapi/plugins/Juniper/JUNOS/samples/sample_command_line.py @@ -35,7 +35,7 @@ rollback_response = conf.rollback() print("Rollback config ... {}".format(rollback_response)) - load_result = conf.load(command, format='set') + load_result = conf.load(command, format='set', ignore_warning=['statement not found']) load_result_tostring = etree.tostring(load_result, encoding='unicode', pretty_print=True) print("Load command ... \n{}".format(load_result_tostring)) diff --git a/networkapi/plugins/Juniper/JUNOS/tests.py b/networkapi/plugins/Juniper/JUNOS/tests.py index f4d2563f5..aac040b60 100644 --- a/networkapi/plugins/Juniper/JUNOS/tests.py +++ b/networkapi/plugins/Juniper/JUNOS/tests.py @@ -64,7 +64,7 @@ def test_connect_success(self, mock_device): self.assertIsNotNone(plugin.configuration) self.assertEqual(connection_response, True) - @patch('jnpr.junos.utils.config.Config', autospec=True) + @patch('jnpr.junos.utils.config.Config') def test_exec_command_success(self, mock_config): """ @@ -82,11 +82,11 @@ def test_exec_command_success(self, mock_config): exec_command_response = plugin.exec_command("any command") # Assert - plugin.configuration.rollback.assert_called_once_with() - plugin.configuration.load.assert_called_once_with("any command", format='set') - plugin.configuration.commit_check.assert_called_once_with() - plugin.configuration.commit.assert_called_once_with() - plugin.configuration.unlock.assert_called_once_with() + plugin.configuration.rollback.assert_called_once() + plugin.configuration.load.assert_called_once() + plugin.configuration.commit_check.assert_called_once() + plugin.configuration.commit.assert_called_once() + plugin.configuration.unlock.assert_called_once() self.assertIsNotNone(exec_command_response) @patch('jnpr.junos.Device')