class TestTelnetConnection(TestCase): """Testing TelnetConnection.""" def setUp(self): """Set up test env.""" self.connection = TelnetConnection('fake', 'fake', 'fake', 'fake') # self.connection._connected = True self.connection._prompt_string = "" def test_determine_linelength_inf(self): """ Test input for infinite breakline length.""" # An input without newlines results in infinite linebreak # The input string is shorter than the limit for i in (15, 50): input_bytes = (" " * i).encode('ascii') self.connection._determine_linebreak(input_bytes) self.assertEqual(self.connection._linebreak, float('inf')) def test_determine_linelength(self): for i in (15, 50): input_bytes = (" " * i + "\n" + " " * 5).encode('ascii') self.connection._determine_linebreak(input_bytes) self.assertEqual(self.connection._linebreak, i) # And now with some more lines input_bytes = ((" " * i + "\n") * 3 + " " * 5).encode('ascii') self.connection._determine_linebreak(input_bytes) self.assertEqual(self.connection._linebreak, i) # And with a prompt string prompt = "test_string" input_bytes = "a" * (i - len(prompt)) + "\n" + "a" * 5 self.connection._prompt_string = prompt self.connection._determine_linebreak(input_bytes.encode('ascii')) self.assertEqual(self.connection._linebreak, i) self.connection._prompt_string = ""
def __init__(self, host, port=None, use_telnet=False, username=None, password=None, ssh_key=None, mode='router', require_ip=False, time_cache=CHANGE_TIME_CACHE_DEFAULT, interface='eth0', dnsmasq='/var/lib/misc'): """Init function.""" self.require_ip = require_ip self.mode = mode self._rx_latest = None self._tx_latest = None self._latest_transfer_check = None self._cache_time = time_cache self._trans_cache_timer = None self._dev_cache_timer = None self._devices_cache = None self._transfer_rates_cache = None self._latest_transfer_data = 0, 0 self.interface = interface self.dnsmasq = dnsmasq if use_telnet: self.connection = TelnetConnection(host, port, username, password) else: self.connection = SshConnection(host, port, username, password, ssh_key)
def __init__(self, host, port, use_telnet=False, username=None, password=None, ssh_key=None, mode='router', require_ip=False, time_cache=CHANGE_TIME_CACHE_DEFAULT): """Init function.""" self.require_ip = require_ip self.mode = mode self._rx_latest = None self._tx_latest = None self._latest_transfer_check = None self._cache_time = time_cache self._trans_cache_timer = None self._transfer_rates_cache = None if use_telnet: self.connection = TelnetConnection(host, port, username, password) else: self.connection = SshConnection(host, port, username, password, ssh_key)
async def test_reconnect(): with mock.patch("asyncio.open_connection", new=telnet_mock.open_connection): connection = TelnetConnection("fake", "fake", "fake", "fake") await connection.async_connect() telnet_mock.raise_exception_on_write(IncompleteReadError("", 42)) new_return = await connection.async_run_command("run command\n") assert new_return == [""]
async def test_sending_cmds(): with mock.patch('asyncio.open_connection', new=telnet_mock.open_connection): # Let's set a short linebreak of 10 telnet_mock.set_linebreak(22) connection = TelnetConnection('fake', 'fake', 'fake', 'fake') await connection.async_connect() # Now let's send some arbitrary short command exp_ret_val = "Some arbitrary long return string." + "." * 100 telnet_mock.set_return(exp_ret_val) new_return = await connection.async_run_command("run command\n") assert new_return[0] == exp_ret_val
def setUp(self): """Set up test env.""" self.connection = TelnetConnection('fake', 'fake', 'fake', 'fake') # self.connection._connected = True self.connection._prompt_string = ""