summaryrefslogtreecommitdiff
path: root/tests/unittests/helpers.py
diff options
context:
space:
mode:
authorBrett Holman <bholman.devel@gmail.com>2021-12-08 14:27:37 -0700
committerGitHub <noreply@github.com>2021-12-08 15:27:37 -0600
commit65c2cfd7f21758746444c8c79444994a4638d563 (patch)
tree3b4c91b0ba35dbd8f043f779a9ea6b41f4665a16 /tests/unittests/helpers.py
parentb21afb0a8ab64543715dffff490253db8ecefb9f (diff)
downloadvyos-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/helpers.py')
-rw-r--r--tests/unittests/helpers.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py
index ccd56793..e9afbd36 100644
--- a/tests/unittests/helpers.py
+++ b/tests/unittests/helpers.py
@@ -12,10 +12,12 @@ import sys
import tempfile
import time
import unittest
+from pathlib import Path
from contextlib import ExitStack, contextmanager
from unittest import mock
from unittest.util import strclass
+import cloudinit
from cloudinit.config.schema import (
SchemaValidationError, validate_cloudconfig_schema)
from cloudinit import cloud
@@ -462,7 +464,7 @@ def wrap_and_call(prefix, mocks, func, *args, **kwargs):
def resourceLocation(subname=None):
- path = os.path.join('tests', 'data')
+ path = cloud_init_project_dir('tests/data')
if not subname:
return path
return os.path.join(path, subname)
@@ -504,4 +506,22 @@ if not hasattr(mock.Mock, 'assert_not_called'):
raise AssertionError(msg)
mock.Mock.assert_not_called = __mock_assert_not_called
+
+def get_top_level_dir() -> Path:
+ """Return the absolute path to the top cloudinit project directory
+
+ @return Path('<top-cloudinit-dir>')
+ """
+ return Path(cloudinit.__file__).parent.parent.resolve()
+
+
+def cloud_init_project_dir(sub_path: str) -> str:
+ """Get a path within the cloudinit project directory
+
+ @return str of the combined path
+
+ Example: cloud_init_project_dir("my/path") -> "/path/to/cloud-init/my/path"
+ """
+ return str(get_top_level_dir() / sub_path)
+
# vi: ts=4 expandtab