def test_calling_link_getter_from_ls_command_output(buffer_connection): from moler.cmd.unix.ls import Ls command_output, expected_result = command_output_and_expected_result() buffer_connection.remote_inject_response([command_output]) ls_cmd = Ls(connection=buffer_connection.moler_connection, options="-l") ls_cmd() links = ls_cmd.get_links() expected_links = { "bcn": { "permissions": "lrwxrwxrwx", "hard_links_count": 1, "owner": "root", "group": "root", "size_bytes": 4, "size_raw": "4", "date": "Mar 20 2015", "name": "bcn", "link": "/bcn" }, "logsremote": { "permissions": "lrwxrwxrwx", "hard_links_count": 1, "owner": "root", "group": "root", "size_bytes": 10, "size_raw": "10", "date": "Mar 20 2015", "name": "logsremote", "link": "/mnt/logs/" }, } assert links == expected_links
def test_moler_test_exception_no_exception(): ConnectionObserver.get_unraised_exceptions() from moler.cmd.unix.ls import Ls cmd = Ls(connection=None) cmd.set_exception("wrong exception") with pytest.raises(MolerStatusException): moler_test_not_raise_exception_when_no_steps_end_for_global_method()
def test_calling_file_getter_from_ls_command_output(buffer_connection): from moler.cmd.unix.ls import Ls command_output, expected_result = command_output_and_expected_result() buffer_connection.remote_inject_response([command_output]) ls_cmd = Ls(connection=buffer_connection.moler_connection, options="-l") ls_cmd() files = ls_cmd.get_files() expected_files = { "getfzmip.txt": { "permissions": "-rw-r--r--", "hard_links_count": 1, "owner": "root", "group": "root", "size_bytes": 51, "size_raw": "51", "date": "Dec 15 10:48", "name": "getfzmip.txt", }, "getfzmip.txt-old.20171215-104858.txt": { "permissions": "-rw-r--r--", "hard_links_count": 1, "owner": "root", "group": "root", "size_bytes": 24, "size_raw": "24", "date": "Dec 15 10:48", "name": "getfzmip.txt-old.20171215-104858.txt", }, } assert files == expected_files
def test_calling_dir_getter_from_ls_command_output(buffer_connection): from moler.cmd.unix.ls import Ls command_output, expected_result = command_output_and_expected_result() buffer_connection.remote_inject_response([command_output]) ls_cmd = Ls(connection=buffer_connection.moler_connection, options="-l") ls_cmd() dirs = ls_cmd.get_dirs() expected_dirs = { "bin": { "permissions": "drwxr-xr-x", "hard_links_count": 2, "owner": "root", "group": "root", "size_bytes": 4096, "size_raw": "4096", "date": "Sep 25 2014", "name": "bin", }, "btslog2": { "permissions": "drwxr-xr-x", "hard_links_count": 5, "owner": "root", "group": "root", "size_bytes": 4096, "size_raw": "4096", "date": "Mar 20 2015", "name": "btslog2" }, } assert dirs == expected_dirs
def test_calling_ls_returns_result_parsed_from_command_output( buffer_connection, command_output_and_expected_result): command_output, expected_result = command_output_and_expected_result buffer_connection.remote_inject_response([command_output]) ls_cmd = Ls(connection=buffer_connection.moler_connection) result = ls_cmd() assert result == expected_result
def test_sudo_su_object(buffer_connection, command_output_and_expected_result_ls_l): from moler.cmd.unix.sudo import Sudo from moler.cmd.unix.ls import Ls command_output = command_output_and_expected_result_ls_l[0] expected_dict = command_output_and_expected_result_ls_l[1] buffer_connection.remote_inject_response([command_output]) cmd_ls = Ls(connection=buffer_connection.moler_connection, options="-l") cmd_su = Su(connection=buffer_connection.moler_connection, cmd_object=cmd_ls) cmd_sudo = Sudo(connection=buffer_connection.moler_connection, cmd_object=cmd_su) ret = cmd_sudo() assert ret == expected_dict
def test_terminal_whoami_ls(terminal_connection): terminal = terminal_connection cmd = Whoami(connection=terminal) ret = cmd() user1 = ret['USER'] cmd = Ls(connection=terminal) cmd() cmd = Whoami(connection=terminal) ret = cmd() user2 = ret['USER'] assert user1 == user2 assert getpass.getuser() == user2
def test_connection_observer_exception_do_not_remove(): ConnectionObserver.get_unraised_exceptions(True) time.sleep(0.1) from moler.cmd.unix.ls import Ls from moler.exceptions import CommandTimeout cmd = Ls(None) none_exceptions = ConnectionObserver.get_unraised_exceptions(True) assert 0 == len(none_exceptions) cmd.set_exception(CommandTimeout(cmd, 0.1)) active_exceptions = ConnectionObserver.get_unraised_exceptions(False) assert 1 == len(active_exceptions) cmd = Ls(None) cmd.set_exception(CommandTimeout(cmd, 0.1)) active_exceptions = ConnectionObserver.get_unraised_exceptions(False) assert 2 == len(active_exceptions) active_exceptions = ConnectionObserver.get_unraised_exceptions(True) assert 2 == len(active_exceptions) none_exceptions = ConnectionObserver.get_unraised_exceptions(True) assert 0 == len(none_exceptions)
def test_connection_observer_one_exception(): ConnectionObserver.get_unraised_exceptions(True) time.sleep(0.1) from moler.cmd.unix.ls import Ls from moler.exceptions import CommandTimeout cmd = Ls(None) none_exceptions = ConnectionObserver.get_unraised_exceptions(True) assert 0 == len(none_exceptions) cmd.set_exception(CommandTimeout(cmd, 0.1)) cmd._is_done = True active_exceptions = ConnectionObserver.get_unraised_exceptions(True) assert 1 == len(active_exceptions) try: cmd.result() except CommandTimeout: pass none_exceptions = ConnectionObserver.get_unraised_exceptions(True) assert 0 == len(none_exceptions) none_exceptions = ConnectionObserver.get_unraised_exceptions(True) assert 0 == len(none_exceptions)
def test_connection_observer_exception_do_not_remove(): ConnectionObserver.get_unraised_exceptions(True) time.sleep(0.1) from moler.cmd.unix.ls import Ls from moler.exceptions import CommandTimeout from moler.exceptions import WrongUsage cmd = Ls(None) none_exceptions = ConnectionObserver.get_unraised_exceptions(True) assert 0 == len(none_exceptions) cmd.set_exception(CommandTimeout(cmd, 0.1)) cmd._is_done = True active_exceptions = ConnectionObserver.get_unraised_exceptions(False) assert 1 == len(active_exceptions) cmd = Ls(None) ctoe = CommandTimeout(cmd, 0.1) cwue = WrongUsage(cmd, "Another exception") cmd.set_exception(ctoe) cmd._is_done = True cmd.set_exception(cwue) active_exceptions = ConnectionObserver.get_unraised_exceptions(False) assert ctoe == active_exceptions[1] assert 2 == len(active_exceptions) active_exceptions = ConnectionObserver.get_unraised_exceptions(True) assert 2 == len(active_exceptions) none_exceptions = ConnectionObserver.get_unraised_exceptions(True) assert 0 == len(none_exceptions)
def test_ls_returns_proper_command_string(buffer_connection): from moler.cmd.unix.ls import Ls ls_cmd = Ls(buffer_connection, options="-l") assert "ls -l" == ls_cmd.command_string
def test_ls_throw_ResultNotAvailableYet(buffer_connection): from moler.exceptions import ResultNotAvailableYet ls_cmd = Ls(buffer_connection, options="-l") with pytest.raises(ResultNotAvailableYet): ls_cmd.get_dirs()
def test_ls_returns_proper_command_string(buffer_connection): ls_cmd = Ls(buffer_connection, options="-l") assert "ls -l" == ls_cmd.command_string