diff options
author | Brett Holman <bholman.devel@gmail.com> | 2021-12-08 14:27:37 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-08 15:27:37 -0600 |
commit | 65c2cfd7f21758746444c8c79444994a4638d563 (patch) | |
tree | 3b4c91b0ba35dbd8f043f779a9ea6b41f4665a16 /tests/unittests/test_subp.py | |
parent | b21afb0a8ab64543715dffff490253db8ecefb9f (diff) | |
download | vyos-cloud-init-65c2cfd7f21758746444c8c79444994a4638d563.tar.gz vyos-cloud-init-65c2cfd7f21758746444c8c79444994a4638d563.zip |
factor out function for getting top level directory of cloudinit (#1136)
Add a test helper to get top level directory
Many tests need to get the location of files & dirs within the cloud-init
project directory.
Tests implement this in various different ways, and often those ways
depend on the current working directory of the pytest invocation.
Create helper functions (and tests) that gets the path of the top
directory or any sub directory under the top directory. This function
does not depend on the environment.
Diffstat (limited to 'tests/unittests/test_subp.py')
-rw-r--r-- | tests/unittests/test_subp.py | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/tests/unittests/test_subp.py b/tests/unittests/test_subp.py index ec513d01..572510d7 100644 --- a/tests/unittests/test_subp.py +++ b/tests/unittests/test_subp.py @@ -10,7 +10,7 @@ import stat from unittest import mock from cloudinit import subp, util -from tests.unittests.helpers import CiTestCase +from tests.unittests.helpers import CiTestCase, get_top_level_dir BASH = subp.which('bash') @@ -232,13 +232,17 @@ class TestSubp(CiTestCase): the default encoding will be set to ascii. In such an environment Popen(['command', 'non-ascii-arg']) would cause a UnicodeDecodeError. """ - python_prog = '\n'.join([ - 'import json, sys', - 'from cloudinit.subp import subp', - 'data = sys.stdin.read()', - 'cmd = json.loads(data)', - 'subp(cmd, capture=False)', - '']) + python_prog = '\n'.join( + [ + 'import json, sys', + 'sys.path.insert(0, "{}")'.format(get_top_level_dir()), + 'from cloudinit.subp import subp', + 'data = sys.stdin.read()', + 'cmd = json.loads(data)', + 'subp(cmd, capture=False)', + '', + ] + ) cmd = [BASH, '-c', 'echo -n "$@"', '--', self.utf8_valid.decode("utf-8")] python_subp = [sys.executable, '-c', python_prog] |