summaryrefslogtreecommitdiff
path: root/tests/unittests/test_datasource/test_nocloud.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2013-02-07 12:08:30 -0500
committerScott Moser <smoser@ubuntu.com>2013-02-07 12:08:30 -0500
commita0f882fdd306ef7b3b90c53d5622eb5a6878cb81 (patch)
treeb317d68d593f74e7f4211513e7deba6f0ca61e1f /tests/unittests/test_datasource/test_nocloud.py
parent88a369c5324e74a2d1bb8dd0bdf8fdc9a95393c8 (diff)
downloadvyos-cloud-init-a0f882fdd306ef7b3b90c53d5622eb5a6878cb81.tar.gz
vyos-cloud-init-a0f882fdd306ef7b3b90c53d5622eb5a6878cb81.zip
more test cases for nocloud including one for config seed
Diffstat (limited to 'tests/unittests/test_datasource/test_nocloud.py')
-rw-r--r--tests/unittests/test_datasource/test_nocloud.py113
1 files changed, 107 insertions, 6 deletions
diff --git a/tests/unittests/test_datasource/test_nocloud.py b/tests/unittests/test_datasource/test_nocloud.py
index 850d3214..28e0a472 100644
--- a/tests/unittests/test_datasource/test_nocloud.py
+++ b/tests/unittests/test_datasource/test_nocloud.py
@@ -1,16 +1,106 @@
+from cloudinit import helpers
+from tests.unittests.helpers import populate_dir
from cloudinit.sources import DataSourceNoCloud
+from cloudinit import util
from mocker import MockerTestCase
+import os
+import yaml
class TestNoCloudDataSource(MockerTestCase):
def setUp(self):
+ self.tmp = self.makeDir()
+ self.paths = helpers.Paths({'cloud_dir': self.tmp})
+
+ self.cmdline = "root=TESTCMDLINE"
+
+ self.unapply = []
+ self.apply_patches([(util, 'get_cmdline', self._getcmdline)])
super(TestNoCloudDataSource, self).setUp()
- def test_parse_cmdline_data_valid(self):
- parse = DataSourceNoCloud.parse_cmdline_data
+ def tearDown(self):
+ apply_patches([i for i in reversed(self.unapply)])
+ super(TestNoCloudDataSource, self).setUp()
+
+ def apply_patches(self, patches):
+ ret = apply_patches(patches)
+ self.unapply += ret
+
+ def _getcmdline(self):
+ return self.cmdline
+
+ def test_nocloud_seed_dir(self):
+ md = {'instance-id': 'IID', 'dsmode': 'local'}
+ ud = "USER_DATA_HERE"
+ populate_dir(os.path.join(self.paths.seed_dir, "nocloud"),
+ {'user-data': ud, 'meta-data': yaml.safe_dump(md)})
+
+ sys_cfg = {
+ 'datasource': {'NoCloud': {'fs_label': None}}
+ }
+
+ ds = DataSourceNoCloud.DataSourceNoCloud
+
+ dsrc = ds(sys_cfg=sys_cfg, distro=None, paths=self.paths)
+ ret = dsrc.get_data()
+ self.assertEqual(dsrc.userdata_raw, ud)
+ self.assertEqual(dsrc.metadata, md)
+ self.assertTrue(ret)
+
+ def test_fs_label(self):
+ #find_devs_with should not be called ff fs_label is None
+ ds = DataSourceNoCloud.DataSourceNoCloud
+
+ class PsuedoException(Exception):
+ pass
+
+ def my_find_devs_with(*args, **kwargs):
+ _f = (args, kwargs)
+ raise PsuedoException
+
+ self.apply_patches([(util, 'find_devs_with', my_find_devs_with)])
+ # by default, NoCloud should search for filesystems by label
+ sys_cfg = {'datasource': {'NoCloud': {}}}
+ dsrc = ds(sys_cfg=sys_cfg, distro=None, paths=self.paths)
+ self.assertRaises(PsuedoException, dsrc.get_data)
+
+ # but disabling searching should just end up with None found
+ sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}}
+ dsrc = ds(sys_cfg=sys_cfg, distro=None, paths=self.paths)
+ ret = dsrc.get_data()
+ self.assertFalse(ret)
+
+ def test_no_datasource_expected(self):
+ #no source should be found if no cmdline, config, and fs_label=None
+ sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}}
+
+ ds = DataSourceNoCloud.DataSourceNoCloud
+ dsrc = ds(sys_cfg=sys_cfg, distro=None, paths=self.paths)
+ self.assertFalse(dsrc.get_data())
+
+ def test_seed_in_config(self):
+ ds = DataSourceNoCloud.DataSourceNoCloud
+
+ data = {
+ 'fs_label': None,
+ 'meta-data': {'instance-id': 'IID'},
+ 'user-data': "USER_DATA_RAW",
+ }
+
+ sys_cfg = {'datasource': {'NoCloud': data}}
+ dsrc = ds(sys_cfg=sys_cfg, distro=None, paths=self.paths)
+ ret = dsrc.get_data()
+ self.assertEqual(dsrc.userdata_raw, "USER_DATA_RAW")
+ self.assertEqual(dsrc.metadata.get('instance-id'), 'IID')
+ self.assertTrue(ret)
+
+
+class TestParseCommandLineData(MockerTestCase):
+
+ def test_parse_cmdline_data_valid(self):
ds_id = "ds=nocloud"
pairs = (
("root=/dev/sda1 %(ds_id)s", {}),
@@ -29,13 +119,12 @@ class TestNoCloudDataSource(MockerTestCase):
for (fmt, expected) in pairs:
fill = {}
cmdline = fmt % {'ds_id': ds_id}
- ret = parse(ds_id=ds_id, fill=fill, cmdline=cmdline)
+ ret = DataSourceNoCloud.parse_cmdline_data(ds_id=ds_id, fill=fill,
+ cmdline=cmdline)
self.assertEqual(expected, fill)
self.assertTrue(ret)
def test_parse_cmdline_data_none(self):
- parse = DataSourceNoCloud.parse_cmdline_data
-
ds_id = "ds=foo"
cmdlines = (
"root=/dev/sda1 ro",
@@ -48,9 +137,21 @@ class TestNoCloudDataSource(MockerTestCase):
for cmdline in cmdlines:
fill = {}
- ret = parse(ds_id=ds_id, fill=fill, cmdline=cmdline)
+ ret = DataSourceNoCloud.parse_cmdline_data(ds_id=ds_id, fill=fill,
+ cmdline=cmdline)
self.assertEqual(fill, {})
self.assertFalse(ret)
+def apply_patches(patches):
+ ret = []
+ for (ref, name, replace) in patches:
+ if replace is None:
+ continue
+ orig = getattr(ref, name)
+ setattr(ref, name, replace)
+ ret.append((ref, name, orig))
+ return ret
+
+
# vi: ts=4 expandtab