def test_sudo_forwards_nonsudo_specific_connection_data_into_embedded_command( buffer_connection): command_output_chunks = [ "user@client:~/moler$ sudo pwd", # sudo specific - command echo "\r\n", "[sudo] password for user:"******"\r\n", "/home/user/moler", # pwd first chunk "\r\n", "ute@debdev:~/moler$" ] buffer_connection.remote_inject_response(command_output_chunks) on_new_line_params = [] original_on_new_line = Pwd.on_new_line def pwd_on_new_line(self, line, is_full_line): on_new_line_params.append((line, is_full_line)) original_on_new_line(self, line, is_full_line) cmd_pwd = Pwd(connection=buffer_connection.moler_connection) cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="******", cmd_object=cmd_pwd) with mock.patch.object(Pwd, "on_new_line", pwd_on_new_line): cmd_sudo() assert on_new_line_params == [("/home/user/moler", False), ("/home/user/moler", True), ("ute@debdev:~/moler$", False)]
def test_can_ignore_timeout_of_embedded_command_if_direct_timeout_provided( buffer_connection, command_output_and_expected_result_timeout): command_output = command_output_and_expected_result_timeout buffer_connection.remote_inject_response([command_output]) cmd_pwd = Pwd(connection=buffer_connection.moler_connection) cmd_pwd.timeout = 0.8 cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="******", cmd_object=cmd_pwd) cmd_sudo.terminating_timeout = 0 # no additional sudo timeout for Ctrl-C..till..prompt (shutdown after cmd timeout) start_time = time.time() with pytest.raises(CommandTimeout): cmd_sudo(timeout=0.5) duration = time.time() - start_time assert duration >= 0.5 assert duration < 0.8
def test_command_not_found(buffer_connection, command_output_command_not_found): command_output = command_output_command_not_found buffer_connection.remote_inject_response([command_output]) cmd_pwd = Pwd(connection=buffer_connection.moler_connection) cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="******", cmd_object=cmd_pwd) with pytest.raises(CommandFailure): cmd_sudo()
def test_failing_with_timeout(buffer_connection, command_output_and_expected_result_timeout): command_output = command_output_and_expected_result_timeout buffer_connection.remote_inject_response([command_output]) cmd_pwd = Pwd(connection=buffer_connection.moler_connection) cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="******", cmd_object=cmd_pwd) with pytest.raises(CommandTimeout): cmd_sudo(timeout=0.1)
def test_calling_by_command_object(buffer_connection, command_output_and_expected_result): command_output, expected_result = command_output_and_expected_result buffer_connection.remote_inject_response([command_output]) cmd_pwd = Pwd(connection=buffer_connection.moler_connection) cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="******", cmd_object=cmd_pwd) assert "sudo pwd" == cmd_sudo.command_string result = cmd_sudo() assert result == expected_result
def test_failing_calling_twice_the_same_command_object(buffer_connection, command_output_and_expected_result): command_output, expected_result = command_output_and_expected_result buffer_connection.remote_inject_response([command_output]) cmd_pwd = Pwd(connection=buffer_connection.moler_connection) cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="******", cmd_object=cmd_pwd) result = cmd_sudo() assert result == expected_result cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="******", cmd_object=cmd_pwd) with pytest.raises(CommandFailure): cmd_sudo()
def test_calling_by_command_object_without_slicing( buffer_connection, command_output_and_expected_result): command_output, expected_result = command_output_and_expected_result buffer_connection.remote_inject_response([command_output]) cmd_pwd = Pwd(connection=buffer_connection.moler_connection) cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="******", cmd_object=cmd_pwd) cmd_sudo._max_index_from_end = 0 cmd_sudo._max_index_from_beginning = 0 assert "sudo pwd" == cmd_sudo.command_string result = cmd_sudo() assert result == expected_result