From 6c4f554f5a9a23b9caa1bebf616852cec171ecd8 Mon Sep 17 00:00:00 2001 From: Adrian Likins Date: Tue, 13 Dec 2016 14:34:58 -0500 Subject: [PATCH] Provide slightly better msg on fact cache error (#18759) If the configured fact_cache plugin (fact_caching config) fails, raise a fatal error instead of failing mysteriously later. Fixes #18751 --- lib/ansible/plugins/cache/__init__.py | 9 +++++++++ test/units/plugins/cache/test_cache.py | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/ansible/plugins/cache/__init__.py b/lib/ansible/plugins/cache/__init__.py index 9cda7ca044..b1aecde99a 100644 --- a/lib/ansible/plugins/cache/__init__.py +++ b/lib/ansible/plugins/cache/__init__.py @@ -20,6 +20,7 @@ __metaclass__ = type from collections import MutableMapping from ansible import constants as C +from ansible.errors import AnsibleError from ansible.plugins import cache_loader try: @@ -33,6 +34,14 @@ class FactCache(MutableMapping): def __init__(self, *args, **kwargs): self._plugin = cache_loader.get(C.CACHE_PLUGIN) + if not self._plugin: + raise AnsibleError('Unable to load the facts cache plugin (%s)\n' + 'Check fact cache config options :\n' + 'Current values:\n' + ' fact_caching: %s\n' + ' fact_caching_connection: %s' % + (C.CACHE_PLUGIN, C.CACHE_PLUGIN, C.CACHE_PLUGIN_CONNECTION)) + # Backwards compat: self._display isn't really needed, just import the global display and use that. self._display = display diff --git a/test/units/plugins/cache/test_cache.py b/test/units/plugins/cache/test_cache.py index cd82e1ef2c..c156414bd2 100644 --- a/test/units/plugins/cache/test_cache.py +++ b/test/units/plugins/cache/test_cache.py @@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type from ansible.compat.tests import unittest, mock +from ansible.errors import AnsibleError from ansible.plugins.cache import FactCache from ansible.plugins.cache.base import BaseCacheModule from ansible.plugins.cache.memory import CacheModule as MemoryCache @@ -56,6 +57,14 @@ class TestFactCache(unittest.TestCase): self.assertEqual(type(a_copy), dict) self.assertEqual(a_copy, dict(avocado='fruit', daisy='flower')) + def test_plugin_load_failure(self): + # See https://github.com/ansible/ansible/issues/18751 + # Note no fact_connection config set, so this will fail + with mock.patch('ansible.constants.CACHE_PLUGIN', 'json'): + self.assertRaisesRegexp(AnsibleError, + "Unable to load the facts cache plugin.*json.*", + FactCache) + class TestAbstractClass(unittest.TestCase):