summaryrefslogtreecommitdiff
path: root/tests/unittests
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2016-04-29 11:23:08 -0400
committerScott Moser <smoser@ubuntu.com>2016-04-29 11:23:08 -0400
commit0780b3abfb14e8994f6097c5d1d22a92e91bc2f5 (patch)
tree08a534e49053403bc77351c5b2096f94e314e538 /tests/unittests
parentc92f02037afc6b0434c9498904f7d888e00cd55b (diff)
parent89fe4242aa3d825ee1b439cc8d184ddf06382086 (diff)
downloadvyos-cloud-init-0780b3abfb14e8994f6097c5d1d22a92e91bc2f5.tar.gz
vyos-cloud-init-0780b3abfb14e8994f6097c5d1d22a92e91bc2f5.zip
Paths: fix instance path if datasource's id has a '/'.
If the datasource's instance id contained a '/' then the instance_id path would not be as expected under /var/lib/cloud/instances/instance_id. LP: #1575938
Diffstat (limited to 'tests/unittests')
-rw-r--r--tests/unittests/helpers.py9
-rw-r--r--tests/unittests/test_helpers.py33
2 files changed, 37 insertions, 5 deletions
diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py
index 7f4b8784..fb9c83a7 100644
--- a/tests/unittests/helpers.py
+++ b/tests/unittests/helpers.py
@@ -180,13 +180,12 @@ class ResourceUsingTestCase(TestCase):
with open(where, 'r') as fh:
return fh.read()
- def getCloudPaths(self):
+ def getCloudPaths(self, ds=None):
tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmpdir)
- cp = ch.Paths({
- 'cloud_dir': tmpdir,
- 'templates_dir': self.resourceLocation(),
- })
+ cp = ch.Paths({'cloud_dir': tmpdir,
+ 'templates_dir': self.resourceLocation()},
+ ds=ds)
return cp
diff --git a/tests/unittests/test_helpers.py b/tests/unittests/test_helpers.py
new file mode 100644
index 00000000..943a5723
--- /dev/null
+++ b/tests/unittests/test_helpers.py
@@ -0,0 +1,33 @@
+"""Tests of the built-in user data handlers."""
+
+import os
+
+from . import helpers as test_helpers
+
+from cloudinit import sources
+
+
+class MyDataSource(sources.DataSource):
+ _instance_id = None
+
+ def get_instance_id(self):
+ return self._instance_id
+
+
+class TestPaths(test_helpers.ResourceUsingTestCase):
+ def test_get_ipath_and_instance_id_with_slashes(self):
+ myds = MyDataSource(sys_cfg={}, distro=None, paths={})
+ myds._instance_id = "/foo/bar"
+ safe_iid = "_foo_bar"
+ mypaths = self.getCloudPaths(myds)
+
+ self.assertEqual(
+ os.path.join(mypaths.cloud_dir, 'instances', safe_iid),
+ mypaths.get_ipath())
+
+ def test_get_ipath_and_empty_instance_id_returns_none(self):
+ myds = MyDataSource(sys_cfg={}, distro=None, paths={})
+ myds._instance_id = None
+ mypaths = self.getCloudPaths(myds)
+
+ self.assertEqual(None, mypaths.get_ipath())