Skip to content

Commit 16d4fb6

Browse files
committed
Cover modification to execution
1 parent 03b5dcb commit 16d4fb6

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

checkbox-ng/plainbox/impl/test_execution.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import contextlib
20+
1921
from plainbox.impl.execution import (
2022
UnifiedRunner,
2123
get_execution_command_systemd_unit,
24+
get_execution_command_subshell,
2225
)
2326
from plainbox.impl.unit.job import InvalidJob
2427

@@ -104,3 +107,128 @@ def shutil_which(x):
104107
self.assertIn("test_command", result)
105108
# don't log in root commands
106109
self.assertNotIn("-pam", result)
110+
111+
@mock.patch(
112+
"plainbox.impl.execution.get_differential_execution_environment"
113+
)
114+
@mock.patch("plainbox.impl.execution.on_ubuntucore")
115+
def test_get_execution_command_subshell_with_user(
116+
self, mock_on_ubuntucore, mock_get_diff_env
117+
):
118+
job = mock.Mock(shell="bash", command="test_command")
119+
mock_on_ubuntucore.return_value = False
120+
mock_get_diff_env.return_value = {"TEST_VAR": "test_value"}
121+
122+
result = get_execution_command_subshell(
123+
job, {}, "test_session", "/tmp/nest", "ubuntu", None
124+
)
125+
result = " ".join(result)
126+
127+
self.assertIn("sudo", result)
128+
self.assertIn("TEST_VAR=test_value", result)
129+
self.assertIn("test_command", result)
130+
self.assertNotIn("aa-exec", result)
131+
132+
@mock.patch("plainbox.impl.execution.get_execution_environment")
133+
@mock.patch("plainbox.impl.execution.on_ubuntucore")
134+
def test_get_execution_command_subshell_no_user(
135+
self, mock_on_ubuntucore, mock_get_env
136+
):
137+
job = mock.Mock(shell="bash", command="test_command")
138+
mock_on_ubuntucore.return_value = False
139+
mock_get_env.return_value = {"TEST_VAR": "test_value"}
140+
141+
result = get_execution_command_subshell(
142+
job, {}, "test_session", "/tmp/nest", None, None
143+
)
144+
result = " ".join(result)
145+
146+
self.assertNotIn("sudo", result)
147+
self.assertIn("TEST_VAR=test_value", result)
148+
self.assertIn("test_command", result)
149+
self.assertNotIn("aa-exec", result)
150+
151+
@mock.patch("plainbox.impl.execution.get_execution_environment")
152+
@mock.patch("plainbox.impl.execution.on_ubuntucore")
153+
def test_get_execution_command_subshell_on_core(
154+
self, mock_on_ubuntucore, mock_get_env
155+
):
156+
job = mock.Mock(shell="bash", command="test_command")
157+
mock_on_ubuntucore.return_value = True
158+
mock_get_env.return_value = {"TEST_VAR": "test_value"}
159+
160+
result = get_execution_command_subshell(
161+
job, {}, "test_session", "/tmp/nest", None, None
162+
)
163+
result = " ".join(result)
164+
165+
self.assertNotIn("sudo", result)
166+
self.assertIn("TEST_VAR=test_value", result)
167+
self.assertIn("test_command", result)
168+
self.assertIn("aa-exec -p unconfined", result)
169+
170+
@mock.patch("plainbox.impl.execution.get_execution_environment")
171+
@mock.patch("plainbox.impl.execution.get_execution_command_subshell")
172+
@mock.patch("plainbox.impl.execution.get_execution_command_systemd_unit")
173+
@mock.patch("getpass.getuser")
174+
def test_execute_job_subshell(
175+
self,
176+
getuser_mock,
177+
get_execution_command_systemd_unit_mock,
178+
get_execution_command_subshell_mock,
179+
get_execution_environment_mock,
180+
):
181+
@contextlib.contextmanager
182+
def configured_filesystem_mock(self, *args, **kwargs):
183+
yield
184+
185+
self_mock = mock.Mock(configured_filesystem=configured_filesystem_mock)
186+
self_mock._user_provider.return_value = None
187+
188+
job_mock = mock.Mock(user="ubuntu")
189+
job_mock.get_flag_set.return_value = {"preserve-cwd"}
190+
getuser_mock.return_value = "ubuntu"
191+
192+
get_execution_command_subshell_mock.side_effect = Exception("subshell")
193+
get_execution_command_systemd_unit_mock.side_effect = Exception(
194+
"systemd"
195+
)
196+
197+
with self.assertRaises(Exception) as e:
198+
UnifiedRunner.execute_job(self_mock, job_mock, {}, mock.Mock())
199+
200+
self.assertEqual(str(e.exception), "subshell")
201+
202+
@mock.patch("plainbox.impl.execution.get_execution_environment")
203+
@mock.patch("plainbox.impl.execution.get_execution_command_subshell")
204+
@mock.patch("plainbox.impl.execution.get_execution_command_systemd_unit")
205+
@mock.patch("getpass.getuser")
206+
def test_execute_job_systemd(
207+
self,
208+
getuser_mock,
209+
get_execution_command_systemd_unit_mock,
210+
get_execution_command_subshell_mock,
211+
get_execution_environment_mock,
212+
):
213+
@contextlib.contextmanager
214+
def configured_filesystem_mock(self, *args, **kwargs):
215+
yield
216+
217+
self_mock = mock.Mock(configured_filesystem=configured_filesystem_mock)
218+
self_mock._user_provider.return_value = None
219+
220+
job_mock = mock.Mock(user="ubuntu")
221+
job_mock.get_flag_set.return_value = {"preserve-cwd"}
222+
getuser_mock.return_value = "ubuntu"
223+
224+
get_execution_command_subshell_mock.side_effect = Exception("subshell")
225+
get_execution_command_systemd_unit_mock.side_effect = Exception(
226+
"systemd"
227+
)
228+
229+
with self.assertRaises(Exception) as e:
230+
UnifiedRunner.execute_job(
231+
self_mock, job_mock, {}, mock.Mock(), systemd_unit=True
232+
)
233+
234+
self.assertEqual(str(e.exception), "systemd")

0 commit comments

Comments
 (0)