Merge lp:~smoser/curtin/trunk.run-in-chroot-revival into lp:~curtin-dev/curtin/trunk

Proposed by Scott Moser
Status: Merged
Merged at revision: 422
Proposed branch: lp:~smoser/curtin/trunk.run-in-chroot-revival
Merge into: lp:~curtin-dev/curtin/trunk
Diff against target: 65 lines (+41/-0)
2 files modified
curtin/util.py (+10/-0)
tests/unittests/test_util.py (+31/-0)
To merge this branch: bzr merge lp:~smoser/curtin/trunk.run-in-chroot-revival
Reviewer Review Type Date Requested Status
Ryan Harper (community) Approve
Server Team CI bot continuous-integration Approve
Review via email: mp+304271@code.launchpad.net

Commit message

add support for RunInChroot in curtin.util

In my merge committed to trunk in revno 404, we dropped 'RunInChroot'.
That method was being used from curthooks in maas-images project to
make a chrootable target. In order to support installation of old images
that depend on RunInChroot we need to add the method back.

This does that by just utilizing ChrootableTarget. It also adds
a couple unit tests to ensure that this keeps working.

To post a comment you must log in.
422. By Scott Moser

fix tox, remove 'print'

Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ryan Harper (raharper) :
Revision history for this message
Scott Moser (smoser) wrote :

To address your concerns about subp tests, I added some more here.
https://code.launchpad.net/~smoser/curtin/trunk.subp-more-tests/+merge/304297

Revision history for this message
Ryan Harper (raharper) wrote :

Additional tests look good. We can merge this and those after.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'curtin/util.py'
--- curtin/util.py 2016-08-11 20:30:27 +0000
+++ curtin/util.py 2016-08-29 17:12:52 +0000
@@ -143,6 +143,8 @@
143 a list of times to sleep in between retries. After each failure143 a list of times to sleep in between retries. After each failure
144 subp will sleep for N seconds and then try again. A value of [1, 3]144 subp will sleep for N seconds and then try again. A value of [1, 3]
145 means to run, sleep 1, run, sleep 3, run and then return exit code.145 means to run, sleep 1, run, sleep 3, run and then return exit code.
146 :param target:
147 run the command as 'chroot target <args>'
146 """148 """
147 retries = []149 retries = []
148 if "retries" in kwargs:150 if "retries" in kwargs:
@@ -1082,4 +1084,12 @@
1082 return os.path.join(target, path)1084 return os.path.join(target, path)
10831085
10841086
1087class RunInChroot(ChrootableTarget):
1088 """Backwards compatibility for RunInChroot (LP: #1617375).
1089 It needs to work like:
1090 with RunInChroot("/target") as in_chroot:
1091 in_chroot(["your", "chrooted", "command"])"""
1092 __call__ = ChrootableTarget.subp
1093
1094
1085# vi: ts=4 expandtab syntax=python1095# vi: ts=4 expandtab syntax=python
10861096
=== modified file 'tests/unittests/test_util.py'
--- tests/unittests/test_util.py 2016-08-08 18:40:17 +0000
+++ tests/unittests/test_util.py 2016-08-29 17:12:52 +0000
@@ -363,4 +363,35 @@
363 self.assertEqual("/target/my/path/",363 self.assertEqual("/target/my/path/",
364 util.target_path("/target/", "///my/path/"))364 util.target_path("/target/", "///my/path/"))
365365
366
367class TestRunInChroot(TestCase):
368 """Test the legacy 'RunInChroot'.
369
370 The test works by mocking ChrootableTarget's __enter__ to do nothing.
371 The assumptions made are:
372 a.) RunInChroot is a subclass of ChrootableTarget
373 b.) ChrootableTarget's __exit__ only un-does work that its __enter__
374 did. Meaning for our mocked case, it does nothing."""
375
376 @mock.patch.object(util.ChrootableTarget, "__enter__", new=lambda a: a)
377 def test_run_in_chroot_with_target_slash(self):
378 with util.RunInChroot("/") as i:
379 out, err = i(['echo', 'HI MOM'], capture=True)
380 self.assertEqual('HI MOM\n', out)
381
382 @mock.patch.object(util.ChrootableTarget, "__enter__", new=lambda a: a)
383 @mock.patch("curtin.util.subp")
384 def test_run_in_chroot_with_target(self, m_subp):
385 my_stdout = "my output"
386 my_stderr = "my stderr"
387 cmd = ['echo', 'HI MOM']
388 target = "/foo"
389 m_subp.return_value = (my_stdout, my_stderr)
390 with util.RunInChroot(target) as i:
391 out, err = i(cmd)
392 self.assertEqual(my_stdout, out)
393 self.assertEqual(my_stderr, err)
394 m_subp.assert_called_with(cmd, target=target)
395
396
366# vi: ts=4 expandtab syntax=python397# vi: ts=4 expandtab syntax=python

Subscribers

People subscribed via source and target branches