better api and tests added
* _copy_results = deepcopy for better performance * _copy_results_exclude to deepcopy but exclude certain fields. Pop fields that do not need to be deep copied. Re-assign popped fields after deep copy so we don't modify the original, to be copied, object. * _copy_results_exclude unit tests
This commit is contained in:
parent
93490960ff
commit
66a8f7e873
2 changed files with 97 additions and 14 deletions
|
@ -22,7 +22,7 @@ __metaclass__ = type
|
||||||
import json
|
import json
|
||||||
import difflib
|
import difflib
|
||||||
import warnings
|
import warnings
|
||||||
from copy import copy, deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
from ansible.compat.six import string_types
|
from ansible.compat.six import string_types
|
||||||
|
|
||||||
|
@ -59,9 +59,20 @@ class CallbackBase:
|
||||||
version = getattr(self, 'CALLBACK_VERSION', '1.0')
|
version = getattr(self, 'CALLBACK_VERSION', '1.0')
|
||||||
self._display.vvvv('Loaded callback %s of type %s, v%s' % (name, ctype, version))
|
self._display.vvvv('Loaded callback %s of type %s, v%s' % (name, ctype, version))
|
||||||
|
|
||||||
def _copy_result(self, result):
|
''' helper for callbacks, so they don't all have to include deepcopy '''
|
||||||
''' helper for callbacks, so they don't all have to include deepcopy '''
|
_copy_result = deepcopy
|
||||||
return deepcopy(result)
|
|
||||||
|
def _copy_result_exclude(self, result, exclude):
|
||||||
|
values = []
|
||||||
|
for e in exclude:
|
||||||
|
values.append(getattr(result, e))
|
||||||
|
setattr(result, e, None)
|
||||||
|
|
||||||
|
result_copy = deepcopy(result)
|
||||||
|
for i,e in enumerate(exclude):
|
||||||
|
setattr(result, e, values[i])
|
||||||
|
|
||||||
|
return result_copy
|
||||||
|
|
||||||
def _dump_results(self, result, indent=None, sort_keys=True, keep_invocation=False):
|
def _dump_results(self, result, indent=None, sort_keys=True, keep_invocation=False):
|
||||||
if result.get('_ansible_no_log', False):
|
if result.get('_ansible_no_log', False):
|
||||||
|
@ -128,18 +139,9 @@ class CallbackBase:
|
||||||
|
|
||||||
return item
|
return item
|
||||||
|
|
||||||
def deepcopy_exclude(self, copyme, exclude=[]):
|
|
||||||
res = copy(copyme)
|
|
||||||
try:
|
|
||||||
setattr(res, exclude, None)
|
|
||||||
except TypeError:
|
|
||||||
for e in exclude:
|
|
||||||
setattr(res, e, None)
|
|
||||||
return deepcopy(res)
|
|
||||||
|
|
||||||
def _process_items(self, result):
|
def _process_items(self, result):
|
||||||
for res in result._result['results']:
|
for res in result._result['results']:
|
||||||
newres = self.deepcopy_exclude(result, '_result')
|
newres = self._copy_result_exclude(result, ['_result'])
|
||||||
res['item'] = self._get_item(res)
|
res['item'] = self._get_item(res)
|
||||||
newres._result = res
|
newres._result = res
|
||||||
if 'failed' in res and res['failed']:
|
if 'failed' in res and res['failed']:
|
||||||
|
|
81
test/units/plugins/callback/test_callback.py
Normal file
81
test/units/plugins/callback/test_callback.py
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
# (c) 2012-2014, Chris Meyers <chris.meyers.fsu@gmail.com>
|
||||||
|
#
|
||||||
|
# This file is part of Ansible
|
||||||
|
#
|
||||||
|
# Ansible is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# Ansible is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# Make coding more python3-ish
|
||||||
|
from __future__ import (absolute_import, division, print_function)
|
||||||
|
__metaclass__ = type
|
||||||
|
|
||||||
|
from six import PY3
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
|
from ansible.compat.tests import unittest
|
||||||
|
from ansible.compat.tests.mock import patch, mock_open
|
||||||
|
|
||||||
|
from ansible.plugins.callback import CallbackBase
|
||||||
|
import ansible.plugins.callback as callish
|
||||||
|
|
||||||
|
class TestCopyResultExclude(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
class DummyClass():
|
||||||
|
def __init__(self):
|
||||||
|
self.bar = [ 1, 2, 3 ]
|
||||||
|
self.a = {
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
self.b = {
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
}
|
||||||
|
self.foo = DummyClass()
|
||||||
|
self.cb = CallbackBase()
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_copy_logic(self):
|
||||||
|
res = self.cb._copy_result_exclude(self.foo, ())
|
||||||
|
self.assertEqual(self.foo.bar, res.bar)
|
||||||
|
|
||||||
|
def test_copy_deep(self):
|
||||||
|
res = self.cb._copy_result_exclude(self.foo, ())
|
||||||
|
self.assertNotEqual(id(self.foo.bar), id(res.bar))
|
||||||
|
|
||||||
|
def test_no_exclude(self):
|
||||||
|
res = self.cb._copy_result_exclude(self.foo, ())
|
||||||
|
self.assertEqual(self.foo.bar, res.bar)
|
||||||
|
self.assertEqual(self.foo.a, res.a)
|
||||||
|
self.assertEqual(self.foo.b, res.b)
|
||||||
|
|
||||||
|
def test_exclude(self):
|
||||||
|
res = self.cb._copy_result_exclude(self.foo, ['bar', 'b'])
|
||||||
|
self.assertIsNone(res.bar)
|
||||||
|
self.assertIsNone(res.b)
|
||||||
|
self.assertEqual(self.foo.a, res.a)
|
||||||
|
|
||||||
|
def test_result_unmodified(self):
|
||||||
|
bar_id = id(self.foo.bar)
|
||||||
|
a_id = id(self.foo.a)
|
||||||
|
res = self.cb._copy_result_exclude(self.foo, ['bar', 'a'])
|
||||||
|
|
||||||
|
self.assertEqual(self.foo.bar, [ 1, 2, 3 ])
|
||||||
|
self.assertEqual(bar_id, id(self.foo.bar))
|
||||||
|
|
||||||
|
self.assertEqual(self.foo.a, dict(b=2, c=3))
|
||||||
|
self.assertEqual(a_id, id(self.foo.a))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue