def test_terminal_cmd_whoami_during_ping(terminal_connection): terminal = terminal_connection cmd_whoami = Whoami(connection=terminal) cmd_ping = Ping(connection=terminal, destination="127.0.0.1", options='-c 3') cmd_ping.start(timeout=3) cmd_whoami.start(timeout=5) ret_whoami = cmd_whoami.await_done(timeout=5) assert 'USER' in ret_whoami assert ret_whoami['USER'] is not None assert getpass.getuser() == ret_whoami['USER'] ret_ping = cmd_ping.result() assert 'packets_transmitted' in ret_ping assert 3 == int(ret_ping['packets_transmitted']) assert 'packet_loss' in ret_ping assert 0 == int(ret_ping['packet_loss'])
def test_terminal_timeout_next_command(terminal_connection): terminal = terminal_connection max_nr = 5 for i in range(1, max_nr): cmd = Ping(connection=terminal, destination="127.0.0.1") with pytest.raises(CommandTimeout): cmd(timeout=0.3) cmd = Whoami(connection=terminal) ret = cmd() user = ret['USER'] assert getpass.getuser() == user
def test_timeout_before_command_sent(buffer_connection, command_output_and_expected_result_ping): from moler.cmd.unix.uptime import Uptime from moler.cmd.unix.ping import Ping command_output, expected_result = command_output_and_expected_result_ping ping_cmd = Ping(connection=buffer_connection.moler_connection, prompt="host:.*#", destination="localhost", options="-w 5") uptime_cmd = Uptime(connection=buffer_connection.moler_connection, prompt="host:.*#") ping_cmd.start(timeout=2) time.sleep(0.05) buffer_connection.moler_connection.data_received( command_output[0].encode("utf-8"), datetime.datetime.now()) with pytest.raises(CommandTimeout): uptime_cmd(timeout=0.2) buffer_connection.moler_connection.data_received( command_output[1].encode("utf-8"), datetime.datetime.now()) ping_cmd.await_done(timeout=0.2) ping_ret = ping_cmd.result() assert ping_ret == expected_result
def test_ping_observer_timeout(buffer_connection): from moler.exceptions import CommandTimeout with pytest.raises(CommandTimeout, match=r'Ping\(\"ping localhost\", id:\S+\) await_done time \d+.\d+ >= \d+.\d+ sec timeout'): cmd_ping = Ping(buffer_connection.moler_connection, destination='localhost') cmd_ping()
def test_ping_returns_proper_command_string(buffer_connection): ping_cmd = Ping(buffer_connection, destination="localhost", options="-c 5") assert "ping localhost -c 5" == ping_cmd.command_string
import time from moler.cmd.unix.ping import Ping from moler.connection_factory import get_connection host = 'www.google.com' terminal = get_connection(io_type='terminal', variant='threaded') with terminal.open(): ping_cmd = Ping(connection=terminal.moler_connection, destination=host, options="-w 6") print("Start pinging {} ...".format(host)) ping_cmd.start() print("Doing other stuff while pinging {} ...".format(host)) time.sleep(3) ping_stats = ping_cmd.await_done(timeout=4) print("ping {}: {}={}, {}={} [{}]".format(host, 'packet_loss', ping_stats['packet_loss'], 'time_avg', ping_stats['time_avg'], ping_stats['time_unit'])) # result: """ Start pinging www.google.com ... Doing other stuff while pinging www.google.com ... ping www.google.com: packet_loss=0, time_avg=50.000 [ms] """
def test_ping_observer_timeout(buffer_connection): from moler.exceptions import CommandTimeout with pytest.raises(CommandTimeout): cmd_ping = Ping(buffer_connection.moler_connection, destination='localhost') cmd_ping(timeout=0.1)