2015-12-29 17:40:18 +01:00
|
|
|
# (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
|
|
|
|
|
2018-12-04 23:42:24 +01:00
|
|
|
import json
|
2016-02-11 09:09:24 +01:00
|
|
|
import re
|
|
|
|
import textwrap
|
2016-11-29 16:40:46 +01:00
|
|
|
import types
|
|
|
|
|
2018-10-13 05:01:14 +02:00
|
|
|
from units.compat import unittest
|
2019-08-02 17:18:14 +02:00
|
|
|
from units.compat.mock import MagicMock
|
2016-11-29 16:40:46 +01:00
|
|
|
|
2015-12-29 17:40:18 +01:00
|
|
|
from ansible.plugins.callback import CallbackBase
|
|
|
|
|
|
|
|
|
2016-10-13 23:47:29 +02:00
|
|
|
class TestCallback(unittest.TestCase):
|
|
|
|
# FIXME: This doesn't really test anything...
|
2016-11-29 16:40:46 +01:00
|
|
|
def test_init(self):
|
|
|
|
CallbackBase()
|
|
|
|
|
|
|
|
def test_display(self):
|
|
|
|
display_mock = MagicMock()
|
|
|
|
display_mock.verbosity = 0
|
|
|
|
cb = CallbackBase(display=display_mock)
|
|
|
|
self.assertIs(cb._display, display_mock)
|
|
|
|
|
|
|
|
def test_display_verbose(self):
|
|
|
|
display_mock = MagicMock()
|
|
|
|
display_mock.verbosity = 5
|
|
|
|
cb = CallbackBase(display=display_mock)
|
|
|
|
self.assertIs(cb._display, display_mock)
|
|
|
|
|
|
|
|
# TODO: import callback module so we can patch callback.cli/callback.C
|
|
|
|
|
|
|
|
|
|
|
|
class TestCallbackResults(unittest.TestCase):
|
2018-06-21 22:14:57 +02:00
|
|
|
|
|
|
|
def test_get_item_label(self):
|
|
|
|
cb = CallbackBase()
|
|
|
|
results = {'item': 'some_item'}
|
|
|
|
res = cb._get_item_label(results)
|
2019-11-06 14:54:25 +01:00
|
|
|
self.assertEqual(res, 'some_item')
|
2018-06-21 22:14:57 +02:00
|
|
|
|
|
|
|
def test_get_item_label_no_log(self):
|
|
|
|
cb = CallbackBase()
|
|
|
|
results = {'item': 'some_item', '_ansible_no_log': True}
|
|
|
|
res = cb._get_item_label(results)
|
2019-11-06 14:54:25 +01:00
|
|
|
self.assertEqual(res, "(censored due to no_log)")
|
2018-06-21 22:14:57 +02:00
|
|
|
|
|
|
|
results = {'item': 'some_item', '_ansible_no_log': False}
|
|
|
|
res = cb._get_item_label(results)
|
2019-11-06 14:54:25 +01:00
|
|
|
self.assertEqual(res, "some_item")
|
2018-06-21 22:14:57 +02:00
|
|
|
|
2017-12-11 23:41:12 +01:00
|
|
|
def test_clean_results_debug_task(self):
|
2016-11-29 16:40:46 +01:00
|
|
|
cb = CallbackBase()
|
|
|
|
result = {'item': 'some_item',
|
|
|
|
'invocation': 'foo --bar whatever [some_json]',
|
2017-12-11 23:41:12 +01:00
|
|
|
'a': 'a single a in result note letter a is in invocation',
|
|
|
|
'b': 'a single b in result note letter b is not in invocation',
|
2016-11-29 16:40:46 +01:00
|
|
|
'changed': True}
|
|
|
|
|
2017-12-11 23:41:12 +01:00
|
|
|
cb._clean_results(result, 'debug')
|
|
|
|
|
|
|
|
# See https://github.com/ansible/ansible/issues/33723
|
|
|
|
self.assertTrue('a' in result)
|
|
|
|
self.assertTrue('b' in result)
|
|
|
|
self.assertFalse('invocation' in result)
|
2018-02-16 15:59:54 +01:00
|
|
|
self.assertFalse('changed' in result)
|
2017-12-11 23:41:12 +01:00
|
|
|
|
|
|
|
def test_clean_results_debug_task_no_invocation(self):
|
|
|
|
cb = CallbackBase()
|
|
|
|
result = {'item': 'some_item',
|
|
|
|
'a': 'a single a in result note letter a is in invocation',
|
|
|
|
'b': 'a single b in result note letter b is not in invocation',
|
|
|
|
'changed': True}
|
|
|
|
|
2016-11-29 16:40:46 +01:00
|
|
|
cb._clean_results(result, 'debug')
|
2017-12-11 23:41:12 +01:00
|
|
|
self.assertTrue('a' in result)
|
|
|
|
self.assertTrue('b' in result)
|
2018-02-16 15:59:54 +01:00
|
|
|
self.assertFalse('changed' in result)
|
2017-12-11 23:41:12 +01:00
|
|
|
self.assertFalse('invocation' in result)
|
|
|
|
|
|
|
|
def test_clean_results_debug_task_empty_results(self):
|
|
|
|
cb = CallbackBase()
|
|
|
|
result = {}
|
|
|
|
cb._clean_results(result, 'debug')
|
|
|
|
self.assertFalse('invocation' in result)
|
|
|
|
self.assertEqual(len(result), 0)
|
|
|
|
|
|
|
|
def test_clean_results(self):
|
|
|
|
cb = CallbackBase()
|
|
|
|
result = {'item': 'some_item',
|
|
|
|
'invocation': 'foo --bar whatever [some_json]',
|
|
|
|
'a': 'a single a in result note letter a is in invocation',
|
|
|
|
'b': 'a single b in result note letter b is not in invocation',
|
|
|
|
'changed': True}
|
|
|
|
|
|
|
|
expected_result = result.copy()
|
|
|
|
cb._clean_results(result, 'ebug')
|
|
|
|
self.assertEqual(result, expected_result)
|
2016-11-29 16:40:46 +01:00
|
|
|
|
|
|
|
|
2018-12-04 23:42:24 +01:00
|
|
|
class TestCallbackDumpResults(object):
|
2016-11-29 16:40:46 +01:00
|
|
|
def test_internal_keys(self):
|
|
|
|
cb = CallbackBase()
|
|
|
|
result = {'item': 'some_item',
|
|
|
|
'_ansible_some_var': 'SENTINEL',
|
|
|
|
'testing_ansible_out': 'should_be_left_in LEFTIN',
|
|
|
|
'invocation': 'foo --bar whatever [some_json]',
|
|
|
|
'some_dict_key': {'a_sub_dict_for_key': 'baz'},
|
|
|
|
'bad_dict_key': {'_ansible_internal_blah': 'SENTINEL'},
|
|
|
|
'changed': True}
|
|
|
|
json_out = cb._dump_results(result)
|
2018-12-04 23:42:24 +01:00
|
|
|
assert '"_ansible_' not in json_out
|
|
|
|
assert 'SENTINEL' not in json_out
|
|
|
|
assert 'LEFTIN' in json_out
|
2016-11-29 16:40:46 +01:00
|
|
|
|
|
|
|
def test_exception(self):
|
|
|
|
cb = CallbackBase()
|
|
|
|
result = {'item': 'some_item LEFTIN',
|
|
|
|
'exception': ['frame1', 'SENTINEL']}
|
|
|
|
json_out = cb._dump_results(result)
|
2018-12-04 23:42:24 +01:00
|
|
|
assert 'SENTINEL' not in json_out
|
|
|
|
assert 'exception' not in json_out
|
|
|
|
assert 'LEFTIN' in json_out
|
2016-11-29 16:40:46 +01:00
|
|
|
|
|
|
|
def test_verbose(self):
|
|
|
|
cb = CallbackBase()
|
|
|
|
result = {'item': 'some_item LEFTIN',
|
|
|
|
'_ansible_verbose_always': 'chicane'}
|
|
|
|
json_out = cb._dump_results(result)
|
2018-12-04 23:42:24 +01:00
|
|
|
assert 'SENTINEL' not in json_out
|
|
|
|
assert 'LEFTIN' in json_out
|
2016-11-29 16:40:46 +01:00
|
|
|
|
|
|
|
def test_diff(self):
|
|
|
|
cb = CallbackBase()
|
|
|
|
result = {'item': 'some_item LEFTIN',
|
|
|
|
'diff': ['remove stuff', 'added LEFTIN'],
|
|
|
|
'_ansible_verbose_always': 'chicane'}
|
|
|
|
json_out = cb._dump_results(result)
|
2018-12-04 23:42:24 +01:00
|
|
|
assert 'SENTINEL' not in json_out
|
|
|
|
assert 'LEFTIN' in json_out
|
|
|
|
|
|
|
|
def test_mixed_keys(self):
|
|
|
|
cb = CallbackBase()
|
|
|
|
result = {3: 'pi',
|
|
|
|
'tau': 6}
|
|
|
|
json_out = cb._dump_results(result)
|
|
|
|
round_trip_result = json.loads(json_out)
|
|
|
|
assert len(round_trip_result) == 2
|
|
|
|
assert '3' in round_trip_result
|
|
|
|
assert 'tau' in round_trip_result
|
|
|
|
assert round_trip_result['3'] == 'pi'
|
|
|
|
assert round_trip_result['tau'] == 6
|
2016-11-29 16:40:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestCallbackDiff(unittest.TestCase):
|
2016-02-11 09:09:24 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.cb = CallbackBase()
|
|
|
|
|
|
|
|
def _strip_color(self, s):
|
|
|
|
return re.sub('\033\\[[^m]*m', '', s)
|
|
|
|
|
2016-11-29 16:40:46 +01:00
|
|
|
def test_difflist(self):
|
|
|
|
# TODO: split into smaller tests?
|
2018-09-20 19:31:48 +02:00
|
|
|
difflist = [{'before': u'preface\nThe Before String\npostscript',
|
|
|
|
'after': u'preface\nThe After String\npostscript',
|
|
|
|
'before_header': u'just before',
|
|
|
|
'after_header': u'just after'
|
2016-11-29 16:40:46 +01:00
|
|
|
},
|
2018-09-20 19:31:48 +02:00
|
|
|
{'before': u'preface\nThe Before String\npostscript',
|
|
|
|
'after': u'preface\nThe After String\npostscript',
|
2016-11-29 16:40:46 +01:00
|
|
|
},
|
|
|
|
{'src_binary': 'chicane'},
|
|
|
|
{'dst_binary': 'chicanery'},
|
|
|
|
{'dst_larger': 1},
|
|
|
|
{'src_larger': 2},
|
2018-09-20 19:31:48 +02:00
|
|
|
{'prepared': u'what does prepared do?'},
|
|
|
|
{'before_header': u'just before'},
|
|
|
|
{'after_header': u'just after'}]
|
2016-11-29 16:40:46 +01:00
|
|
|
|
2016-02-11 09:09:24 +01:00
|
|
|
res = self.cb._get_diff(difflist)
|
2016-11-29 16:40:46 +01:00
|
|
|
|
2018-09-20 19:31:48 +02:00
|
|
|
self.assertIn(u'Before String', res)
|
|
|
|
self.assertIn(u'After String', res)
|
|
|
|
self.assertIn(u'just before', res)
|
|
|
|
self.assertIn(u'just after', res)
|
2016-11-29 16:40:46 +01:00
|
|
|
|
2016-02-11 09:09:24 +01:00
|
|
|
def test_simple_diff(self):
|
|
|
|
self.assertMultiLineEqual(
|
|
|
|
self._strip_color(self.cb._get_diff({
|
|
|
|
'before_header': 'somefile.txt',
|
|
|
|
'after_header': 'generated from template somefile.j2',
|
|
|
|
'before': 'one\ntwo\nthree\n',
|
|
|
|
'after': 'one\nthree\nfour\n',
|
|
|
|
})),
|
|
|
|
textwrap.dedent('''\
|
|
|
|
--- before: somefile.txt
|
|
|
|
+++ after: generated from template somefile.j2
|
|
|
|
@@ -1,3 +1,3 @@
|
|
|
|
one
|
|
|
|
-two
|
|
|
|
three
|
|
|
|
+four
|
|
|
|
|
|
|
|
'''))
|
|
|
|
|
|
|
|
def test_new_file(self):
|
|
|
|
self.assertMultiLineEqual(
|
|
|
|
self._strip_color(self.cb._get_diff({
|
|
|
|
'before_header': 'somefile.txt',
|
|
|
|
'after_header': 'generated from template somefile.j2',
|
|
|
|
'before': '',
|
|
|
|
'after': 'one\ntwo\nthree\n',
|
|
|
|
})),
|
|
|
|
textwrap.dedent('''\
|
|
|
|
--- before: somefile.txt
|
|
|
|
+++ after: generated from template somefile.j2
|
|
|
|
@@ -0,0 +1,3 @@
|
|
|
|
+one
|
|
|
|
+two
|
|
|
|
+three
|
|
|
|
|
|
|
|
'''))
|
|
|
|
|
|
|
|
def test_clear_file(self):
|
|
|
|
self.assertMultiLineEqual(
|
|
|
|
self._strip_color(self.cb._get_diff({
|
|
|
|
'before_header': 'somefile.txt',
|
|
|
|
'after_header': 'generated from template somefile.j2',
|
|
|
|
'before': 'one\ntwo\nthree\n',
|
|
|
|
'after': '',
|
|
|
|
})),
|
|
|
|
textwrap.dedent('''\
|
|
|
|
--- before: somefile.txt
|
|
|
|
+++ after: generated from template somefile.j2
|
|
|
|
@@ -1,3 +0,0 @@
|
|
|
|
-one
|
|
|
|
-two
|
|
|
|
-three
|
|
|
|
|
|
|
|
'''))
|
|
|
|
|
|
|
|
def test_no_trailing_newline_before(self):
|
|
|
|
self.assertMultiLineEqual(
|
|
|
|
self._strip_color(self.cb._get_diff({
|
|
|
|
'before_header': 'somefile.txt',
|
|
|
|
'after_header': 'generated from template somefile.j2',
|
|
|
|
'before': 'one\ntwo\nthree',
|
|
|
|
'after': 'one\ntwo\nthree\n',
|
|
|
|
})),
|
|
|
|
textwrap.dedent('''\
|
|
|
|
--- before: somefile.txt
|
|
|
|
+++ after: generated from template somefile.j2
|
|
|
|
@@ -1,3 +1,3 @@
|
|
|
|
one
|
|
|
|
two
|
|
|
|
-three
|
|
|
|
\\ No newline at end of file
|
|
|
|
+three
|
|
|
|
|
|
|
|
'''))
|
|
|
|
|
|
|
|
def test_no_trailing_newline_after(self):
|
|
|
|
self.assertMultiLineEqual(
|
|
|
|
self._strip_color(self.cb._get_diff({
|
|
|
|
'before_header': 'somefile.txt',
|
|
|
|
'after_header': 'generated from template somefile.j2',
|
|
|
|
'before': 'one\ntwo\nthree\n',
|
|
|
|
'after': 'one\ntwo\nthree',
|
|
|
|
})),
|
|
|
|
textwrap.dedent('''\
|
|
|
|
--- before: somefile.txt
|
|
|
|
+++ after: generated from template somefile.j2
|
|
|
|
@@ -1,3 +1,3 @@
|
|
|
|
one
|
|
|
|
two
|
|
|
|
-three
|
|
|
|
+three
|
|
|
|
\\ No newline at end of file
|
|
|
|
|
|
|
|
'''))
|
|
|
|
|
|
|
|
def test_no_trailing_newline_both(self):
|
|
|
|
self.assertMultiLineEqual(
|
|
|
|
self.cb._get_diff({
|
|
|
|
'before_header': 'somefile.txt',
|
|
|
|
'after_header': 'generated from template somefile.j2',
|
|
|
|
'before': 'one\ntwo\nthree',
|
|
|
|
'after': 'one\ntwo\nthree',
|
|
|
|
}),
|
|
|
|
'')
|
|
|
|
|
|
|
|
def test_no_trailing_newline_both_with_some_changes(self):
|
|
|
|
self.assertMultiLineEqual(
|
|
|
|
self._strip_color(self.cb._get_diff({
|
|
|
|
'before_header': 'somefile.txt',
|
|
|
|
'after_header': 'generated from template somefile.j2',
|
|
|
|
'before': 'one\ntwo\nthree',
|
|
|
|
'after': 'one\nfive\nthree',
|
|
|
|
})),
|
|
|
|
textwrap.dedent('''\
|
|
|
|
--- before: somefile.txt
|
|
|
|
+++ after: generated from template somefile.j2
|
|
|
|
@@ -1,3 +1,3 @@
|
|
|
|
one
|
|
|
|
-two
|
|
|
|
+five
|
|
|
|
three
|
|
|
|
\\ No newline at end of file
|
|
|
|
|
|
|
|
'''))
|
|
|
|
|
|
|
|
def test_diff_dicts(self):
|
|
|
|
self.assertMultiLineEqual(
|
|
|
|
self._strip_color(self.cb._get_diff({
|
|
|
|
'before': dict(one=1, two=2, three=3),
|
|
|
|
'after': dict(one=1, three=3, four=4),
|
|
|
|
})),
|
|
|
|
textwrap.dedent('''\
|
|
|
|
--- before
|
|
|
|
+++ after
|
|
|
|
@@ -1,5 +1,5 @@
|
|
|
|
{
|
|
|
|
+ "four": 4,
|
|
|
|
"one": 1,
|
|
|
|
- "three": 3,
|
|
|
|
- "two": 2
|
|
|
|
+ "three": 3
|
|
|
|
}
|
|
|
|
|
|
|
|
'''))
|
|
|
|
|
2019-09-20 21:47:18 +02:00
|
|
|
def test_diff_before_none(self):
|
|
|
|
self.assertMultiLineEqual(
|
|
|
|
self._strip_color(self.cb._get_diff({
|
|
|
|
'before': None,
|
|
|
|
'after': 'one line\n',
|
|
|
|
})),
|
|
|
|
textwrap.dedent('''\
|
|
|
|
--- before
|
|
|
|
+++ after
|
|
|
|
@@ -0,0 +1 @@
|
|
|
|
+one line
|
|
|
|
|
|
|
|
'''))
|
|
|
|
|
|
|
|
def test_diff_after_none(self):
|
|
|
|
self.assertMultiLineEqual(
|
|
|
|
self._strip_color(self.cb._get_diff({
|
|
|
|
'before': 'one line\n',
|
|
|
|
'after': None,
|
|
|
|
})),
|
|
|
|
textwrap.dedent('''\
|
|
|
|
--- before
|
|
|
|
+++ after
|
|
|
|
@@ -1 +0,0 @@
|
|
|
|
-one line
|
|
|
|
|
|
|
|
'''))
|
|
|
|
|
2016-11-29 16:40:46 +01:00
|
|
|
|
|
|
|
class TestCallbackOnMethods(unittest.TestCase):
|
|
|
|
def _find_on_methods(self, callback):
|
|
|
|
cb_dir = dir(callback)
|
|
|
|
method_names = [x for x in cb_dir if '_on_' in x]
|
|
|
|
methods = [getattr(callback, mn) for mn in method_names]
|
|
|
|
return methods
|
|
|
|
|
|
|
|
def test_are_methods(self):
|
|
|
|
cb = CallbackBase()
|
|
|
|
for method in self._find_on_methods(cb):
|
|
|
|
self.assertIsInstance(method, types.MethodType)
|
|
|
|
|
|
|
|
def test_on_any(self):
|
2016-10-13 23:47:29 +02:00
|
|
|
cb = CallbackBase()
|
2016-11-29 16:40:46 +01:00
|
|
|
cb.v2_on_any('whatever', some_keyword='blippy')
|
|
|
|
cb.on_any('whatever', some_keyword='blippy')
|