Skip to content

Commit ab06cc3

Browse files
authored
preserve-cwd only when preserve-cwd is provided (bugfix) (#2352)
* preserve-cwd only when preserve-cwd is provided Snaps services cwd is calculated by snapd and depends on the user. Using the service cwd as the job cwd present one challenge, if the job is a user job it may not have access to that snap cwd (that may be in /root/snap/...). Additionally I believe that preserving cwd always in snaps was something done in the past because `/tmp` is private for snaps. For now `/var/tmp` is not, and we use this quite a lot, so I'll use that * Update test to the new nominal behaviour
1 parent 6ca9f1f commit ab06cc3

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

checkbox-ng/plainbox/impl/execution.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,17 @@ def get_proper_job_cwd(self, job):
426426
:returns:
427427
Pathname of the new directory
428428
"""
429-
if "preserve-cwd" in job.get_flag_set() or os.getenv("SNAP"):
429+
if "preserve-cwd" in job.get_flag_set():
430430
yield os.getcwd()
431431
return
432432
# Create a nest for all the private executables needed for execution
433433
prefix = "cwd-"
434434
suffix = ".{}".format(job.checksum)
435435
try:
436-
with tempfile.TemporaryDirectory(suffix, prefix) as cwd_dir:
436+
# use /var/tmp because in snaps /tmp is private to the snap
437+
with tempfile.TemporaryDirectory(
438+
suffix, prefix, "/var/tmp"
439+
) as cwd_dir:
437440
os.chmod(cwd_dir, 0o777)
438441
logger.debug(
439442
_("Job temporary current working directory: %s"), cwd_dir

checkbox-ng/plainbox/impl/test_execution.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,30 @@ def test_get_proper_job_cwd_preserve_cwd(self, os_cwd_mock):
334334

335335
@mock.patch("os.getcwd")
336336
@mock.patch("os.getenv")
337-
def test_get_proper_job_cwd_snap(self, os_getenv_mock, os_cwd_mock):
337+
@mock.patch("os.chmod")
338+
def test_get_proper_job_cwd_snap(
339+
self, chmod_mock, os_getenv_mock, os_cwd_mock
340+
):
338341
self_mock = mock.Mock()
339342
job_mock = mock.Mock()
340343
job_mock.get_flag_set.return_value = {}
341344
os_getenv_mock.return_value = "/snap/checkbox24"
342-
with UnifiedRunner.get_proper_job_cwd(self_mock, job_mock) as cwd:
343-
self.assertEqual(cwd, os_cwd_mock())
345+
346+
class TemporaryDirectoryMock:
347+
def __init__(self, suffix, prefix, dir, *args, **kwargs):
348+
self.dir = dir
349+
350+
def __enter__(self):
351+
return self.dir + "/some"
352+
353+
def __exit__(self, *args): ...
354+
355+
with mock.patch(
356+
"tempfile.TemporaryDirectory", new=TemporaryDirectoryMock
357+
) as tmp:
358+
with UnifiedRunner.get_proper_job_cwd(self_mock, job_mock) as cwd:
359+
self.assertTrue(str(cwd).startswith("/var/tmp"))
360+
self.assertTrue(self_mock._check_leftovers.called)
344361

345362

346363
class TestDangerousNsenter(TestCase):

0 commit comments

Comments
 (0)