mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Making the switch to v2
This commit is contained in:
parent
8cf4452d48
commit
ce3ef7f4c1
486 changed files with 7948 additions and 9070 deletions
21
test/units/playbook/__init__.py
Normal file
21
test/units/playbook/__init__.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
# (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
|
||||
|
77
test/units/playbook/test_block.py
Normal file
77
test/units/playbook/test_block.py
Normal file
|
@ -0,0 +1,77 @@
|
|||
# (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
|
||||
|
||||
from ansible.playbook.block import Block
|
||||
from ansible.playbook.task import Task
|
||||
from ansible.compat.tests import unittest
|
||||
|
||||
class TestBlock(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_construct_empty_block(self):
|
||||
b = Block()
|
||||
|
||||
def test_construct_block_with_role(self):
|
||||
pass
|
||||
|
||||
def test_load_block_simple(self):
|
||||
ds = dict(
|
||||
block = [],
|
||||
rescue = [],
|
||||
always = [],
|
||||
#otherwise = [],
|
||||
)
|
||||
b = Block.load(ds)
|
||||
self.assertEqual(b.block, [])
|
||||
self.assertEqual(b.rescue, [])
|
||||
self.assertEqual(b.always, [])
|
||||
# not currently used
|
||||
#self.assertEqual(b.otherwise, [])
|
||||
|
||||
def test_load_block_with_tasks(self):
|
||||
ds = dict(
|
||||
block = [dict(action='block')],
|
||||
rescue = [dict(action='rescue')],
|
||||
always = [dict(action='always')],
|
||||
#otherwise = [dict(action='otherwise')],
|
||||
)
|
||||
b = Block.load(ds)
|
||||
self.assertEqual(len(b.block), 1)
|
||||
assert isinstance(b.block[0], Task)
|
||||
self.assertEqual(len(b.rescue), 1)
|
||||
assert isinstance(b.rescue[0], Task)
|
||||
self.assertEqual(len(b.always), 1)
|
||||
assert isinstance(b.always[0], Task)
|
||||
# not currently used
|
||||
#self.assertEqual(len(b.otherwise), 1)
|
||||
#assert isinstance(b.otherwise[0], Task)
|
||||
|
||||
def test_load_implicit_block(self):
|
||||
ds = [dict(action='foo')]
|
||||
b = Block.load(ds)
|
||||
self.assertEqual(len(b.block), 1)
|
||||
assert isinstance(b.block[0], Task)
|
||||
|
132
test/units/playbook/test_play.py
Normal file
132
test/units/playbook/test_play.py
Normal file
|
@ -0,0 +1,132 @@
|
|||
# (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
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import patch, MagicMock
|
||||
|
||||
from ansible.errors import AnsibleError, AnsibleParserError
|
||||
from ansible.playbook.play import Play
|
||||
from ansible.playbook.role import Role
|
||||
from ansible.playbook.task import Task
|
||||
|
||||
from test.mock.loader import DictDataLoader
|
||||
|
||||
class TestPlay(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_empty_play(self):
|
||||
p = Play.load(dict())
|
||||
self.assertEqual(str(p), "PLAY: <no name specified>")
|
||||
|
||||
def test_basic_play(self):
|
||||
p = Play.load(dict(
|
||||
name="test play",
|
||||
hosts=['foo'],
|
||||
gather_facts=False,
|
||||
connection='local',
|
||||
remote_user="root",
|
||||
sudo=True,
|
||||
sudo_user="testing",
|
||||
))
|
||||
|
||||
def test_play_with_user_conflict(self):
|
||||
p = Play.load(dict(
|
||||
name="test play",
|
||||
hosts=['foo'],
|
||||
user="testing",
|
||||
gather_facts=False,
|
||||
))
|
||||
self.assertEqual(p.remote_user, "testing")
|
||||
|
||||
def test_play_with_user_conflict(self):
|
||||
play_data = dict(
|
||||
name="test play",
|
||||
hosts=['foo'],
|
||||
user="testing",
|
||||
remote_user="testing",
|
||||
)
|
||||
self.assertRaises(AnsibleParserError, Play.load, play_data)
|
||||
|
||||
def test_play_with_tasks(self):
|
||||
p = Play.load(dict(
|
||||
name="test play",
|
||||
hosts=['foo'],
|
||||
gather_facts=False,
|
||||
tasks=[dict(action='shell echo "hello world"')],
|
||||
))
|
||||
|
||||
def test_play_with_handlers(self):
|
||||
p = Play.load(dict(
|
||||
name="test play",
|
||||
hosts=['foo'],
|
||||
gather_facts=False,
|
||||
handlers=[dict(action='shell echo "hello world"')],
|
||||
))
|
||||
|
||||
def test_play_with_pre_tasks(self):
|
||||
p = Play.load(dict(
|
||||
name="test play",
|
||||
hosts=['foo'],
|
||||
gather_facts=False,
|
||||
pre_tasks=[dict(action='shell echo "hello world"')],
|
||||
))
|
||||
|
||||
def test_play_with_post_tasks(self):
|
||||
p = Play.load(dict(
|
||||
name="test play",
|
||||
hosts=['foo'],
|
||||
gather_facts=False,
|
||||
post_tasks=[dict(action='shell echo "hello world"')],
|
||||
))
|
||||
|
||||
def test_play_with_roles(self):
|
||||
fake_loader = DictDataLoader({
|
||||
'/etc/ansible/roles/foo/tasks.yml': """
|
||||
- name: role task
|
||||
shell: echo "hello world"
|
||||
""",
|
||||
})
|
||||
|
||||
p = Play.load(dict(
|
||||
name="test play",
|
||||
hosts=['foo'],
|
||||
gather_facts=False,
|
||||
roles=['foo'],
|
||||
), loader=fake_loader)
|
||||
|
||||
tasks = p.compile()
|
||||
|
||||
def test_play_compile(self):
|
||||
p = Play.load(dict(
|
||||
name="test play",
|
||||
hosts=['foo'],
|
||||
gather_facts=False,
|
||||
tasks=[dict(action='shell echo "hello world"')],
|
||||
))
|
||||
|
||||
tasks = p.compile()
|
||||
self.assertEqual(len(tasks), 1)
|
||||
self.assertIsInstance(tasks[0], Task)
|
69
test/units/playbook/test_playbook.py
Normal file
69
test/units/playbook/test_playbook.py
Normal file
|
@ -0,0 +1,69 @@
|
|||
# (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
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import patch, MagicMock
|
||||
|
||||
from ansible.errors import AnsibleError, AnsibleParserError
|
||||
from ansible.playbook import Playbook
|
||||
from ansible.vars import VariableManager
|
||||
|
||||
from test.mock.loader import DictDataLoader
|
||||
|
||||
class TestPlaybook(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_empty_playbook(self):
|
||||
fake_loader = DictDataLoader({})
|
||||
p = Playbook(loader=fake_loader)
|
||||
|
||||
def test_basic_playbook(self):
|
||||
fake_loader = DictDataLoader({
|
||||
"test_file.yml":"""
|
||||
- hosts: all
|
||||
""",
|
||||
})
|
||||
p = Playbook.load("test_file.yml", loader=fake_loader)
|
||||
plays = p.get_plays()
|
||||
|
||||
def test_bad_playbook_files(self):
|
||||
fake_loader = DictDataLoader({
|
||||
# represents a playbook which is not a list of plays
|
||||
"bad_list.yml": """
|
||||
foo: bar
|
||||
|
||||
""",
|
||||
# represents a playbook where a play entry is mis-formatted
|
||||
"bad_entry.yml": """
|
||||
-
|
||||
- "This should be a mapping..."
|
||||
|
||||
""",
|
||||
})
|
||||
vm = VariableManager()
|
||||
self.assertRaises(AnsibleParserError, Playbook.load, "bad_list.yml", vm, fake_loader)
|
||||
self.assertRaises(AnsibleParserError, Playbook.load, "bad_entry.yml", vm, fake_loader)
|
||||
|
167
test/units/playbook/test_role.py
Normal file
167
test/units/playbook/test_role.py
Normal file
|
@ -0,0 +1,167 @@
|
|||
# (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
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import patch, MagicMock
|
||||
|
||||
from ansible.errors import AnsibleError, AnsibleParserError
|
||||
from ansible.playbook.block import Block
|
||||
from ansible.playbook.role import Role
|
||||
from ansible.playbook.role.include import RoleInclude
|
||||
from ansible.playbook.task import Task
|
||||
|
||||
from test.mock.loader import DictDataLoader
|
||||
|
||||
class TestRole(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_load_role_with_tasks(self):
|
||||
|
||||
fake_loader = DictDataLoader({
|
||||
"/etc/ansible/roles/foo/tasks/main.yml": """
|
||||
- shell: echo 'hello world'
|
||||
""",
|
||||
})
|
||||
|
||||
i = RoleInclude.load('foo', loader=fake_loader)
|
||||
r = Role.load(i)
|
||||
|
||||
self.assertEqual(str(r), 'foo')
|
||||
self.assertEqual(len(r._task_blocks), 1)
|
||||
assert isinstance(r._task_blocks[0], Block)
|
||||
|
||||
def test_load_role_with_handlers(self):
|
||||
|
||||
fake_loader = DictDataLoader({
|
||||
"/etc/ansible/roles/foo/handlers/main.yml": """
|
||||
- name: test handler
|
||||
shell: echo 'hello world'
|
||||
""",
|
||||
})
|
||||
|
||||
i = RoleInclude.load('foo', loader=fake_loader)
|
||||
r = Role.load(i)
|
||||
|
||||
self.assertEqual(len(r._handler_blocks), 1)
|
||||
assert isinstance(r._handler_blocks[0], Block)
|
||||
|
||||
def test_load_role_with_vars(self):
|
||||
|
||||
fake_loader = DictDataLoader({
|
||||
"/etc/ansible/roles/foo/defaults/main.yml": """
|
||||
foo: bar
|
||||
""",
|
||||
"/etc/ansible/roles/foo/vars/main.yml": """
|
||||
foo: bam
|
||||
""",
|
||||
})
|
||||
|
||||
i = RoleInclude.load('foo', loader=fake_loader)
|
||||
r = Role.load(i)
|
||||
|
||||
self.assertEqual(r._default_vars, dict(foo='bar'))
|
||||
self.assertEqual(r._role_vars, dict(foo='bam'))
|
||||
|
||||
def test_load_role_with_metadata(self):
|
||||
|
||||
fake_loader = DictDataLoader({
|
||||
'/etc/ansible/roles/foo/meta/main.yml': """
|
||||
allow_duplicates: true
|
||||
dependencies:
|
||||
- bar
|
||||
galaxy_info:
|
||||
a: 1
|
||||
b: 2
|
||||
c: 3
|
||||
""",
|
||||
'/etc/ansible/roles/bar/meta/main.yml': """
|
||||
dependencies:
|
||||
- baz
|
||||
""",
|
||||
'/etc/ansible/roles/baz/meta/main.yml': """
|
||||
dependencies:
|
||||
- bam
|
||||
""",
|
||||
'/etc/ansible/roles/bam/meta/main.yml': """
|
||||
dependencies: []
|
||||
""",
|
||||
'/etc/ansible/roles/bad1/meta/main.yml': """
|
||||
1
|
||||
""",
|
||||
'/etc/ansible/roles/bad2/meta/main.yml': """
|
||||
foo: bar
|
||||
""",
|
||||
'/etc/ansible/roles/recursive1/meta/main.yml': """
|
||||
dependencies: ['recursive2']
|
||||
""",
|
||||
'/etc/ansible/roles/recursive2/meta/main.yml': """
|
||||
dependencies: ['recursive1']
|
||||
""",
|
||||
})
|
||||
|
||||
i = RoleInclude.load('foo', loader=fake_loader)
|
||||
r = Role.load(i)
|
||||
|
||||
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)
|
||||
self.assertEqual(r._metadata.allow_duplicates, True)
|
||||
self.assertEqual(r._metadata.galaxy_info, dict(a=1, b=2, c=3))
|
||||
|
||||
all_deps = r.get_all_dependencies()
|
||||
self.assertEqual(len(all_deps), 3)
|
||||
self.assertEqual(all_deps[0].get_name(), 'bar')
|
||||
self.assertEqual(all_deps[1].get_name(), 'baz')
|
||||
self.assertEqual(all_deps[2].get_name(), 'bam')
|
||||
|
||||
i = RoleInclude.load('bad1', loader=fake_loader)
|
||||
self.assertRaises(AnsibleParserError, Role.load, i)
|
||||
|
||||
i = RoleInclude.load('bad2', loader=fake_loader)
|
||||
self.assertRaises(AnsibleParserError, Role.load, i)
|
||||
|
||||
i = RoleInclude.load('recursive1', loader=fake_loader)
|
||||
self.assertRaises(AnsibleError, Role.load, i)
|
||||
|
||||
def test_load_role_complex(self):
|
||||
|
||||
# FIXME: add tests for the more complex uses of
|
||||
# params and tags/when statements
|
||||
|
||||
fake_loader = DictDataLoader({
|
||||
"/etc/ansible/roles/foo/tasks/main.yml": """
|
||||
- shell: echo 'hello world'
|
||||
""",
|
||||
})
|
||||
|
||||
i = RoleInclude.load(dict(role='foo'), loader=fake_loader)
|
||||
r = Role.load(i)
|
||||
|
||||
self.assertEqual(r.get_name(), "foo")
|
||||
|
87
test/units/playbook/test_task.py
Normal file
87
test/units/playbook/test_task.py
Normal file
|
@ -0,0 +1,87 @@
|
|||
# (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
|
||||
|
||||
from ansible.playbook.task import Task
|
||||
from ansible.compat.tests import unittest
|
||||
|
||||
basic_shell_task = dict(
|
||||
name = 'Test Task',
|
||||
shell = 'echo hi'
|
||||
)
|
||||
|
||||
kv_shell_task = dict(
|
||||
action = 'shell echo hi'
|
||||
)
|
||||
|
||||
class TestTask(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_construct_empty_task(self):
|
||||
t = Task()
|
||||
|
||||
def test_construct_task_with_role(self):
|
||||
pass
|
||||
|
||||
def test_construct_task_with_block(self):
|
||||
pass
|
||||
|
||||
def test_construct_task_with_role_and_block(self):
|
||||
pass
|
||||
|
||||
def test_load_task_simple(self):
|
||||
t = Task.load(basic_shell_task)
|
||||
assert t is not None
|
||||
self.assertEqual(t.name, basic_shell_task['name'])
|
||||
self.assertEqual(t.action, 'command')
|
||||
self.assertEqual(t.args, dict(_raw_params='echo hi', _uses_shell=True))
|
||||
|
||||
def test_load_task_kv_form(self):
|
||||
t = Task.load(kv_shell_task)
|
||||
self.assertEqual(t.action, 'command')
|
||||
self.assertEqual(t.args, dict(_raw_params='echo hi', _uses_shell=True))
|
||||
|
||||
def test_task_auto_name(self):
|
||||
assert 'name' not in kv_shell_task
|
||||
t = Task.load(kv_shell_task)
|
||||
#self.assertEqual(t.name, 'shell echo hi')
|
||||
|
||||
def test_task_auto_name_with_role(self):
|
||||
pass
|
||||
|
||||
def test_load_task_complex_form(self):
|
||||
pass
|
||||
|
||||
def test_can_load_module_complex_form(self):
|
||||
pass
|
||||
|
||||
def test_local_action_implies_delegate(self):
|
||||
pass
|
||||
|
||||
def test_local_action_conflicts_with_delegate(self):
|
||||
pass
|
||||
|
||||
def test_delegate_to_parses(self):
|
||||
pass
|
Loading…
Add table
Add a link
Reference in a new issue