def execute_flow(self, supported_os, shell_name, shell_type, resource_name): with self._snmp_handler.get_snmp_service() as snpm_service: juniper_snmp_autoload = JuniperSnmpAutoload( snpm_service, shell_name, shell_type, resource_name, self._logger) return juniper_snmp_autoload.discover(supported_os)
def setUp(self): self._snmp_handler = Mock() self._logger = Mock() self._config = Mock() self._cli_service = Mock() self._snmp_community = Mock() self._autoload_operations_instance = JuniperSnmpAutoload( snmp_handler=self._snmp_handler, logger=self._logger, config=self._config, cli_service=self._cli_service, snmp_community=self._snmp_community)
def setUp(self): self._snmp_handler = Mock() self._logger = Mock() self._config = Mock() self._cli_service = Mock() self._snmp_community = Mock() self._autoload_operations_instance = JuniperSnmpAutoload(snmp_handler=self._snmp_handler, logger=self._logger, config=self._config, cli_service=self._cli_service, snmp_community=self._snmp_community)
# MIB_DATA_MAP = { 'JUNIPER-MIB::jnxContainersTable': G_DATA['jnxContainersTable'], 'JUNIPER-MIB::jnxContentsTable': G_DATA['jnxContentsTable'], 'SNMPv2-MIB::system': G_DATA['system'], 'JUNIPER-IF-MIB::ifChassisTable': G_DATA['ifChassisTable'], 'IF-MIB::interfaces': G_DATA['interfaces'], 'IP-MIB::ipAddrTable': G_DATA['ipAddrTable'], 'IEEE8023-LAG-MIB::dot3adAggPortAttachedAggID': G_DATA['dot3adAggPortTable'], 'EtherLike-MIB::dot3StatsDuplexStatus': G_DATA['dot3StatsDuplexStatus'] } snmp_handler = FakeSnmpHandler(MIB_DATA_MAP) snmp_autoload = JuniperSnmpAutoload(snmp_handler, logger=logger) print(snmp_autoload.discover_snmp()) # print(snmp_autoload._get_device_details()) # print(snmp_autoload.ports[599].attributes) # print(snmp_autoload._generate_description_string()) # print(snmp_autoload.elements) # snmp_autoload.add_ports_if_attributes(build_mib_dict(NTT_IF_MIB, "if attrs"), build_mib_dict(NTT_JUNIPER_IF_MIB, "if mmm")) # print('\n'.join(map(str, ['{0}, {1}'.format(element.relative_path, element.type_string) for element in snmp_autoload.elements.values() if element.type in ELEMENT_DEFINITION]))) # print('\n'.join(map(str, [port.attributes["ifDescr"] for port in snmp_autoload.ports.values() if port.relative_path is None and port.logical_unit is "0"]))) # print('\n'.join(map(str, [ # "{0}, {1}, {2}, {3}".format(port.attributes["ifDescr"], port.attributes["ifType"], port.relative_path, # port.index) for port in # snmp_autoload.ports.values()])))
class TestJuniperSnmpAutoload(TestCase): def setUp(self): self._snmp_handler = Mock() self._logger = Mock() self._config = Mock() self._cli_service = Mock() self._snmp_community = Mock() self._autoload_operations_instance = JuniperSnmpAutoload(snmp_handler=self._snmp_handler, logger=self._logger, config=self._config, cli_service=self._cli_service, snmp_community=self._snmp_community) def _mock_methods(self): self._autoload_operations_instance._is_valid_device_os = Mock() self._autoload_operations_instance.enable_snmp = Mock() self._autoload_operations_instance.disable_snmp = Mock() self._autoload_operations_instance._build_root = Mock() self._autoload_operations_instance._build_chassis = Mock() self._autoload_operations_instance._build_power_modules = Mock() self._autoload_operations_instance._build_modules = Mock() self._autoload_operations_instance._build_sub_modules = Mock() self._autoload_operations_instance._build_ports = Mock() self._autoload_operations_instance._root = Mock() def test_discover_is_valid_device_os_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._is_valid_device_os.assert_called_once_with() def test_discover_build_root_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._build_root.assert_called_once_with() def test_discover_build_chassis_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._build_chassis.assert_called_once_with() def test_discover_build_power_modules_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._build_power_modules.assert_called_once_with() def test_discover_build_modules_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._build_modules.assert_called_once_with() def test_discover_build_sub_modules_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._build_sub_modules.assert_called_once_with() def test_discover_build_ports_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._build_ports.assert_called_once_with() def test_enable_snmp_if_community_string_is_present(self): """Check that enable_snmp doesn't send command to add community string if such one is already on the device""" self._cli_service.send_config_command.return_value = "authorization read-only;" self._autoload_operations_instance.enable_snmp() self._cli_service.send_config_command.assert_called_once() self._cli_service.send_command_list.assert_not_called() self._cli_service.commit.assert_not_called() def test_enable_snmp_if_no_community_string(self): """Check that enable_snmp send commands to add community string if such one is not on the device""" self._cli_service.send_config_command.return_value = "" self._autoload_operations_instance.enable_snmp() self._cli_service.send_config_command.assert_called_once() self._cli_service.send_command_list.assert_called_once() self._cli_service.commit.assert_called_once() def test_disable_snmp(self): self._autoload_operations_instance.disable_snmp() self._cli_service.send_config_command.assert_called_once() self._cli_service.commit.assert_called_once() @mock.patch('cloudshell.networking.juniper.autoload.juniper_snmp_autoload.get_attribute_by_name') def test_discover_calls_enable_snmp(self, get_attribute_by_name_func): """Check that discover method calls enable_snmp if 'Enable SNMP' attr is 'True'""" get_attribute_by_name_func.return_value = 'True' self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance.enable_snmp.assert_called_once() @mock.patch('cloudshell.networking.juniper.autoload.juniper_snmp_autoload.get_attribute_by_name') def test_discover_doesnt_call_enable_snmp(self, get_attribute_by_name_func): """Check that discover method doesn't call enable_snmp if 'Enable SNMP' attr is 'False'""" get_attribute_by_name_func.return_value = 'False' self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance.enable_snmp.assert_not_called() @mock.patch('cloudshell.networking.juniper.autoload.juniper_snmp_autoload.get_attribute_by_name') def test_discover_calls_disable_snmp(self, get_attribute_by_name_func): """Check that discover method calls disable_snmp if 'Disable SNMP' attr is 'True'""" get_attribute_by_name_func.return_value = 'True' self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance.disable_snmp.assert_called_once() @mock.patch('cloudshell.networking.juniper.autoload.juniper_snmp_autoload.get_attribute_by_name') def test_discover_doesnt_call_disable_snmp(self, get_attribute_by_name_func): """Check that discover method doesn't call disable_snmp if 'Disable SNMP' attr is 'False'""" get_attribute_by_name_func.return_value = 'False' self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance.disable_snmp.assert_not_called()
class TestJuniperSnmpAutoload(TestCase): def setUp(self): self._snmp_handler = Mock() self._logger = Mock() self._config = Mock() self._cli_service = Mock() self._snmp_community = Mock() self._autoload_operations_instance = JuniperSnmpAutoload( snmp_handler=self._snmp_handler, logger=self._logger, config=self._config, cli_service=self._cli_service, snmp_community=self._snmp_community) def _mock_methods(self): self._autoload_operations_instance._is_valid_device_os = Mock() self._autoload_operations_instance.enable_snmp = Mock() self._autoload_operations_instance.disable_snmp = Mock() self._autoload_operations_instance._build_root = Mock() self._autoload_operations_instance._build_chassis = Mock() self._autoload_operations_instance._build_power_modules = Mock() self._autoload_operations_instance._build_modules = Mock() self._autoload_operations_instance._build_sub_modules = Mock() self._autoload_operations_instance._build_ports = Mock() self._autoload_operations_instance._root = Mock() def test_discover_is_valid_device_os_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._is_valid_device_os.assert_called_once_with( ) def test_discover_build_root_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._build_root.assert_called_once_with( ) def test_discover_build_chassis_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._build_chassis.assert_called_once_with( ) def test_discover_build_power_modules_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._build_power_modules.assert_called_once_with( ) def test_discover_build_modules_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._build_modules.assert_called_once_with( ) def test_discover_build_sub_modules_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._build_sub_modules.assert_called_once_with( ) def test_discover_build_ports_call(self): self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance._build_ports.assert_called_once_with( ) def test_enable_snmp_if_community_string_is_present(self): """Check that enable_snmp doesn't send command to add community string if such one is already on the device""" self._cli_service.send_config_command.return_value = "authorization read-only;" self._autoload_operations_instance.enable_snmp() self._cli_service.send_config_command.assert_called_once() self._cli_service.send_command_list.assert_not_called() self._cli_service.commit.assert_not_called() def test_enable_snmp_if_no_community_string(self): """Check that enable_snmp send commands to add community string if such one is not on the device""" self._cli_service.send_config_command.return_value = "" self._autoload_operations_instance.enable_snmp() self._cli_service.send_config_command.assert_called_once() self._cli_service.send_command_list.assert_called_once() self._cli_service.commit.assert_called_once() def test_disable_snmp(self): self._autoload_operations_instance.disable_snmp() self._cli_service.send_config_command.assert_called_once() self._cli_service.commit.assert_called_once() @mock.patch( 'cloudshell.networking.juniper.autoload.juniper_snmp_autoload.get_attribute_by_name' ) def test_discover_calls_enable_snmp(self, get_attribute_by_name_func): """Check that discover method calls enable_snmp if 'Enable SNMP' attr is 'True'""" get_attribute_by_name_func.return_value = 'True' self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance.enable_snmp.assert_called_once() @mock.patch( 'cloudshell.networking.juniper.autoload.juniper_snmp_autoload.get_attribute_by_name' ) def test_discover_doesnt_call_enable_snmp(self, get_attribute_by_name_func): """Check that discover method doesn't call enable_snmp if 'Enable SNMP' attr is 'False'""" get_attribute_by_name_func.return_value = 'False' self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance.enable_snmp.assert_not_called() @mock.patch( 'cloudshell.networking.juniper.autoload.juniper_snmp_autoload.get_attribute_by_name' ) def test_discover_calls_disable_snmp(self, get_attribute_by_name_func): """Check that discover method calls disable_snmp if 'Disable SNMP' attr is 'True'""" get_attribute_by_name_func.return_value = 'True' self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance.disable_snmp.assert_called_once() @mock.patch( 'cloudshell.networking.juniper.autoload.juniper_snmp_autoload.get_attribute_by_name' ) def test_discover_doesnt_call_disable_snmp(self, get_attribute_by_name_func): """Check that discover method doesn't call disable_snmp if 'Disable SNMP' attr is 'False'""" get_attribute_by_name_func.return_value = 'False' self._mock_methods() self._autoload_operations_instance.discover() self._autoload_operations_instance.disable_snmp.assert_not_called()