diff --git a/CHANGELOG.md b/CHANGELOG.md index f2685de75..dd5fe7551 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## moler 4.3.2 + * rm fails on permission denied + ## moler 4.3.1 * duplicates in ping diff --git a/README.md b/README.md index 14a8b70ef..c9b777ae8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![image](https://img.shields.io/badge/pypi-v4.3.0-blue.svg)](https://pypi.org/project/moler/) +[![image](https://img.shields.io/badge/pypi-v4.3.2-blue.svg)](https://pypi.org/project/moler/) [![image](https://img.shields.io/badge/python-3.7%20%7C%203.8%20%7C%203.9%20%7C%203.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-blue.svg)](https://pypi.org/project/moler/) [![Build Status](https://github.com/nokia/moler/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/nokia/moler/actions) [![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](./LICENSE) diff --git a/moler/cmd/unix/rm.py b/moler/cmd/unix/rm.py index d03fae8ac..af033d433 100644 --- a/moler/cmd/unix/rm.py +++ b/moler/cmd/unix/rm.py @@ -4,10 +4,12 @@ """ __author__ = 'Bartosz Odziomek, Marcin Usielski' -__copyright__ = 'Copyright (C) 2018-2023, Nokia' +__copyright__ = 'Copyright (C) 2018-2025, Nokia' __email__ = 'bartosz.odziomek@nokia.com, marcin.usielski@nokia.com' -from moler.cmd.unix.genericunix import GenericUnixCommand +from moler.helpers import copy_list +from moler.cmd.unix.genericunix import GenericUnixCommand, cmd_failure_causes +import re class Rm(GenericUnixCommand): @@ -25,6 +27,10 @@ def __init__(self, connection, file, options=None, prompt=None, newline_chars=No self.file = file self.options = options self.ret_required = False + _cmd_failure_causes = copy_list(cmd_failure_causes) + _cmd_failure_causes.append(r"cannot remove\s*'.*':\s*Permission denied") + r_cmd_failure_cause_alternatives = "|".join(_cmd_failure_causes) + self.re_fail = re.compile(r_cmd_failure_cause_alternatives, re.IGNORECASE) def build_command_string(self): """ diff --git a/setup.py b/setup.py index a6ab78069..d0284d944 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ setup( name='moler', - version='4.3.1', + version='4.3.2', description='Moler is a library for working with terminals, mainly for automated tests', # Required long_description=long_description, long_description_content_type='text/markdown', diff --git a/test/cmd/unix/test_cmd_rm.py b/test/cmd/unix/test_cmd_rm.py index 03b1eb119..3abd3c56d 100644 --- a/test/cmd/unix/test_cmd_rm.py +++ b/test/cmd/unix/test_cmd_rm.py @@ -3,9 +3,9 @@ Rm command module. """ -__author__ = 'Bartosz Odziomek' -__copyright__ = 'Copyright (C) 2018, Nokia' -__email__ = 'bartosz.odziomek@nokia.com' +__author__ = 'Bartosz Odziomek, Marcin Usielski' +__copyright__ = 'Copyright (C) 2018-2025, Nokia' +__email__ = 'bartosz.odziomek@nokia.com, marcin.usielski@nokia.com' def test_rm_returns_proper_command_string(buffer_connection): @@ -13,3 +13,21 @@ def test_rm_returns_proper_command_string(buffer_connection): rm_cmd = Rm(connection=buffer_connection.moler_connection, file="test.txt") assert "rm test.txt" == rm_cmd.command_string + + +def test_rm_permission_denied(buffer_connection): + from moler.cmd.unix.rm import Rm + from moler.exceptions import MolerException + output = """rm protected.txt +rm: cannot remove 'protected.txt': Permission denied + cannot remove'.*': Permission denied +moler_bash#""" + + rm_cmd = Rm(connection=buffer_connection.moler_connection, file="protected.txt") + buffer_connection.remote_inject_response([output]) + try: + rm_cmd() + except MolerException as exc: + assert "Permission denied" in str(exc) + else: + assert False, "Exception not raised for permission denied" diff --git a/test/cmd/unix/test_cmd_su.py b/test/cmd/unix/test_cmd_su.py index 56d95bf6f..6c9978db1 100644 --- a/test/cmd/unix/test_cmd_su.py +++ b/test/cmd/unix/test_cmd_su.py @@ -4,7 +4,7 @@ """ __author__ = 'Agnieszka Bylica, Marcin Usielski' -__copyright__ = 'Copyright (C) 2018-2023, Nokia' +__copyright__ = 'Copyright (C) 2018-2025, Nokia' __email__ = 'agnieszka.bylica@nokia.com, marcin.usielski@nokia.com' import pytest @@ -66,6 +66,28 @@ def test_sudo_su(buffer_connection): assert ret == expected_dict +def test_sudo_su_twice_failed_object(buffer_connection): + from moler.cmd.unix.sudo import Sudo + from moler.cmd.unix.rm import Rm + from moler.exceptions import CommandFailure + rm_output_denied = """rm important_file.txt +rm: cannot remove 'important_file.txt': Permission denied +moler_bash#""" + + buffer_connection.remote_inject_response([rm_output_denied]) + + cmd_rm = Rm(connection=buffer_connection.moler_connection, file="important_file.txt") + with pytest.raises(CommandFailure): + cmd_rm() + + cmd_su = Su(connection=buffer_connection.moler_connection, prompt=r"moler_bash#", + cmd_object=cmd_rm) + cmd_sudo = Sudo(connection=buffer_connection.moler_connection, cmd_object=cmd_su) + with pytest.raises(CommandFailure) as exc: + cmd_sudo() + assert "Not allowed to run again" in str(exc.value) + + 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 @@ -129,7 +151,7 @@ def test_su_catches_missing_binary_failure(buffer_connection): @pytest.fixture def command_output_and_expected_result_auth(): output = """xyz@debian:~/Moler$ su -Password: +Password: su: Authentication failure xyz@debian:~/Moler$""" result = dict() @@ -138,7 +160,7 @@ def command_output_and_expected_result_auth(): @pytest.fixture def command_output_and_expected_result_command_format_failure(): - output = """xyz@debian:~/Moler$ su -g + output = """xyz@debian:~/Moler$ su -g su: invalid option -- 'g' Usage: su [options] [LOGIN] @@ -201,7 +223,7 @@ def command_output_and_expected_result_ls_l(): @pytest.fixture() def command_output_and_expected_result_pwd(): output = """user@client:~/moler$ su -c 'pwd' -password: +password: /home/user/moler ute@debdev:~/moler$ """ result = { diff --git a/test/cmd/unix/test_cmd_sudo.py b/test/cmd/unix/test_cmd_sudo.py index 868e13002..689ac718a 100644 --- a/test/cmd/unix/test_cmd_sudo.py +++ b/test/cmd/unix/test_cmd_sudo.py @@ -4,7 +4,7 @@ """ __author__ = 'Marcin Usielski' -__copyright__ = 'Copyright (C) 2018-2023, Nokia' +__copyright__ = 'Copyright (C) 2018-2025, Nokia' __email__ = 'marcin.usielski@nokia.com' from moler.cmd.unix.sudo import Sudo @@ -97,6 +97,22 @@ def test_failing_calling_twice_the_same_command_object(buffer_connection, comman cmd_sudo() +def test_failing_calling_twice_the_same_command_object_failed(buffer_connection): + from moler.cmd.unix.rm import Rm + rm_output_denied = """rm important_file.txt +rm: cannot remove 'protected.txt': Permission denied +moler_bash#""" + + buffer_connection.remote_inject_response([rm_output_denied]) + + cmd_rm = Rm(connection=buffer_connection.moler_connection, file="important_file.txt") + with pytest.raises(CommandFailure): + cmd_rm() + cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="pass", cmd_object=cmd_rm) + with pytest.raises(CommandFailure) as exc: + cmd_sudo() + assert "Not allowed to run again" in str(exc.value) + 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])