← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~smoser/cloud-init:bug/1742479-no-manual_cache_clean-warning into cloud-init:master

 

Scott Moser has proposed merging ~smoser/cloud-init:bug/1742479-no-manual_cache_clean-warning into cloud-init:master.

Requested reviews:
  cloud-init commiters (cloud-init-dev)
Related bugs:
  Bug #1742479 in cloud-init (Ubuntu): "setting manual_cache_clean causes warning"
  https://bugs.launchpad.net/ubuntu/+source/cloud-init/+bug/1742479

For more details, see:
https://code.launchpad.net/~smoser/cloud-init/+git/cloud-init/+merge/335956
-- 
Your team cloud-init commiters is requested to review the proposed merge of ~smoser/cloud-init:bug/1742479-no-manual_cache_clean-warning into cloud-init:master.
diff --git a/cloudinit/cmd/main.py b/cloudinit/cmd/main.py
index 30b37fe..e017ca8 100644
--- a/cloudinit/cmd/main.py
+++ b/cloudinit/cmd/main.py
@@ -417,11 +417,17 @@ def main_init(name, args):
 
 
 def di_report_warn(datasource, cfg):
-    if 'di_report' not in cfg:
+    if 'di_report' not in cfg or cfg.get('di_report') is None:
         LOG.debug("no di_report found in config.")
         return
 
     dicfg = cfg.get('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 8a9f1ab..e42498d 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 787ca20..d63b760 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):

Follow ups