diff --git a/cloudbaseinit/plugins/common/userdataplugins/cloudconfig.py b/cloudbaseinit/plugins/common/userdataplugins/cloudconfig.py index 9300e46f..1c62c1ea 100644 --- a/cloudbaseinit/plugins/common/userdataplugins/cloudconfig.py +++ b/cloudbaseinit/plugins/common/userdataplugins/cloudconfig.py @@ -61,10 +61,9 @@ class CloudConfigPluginExecutor(object): try: content = yaml.load(stream, Loader=loader) except (TypeError, ValueError, AttributeError): - msg = "Invalid yaml stream provided." - LOG.error(msg) - raise CloudConfigError(msg) - + raise CloudConfigError("Invalid yaml stream provided.") + if not content: + raise CloudConfigError("Empty yaml stream provided.") return cls(**content) def execute(self): @@ -99,8 +98,9 @@ class CloudConfigPlugin(base.BaseUserDataPlugin): """ try: executor = CloudConfigPluginExecutor.from_yaml(part) - except CloudConfigError: - LOG.error("Could not process the type %r", type(part)) + except CloudConfigError as ex: + LOG.error('Could not process part type %(type)r: %(err)r', + {'type': type(part), 'err': str(ex)}) else: return executor.execute() diff --git a/cloudbaseinit/tests/plugins/common/userdataplugins/test_cloudconfig.py b/cloudbaseinit/tests/plugins/common/userdataplugins/test_cloudconfig.py index 4c411d77..1797c81d 100644 --- a/cloudbaseinit/tests/plugins/common/userdataplugins/test_cloudconfig.py +++ b/cloudbaseinit/tests/plugins/common/userdataplugins/test_cloudconfig.py @@ -52,25 +52,26 @@ class CloudConfigPluginTests(unittest.TestCase): CONF.cloud_config_plugins = orig def test_executor_from_yaml(self): - expected_logging = ["Invalid yaml stream provided."] - for invalid in (mock.sentinel.yaml, None, 1, int): - with testutils.LogSnatcher('cloudbaseinit.plugins.' - 'common.userdataplugins.' - 'cloudconfig') as snatcher: - with self.assertRaises(cloudconfig.CloudConfigError) as cm: - cloudconfig.CloudConfigPluginExecutor.from_yaml(invalid) - self.assertEqual(expected_logging, snatcher.output) - self.assertEqual("Invalid yaml stream provided.", - str(cm.exception)) + for invalid in (mock.sentinel.yaml, None, 1, int, '{}'): + with self.assertRaises(cloudconfig.CloudConfigError): + cloudconfig.CloudConfigPluginExecutor.from_yaml(invalid) - executor = cloudconfig.CloudConfigPluginExecutor.from_yaml('{}') + executor = cloudconfig.CloudConfigPluginExecutor.from_yaml('{f: 1}') self.assertIsInstance(executor, cloudconfig.CloudConfigPluginExecutor) - def test_invalid_type(self): + def _test_invalid_type(self, part, err_msg): with testutils.LogSnatcher('cloudbaseinit.plugins.common.' 'userdataplugins.cloudconfig') as snatcher: - self.plugin.process_non_multipart({'unsupported'}) + self.plugin.process_non_multipart(part) - expected = ["Invalid yaml stream provided.", - "Could not process the type %r" % set] - self.assertEqual(expected, snatcher.output) + expected = ("Could not process part type %(type)r: %(err)r" + % {'type': type(part), 'err': err_msg}) + self.assertEqual([expected], snatcher.output) + + def test_invalid_type(self): + self._test_invalid_type({'unsupported'}, + "Invalid yaml stream provided.") + + def test_invalid_type_empty(self): + self._test_invalid_type('#comment', + 'Empty yaml stream provided.')