Warn if multiple providers found

If many providers produce a given name as an output
it is nice to be able to warn the user when this is
happening as early as possible.

Change-Id: Ia5f963120827c0228d318a799c8bc3c21b9687ac
This commit is contained in:
Joshua Harlow
2013-09-05 12:56:17 -07:00
committed by Ivan A. Melnikov
parent 264994bbf2
commit dd2c059149
2 changed files with 28 additions and 7 deletions

View File

@@ -26,10 +26,7 @@ from taskflow import states
from taskflow.utils import misc
from taskflow.utils import threading_utils
LOG = logging.getLogger(__name__)
STATES_WITH_RESULTS = (states.SUCCESS, states.REVERTING, states.FAILURE)
@@ -214,9 +211,9 @@ class Storage(object):
injector_uuid = uuidutils.generate_uuid()
self.add_task(injector_uuid, self.injector_name)
self.save(injector_uuid, pairs)
for name in pairs.iterkeys():
entries = self._reverse_mapping.setdefault(name, [])
entries.append((injector_uuid, name))
self.set_result_mapping(injector_uuid,
dict((name, name)
for name in pairs.iterkeys()))
def set_result_mapping(self, uuid, mapping):
"""Set mapping for naming task results
@@ -232,6 +229,9 @@ class Storage(object):
for name, index in mapping.iteritems():
entries = self._reverse_mapping.setdefault(name, [])
entries.append((uuid, index))
if len(entries) > 1:
LOG.warning("Multiple provider mappings being created for %r",
name)
def fetch(self, name):
"""Fetch named task result"""
@@ -240,7 +240,7 @@ class Storage(object):
except KeyError:
raise exceptions.NotFound("Name %r is not mapped" % name)
# Return the first one that is found.
for uuid, index in indexes:
for uuid, index in reversed(indexes):
try:
result = self.get(uuid)
return _item_from_result(result, index, name)

View File

@@ -223,3 +223,24 @@ class StorageTest(test.TestCase):
with self.assertRaisesRegexp(exceptions.NotFound,
'^Unable to find result'):
s.fetch('b')
@mock.patch.object(storage.LOG, 'warning')
def test_multiple_providers_are_checked(self, mocked_warning):
s = self._get_storage()
s.add_task('42', 'my task')
s.set_result_mapping('42', {'result': 'key'})
self.assertEquals(mocked_warning.mock_calls, [])
s.add_task('43', 'my other task')
s.set_result_mapping('43', {'result': 'key'})
mocked_warning.assert_called_once_with(
mock.ANY, 'result')
@mock.patch.object(storage.LOG, 'warning')
def test_multiple_providers_with_inject_are_checked(self, mocked_warning):
s = self._get_storage()
s.inject({'result': 'DONE'})
self.assertEquals(mocked_warning.mock_calls, [])
s.add_task('43', 'my other task')
s.set_result_mapping('43', {'result': 'key'})
mocked_warning.assert_called_once_with(
mock.ANY, 'result')