Merge ~smoser/cloud-init:feature/subp_add_update_env into cloud-init:master

Proposed by Scott Moser
Status: Merged
Merged at revision: 40a400e42603aa1b80d9f623bc779799b370c091
Proposed branch: ~smoser/cloud-init:feature/subp_add_update_env
Merge into: cloud-init:master
Diff against target: 69 lines (+32/-1)
2 files modified
cloudinit/util.py (+8/-1)
tests/unittests/test_util.py (+24/-0)
Reviewer Review Type Date Requested Status
cloud-init Commiters Pending
Review via email: mp+306391@code.launchpad.net
To post a comment you must log in.

There was an error fetching revisions from git servers. Please try again in a few minutes. If the problem persists, contact Launchpad support.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/util.py b/cloudinit/util.py
2index 6c5cf74..05cb587 100644
3--- a/cloudinit/util.py
4+++ b/cloudinit/util.py
5@@ -1762,7 +1762,7 @@ def delete_dir_contents(dirname):
6
7
8 def subp(args, data=None, rcs=None, env=None, capture=True, shell=False,
9- logstring=False, decode="replace", target=None):
10+ logstring=False, decode="replace", target=None, update_env=None):
11
12 # not supported in cloud-init (yet), for now kept in the call signature
13 # to ease maintaining code shared between cloud-init and curtin
14@@ -1773,6 +1773,13 @@ def subp(args, data=None, rcs=None, env=None, capture=True, shell=False,
15 rcs = [0]
16
17 devnull_fp = None
18+
19+ if update_env:
20+ if env is None:
21+ env = os.environ
22+ env = env.copy()
23+ env.update(update_env)
24+
25 try:
26 if target_path(target) != "/":
27 args = ['chroot', target] + list(args)
28diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py
29index d2031f5..81a13e2 100644
30--- a/tests/unittests/test_util.py
31+++ b/tests/unittests/test_util.py
32@@ -516,6 +516,7 @@ class TestSubp(helpers.TestCase):
33 utf8_invalid = b'ab\xaadef'
34 utf8_valid = b'start \xc3\xa9 end'
35 utf8_valid_2 = b'd\xc3\xa9j\xc8\xa7'
36+ printenv = ['bash', '-c', 'for n in "$@"; do echo "$n=${!n}"; done', '--']
37
38 def printf_cmd(self, *args):
39 # bash's printf supports \xaa. So does /usr/bin/printf
40@@ -566,6 +567,29 @@ class TestSubp(helpers.TestCase):
41 self.assertEqual(err, data)
42 self.assertEqual(out, b'')
43
44+ def test_subp_reads_env(self):
45+ with mock.patch.dict("os.environ", values={'FOO': 'BAR'}):
46+ out, err = util.subp(self.printenv + ['FOO'], capture=True)
47+ self.assertEqual('FOO=BAR', out.splitlines()[0])
48+
49+ def test_subp_env_and_update_env(self):
50+ out, err = util.subp(
51+ self.printenv + ['FOO', 'HOME', 'K1', 'K2'], capture=True,
52+ env={'FOO': 'BAR'},
53+ update_env={'HOME': '/myhome', 'K2': 'V2'})
54+ self.assertEqual(
55+ ['FOO=BAR', 'HOME=/myhome', 'K1=', 'K2=V2'], out.splitlines())
56+
57+ def test_subp_update_env(self):
58+ extra = {'FOO': 'BAR', 'HOME': '/root', 'K1': 'V1'}
59+ with mock.patch.dict("os.environ", values=extra) as env:
60+ out, err = util.subp(
61+ self.printenv + ['FOO', 'HOME', 'K1', 'K2'], capture=True,
62+ update_env={'HOME': '/myhome', 'K2': 'V2'})
63+
64+ self.assertEqual(
65+ ['FOO=BAR', 'HOME=/myhome', 'K1=V1', 'K2=V2'], out.splitlines())
66+
67 def test_returns_none_if_no_capture(self):
68 (out, err) = util.subp(self.stdin2out, data=b'', capture=False)
69 self.assertEqual(err, None)

Subscribers

People subscribed via source and target branches