mirror of https://github.com/ansible/ansible.git
Adding Play class for v2
parent
afd8cca345
commit
0ed9746db3
@ -0,0 +1,76 @@
|
||||
# (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/>.
|
||||
|
||||
from types import NoneType
|
||||
|
||||
|
||||
def load_list_of_blocks(ds, role=None, loader=None):
|
||||
'''
|
||||
Given a list of mixed task/block data (parsed from YAML),
|
||||
return a list of Block() objects, where implicit blocks
|
||||
are created for each bare Task.
|
||||
'''
|
||||
|
||||
# we import here to prevent a circular dependency with imports
|
||||
from ansible.playbook.block import Block
|
||||
|
||||
assert type(ds) in (list, NoneType)
|
||||
|
||||
block_list = []
|
||||
if ds:
|
||||
for block in ds:
|
||||
b = Block.load(block, role=role, loader=loader)
|
||||
block_list.append(b)
|
||||
|
||||
return block_list
|
||||
|
||||
def load_list_of_tasks(ds, block=None, role=None, loader=None):
|
||||
'''
|
||||
Given a list of task datastructures (parsed from YAML),
|
||||
return a list of Task() objects.
|
||||
'''
|
||||
|
||||
# we import here to prevent a circular dependency with imports
|
||||
from ansible.playbook.task import Task
|
||||
|
||||
assert type(ds) == list
|
||||
|
||||
task_list = []
|
||||
for task in ds:
|
||||
t = Task.load(task, block=block, role=role, loader=loader)
|
||||
task_list.append(t)
|
||||
|
||||
return task_list
|
||||
|
||||
def load_list_of_roles(ds, loader=None):
|
||||
'''
|
||||
Loads and returns a list of RoleInclude objects from the datastructure
|
||||
list of role definitions
|
||||
'''
|
||||
|
||||
# we import here to prevent a circular dependency with imports
|
||||
from ansible.playbook.role.include import RoleInclude
|
||||
|
||||
assert isinstance(ds, list)
|
||||
|
||||
roles = []
|
||||
for role_def in ds:
|
||||
i = RoleInclude.load(role_def, loader=loader)
|
||||
roles.append(i)
|
||||
|
||||
return roles
|
||||
|
||||
@ -0,0 +1,120 @@
|
||||
# (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)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue