44"""
55
66__author__ = 'Agnieszka Bylica, Marcin Usielski'
7- __copyright__ = 'Copyright (C) 2018-2023 , Nokia'
7+ __copyright__ = 'Copyright (C) 2018-2025 , Nokia'
8899
1010import pytest
@@ -66,6 +66,29 @@ def test_sudo_su(buffer_connection):
6666 assert ret == expected_dict
6767
6868
69+ def test_sudo_su_twice_failed_object (buffer_connection ):
70+ from moler .cmd .unix .sudo import Sudo
71+ from moler .cmd .unix .rm import Rm
72+ from moler .exceptions import CommandFailure
73+ rm_output_denied = """rm important_file.txt
74+ rm: cannot remove 'protected.txt': Permission denied
75+ moler_bash#"""
76+
77+ buffer_connection .remote_inject_response ([rm_output_denied ])
78+
79+ cmd_rm = Rm (connection = buffer_connection .moler_connection , file = "important_file.txt" )
80+ with pytest .raises (CommandFailure ):
81+ cmd_rm ()
82+
83+ cmd_su = Su (connection = buffer_connection .moler_connection , prompt = r"moler_bash#" ,
84+ cmd_object = cmd_rm )
85+ cmd_sudo = Sudo (connection = buffer_connection .moler_connection , cmd_object = cmd_su )
86+ with pytest .raises (CommandFailure ) as exc :
87+ cmd_sudo ()
88+ print (exc .value )
89+ assert "Not allowed to run again" in str (exc .value )
90+
91+
6992def test_sudo_su_object (buffer_connection , command_output_and_expected_result_ls_l ):
7093 from moler .cmd .unix .sudo import Sudo
7194 from moler .cmd .unix .ls import Ls
@@ -129,7 +152,7 @@ def test_su_catches_missing_binary_failure(buffer_connection):
129152@pytest .fixture
130153def command_output_and_expected_result_auth ():
131154 output = """xyz@debian:~/Moler$ su
132- Password:
155+ Password:
133156su: Authentication failure
134157xyz@debian:~/Moler$"""
135158 result = dict ()
@@ -138,7 +161,7 @@ def command_output_and_expected_result_auth():
138161
139162@pytest .fixture
140163def command_output_and_expected_result_command_format_failure ():
141- output = """xyz@debian:~/Moler$ su -g
164+ output = """xyz@debian:~/Moler$ su -g
142165su: invalid option -- 'g'
143166Usage: su [options] [LOGIN]
144167
@@ -201,7 +224,7 @@ def command_output_and_expected_result_ls_l():
201224@pytest .fixture ()
202225def command_output_and_expected_result_pwd ():
203226 output = """user@client:~/moler$ su -c 'pwd'
204- password:
227+ password:
205228/home/user/moler
206229ute@debdev:~/moler$ """
207230 result = {
0 commit comments