def command_parse(python_dict, fifo_queue, thread_lock): # pylint: disable=inconsistent-return-statements """Function to get and parse commands from devices :type python_dict: Dict :param python_dict: A dictionary of connection data :type fifo_queue: queue.Queue Object :param fifo_queue: The FIFO queue :type thread_lock: threading.Lock Object :param thread_lock: The thread lock :rtype: None :returns: None, but it does put a item in the fifo_queue """ with thread_lock: allowed_device_types = {'ios', 'iosxe', 'iosxr', 'nxos'} if python_dict.get('device_type') not in allowed_device_types: return None command = python_dict.get('command') netmiko_obj = QuickNetmiko(python_dict.get('device_ip_name'), python_dict.get('device_type'), python_dict.get('username'), python_dict.get('password')) command_result = netmiko_obj.send_commands(command) genie_parse_obj = GenieCommandParse(python_dict.get('device_type')) parse_result = genie_parse_obj.parse_string(command, command_result) fifo_queue.put((parse_result, command_result))
def test_send_list_commands_no_lookup(monkeypatch, get_ios_test_data_int_brief, get_ios_test_data_int_g_0_1): def mock_init(*args, **kwargs): return None def mock_command_get(*args, **kwargs): temp_data = get_ios_test_data_int_brief + get_ios_test_data_int_g_0_1 return temp_data def mock_connection_disco(*args, **kwargs): return True monkeypatch.setattr(ConnectHandler, '__init__', mock_init) monkeypatch.setattr(CiscoIosBase, '__init__', mock_init) monkeypatch.setattr(CiscoIosSSH, 'send_command', mock_command_get) monkeypatch.setattr(CiscoIosSSH, 'disconnect', mock_connection_disco) nm_obj = QuickNetmiko('10.0.0.100', 'cisco_ios', 'temp', 'temp') returned_data = nm_obj.send_commands( ['show ip interface brief', 'show interfaces GigabitEthernet 0/1']) expected_data = get_ios_test_data_int_brief + get_ios_test_data_int_g_0_1 side_effect = expected_data + expected_data assert returned_data == side_effect
def docker_app_run(python_dict): """Function to get and parse commands from devices :type python_dict: Dict :param python_dict: A dictionary of connection data :rtype: (Dict, String) :returns:A tuple of a Dict and a String """ allowed_device_types = {'ios', 'iosxe', 'iosxr', 'nxos'} if python_dict.get('device_type') not in allowed_device_types: return None vault = VaultDataKv2() command = python_dict.get('command') netmiko_obj = QuickNetmiko(python_dict.get('device_ip_name'), python_dict.get('device_type'), vault.get_latest_data().get('username'), vault.get_latest_data().get('password')) command_result = netmiko_obj.send_commands(command) genie_parse_obj = GenieCommandParse(python_dict.get('device_type')) parse_result = genie_parse_obj.parse_string(command, command_result) return parse_result
def test_send_single_command_lookup_telnet(monkeypatch, get_ios_test_data_int_brief): def mock_init(*args, **kwargs): return None def mock_command_get(*args, **kwargs): return get_ios_test_data_int_brief def mock_connection_disco(*args, **kwargs): return True def socket_lookup(*args, **kwargs): return '10.0.0.100' monkeypatch.setattr(socket, 'gethostbyname', socket_lookup) monkeypatch.setattr(ConnectHandler, '__init__', mock_init) monkeypatch.setattr(CiscoIosBase, '__init__', mock_init) monkeypatch.setattr(CiscoIosTelnet, 'send_command', mock_command_get) monkeypatch.setattr(CiscoIosTelnet, 'disconnect', mock_connection_disco) nm_obj = QuickNetmiko('FAKE-DEVICE', 'ios', 'temp', 'temp', protocol='telnet') returned_data = nm_obj.send_commands('show ip interface brief') assert returned_data == get_ios_test_data_int_brief
def test_quick_netmiko_bad_device_type_telnet(): with pytest.raises(BadTelnetDeviceType): QuickNetmiko('10.0.0.100', 'unknown', 'temp', 'temp', protocol='telnet')
def test_quick_netmiko_fail_dns_socket_gaierror(monkeypatch): def socket_gaierror(*args): raise socket.gaierror monkeypatch.setattr(socket, 'gethostbyname', socket_gaierror) with pytest.raises(FailedDnsLookup): QuickNetmiko('test_name', 'cisco_ios', 'temp', 'temp')
def test_send_single_command_no_lookup_bad_command_data( monkeypatch, get_ios_test_data_int_brief): def mock_init(*args, **kwargs): return None def mock_command_get(*args, **kwargs): return get_ios_test_data_int_brief def mock_connection_disco(*args, **kwargs): return True monkeypatch.setattr(ConnectHandler, '__init__', mock_init) monkeypatch.setattr(CiscoIosBase, '__init__', mock_init) monkeypatch.setattr(CiscoIosSSH, 'send_command', mock_command_get) monkeypatch.setattr(CiscoIosSSH, 'disconnect', mock_connection_disco) nm_obj = QuickNetmiko('10.0.0.100', 'cisco_ios', 'temp', 'temp') with pytest.raises(TypeError): nm_obj.send_commands(1)
def test_send_single_command_no_lookup(monkeypatch, get_ios_test_data_int_brief): def mock_init(*args, **kwargs): return None def mock_command_get(*args, **kwargs): return get_ios_test_data_int_brief def mock_connection_disco(*args, **kwargs): return True monkeypatch.setattr(ConnectHandler, '__init__', mock_init) monkeypatch.setattr(CiscoIosBase, '__init__', mock_init) monkeypatch.setattr(CiscoIosSSH, 'send_command', mock_command_get) monkeypatch.setattr(CiscoIosSSH, 'disconnect', mock_connection_disco) nm_obj = QuickNetmiko('10.0.0.100', 'cisco_ios', 'temp', 'temp') returned_data = nm_obj.send_commands('show ip interface brief') assert returned_data == get_ios_test_data_int_brief
def test_quick_netmiko_bad_device_type_ssh(): with pytest.raises(BadSshDeviceType): QuickNetmiko('10.0.0.100', 'unknown', 'temp', 'temp')
def test_quick_netmiko_bad_protocol(): with pytest.raises(BadProtocol): QuickNetmiko('10.0.0.100', 'unknown', 'temp', 'temp', protocol='na')