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 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
1=== modified file 'curtin/util.py'
2--- curtin/util.py 2016-08-11 20:30:27 +0000
3+++ curtin/util.py 2016-08-29 17:12:52 +0000
4@@ -143,6 +143,8 @@
5 a list of times to sleep in between retries. After each failure
6 subp will sleep for N seconds and then try again. A value of [1, 3]
7 means to run, sleep 1, run, sleep 3, run and then return exit code.
8+ :param target:
9+ run the command as 'chroot target <args>'
10 """
11 retries = []
12 if "retries" in kwargs:
13@@ -1082,4 +1084,12 @@
14 return os.path.join(target, path)
15
16
17+class RunInChroot(ChrootableTarget):
18+ """Backwards compatibility for RunInChroot (LP: #1617375).
19+ It needs to work like:
20+ with RunInChroot("/target") as in_chroot:
21+ in_chroot(["your", "chrooted", "command"])"""
22+ __call__ = ChrootableTarget.subp
23+
24+
25 # vi: ts=4 expandtab syntax=python
26
27=== modified file 'tests/unittests/test_util.py'
28--- tests/unittests/test_util.py 2016-08-08 18:40:17 +0000
29+++ tests/unittests/test_util.py 2016-08-29 17:12:52 +0000
30@@ -363,4 +363,35 @@
31 self.assertEqual("/target/my/path/",
32 util.target_path("/target/", "///my/path/"))
33
34+
35+class TestRunInChroot(TestCase):
36+ """Test the legacy 'RunInChroot'.
37+
38+ The test works by mocking ChrootableTarget's __enter__ to do nothing.
39+ The assumptions made are:
40+ a.) RunInChroot is a subclass of ChrootableTarget
41+ b.) ChrootableTarget's __exit__ only un-does work that its __enter__
42+ did. Meaning for our mocked case, it does nothing."""
43+
44+ @mock.patch.object(util.ChrootableTarget, "__enter__", new=lambda a: a)
45+ def test_run_in_chroot_with_target_slash(self):
46+ with util.RunInChroot("/") as i:
47+ out, err = i(['echo', 'HI MOM'], capture=True)
48+ self.assertEqual('HI MOM\n', out)
49+
50+ @mock.patch.object(util.ChrootableTarget, "__enter__", new=lambda a: a)
51+ @mock.patch("curtin.util.subp")
52+ def test_run_in_chroot_with_target(self, m_subp):
53+ my_stdout = "my output"
54+ my_stderr = "my stderr"
55+ cmd = ['echo', 'HI MOM']
56+ target = "/foo"
57+ m_subp.return_value = (my_stdout, my_stderr)
58+ with util.RunInChroot(target) as i:
59+ out, err = i(cmd)
60+ self.assertEqual(my_stdout, out)
61+ self.assertEqual(my_stderr, err)
62+ m_subp.assert_called_with(cmd, target=target)
63+
64+
65 # vi: ts=4 expandtab syntax=python

Subscribers

People subscribed via source and target branches