mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
284 lines
11 KiB
Python
284 lines
11 KiB
Python
10 years ago
|
# coding: utf-8
|
||
|
# (c) 2015, Toshio Kuratomi <tkuratomi@ansible.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
|
||
|
|
||
10 years ago
|
from six import text_type, binary_type
|
||
10 years ago
|
from six.moves import StringIO
|
||
10 years ago
|
from collections import Sequence, Set, Mapping
|
||
|
|
||
|
from ansible.compat.tests import unittest
|
||
|
from ansible.compat.tests.mock import patch
|
||
|
|
||
|
from ansible.parsing.yaml.loader import AnsibleLoader
|
||
|
|
||
10 years ago
|
|
||
10 years ago
|
class TestAnsibleLoaderBasic(unittest.TestCase):
|
||
10 years ago
|
|
||
|
def setUp(self):
|
||
|
pass
|
||
|
|
||
|
def tearDown(self):
|
||
|
pass
|
||
|
|
||
|
def test_parse_number(self):
|
||
|
stream = StringIO("""
|
||
|
1
|
||
|
""")
|
||
10 years ago
|
loader = AnsibleLoader(stream, 'myfile.yml')
|
||
10 years ago
|
data = loader.get_single_data()
|
||
|
self.assertEqual(data, 1)
|
||
10 years ago
|
# No line/column info saved yet
|
||
10 years ago
|
|
||
|
def test_parse_string(self):
|
||
|
stream = StringIO("""
|
||
|
Ansible
|
||
|
""")
|
||
10 years ago
|
loader = AnsibleLoader(stream, 'myfile.yml')
|
||
10 years ago
|
data = loader.get_single_data()
|
||
|
self.assertEqual(data, u'Ansible')
|
||
10 years ago
|
self.assertIsInstance(data, text_type)
|
||
10 years ago
|
|
||
10 years ago
|
self.assertEqual(data.ansible_pos, ('myfile.yml', 2, 17))
|
||
10 years ago
|
|
||
10 years ago
|
def test_parse_utf8_string(self):
|
||
|
stream = StringIO("""
|
||
|
Cafè Eñyei
|
||
|
""")
|
||
10 years ago
|
loader = AnsibleLoader(stream, 'myfile.yml')
|
||
10 years ago
|
data = loader.get_single_data()
|
||
|
self.assertEqual(data, u'Cafè Eñyei')
|
||
10 years ago
|
self.assertIsInstance(data, text_type)
|
||
10 years ago
|
|
||
10 years ago
|
self.assertEqual(data.ansible_pos, ('myfile.yml', 2, 17))
|
||
10 years ago
|
|
||
10 years ago
|
def test_parse_dict(self):
|
||
|
stream = StringIO("""
|
||
|
webster: daniel
|
||
|
oed: oxford
|
||
|
""")
|
||
10 years ago
|
loader = AnsibleLoader(stream, 'myfile.yml')
|
||
10 years ago
|
data = loader.get_single_data()
|
||
|
self.assertEqual(data, {'webster': 'daniel', 'oed': 'oxford'})
|
||
|
self.assertEqual(len(data), 2)
|
||
10 years ago
|
self.assertIsInstance(list(data.keys())[0], text_type)
|
||
|
self.assertIsInstance(list(data.values())[0], text_type)
|
||
10 years ago
|
|
||
10 years ago
|
# Beginning of the first key
|
||
10 years ago
|
self.assertEqual(data.ansible_pos, ('myfile.yml', 2, 17))
|
||
10 years ago
|
|
||
10 years ago
|
self.assertEqual(data[u'webster'].ansible_pos, ('myfile.yml', 2, 26))
|
||
|
self.assertEqual(data[u'oed'].ansible_pos, ('myfile.yml', 3, 22))
|
||
10 years ago
|
|
||
10 years ago
|
def test_parse_list(self):
|
||
|
stream = StringIO("""
|
||
|
- a
|
||
|
- b
|
||
|
""")
|
||
10 years ago
|
loader = AnsibleLoader(stream, 'myfile.yml')
|
||
10 years ago
|
data = loader.get_single_data()
|
||
|
self.assertEqual(data, [u'a', u'b'])
|
||
|
self.assertEqual(len(data), 2)
|
||
10 years ago
|
self.assertIsInstance(data[0], text_type)
|
||
10 years ago
|
|
||
|
self.assertEqual(data.ansible_pos, ('myfile.yml', 2, 17))
|
||
|
|
||
|
self.assertEqual(data[0].ansible_pos, ('myfile.yml', 2, 19))
|
||
|
self.assertEqual(data[1].ansible_pos, ('myfile.yml', 3, 19))
|
||
10 years ago
|
|
||
10 years ago
|
def test_parse_short_dict(self):
|
||
|
stream = StringIO("""{"foo": "bar"}""")
|
||
|
loader = AnsibleLoader(stream, 'myfile.yml')
|
||
|
data = loader.get_single_data()
|
||
|
self.assertEqual(data, dict(foo=u'bar'))
|
||
|
|
||
|
self.assertEqual(data.ansible_pos, ('myfile.yml', 1, 1))
|
||
|
self.assertEqual(data[u'foo'].ansible_pos, ('myfile.yml', 1, 9))
|
||
|
|
||
|
stream = StringIO("""foo: bar""")
|
||
|
loader = AnsibleLoader(stream, 'myfile.yml')
|
||
|
data = loader.get_single_data()
|
||
|
self.assertEqual(data, dict(foo=u'bar'))
|
||
|
|
||
|
self.assertEqual(data.ansible_pos, ('myfile.yml', 1, 1))
|
||
|
self.assertEqual(data[u'foo'].ansible_pos, ('myfile.yml', 1, 6))
|
||
|
|
||
|
def test_error_conditions(self):
|
||
|
stream = StringIO("""{""")
|
||
|
loader = AnsibleLoader(stream, 'myfile.yml')
|
||
|
self.assertRaises(loader.get_single_data)
|
||
|
|
||
|
def test_front_matter(self):
|
||
|
stream = StringIO("""---\nfoo: bar""")
|
||
|
loader = AnsibleLoader(stream, 'myfile.yml')
|
||
|
data = loader.get_single_data()
|
||
|
self.assertEqual(data, dict(foo=u'bar'))
|
||
|
|
||
|
self.assertEqual(data.ansible_pos, ('myfile.yml', 2, 1))
|
||
|
self.assertEqual(data[u'foo'].ansible_pos, ('myfile.yml', 2, 6))
|
||
|
|
||
|
# Initial indent (See: #6348)
|
||
|
stream = StringIO(""" - foo: bar\n baz: qux""")
|
||
|
loader = AnsibleLoader(stream, 'myfile.yml')
|
||
|
data = loader.get_single_data()
|
||
|
self.assertEqual(data, [{u'foo': u'bar', u'baz': u'qux'}])
|
||
|
|
||
|
self.assertEqual(data.ansible_pos, ('myfile.yml', 1, 2))
|
||
|
self.assertEqual(data[0].ansible_pos, ('myfile.yml', 1, 4))
|
||
|
self.assertEqual(data[0][u'foo'].ansible_pos, ('myfile.yml', 1, 9))
|
||
|
self.assertEqual(data[0][u'baz'].ansible_pos, ('myfile.yml', 2, 9))
|
||
|
|
||
|
|
||
10 years ago
|
class TestAnsibleLoaderPlay(unittest.TestCase):
|
||
|
|
||
|
def setUp(self):
|
||
10 years ago
|
stream = StringIO("""
|
||
|
- hosts: localhost
|
||
|
vars:
|
||
|
number: 1
|
||
|
string: Ansible
|
||
|
utf8_string: Cafè Eñyei
|
||
|
dictionary:
|
||
|
webster: daniel
|
||
|
oed: oxford
|
||
|
list:
|
||
|
- a
|
||
|
- b
|
||
|
- 1
|
||
|
- 2
|
||
|
tasks:
|
||
|
- name: Test case
|
||
|
ping:
|
||
|
data: "{{ utf8_string }}"
|
||
|
|
||
|
- name: Test 2
|
||
|
ping:
|
||
|
data: "Cafè Eñyei"
|
||
|
|
||
|
- name: Test 3
|
||
|
command: "printf 'Cafè Eñyei\\n'"
|
||
|
""")
|
||
10 years ago
|
self.play_filename = '/path/to/myplay.yml'
|
||
|
stream.name = self.play_filename
|
||
|
self.loader = AnsibleLoader(stream)
|
||
|
self.data = self.loader.get_single_data()
|
||
|
|
||
|
def tearDown(self):
|
||
|
pass
|
||
|
|
||
|
def test_data_complete(self):
|
||
|
self.assertEqual(len(self.data), 1)
|
||
|
self.assertIsInstance(self.data, list)
|
||
|
self.assertEqual(frozenset(self.data[0].keys()), frozenset((u'hosts', u'vars', u'tasks')))
|
||
10 years ago
|
|
||
10 years ago
|
self.assertEqual(self.data[0][u'hosts'], u'localhost')
|
||
10 years ago
|
|
||
10 years ago
|
self.assertEqual(self.data[0][u'vars'][u'number'], 1)
|
||
|
self.assertEqual(self.data[0][u'vars'][u'string'], u'Ansible')
|
||
|
self.assertEqual(self.data[0][u'vars'][u'utf8_string'], u'Cafè Eñyei')
|
||
|
self.assertEqual(self.data[0][u'vars'][u'dictionary'],
|
||
10 years ago
|
{u'webster': u'daniel',
|
||
|
u'oed': u'oxford'})
|
||
10 years ago
|
self.assertEqual(self.data[0][u'vars'][u'list'], [u'a', u'b', 1, 2])
|
||
10 years ago
|
|
||
10 years ago
|
self.assertEqual(self.data[0][u'tasks'],
|
||
10 years ago
|
[{u'name': u'Test case', u'ping': {u'data': u'{{ utf8_string }}'}},
|
||
|
{u'name': u'Test 2', u'ping': {u'data': u'Cafè Eñyei'}},
|
||
|
{u'name': u'Test 3', u'command': u'printf \'Cafè Eñyei\n\''},
|
||
10 years ago
|
])
|
||
10 years ago
|
|
||
|
def walk(self, data):
|
||
|
# Make sure there's no str in the data
|
||
10 years ago
|
self.assertNotIsInstance(data, binary_type)
|
||
10 years ago
|
|
||
|
# Descend into various container types
|
||
10 years ago
|
if isinstance(data, text_type):
|
||
10 years ago
|
# strings are a sequence so we have to be explicit here
|
||
|
return
|
||
|
elif isinstance(data, (Sequence, Set)):
|
||
|
for element in data:
|
||
|
self.walk(element)
|
||
|
elif isinstance(data, Mapping):
|
||
|
for k, v in data.items():
|
||
|
self.walk(k)
|
||
|
self.walk(v)
|
||
|
|
||
|
# Scalars were all checked so we're good to go
|
||
|
return
|
||
10 years ago
|
|
||
|
def test_no_str_in_data(self):
|
||
|
# Checks that no strings are str type
|
||
|
self.walk(self.data)
|
||
|
|
||
|
def check_vars(self):
|
||
|
# Numbers don't have line/col information yet
|
||
10 years ago
|
#self.assertEqual(self.data[0][u'vars'][u'number'].ansible_pos, (self.play_filename, 4, 21))
|
||
10 years ago
|
|
||
10 years ago
|
self.assertEqual(self.data[0][u'vars'][u'string'].ansible_pos, (self.play_filename, 5, 29))
|
||
|
self.assertEqual(self.data[0][u'vars'][u'utf8_string'].ansible_pos, (self.play_filename, 6, 34))
|
||
10 years ago
|
|
||
10 years ago
|
self.assertEqual(self.data[0][u'vars'][u'dictionary'].ansible_pos, (self.play_filename, 8, 23))
|
||
|
self.assertEqual(self.data[0][u'vars'][u'dictionary'][u'webster'].ansible_pos, (self.play_filename, 8, 32))
|
||
|
self.assertEqual(self.data[0][u'vars'][u'dictionary'][u'oed'].ansible_pos, (self.play_filename, 9, 28))
|
||
10 years ago
|
|
||
10 years ago
|
self.assertEqual(self.data[0][u'vars'][u'list'].ansible_pos, (self.play_filename, 11, 23))
|
||
|
self.assertEqual(self.data[0][u'vars'][u'list'][0].ansible_pos, (self.play_filename, 11, 25))
|
||
|
self.assertEqual(self.data[0][u'vars'][u'list'][1].ansible_pos, (self.play_filename, 12, 25))
|
||
|
# Numbers don't have line/col info yet
|
||
|
#self.assertEqual(self.data[0][u'vars'][u'list'][2].ansible_pos, (self.play_filename, 13, 25))
|
||
|
#self.assertEqual(self.data[0][u'vars'][u'list'][3].ansible_pos, (self.play_filename, 14, 25))
|
||
10 years ago
|
|
||
|
def check_tasks(self):
|
||
|
#
|
||
|
# First Task
|
||
|
#
|
||
10 years ago
|
self.assertEqual(self.data[0][u'tasks'][0].ansible_pos, (self.play_filename, 16, 23))
|
||
|
self.assertEqual(self.data[0][u'tasks'][0][u'name'].ansible_pos, (self.play_filename, 16, 29))
|
||
|
self.assertEqual(self.data[0][u'tasks'][0][u'ping'].ansible_pos, (self.play_filename, 18, 25))
|
||
|
self.assertEqual(self.data[0][u'tasks'][0][u'ping'][u'data'].ansible_pos, (self.play_filename, 18, 31))
|
||
10 years ago
|
|
||
|
#
|
||
|
# Second Task
|
||
|
#
|
||
10 years ago
|
self.assertEqual(self.data[0][u'tasks'][1].ansible_pos, (self.play_filename, 20, 23))
|
||
|
self.assertEqual(self.data[0][u'tasks'][1][u'name'].ansible_pos, (self.play_filename, 20, 29))
|
||
|
self.assertEqual(self.data[0][u'tasks'][1][u'ping'].ansible_pos, (self.play_filename, 22, 25))
|
||
|
self.assertEqual(self.data[0][u'tasks'][1][u'ping'][u'data'].ansible_pos, (self.play_filename, 22, 31))
|
||
10 years ago
|
|
||
|
#
|
||
|
# Third Task
|
||
|
#
|
||
10 years ago
|
self.assertEqual(self.data[0][u'tasks'][2].ansible_pos, (self.play_filename, 24, 23))
|
||
|
self.assertEqual(self.data[0][u'tasks'][2][u'name'].ansible_pos, (self.play_filename, 24, 29))
|
||
|
self.assertEqual(self.data[0][u'tasks'][2][u'command'].ansible_pos, (self.play_filename, 25, 32))
|
||
10 years ago
|
|
||
|
def test_line_numbers(self):
|
||
|
# Check the line/column numbers are correct
|
||
10 years ago
|
# Note: Remember, currently dicts begin at the start of their first entry
|
||
|
self.assertEqual(self.data[0].ansible_pos, (self.play_filename, 2, 19))
|
||
|
self.assertEqual(self.data[0][u'hosts'].ansible_pos, (self.play_filename, 2, 26))
|
||
|
self.assertEqual(self.data[0][u'vars'].ansible_pos, (self.play_filename, 4, 21))
|
||
10 years ago
|
|
||
|
self.check_vars()
|
||
|
|
||
10 years ago
|
self.assertEqual(self.data[0][u'tasks'].ansible_pos, (self.play_filename, 16, 21))
|
||
10 years ago
|
|
||
|
self.check_tasks()
|