summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2018-01-12 14:23:26 -0700
committerChad Smith <blackboxsw@gmail.com>2018-01-12 14:23:26 -0700
commit6299e8d0cc230b0c9b41a69a5963bcd2c252c337 (patch)
tree4da09bab37ece6f2752d710f384746e8bb186ac8
parent78372f16d2711812793196aa8003ad51693ca472 (diff)
downloadvyos-cloud-init-6299e8d0cc230b0c9b41a69a5963bcd2c252c337.tar.gz
vyos-cloud-init-6299e8d0cc230b0c9b41a69a5963bcd2c252c337.zip
Do not log warning on config files that represent None.
This issue was first identified when manual_cache_clean was set, as ds-identify would write /run/cloud-init/cloud.cfg with # manual_cache_clean that would generate a warning as cloud-init expected to load a dict. Any other "empty" config would also log such a warning. Also fix reading of di_report to allow it to be None, as ds-identify would write: di_report: # manual_cache_clean which reads as 'di_report: None' rather than di_report: {}. LP: #1742479
-rw-r--r--cloudinit/cmd/main.py8
-rw-r--r--cloudinit/util.py10
-rw-r--r--tests/unittests/test_util.py8
3 files changed, 20 insertions, 6 deletions
diff --git a/cloudinit/cmd/main.py b/cloudinit/cmd/main.py
index 30b37fe1..d2f1b778 100644
--- a/cloudinit/cmd/main.py
+++ b/cloudinit/cmd/main.py
@@ -421,7 +421,13 @@ def di_report_warn(datasource, cfg):
LOG.debug("no di_report found in config.")
return
- dicfg = cfg.get('di_report', {})
+ dicfg = cfg['di_report']
+ if dicfg is None:
+ # ds-identify may write 'di_report:\n #comment\n'
+ # which reads as {'di_report': None}
+ LOG.debug("di_report was None.")
+ return
+
if not isinstance(dicfg, dict):
LOG.warning("di_report config not a dictionary: %s", dicfg)
return
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 8a9f1ab2..e42498d9 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -891,17 +891,17 @@ def load_yaml(blob, default=None, allowed=(dict,)):
"of length %s with allowed root types %s",
len(blob), allowed)
converted = safeyaml.load(blob)
- if not isinstance(converted, allowed):
+ if converted is None:
+ LOG.debug("loaded blob returned None, returning default.")
+ converted = default
+ elif not isinstance(converted, allowed):
# Yes this will just be caught, but thats ok for now...
raise TypeError(("Yaml load allows %s root types,"
" but got %s instead") %
(allowed, type_utils.obj_name(converted)))
loaded = converted
except (yaml.YAMLError, TypeError, ValueError):
- if len(blob) == 0:
- LOG.debug("load_yaml given empty string, returning default")
- else:
- logexc(LOG, "Failed loading yaml blob")
+ logexc(LOG, "Failed loading yaml blob")
return loaded
diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py
index 787ca208..d63b760e 100644
--- a/tests/unittests/test_util.py
+++ b/tests/unittests/test_util.py
@@ -299,6 +299,14 @@ class TestLoadYaml(helpers.TestCase):
default=self.mydefault),
myobj)
+ def test_none_returns_default(self):
+ """If yaml.load returns None, then default should be returned."""
+ blobs = ("", " ", "# foo\n", "#")
+ mdef = self.mydefault
+ self.assertEqual(
+ [(b, self.mydefault) for b in blobs],
+ [(b, util.load_yaml(blob=b, default=mdef)) for b in blobs])
+
class TestMountinfoParsing(helpers.ResourceUsingTestCase):
def test_invalid_mountinfo(self):