Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## moler 4.3.2
* rm fails on permission denied

## moler 4.3.1
* duplicates in ping

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
10 changes: 8 additions & 2 deletions moler/cmd/unix/rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
24 changes: 21 additions & 3 deletions test/cmd/unix/test_cmd_rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,31 @@
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):
from moler.cmd.unix.rm import Rm
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"
30 changes: 26 additions & 4 deletions test/cmd/unix/test_cmd_su.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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]

Expand Down Expand Up @@ -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 = {
Expand Down
18 changes: 17 additions & 1 deletion test/cmd/unix/test_cmd_sudo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down