summaryrefslogtreecommitdiff
path: root/tests/unittests
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2018-01-23 18:08:14 -0700
committerChad Smith <chad.smith@canonical.com>2018-01-23 18:08:14 -0700
commit183d5785954af3a1e7603798d4a91ab126eb7bb9 (patch)
treec9ef1b49b37f15bc140ab822884b2c901d9253bc /tests/unittests
parentc02a4d4c88cc2c6ec9f03ddf86703f5b67e04348 (diff)
downloadvyos-cloud-init-183d5785954af3a1e7603798d4a91ab126eb7bb9.tar.gz
vyos-cloud-init-183d5785954af3a1e7603798d4a91ab126eb7bb9.zip
subp: make ProcessExecutionError have expected types in stderr, stdout.
When subp raised a ProcessExecutionError, that exception's stderr and stdout might end up being the string '-' rather than bytes. This mean that:    try:        subp(mycommand, decode=False)    except ProcessExecutionError as e:        pass Would have 'e.stdout' set to '-' while the caller would expect bytes. Also reduce the try/except block in subp to a specifically the two lines that may raise an OSError.
Diffstat (limited to 'tests/unittests')
-rw-r--r--tests/unittests/test_util.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py
index d63b760e..4a92e741 100644
--- a/tests/unittests/test_util.py
+++ b/tests/unittests/test_util.py
@@ -623,6 +623,7 @@ class TestSubp(helpers.CiTestCase):
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', '--']
+ bogus_command = 'this-is-not-expected-to-be-a-program-name'
def printf_cmd(self, *args):
# bash's printf supports \xaa. So does /usr/bin/printf
@@ -712,6 +713,20 @@ class TestSubp(helpers.CiTestCase):
self.assertIsNone(err)
self.assertIsNone(out)
+ def test_exception_has_out_err_are_bytes_if_decode_false(self):
+ """Raised exc should have stderr, stdout as bytes if no decode."""
+ with self.assertRaises(util.ProcessExecutionError) as cm:
+ util.subp([self.bogus_command], decode=False)
+ self.assertTrue(isinstance(cm.exception.stdout, bytes))
+ self.assertTrue(isinstance(cm.exception.stderr, bytes))
+
+ def test_exception_has_out_err_are_bytes_if_decode_true(self):
+ """Raised exc should have stderr, stdout as string if no decode."""
+ with self.assertRaises(util.ProcessExecutionError) as cm:
+ util.subp([self.bogus_command], decode=True)
+ self.assertTrue(isinstance(cm.exception.stdout, six.string_types))
+ self.assertTrue(isinstance(cm.exception.stderr, six.string_types))
+
def test_bunch_of_slashes_in_path(self):
self.assertEqual("/target/my/path/",
util.target_path("/target/", "//my/path/"))