summaryrefslogtreecommitdiff
path: root/tests/unittests/test_util.py
diff options
context:
space:
mode:
authorScott Moser <smoser@brickies.net>2016-09-21 15:45:45 -0400
committerScott Moser <smoser@brickies.net>2016-09-21 16:33:19 -0400
commit40a400e42603aa1b80d9f623bc779799b370c091 (patch)
treec6b06266be9ba57f5764290d04d61584e732512c /tests/unittests/test_util.py
parent970dbd13f5ae40b0f95ea390b72d2b3426e8e4d9 (diff)
downloadvyos-cloud-init-40a400e42603aa1b80d9f623bc779799b370c091.tar.gz
vyos-cloud-init-40a400e42603aa1b80d9f623bc779799b370c091.zip
subp: add 'update_env' argument
In order for a caller to use 'env' argument of subp, they will realistically do: env = os.environ.copy() env['FOO'] = 'BZR' subp(cmd, env=env) This shortens that to be: subp(cmd, update_env={'FOO': 'BZR'}) Add tests, and update growpart tests to use mock when playing with os.environ.
Diffstat (limited to 'tests/unittests/test_util.py')
-rw-r--r--tests/unittests/test_util.py30
1 files changed, 28 insertions, 2 deletions
diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py
index d2031f59..30f603cb 100644
--- a/tests/unittests/test_util.py
+++ b/tests/unittests/test_util.py
@@ -223,8 +223,10 @@ class TestKeyValStrings(helpers.TestCase):
class TestGetCmdline(helpers.TestCase):
def test_cmdline_reads_debug_env(self):
- os.environ['DEBUG_PROC_CMDLINE'] = 'abcd 123'
- self.assertEqual(os.environ['DEBUG_PROC_CMDLINE'], util.get_cmdline())
+ with mock.patch.dict("os.environ",
+ values={'DEBUG_PROC_CMDLINE': 'abcd 123'}):
+ ret = util.get_cmdline()
+ self.assertEqual("abcd 123", ret)
class TestLoadYaml(helpers.TestCase):
@@ -516,6 +518,7 @@ class TestSubp(helpers.TestCase):
utf8_invalid = b'ab\xaadef'
utf8_valid = b'start \xc3\xa9 end'
utf8_valid_2 = b'd\xc3\xa9j\xc8\xa7'
+ printenv = ['bash', '-c', 'for n in "$@"; do echo "$n=${!n}"; done', '--']
def printf_cmd(self, *args):
# bash's printf supports \xaa. So does /usr/bin/printf
@@ -566,6 +569,29 @@ class TestSubp(helpers.TestCase):
self.assertEqual(err, data)
self.assertEqual(out, b'')
+ def test_subp_reads_env(self):
+ with mock.patch.dict("os.environ", values={'FOO': 'BAR'}):
+ out, err = util.subp(self.printenv + ['FOO'], capture=True)
+ self.assertEqual('FOO=BAR', out.splitlines()[0])
+
+ def test_subp_env_and_update_env(self):
+ out, err = util.subp(
+ self.printenv + ['FOO', 'HOME', 'K1', 'K2'], capture=True,
+ env={'FOO': 'BAR'},
+ update_env={'HOME': '/myhome', 'K2': 'V2'})
+ self.assertEqual(
+ ['FOO=BAR', 'HOME=/myhome', 'K1=', 'K2=V2'], out.splitlines())
+
+ def test_subp_update_env(self):
+ extra = {'FOO': 'BAR', 'HOME': '/root', 'K1': 'V1'}
+ with mock.patch.dict("os.environ", values=extra):
+ out, err = util.subp(
+ self.printenv + ['FOO', 'HOME', 'K1', 'K2'], capture=True,
+ update_env={'HOME': '/myhome', 'K2': 'V2'})
+
+ self.assertEqual(
+ ['FOO=BAR', 'HOME=/myhome', 'K1=V1', 'K2=V2'], out.splitlines())
+
def test_returns_none_if_no_capture(self):
(out, err) = util.subp(self.stdin2out, data=b'', capture=False)
self.assertEqual(err, None)