summaryrefslogtreecommitdiff
path: root/tests/unittests/test_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/test_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/test_helpers.py')
-rw-r--r--tests/unittests/test_helpers.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/unittests/test_helpers.py b/tests/unittests/test_helpers.py
index c6f9b94a..f491f8cd 100644
--- a/tests/unittests/test_helpers.py
+++ b/tests/unittests/test_helpers.py
@@ -3,6 +3,7 @@
"""Tests of the built-in user data handlers."""
import os
+from pathlib import Path
from tests.unittests import helpers as test_helpers
@@ -34,4 +35,36 @@ class TestPaths(test_helpers.ResourceUsingTestCase):
self.assertIsNone(mypaths.get_ipath())
+
+class Testcloud_init_project_dir:
+ top_dir = test_helpers.get_top_level_dir()
+
+ @staticmethod
+ def _get_top_level_dir_alt_implementation():
+ """Alternative implementation for comparing against.
+
+ Note: Recursively searching for .git/ fails during build tests due to
+ .git not existing. This implementation assumes that ../../../ is the
+ relative path to the cloud-init project directory form this file.
+ """
+ out = Path(__file__).parent.parent.parent.resolve()
+ return out
+
+ def test_top_level_dir(self):
+ """Assert the location of the top project directory is correct"""
+ assert (self.top_dir ==
+ self._get_top_level_dir_alt_implementation())
+
+ def test_cloud_init_project_dir(self):
+ """Assert cloud_init_project_dir produces an expected location
+
+ Compare the returned value to an alternate (naive) implementation
+ """
+ assert (
+ str(Path(self.top_dir, "test"))
+ == test_helpers.cloud_init_project_dir("test")
+ == str(Path(self._get_top_level_dir_alt_implementation(), "test"))
+ )
+
+
# vi: ts=4 expandtab