2014-10-15 22:35:16 +00:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@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/>.
|
|
|
|
|
2014-10-15 23:25:48 +00:00
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2014-10-15 22:35:16 +00:00
|
|
|
|
2018-10-13 03:01:14 +00:00
|
|
|
from units.compat import unittest
|
|
|
|
from units.compat.builtins import BUILTINS
|
|
|
|
from units.compat.mock import mock_open, patch
|
2014-10-15 22:35:16 +00:00
|
|
|
from ansible.errors import AnsibleError
|
2017-05-30 17:05:19 +00:00
|
|
|
from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject
|
2014-10-15 22:35:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestErrors(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2014-10-23 20:39:27 +00:00
|
|
|
self.message = 'This is the error message'
|
2015-10-08 14:04:15 +00:00
|
|
|
self.unicode_message = 'This is an error with \xf0\x9f\x98\xa8 in it'
|
2014-10-15 22:35:16 +00:00
|
|
|
|
2014-10-16 00:59:51 +00:00
|
|
|
self.obj = AnsibleBaseYAMLObject()
|
|
|
|
|
2014-10-15 22:35:16 +00:00
|
|
|
def test_basic_error(self):
|
|
|
|
e = AnsibleError(self.message)
|
2016-01-19 17:09:04 +00:00
|
|
|
self.assertEqual(e.message, self.message)
|
|
|
|
self.assertEqual(e.__repr__(), self.message)
|
2014-10-15 22:35:16 +00:00
|
|
|
|
2015-10-08 14:04:15 +00:00
|
|
|
def test_basic_unicode_error(self):
|
|
|
|
e = AnsibleError(self.unicode_message)
|
2016-01-19 17:09:04 +00:00
|
|
|
self.assertEqual(e.message, self.unicode_message)
|
|
|
|
self.assertEqual(e.__repr__(), self.unicode_message)
|
2015-10-08 14:04:15 +00:00
|
|
|
|
2018-12-04 17:32:02 +00:00
|
|
|
@patch.object(AnsibleError, '_get_error_lines_from_file')
|
|
|
|
def test_error_with_kv(self, mock_method):
|
|
|
|
''' This tests a task with both YAML and k=v syntax
|
|
|
|
|
|
|
|
- lineinfile: line=foo path=bar
|
|
|
|
line: foo
|
|
|
|
|
|
|
|
An accurate error message and position indicator are expected.
|
|
|
|
|
|
|
|
_get_error_lines_from_file() returns (target_line, prev_line)
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.obj.ansible_pos = ('foo.yml', 2, 1)
|
|
|
|
|
|
|
|
mock_method.return_value = [' line: foo\n', '- lineinfile: line=foo path=bar\n']
|
|
|
|
|
|
|
|
e = AnsibleError(self.message, self.obj)
|
|
|
|
self.assertEqual(
|
|
|
|
e.message,
|
|
|
|
("This is the error message\n\nThe error appears to be in 'foo.yml': line 1, column 19, but may\nbe elsewhere in the "
|
|
|
|
"file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- lineinfile: line=foo path=bar\n"
|
|
|
|
" ^ here\n\n"
|
|
|
|
"There appears to be both 'k=v' shorthand syntax and YAML in this task. Only one syntax may be used.\n")
|
|
|
|
)
|
|
|
|
|
2014-10-23 20:39:27 +00:00
|
|
|
@patch.object(AnsibleError, '_get_error_lines_from_file')
|
2014-10-16 00:59:51 +00:00
|
|
|
def test_error_with_object(self, mock_method):
|
2015-04-01 21:54:22 +00:00
|
|
|
self.obj.ansible_pos = ('foo.yml', 1, 1)
|
2014-10-15 22:35:16 +00:00
|
|
|
|
2014-10-23 20:39:27 +00:00
|
|
|
mock_method.return_value = ('this is line 1\n', '')
|
2014-10-16 00:59:51 +00:00
|
|
|
e = AnsibleError(self.message, self.obj)
|
2014-10-15 22:35:16 +00:00
|
|
|
|
2017-02-10 17:33:48 +00:00
|
|
|
self.assertEqual(
|
|
|
|
e.message,
|
2018-12-04 17:32:02 +00:00
|
|
|
("This is the error message\n\nThe error appears to be in 'foo.yml': line 1, column 1, but may\nbe elsewhere in the file depending on the "
|
2017-02-10 17:33:48 +00:00
|
|
|
"exact syntax problem.\n\nThe offending line appears to be:\n\n\nthis is line 1\n^ here\n")
|
|
|
|
)
|
2014-10-16 00:59:51 +00:00
|
|
|
|
2014-10-23 20:39:27 +00:00
|
|
|
def test_get_error_lines_from_file(self):
|
2014-10-16 00:59:51 +00:00
|
|
|
m = mock_open()
|
|
|
|
m.return_value.readlines.return_value = ['this is line 1\n']
|
|
|
|
|
2014-10-21 05:24:09 +00:00
|
|
|
with patch('{0}.open'.format(BUILTINS), m):
|
2014-10-16 00:59:51 +00:00
|
|
|
# this line will be found in the file
|
2015-04-01 21:54:22 +00:00
|
|
|
self.obj.ansible_pos = ('foo.yml', 1, 1)
|
2014-10-16 00:59:51 +00:00
|
|
|
e = AnsibleError(self.message, self.obj)
|
2017-02-10 17:33:48 +00:00
|
|
|
self.assertEqual(
|
|
|
|
e.message,
|
2018-12-04 17:32:02 +00:00
|
|
|
("This is the error message\n\nThe error appears to be in 'foo.yml': line 1, column 1, but may\nbe elsewhere in the file depending on "
|
2017-02-10 17:33:48 +00:00
|
|
|
"the exact syntax problem.\n\nThe offending line appears to be:\n\n\nthis is line 1\n^ here\n")
|
|
|
|
)
|
2014-10-16 00:59:51 +00:00
|
|
|
|
|
|
|
# this line will not be found, as it is out of the index range
|
2015-04-01 21:54:22 +00:00
|
|
|
self.obj.ansible_pos = ('foo.yml', 2, 1)
|
2014-10-16 00:59:51 +00:00
|
|
|
e = AnsibleError(self.message, self.obj)
|
2017-02-10 17:33:48 +00:00
|
|
|
self.assertEqual(
|
|
|
|
e.message,
|
2018-12-04 17:32:02 +00:00
|
|
|
("This is the error message\n\nThe error appears to be in 'foo.yml': line 2, column 1, but may\nbe elsewhere in the file depending on "
|
2017-02-10 17:33:48 +00:00
|
|
|
"the exact syntax problem.\n\n(specified line no longer in file, maybe it changed?)")
|
|
|
|
)
|
2015-04-01 21:54:22 +00:00
|
|
|
|
2015-10-08 14:04:15 +00:00
|
|
|
m = mock_open()
|
|
|
|
m.return_value.readlines.return_value = ['this line has unicode \xf0\x9f\x98\xa8 in it!\n']
|
|
|
|
|
|
|
|
with patch('{0}.open'.format(BUILTINS), m):
|
|
|
|
# this line will be found in the file
|
|
|
|
self.obj.ansible_pos = ('foo.yml', 1, 1)
|
|
|
|
e = AnsibleError(self.unicode_message, self.obj)
|
2017-02-10 17:33:48 +00:00
|
|
|
self.assertEqual(
|
|
|
|
e.message,
|
2018-12-04 17:32:02 +00:00
|
|
|
("This is an error with \xf0\x9f\x98\xa8 in it\n\nThe error appears to be in 'foo.yml': line 1, column 1, but may\nbe elsewhere in the "
|
2017-02-10 17:33:48 +00:00
|
|
|
"file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\nthis line has unicode \xf0\x9f\x98\xa8 in it!\n^ "
|
|
|
|
"here\n")
|
|
|
|
)
|