2014-10-19 07:14:30 +02: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/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2018-10-13 05:01:14 +02:00
|
|
|
from units.compat import unittest
|
|
|
|
from units.compat.mock import patch, MagicMock
|
2014-10-26 05:42:35 +01:00
|
|
|
|
2014-10-28 16:24:33 +01:00
|
|
|
from ansible.errors import AnsibleError, AnsibleParserError
|
2018-10-05 10:22:25 +02:00
|
|
|
from ansible.module_utils.common._collections_compat import Container
|
2014-10-19 07:14:30 +02:00
|
|
|
from ansible.playbook.block import Block
|
|
|
|
from ansible.playbook.task import Task
|
2014-10-26 05:42:35 +01:00
|
|
|
|
2015-05-08 18:34:19 +02:00
|
|
|
from units.mock.loader import DictDataLoader
|
2016-06-01 00:02:39 +02:00
|
|
|
from units.mock.path import mock_unfrackpath_noop
|
2014-10-19 07:14:30 +02:00
|
|
|
|
2016-12-05 13:01:45 +01:00
|
|
|
from ansible.playbook.role import Role
|
|
|
|
from ansible.playbook.role.include import RoleInclude
|
|
|
|
from ansible.playbook.role import hash_params
|
|
|
|
|
|
|
|
|
|
|
|
class TestHashParams(unittest.TestCase):
|
|
|
|
def test(self):
|
|
|
|
params = {'foo': 'bar'}
|
|
|
|
res = hash_params(params)
|
|
|
|
self._assert_set(res)
|
|
|
|
self._assert_hashable(res)
|
|
|
|
|
|
|
|
def _assert_hashable(self, res):
|
|
|
|
a_dict = {}
|
|
|
|
try:
|
|
|
|
a_dict[res] = res
|
|
|
|
except TypeError as e:
|
|
|
|
self.fail('%s is not hashable: %s' % (res, e))
|
|
|
|
|
|
|
|
def _assert_set(self, res):
|
|
|
|
self.assertIsInstance(res, frozenset)
|
|
|
|
|
|
|
|
def test_dict_tuple(self):
|
|
|
|
params = {'foo': (1, 'bar',)}
|
|
|
|
res = hash_params(params)
|
|
|
|
self._assert_set(res)
|
|
|
|
|
|
|
|
def test_tuple(self):
|
|
|
|
params = (1, None, 'foo')
|
|
|
|
res = hash_params(params)
|
|
|
|
self._assert_hashable(res)
|
|
|
|
|
|
|
|
def test_tuple_dict(self):
|
|
|
|
params = ({'foo': 'bar'}, 37)
|
|
|
|
res = hash_params(params)
|
|
|
|
self._assert_hashable(res)
|
|
|
|
|
|
|
|
def test_list(self):
|
|
|
|
params = ['foo', 'bar', 1, 37, None]
|
|
|
|
res = hash_params(params)
|
|
|
|
self._assert_set(res)
|
|
|
|
self._assert_hashable(res)
|
|
|
|
|
|
|
|
def test_dict_with_list_value(self):
|
|
|
|
params = {'foo': [1, 4, 'bar']}
|
|
|
|
res = hash_params(params)
|
|
|
|
self._assert_set(res)
|
|
|
|
self._assert_hashable(res)
|
|
|
|
|
|
|
|
def test_empty_set(self):
|
|
|
|
params = set([])
|
|
|
|
res = hash_params(params)
|
|
|
|
self._assert_hashable(res)
|
|
|
|
self._assert_set(res)
|
|
|
|
|
|
|
|
def test_generator(self):
|
|
|
|
def my_generator():
|
|
|
|
for i in ['a', 1, None, {}]:
|
|
|
|
yield i
|
|
|
|
|
|
|
|
params = my_generator()
|
|
|
|
res = hash_params(params)
|
|
|
|
self._assert_hashable(res)
|
|
|
|
|
|
|
|
def test_container_but_not_iterable(self):
|
|
|
|
# This is a Container that is not iterable, which is unlikely but...
|
2018-10-05 10:22:25 +02:00
|
|
|
class MyContainer(Container):
|
2016-12-05 13:01:45 +01:00
|
|
|
def __init__(self, some_thing):
|
|
|
|
self.data = []
|
|
|
|
self.data.append(some_thing)
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return item in self.data
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.data)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.data)
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
foo = MyContainer('foo bar')
|
|
|
|
params = foo
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, hash_params, params)
|
|
|
|
|
|
|
|
|
2014-10-19 07:14:30 +02:00
|
|
|
class TestRole(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
pass
|
|
|
|
|
2016-06-01 00:02:39 +02:00
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
2014-10-28 20:35:29 +01:00
|
|
|
def test_load_role_with_tasks(self):
|
|
|
|
|
|
|
|
fake_loader = DictDataLoader({
|
2015-05-14 21:31:11 +02:00
|
|
|
"/etc/ansible/roles/foo_tasks/tasks/main.yml": """
|
2014-10-28 20:35:29 +01:00
|
|
|
- shell: echo 'hello world'
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
2015-07-10 08:50:33 +02:00
|
|
|
mock_play = MagicMock()
|
|
|
|
mock_play.ROLE_CACHE = {}
|
|
|
|
|
2015-08-03 22:33:32 +02:00
|
|
|
i = RoleInclude.load('foo_tasks', play=mock_play, loader=fake_loader)
|
2015-07-10 08:50:33 +02:00
|
|
|
r = Role.load(i, play=mock_play)
|
2014-10-28 20:35:29 +01:00
|
|
|
|
2015-05-14 21:31:11 +02:00
|
|
|
self.assertEqual(str(r), 'foo_tasks')
|
2014-10-28 20:35:29 +01:00
|
|
|
self.assertEqual(len(r._task_blocks), 1)
|
|
|
|
assert isinstance(r._task_blocks[0], Block)
|
|
|
|
|
2018-06-07 16:18:07 +02:00
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
|
|
|
def test_load_role_with_tasks_dir_vs_file(self):
|
|
|
|
|
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
"/etc/ansible/roles/foo_tasks/tasks/custom_main/foo.yml": """
|
|
|
|
- command: bar
|
|
|
|
""",
|
|
|
|
"/etc/ansible/roles/foo_tasks/tasks/custom_main.yml": """
|
|
|
|
- command: baz
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
|
|
|
mock_play = MagicMock()
|
|
|
|
mock_play.ROLE_CACHE = {}
|
|
|
|
|
|
|
|
i = RoleInclude.load('foo_tasks', play=mock_play, loader=fake_loader)
|
|
|
|
r = Role.load(i, play=mock_play, from_files=dict(tasks='custom_main'))
|
|
|
|
|
|
|
|
self.assertEqual(r._task_blocks[0]._ds[0]['command'], 'baz')
|
|
|
|
|
2016-06-01 00:02:39 +02:00
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
2014-10-28 20:35:29 +01:00
|
|
|
def test_load_role_with_handlers(self):
|
|
|
|
|
|
|
|
fake_loader = DictDataLoader({
|
2015-05-14 21:31:11 +02:00
|
|
|
"/etc/ansible/roles/foo_handlers/handlers/main.yml": """
|
2014-10-28 20:35:29 +01:00
|
|
|
- name: test handler
|
|
|
|
shell: echo 'hello world'
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
2015-07-10 08:50:33 +02:00
|
|
|
mock_play = MagicMock()
|
|
|
|
mock_play.ROLE_CACHE = {}
|
|
|
|
|
2015-08-03 22:33:32 +02:00
|
|
|
i = RoleInclude.load('foo_handlers', play=mock_play, loader=fake_loader)
|
2015-07-10 08:50:33 +02:00
|
|
|
r = Role.load(i, play=mock_play)
|
2014-10-28 20:35:29 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(r._handler_blocks), 1)
|
|
|
|
assert isinstance(r._handler_blocks[0], Block)
|
|
|
|
|
2016-06-01 00:02:39 +02:00
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
2014-10-28 20:35:29 +01:00
|
|
|
def test_load_role_with_vars(self):
|
|
|
|
|
|
|
|
fake_loader = DictDataLoader({
|
2015-05-14 21:31:11 +02:00
|
|
|
"/etc/ansible/roles/foo_vars/defaults/main.yml": """
|
2014-10-28 20:35:29 +01:00
|
|
|
foo: bar
|
|
|
|
""",
|
2015-05-14 21:31:11 +02:00
|
|
|
"/etc/ansible/roles/foo_vars/vars/main.yml": """
|
2014-10-28 20:35:29 +01:00
|
|
|
foo: bam
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
2015-07-10 08:50:33 +02:00
|
|
|
mock_play = MagicMock()
|
|
|
|
mock_play.ROLE_CACHE = {}
|
|
|
|
|
2015-08-03 22:33:32 +02:00
|
|
|
i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader)
|
2015-07-10 08:50:33 +02:00
|
|
|
r = Role.load(i, play=mock_play)
|
2014-10-28 20:35:29 +01:00
|
|
|
|
|
|
|
self.assertEqual(r._default_vars, dict(foo='bar'))
|
|
|
|
self.assertEqual(r._role_vars, dict(foo='bam'))
|
|
|
|
|
2018-04-10 23:14:38 +02:00
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
|
|
|
def test_load_role_with_vars_dirs(self):
|
|
|
|
|
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
"/etc/ansible/roles/foo_vars/defaults/main/foo.yml": """
|
|
|
|
foo: bar
|
|
|
|
""",
|
|
|
|
"/etc/ansible/roles/foo_vars/vars/main/bar.yml": """
|
|
|
|
foo: bam
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
|
|
|
mock_play = MagicMock()
|
|
|
|
mock_play.ROLE_CACHE = {}
|
|
|
|
|
|
|
|
i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader)
|
|
|
|
r = Role.load(i, play=mock_play)
|
|
|
|
|
|
|
|
self.assertEqual(r._default_vars, dict(foo='bar'))
|
|
|
|
self.assertEqual(r._role_vars, dict(foo='bam'))
|
|
|
|
|
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
|
|
|
def test_load_role_with_vars_nested_dirs(self):
|
|
|
|
|
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
"/etc/ansible/roles/foo_vars/defaults/main/foo/bar.yml": """
|
|
|
|
foo: bar
|
|
|
|
""",
|
|
|
|
"/etc/ansible/roles/foo_vars/vars/main/bar/foo.yml": """
|
|
|
|
foo: bam
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
|
|
|
mock_play = MagicMock()
|
|
|
|
mock_play.ROLE_CACHE = {}
|
|
|
|
|
|
|
|
i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader)
|
|
|
|
r = Role.load(i, play=mock_play)
|
|
|
|
|
|
|
|
self.assertEqual(r._default_vars, dict(foo='bar'))
|
|
|
|
self.assertEqual(r._role_vars, dict(foo='bam'))
|
|
|
|
|
2018-06-20 22:49:34 +02:00
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
|
|
|
def test_load_role_with_vars_nested_dirs_combined(self):
|
|
|
|
|
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
"/etc/ansible/roles/foo_vars/defaults/main/foo/bar.yml": """
|
|
|
|
foo: bar
|
|
|
|
a: 1
|
|
|
|
""",
|
|
|
|
"/etc/ansible/roles/foo_vars/defaults/main/bar/foo.yml": """
|
|
|
|
foo: bam
|
|
|
|
b: 2
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
|
|
|
mock_play = MagicMock()
|
|
|
|
mock_play.ROLE_CACHE = {}
|
|
|
|
|
|
|
|
i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader)
|
|
|
|
r = Role.load(i, play=mock_play)
|
|
|
|
|
|
|
|
self.assertEqual(r._default_vars, dict(foo='bar', a=1, b=2))
|
|
|
|
|
2018-04-10 23:14:38 +02:00
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
|
|
|
def test_load_role_with_vars_dir_vs_file(self):
|
|
|
|
|
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
"/etc/ansible/roles/foo_vars/vars/main/foo.yml": """
|
|
|
|
foo: bar
|
|
|
|
""",
|
|
|
|
"/etc/ansible/roles/foo_vars/vars/main.yml": """
|
|
|
|
foo: bam
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
|
|
|
mock_play = MagicMock()
|
|
|
|
mock_play.ROLE_CACHE = {}
|
|
|
|
|
|
|
|
i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader)
|
|
|
|
r = Role.load(i, play=mock_play)
|
|
|
|
|
|
|
|
self.assertEqual(r._role_vars, dict(foo='bam'))
|
|
|
|
|
2016-06-01 00:02:39 +02:00
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
2014-10-28 20:35:29 +01:00
|
|
|
def test_load_role_with_metadata(self):
|
|
|
|
|
|
|
|
fake_loader = DictDataLoader({
|
2015-05-14 21:31:11 +02:00
|
|
|
'/etc/ansible/roles/foo_metadata/meta/main.yml': """
|
2014-10-28 20:35:29 +01:00
|
|
|
allow_duplicates: true
|
|
|
|
dependencies:
|
2015-05-14 21:31:11 +02:00
|
|
|
- bar_metadata
|
2014-10-28 20:35:29 +01:00
|
|
|
galaxy_info:
|
|
|
|
a: 1
|
|
|
|
b: 2
|
|
|
|
c: 3
|
|
|
|
""",
|
2015-05-14 21:31:11 +02:00
|
|
|
'/etc/ansible/roles/bar_metadata/meta/main.yml': """
|
2014-10-28 20:35:29 +01:00
|
|
|
dependencies:
|
2015-05-14 21:31:11 +02:00
|
|
|
- baz_metadata
|
2014-10-28 20:35:29 +01:00
|
|
|
""",
|
2015-05-14 21:31:11 +02:00
|
|
|
'/etc/ansible/roles/baz_metadata/meta/main.yml': """
|
2014-10-28 20:35:29 +01:00
|
|
|
dependencies:
|
2015-05-14 21:31:11 +02:00
|
|
|
- bam_metadata
|
2014-10-28 20:35:29 +01:00
|
|
|
""",
|
2015-05-14 21:31:11 +02:00
|
|
|
'/etc/ansible/roles/bam_metadata/meta/main.yml': """
|
2014-10-28 20:35:29 +01:00
|
|
|
dependencies: []
|
|
|
|
""",
|
2015-05-14 21:31:11 +02:00
|
|
|
'/etc/ansible/roles/bad1_metadata/meta/main.yml': """
|
2014-10-28 20:35:29 +01:00
|
|
|
1
|
|
|
|
""",
|
2015-05-14 21:31:11 +02:00
|
|
|
'/etc/ansible/roles/bad2_metadata/meta/main.yml': """
|
2014-10-28 20:35:29 +01:00
|
|
|
foo: bar
|
|
|
|
""",
|
2015-05-14 21:31:11 +02:00
|
|
|
'/etc/ansible/roles/recursive1_metadata/meta/main.yml': """
|
|
|
|
dependencies: ['recursive2_metadata']
|
2014-10-28 20:35:29 +01:00
|
|
|
""",
|
2015-05-14 21:31:11 +02:00
|
|
|
'/etc/ansible/roles/recursive2_metadata/meta/main.yml': """
|
|
|
|
dependencies: ['recursive1_metadata']
|
2014-10-28 20:35:29 +01:00
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
2015-07-10 08:50:33 +02:00
|
|
|
mock_play = MagicMock()
|
2019-03-28 18:41:39 +01:00
|
|
|
mock_play.collections = None
|
2015-07-10 08:50:33 +02:00
|
|
|
mock_play.ROLE_CACHE = {}
|
|
|
|
|
2015-08-03 22:33:32 +02:00
|
|
|
i = RoleInclude.load('foo_metadata', play=mock_play, loader=fake_loader)
|
2015-07-10 08:50:33 +02:00
|
|
|
r = Role.load(i, play=mock_play)
|
2014-10-28 20:35:29 +01:00
|
|
|
|
2014-10-28 16:24:33 +01:00
|
|
|
role_deps = r.get_direct_dependencies()
|
|
|
|
|
|
|
|
self.assertEqual(len(role_deps), 1)
|
|
|
|
self.assertEqual(type(role_deps[0]), Role)
|
|
|
|
self.assertEqual(len(role_deps[0].get_parents()), 1)
|
|
|
|
self.assertEqual(role_deps[0].get_parents()[0], r)
|
2014-10-28 20:35:29 +01:00
|
|
|
self.assertEqual(r._metadata.allow_duplicates, True)
|
|
|
|
self.assertEqual(r._metadata.galaxy_info, dict(a=1, b=2, c=3))
|
2014-10-27 19:25:32 +01:00
|
|
|
|
2014-10-28 16:24:33 +01:00
|
|
|
all_deps = r.get_all_dependencies()
|
|
|
|
self.assertEqual(len(all_deps), 3)
|
2015-05-14 21:31:11 +02:00
|
|
|
self.assertEqual(all_deps[0].get_name(), 'bam_metadata')
|
|
|
|
self.assertEqual(all_deps[1].get_name(), 'baz_metadata')
|
|
|
|
self.assertEqual(all_deps[2].get_name(), 'bar_metadata')
|
2014-10-28 20:35:29 +01:00
|
|
|
|
2015-08-03 22:33:32 +02:00
|
|
|
i = RoleInclude.load('bad1_metadata', play=mock_play, loader=fake_loader)
|
2015-07-10 08:50:33 +02:00
|
|
|
self.assertRaises(AnsibleParserError, Role.load, i, play=mock_play)
|
2014-10-28 16:24:33 +01:00
|
|
|
|
2015-08-03 22:33:32 +02:00
|
|
|
i = RoleInclude.load('bad2_metadata', play=mock_play, loader=fake_loader)
|
2015-07-10 08:50:33 +02:00
|
|
|
self.assertRaises(AnsibleParserError, Role.load, i, play=mock_play)
|
2014-10-26 05:42:35 +01:00
|
|
|
|
2015-08-03 22:33:32 +02:00
|
|
|
i = RoleInclude.load('recursive1_metadata', play=mock_play, loader=fake_loader)
|
2015-07-10 08:50:33 +02:00
|
|
|
self.assertRaises(AnsibleError, Role.load, i, play=mock_play)
|
2014-10-26 05:42:35 +01:00
|
|
|
|
2016-06-01 00:02:39 +02:00
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
2014-10-28 20:35:29 +01:00
|
|
|
def test_load_role_complex(self):
|
2014-10-26 05:42:35 +01:00
|
|
|
|
2014-10-28 20:35:29 +01:00
|
|
|
# FIXME: add tests for the more complex uses of
|
|
|
|
# params and tags/when statements
|
2014-10-26 05:42:35 +01:00
|
|
|
|
2014-10-28 20:35:29 +01:00
|
|
|
fake_loader = DictDataLoader({
|
2015-05-14 21:31:11 +02:00
|
|
|
"/etc/ansible/roles/foo_complex/tasks/main.yml": """
|
2014-10-28 20:35:29 +01:00
|
|
|
- shell: echo 'hello world'
|
|
|
|
""",
|
|
|
|
})
|
2014-10-26 05:42:35 +01:00
|
|
|
|
2015-07-10 08:50:33 +02:00
|
|
|
mock_play = MagicMock()
|
|
|
|
mock_play.ROLE_CACHE = {}
|
|
|
|
|
2015-08-03 22:33:32 +02:00
|
|
|
i = RoleInclude.load(dict(role='foo_complex'), play=mock_play, loader=fake_loader)
|
2015-07-10 08:50:33 +02:00
|
|
|
r = Role.load(i, play=mock_play)
|
2014-10-26 05:42:35 +01:00
|
|
|
|
2015-05-14 21:31:11 +02:00
|
|
|
self.assertEqual(r.get_name(), "foo_complex")
|